Skip to content

Instantly share code, notes, and snippets.

@giginet
Created August 28, 2015 15:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save giginet/3d5ab903b789de0d46b0 to your computer and use it in GitHub Desktop.
Save giginet/3d5ab903b789de0d46b0 to your computer and use it in GitHub Desktop.
Sound generation in Python
import sys
import math
import wave
import array
DURATION = 3
FREQUENCY = 440
VOLUME = 100
SAMPLERATE = 44100
DATASIZE = 2
CHANNEL = 2
RANGE = (2 << 14) - 1
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage ./{} [sine/triangle/square]".format(sys.argv[0]))
exit(1)
data = array.array('h')
fpr = int(SAMPLERATE / FREQUENCY)
samples_length = SAMPLERATE * DURATION * CHANNEL
mode = sys.argv[1]
if not mode in ['triangle', 'sine', 'square']:
print("mode is must be 'triangle', 'sine' or 'square'")
exit(1)
for i in range(samples_length):
sample = RANGE * float(VOLUME) / 100
if mode == 'sine':
sample *= math.sin(math.pi * 2 * (i % fpr) / fpr)
elif mode == 'triangle':
sample *= abs((i % fpr) - fpr / 2) / float(fpr)
elif mode == 'square':
sample *= 1 if i % fpr < fpr / 2 else 0
data.append(int(sample))
f = wave.open('{}.wav'.format(mode), 'w')
f.setparams((CHANNEL, DATASIZE, SAMPLERATE, samples_length, "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