Last active
April 4, 2022 13:34
-
-
Save jeffehobbs/9b71154885819adbf00c3cd175cb2510 to your computer and use it in GitHub Desktop.
simple python script that generates a set of smoothly looping tones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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