Skip to content

Instantly share code, notes, and snippets.

@mr1337357
Forked from MLLeKander/main.py
Last active August 29, 2015 14:13
Show Gist options
  • Save mr1337357/f66c76caf1ae34689f01 to your computer and use it in GitHub Desktop.
Save mr1337357/f66c76caf1ae34689f01 to your computer and use it in GitHub Desktop.
#too short to license, so public domain
from wavegen import wavegen
from sys import stdout
import random
def tochar(sample):
return chr(int(sample * 127 + 127))
a = wavegen(440)
ash = wavegen(466.16)
b = wavegen(493.88)
c = wavegen(523.25)
csh = wavegen(554.37)
d = wavegen(587.33)
dsh = wavegen(622.25)
e = wavegen(659.25)
f = wavegen(698.46)
fsh = wavegen(739.99)
g = wavegen(783.99)
gsh = wavegen(830.61)
lfo = wavegen(.5)
#changed scale to h,m3,h,w,h,m3,h
notes = [a,ash,csh,d,e,f,gsh,a.octup(),ash.octup(),csh.octup(),d.octup(),e.octup(),f.octup(),gsh.octup(),a.octup().octup()]
triplets=[1,3,3,6,6]
duplets=[1,2,4,4,8,8,8]
currdiv=duplets
i = 44101
slices = 1
currnote = 1
currndx = len(notes)/2
while True:
if i > 44100 / slices:
currndx = min(max(0,currndx+random.randint(-3,3)),len(notes)-1)
current = notes[currndx]
if currnote == slices:
if random.randint(0,5) == 1:
if currdiv == triplets:
currdiv = duplets
else:
currdiv = triplets
slices = random.choice(currdiv)
lfo = wavegen(slices * .5)
currnote = 0
currnote = currnote + 1
i = 0
samp = current.get_sample(i)
samp = samp * lfo.get_sample(i)
i = i + 1
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=44100):
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