Skip to content

Instantly share code, notes, and snippets.

@KenjiTakahashi
Created February 13, 2013 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenjiTakahashi/4944463 to your computer and use it in GitHub Desktop.
Save KenjiTakahashi/4944463 to your computer and use it in GitHub Desktop.
import wave
import random
import struct
import datetime
SAMPLE_LEN = 44100 * 3 # 300 seconds of noise, 5 minutes
print "Create file using wave, storing frames in an array and using writeframes only once"
noise_output = wave.open('noise2.wav', 'w')
noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))
d1 = datetime.datetime.now()
values = []
for i in range(0, SAMPLE_LEN):
value = random.randint(-32767, 32767)
packed_value = struct.pack('h', value)
values.append(packed_value)
values.append(packed_value)
value_str = ''.join(values)
print value_str
noise_output.writeframes(value_str)
d2 = datetime.datetime.now()
print (d2 - d1), "(time for writing frames)"
noise_output.close()
d3 = datetime.datetime.now()
print (d3 - d2), "(time for closing the file)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment