3 """ Create a simple stereo file containing a sine tone at 441 Hz, using only
4 numpy and python's native wave module. """
10 def create_sine_wave(freq, samplerate, nframes, nchannels):
11 """ create a pure tone """
13 _t = np.tile(np.arange(nframes), (nchannels, 1))
14 # sine wave generation
15 _x = 0.7 * np.sin(2. * np.pi * freq * _t / float(samplerate))
16 # conversion to int and channel interleaving
17 return (_x * 32767.).astype(np.int16).T.flatten()
20 def create_test_sound(pathname, freq=441, duration=None,
21 sampwidth=2, framerate=44100, nchannels=2):
22 """ create a sound file at pathname, overwriting exiting file """
23 nframes = duration or framerate # defaults to 1 second duration
24 fid = wave.open(pathname, 'w')
25 fid.setnchannels(nchannels)
26 fid.setsampwidth(sampwidth)
27 fid.setframerate(framerate)
28 fid.setnframes(nframes)
29 frames = create_sine_wave(freq, framerate, nframes, nchannels)
30 fid.writeframes(frames.tobytes())
35 if __name__ == '__main__':
39 sys.exit(create_test_sound(sys.argv[1]))