-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate.py
107 lines (92 loc) · 3.42 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Generate
# For Python 3
# By Zoe Blade
# Uses additive synthesis to generate 8-bit .wav files, suitable for the Doepfer A-112
import math # For sine wave generation
import struct # For converting the integers to binary data
import sys # For command line arguments
import wave # For .wav output
# Set sensible defaults
mode = 'lowpass'
waveform = 'sawtooth'
outputFilenames = []
acceptableModes = ['highpass', 'lowpass']
acceptableWaveforms = ['sawtooth', 'square', 'triangle']
# Override the defaults
for argument in sys.argv:
# Override the filename
if (argument[-4:].lower() == '.wav'):
outputFilenames.append(argument)
continue
# Override the mode
if (argument[:7] == '--mode='):
if (argument[7:] in acceptableModes):
mode = argument[7:]
continue
else:
print(argument[7:], "ain't any mode I ever heard of")
exit()
# Override the waveform type
if (argument[:11] == '--waveform='):
if (argument[11:] in acceptableWaveforms):
waveform = argument[11:]
continue
else:
print(argument[11:], "ain't any waveform I ever heard of")
exit()
if (len(outputFilenames) == 0):
print("""\
Usage:
python3 generate.py [option...] output.wav
Options: (may appear before or after arguments)
--mode=foo
set mode (default is lowpass, other option is highpass)
--waveform=foo
set which type of waveform to generate (default is sawtooth,
other options are square and triangle)
""")
exit()
# Cycle through files
for outputFilename in outputFilenames:
try:
outputFile = wave.open(outputFilename, 'wb')
outputFile.setnchannels(1) # Mono
outputFile.setsampwidth(1) # 8-bit
outputFile.setframerate(66976) # 261.626Hz for C-4 * 256 samples per cycle
except:
print("I couldn't write to", outputFilename, "Skipping.")
continue
# Make a sine wave lookup table
sineWaveLookupTable = {frame: math.sin(frame / 256 * 2 * math.pi) * 127 for frame in range(256)}
for maxHarmonic in range(1, 257): # Count from 1 to 256, not from 0 to 255
for sample in range(256):
wavestate = 0 # Start off in the middle
if mode == 'highpass':
for harmonic in range(maxHarmonic, 257):
if (waveform == 'sawtooth'):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic)
elif (waveform == 'square'):
if (harmonic & 1):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic)
elif (waveform == 'triangle'):
if (harmonic & 3 == 3):
wavestate = wavestate - (sineWaveLookupTable[sample * harmonic % 256] / harmonic ** 2)
elif (harmonic & 1):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic ** 2)
else: # Mode == 'lowpass'
for harmonic in range(1, maxHarmonic + 1):
if (waveform == 'sawtooth'):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic)
elif (waveform == 'square'):
if (harmonic & 1):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic)
elif (waveform == 'triangle'):
if (harmonic & 3 == 3):
wavestate = wavestate - (sineWaveLookupTable[sample * harmonic % 256] / harmonic ** 2)
elif (harmonic & 1):
wavestate = wavestate + (sineWaveLookupTable[sample * harmonic % 256] / harmonic ** 2)
wavestate = int(wavestate / 1.85)
wavestateAsBinary = struct.pack('B', wavestate + 128);
outputFile.writeframes(wavestateAsBinary)
outputFile.close()
print("Generated", outputFilename)