Skip to content

Instantly share code, notes, and snippets.

@medericmotte
Last active October 26, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medericmotte/5330028059e3e94198a14b4f87a9189e to your computer and use it in GitHub Desktop.
Save medericmotte/5330028059e3e94198a14b4f87a9189e to your computer and use it in GitHub Desktop.
import os, math, wave, array
import sound
import ui
import photos
import console
from scene import *
import time
def saw(t):
return (t*(65534.0/44100.0))%65534.0 - 32767.0
name= 'serialize_non_real_time_synth.wav'
f= 440*math.pow(2, (30-30.0)/12.0)
duration = 5
volume =50
out = array.array('h') # signed short integer (-32768 to 32767) data
sampleRate = 44100.0 # of samples per second (standard)
numChan = 1 # of channels (1: mono, 2: stereo)
dataSize = 2 # 2 bytes because of using signed short integers => bit depth = 16
numSamples= int(sampleRate * duration)
filterbuffers = array.array('h')
for i in range(16):
filterbuffers.append(0)
n= 44100.0/60.0
t=0
fade=0
notes=[0.0,0.0,0.0]
q=0.8
tps=time.time()
print(0)
for i in range(numSamples):
if i<numSamples/2:
fade+=0.01*(1-fade)
else:
fade*=0.99
x= (i%n)/n
t+=f
#Chords/Unison:
notes[0] = (0.25*saw(t)+0.25*saw(0.2+t*1.011)+0.25*saw(0.479+t*1.007)+0.25*saw(0.753+t*1.003))*0.5
notes[1] = (0.25*saw(2*t)+0.25*saw(0.2+2*t*1.011)+0.25*saw(0.479+2*t*1.007)+0.25*saw(0.753+2*t*1.003)) *0.5
notes[2] = (0.25*saw(1.5*t)+0.25*saw(0.2+1.5*t*1.011)+0.25*saw(0.479+1.5*t*1.007)+0.25*saw(0.753+1.5*t*1.003)) *0.5
sample= 0.33*fade*(notes[0]+notes[1]+notes[2])
#Filter:
for j in range(16):
filterbuffers[j]=int(q*filterbuffers[j]+(1-q)*sample)
sample=filterbuffers[j]
out.append(int(sample))
#Delay/Reverb:
'''
if i>= int(sampleRate*0.5):
out[2*i]+= int(0.75* out[2*i-int(sampleRate)])
out[2*i+1]+= int(0.8* out[2*i+1-int(0.8*sampleRate)])
'''
print(time.time()-tps)
f = wave.open(name, 'w')
f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed"))
f.writeframes(out.tostring())
f.close()
print(time.time()-tps)
sound.play_effect(name)
os.remove(name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment