Skip to content

Instantly share code, notes, and snippets.

@medericmotte
Last active October 26, 2018 14:37
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/85b0d3f9eb7bb30c03f87e5c0eb16322 to your computer and use it in GitHub Desktop.
Save medericmotte/85b0d3f9eb7bb30c03f87e5c0eb16322 to your computer and use it in GitHub Desktop.
# generate wav file containing sine waves
import os, math, wave, array
import sound
import ui
import photos
import console
from scene import *
import time
import numpy as np
def shift(arr, num, fill_value=np.nan):
if num >= 0:
return np.concatenate((np.full(num, fill_value), arr[:-num]))
else:
return np.concatenate((arr[-num:], np.full(-num, fill_value)))
def saw(t):
return (t*(65534.0/44100.0))%65534.0 - 32767.0
#Unison:
def sawU(t):
return 0.25*(saw(t)+saw(0.2+t*1.011)+saw(0.479+t*1.007)+saw(0.753+t*1.003))
#Chords:
def sawChord(t):
return (sawU(t)+sawU(0.2+1.5*t)+sawU(0.3+2*t))/3.0
sawChord= np.vectorize(sawChord)
#saw= np.vectorize(saw)
tps=time.time()
print(0)
sampleRate=44100.0
duration=5
numChan=1
dataSize=2
numSamples=int(sampleRate*duration)
samples= np.arange(numSamples)
q=0.8
osc = sawChord(samples*440)*(1-q)
#Approximatin of IIR filters (by 10 order FIR filters):
out =osc*1
for j in range(16):
for i in range(10):
out = osc+q*shift(out,1,0)
osc=out*(1-q)
#out = sawChord(samples*110).astype(np.int16)
#out = np.array(numSamples, dtype = np.int16)
print(time.time()-tps)
name= "parallel_non_real_time_synth.wav"
f = wave.open(name, 'w')
f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed"))
f.writeframes(out.astype(np.int16).tostring())
print(time.time()-tps)
f.close()
sound.play_effect(name)
os.remove(name)
print(time.time()-tps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment