Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Last active April 4, 2022 13:34
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 jeffehobbs/9b71154885819adbf00c3cd175cb2510 to your computer and use it in GitHub Desktop.
Save jeffehobbs/9b71154885819adbf00c3cd175cb2510 to your computer and use it in GitHub Desktop.
simple python script that generates a set of smoothly looping tones
import math, wave, array
filenames = []
for filename in range(32):
x = filename % 8
y = filename / 8
filenames.append(str(math.trunc(y)) + str(x))
# use range to generate tones (lowest Hz, highest Hz, steps in Hz between)
for frequency in range(20, 460, 20):
volume = 100 # percent
data = array.array('h') # signed short integer (-32768 to 32767) data
sampleRate = 44100 # 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
numSamplesPerCyc = int(sampleRate / frequency)
cycle = int(sampleRate / frequency)
for i in range(cycle):
sample = 32767 * float(volume) / 100
sample *= math.sin(math.pi * 2 * (i % numSamplesPerCyc) / numSamplesPerCyc)
data.append(int(sample))
f = wave.open(filenames[iteration] + '.wav', 'w')
f.setparams((numChan, dataSize, sampleRate, cycle, 'NONE', 'Uncompressed'))
f.writeframes(data.tostring())
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment