Skip to content

Instantly share code, notes, and snippets.

@jarlg
Created October 4, 2015 09:34
Show Gist options
  • Save jarlg/f4a02b8f304aad9dbcb9 to your computer and use it in GitHub Desktop.
Save jarlg/f4a02b8f304aad9dbcb9 to your computer and use it in GitHub Desktop.
small test of making audio in python
import wave
import math
import struct
samplerate = 44100
frequency1 = 220
frequency2 = 219
duration = 5
def signal():
samples = []
for x in range(duration * samplerate):
samples.append(
1/2 * (math.sin(frequency1 * 2 * math.pi * x / samplerate) +\
math.sin(frequency2 * 2 * math.pi * x /samplerate))
)
return samples
# w = file.open()
#...
#.. auto: w.close()
with wave.open("test.wav", 'wb') as w:
w.setparams((2, 2, samplerate, samplerate * duration, 'NONE', 'not compressed'))
max_amplitude = 32767.0
samples = [int(sample * max_amplitude) for sample in signal()]
for sample in samples:
w.writeframes(struct.pack('h', sample))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment