Skip to content

Instantly share code, notes, and snippets.

@robbie01
Created June 6, 2018 18:50
Show Gist options
  • Save robbie01/2649b5709b932b663b8025fe25e3b0b9 to your computer and use it in GitHub Desktop.
Save robbie01/2649b5709b932b663b8025fe25e3b0b9 to your computer and use it in GitHub Desktop.
the thx sound generated with scipy square waves
import numpy as np
from random import uniform
from scipy.signal import square
from scipy.io import wavfile
samplerate = 44100
startlength = 8
chirplength = 4
endlength = 16
detune = 1
def distinit(v):
return 200*((2**(1/12))**v) # distribute 0-12 on exp scale 200-400
def genchirp(f0, f1):
signal = np.logspace(0, np.log10(2), chirplength*samplerate)-1 # log ramp
signal = np.interp(signal, [0, 1], [f0, f1])
signal = np.concatenate((np.repeat(f0, startlength*samplerate), signal, np.repeat(f1, endlength*samplerate)))
signal = square(signal.cumsum() * 2 * np.pi / samplerate, np.linspace(0, 1, (startlength+chirplength+endlength)*samplerate)) # pulse wave with linearly increasing duty cycle
return np.float32(signal)
signal = np.array([0]*((startlength+chirplength+endlength)*samplerate), dtype='float32')
targetpool = [36.71, 36.71, 73.42, 73.42, 110.00, 110.00, 146.83, 146.83, 220.00, 220.00, 293.66, 293.66, 293.66, 440.00, 440.00, 440.00, 587.33, 587.33, 587.33, 880.00, 880.00, 880.00, 1174.66, 1174.66, 1174.66, 1479.98, 1479.98, 1479.98]
for t in targetpool:
signal += genchirp(distinit(uniform(0, 12)), t+uniform(-detune, detune))
signal /= len(targetpool)
envelope = np.concatenate((np.linspace(0, 1, 16*samplerate), np.repeat(1, 8*samplerate), np.linspace(1, 0, 4*samplerate)))
signal *= envelope
wavfile.write("audio.wav", samplerate, signal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment