Skip to content

Instantly share code, notes, and snippets.

@pranavrc
Created January 16, 2012 06:23
Show Gist options
  • Save pranavrc/1619379 to your computer and use it in GitHub Desktop.
Save pranavrc/1619379 to your computer and use it in GitHub Desktop.
Function that generates sound of parameter frequency
from struct import pack
from math import sin, pi
import time
from populate import _listen_
def _play_(name, freq, dur, vol, mode='ab'):
""" Generate sound with frequency freq, duration dur, and volume vol """
_dump_ = open(name, mode)
_dump_.write('.snd' + pack('>5L', 24, 8*dur, 2, 8000, 1))
_factor_ = 2 * pi * freq/8000
for count in range(8 * dur):
_wave_ = sin(count * _factor_)
_dump_.write(pack('b', vol * 64 * _wave_))
_dump_.close()
if __name__=="__main__":
_play_("foo.au", 50, 1000, 1, 'ab')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment