Skip to content

Instantly share code, notes, and snippets.

@mr1337357
Last active August 29, 2015 14:13
Show Gist options
  • Save mr1337357/c299547f0dd4890fdeb4 to your computer and use it in GitHub Desktop.
Save mr1337357/c299547f0dd4890fdeb4 to your computer and use it in GitHub Desktop.
Musicgen
#too short to license, so public domain
from wavegen import wavegen
from sys import stdout
import random
def tochar(sample):
sample = sample * 127
sample = sample + 127
sample = int(sample)
return chr(sample)
a = wavegen(440,44100)
ash = wavegen(466.16,44100)
b = wavegen(493.88,44100)
c = wavegen(523.25,44100)
csh = wavegen(554.37,44100)
d = wavegen(587.33,44100)
dsh = wavegen(622.25,44100)
e = wavegen(659.25,44100)
f = wavegen(698.46,44100)
fsh = wavegen(739.99,44100)
g = wavegen(783.99,44100)
gsh = wavegen(830.61,44100)
lfo = wavegen(.5,44100)
notes = [a,b,c,d,e,f,gsh,a.octup(),b.octup(),c.octup(),d.octup(),e.octup(),f.octup(),gsh.octup(),a.octup().octup()]
i = 0
current = a
notelen = 1
currnote = 1
while True:
samp = current.get_sample(i)
samp = samp * lfo.get_sample(i)
if i > 44100 / notelen:
current = random.choice(notes)
if currnote == notelen:
notelen = random.randint(1,4)
lfo = wavegen(notelen * .5,44100)
currnote = 0
currnote = currnote + 1
i = 0
i = i + 1
#samp = samp / 3
stdout.write(tochar(samp))
#too short to license, so public domain
from math import pi,sin
class wavegen():
samprate=None
freq=None
def __init__(self,freq,samprate):
self.samprate=float(samprate)
self.freq=float(freq)
def get_sample(self,time):
return sin(time*2*pi*self.freq/self.samprate)
def octup(self):
return wavegen(self.freq*2,self.samprate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment