source: tests/create_tests_source.py @ b610be5

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since b610be5 was b610be5, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[tests] pure python create_sine_wave

  • Property mode set to 100755
File size: 1.2 KB
Line 
1#! /usr/bin/env python
2
3""" Create a simple stereo file containing a sine tone at 441 Hz, using only
4python's built-in modules. """
5
6import wave
7import math
8import struct
9
10
11def create_sine_wave(freq, samplerate, nframes, nchannels):
12    """ create a pure tone (without numpy) """
13    _x = [0.7 * math.sin(2. * math.pi * freq * t / float(samplerate))
14          for t in range(nframes)]
15    _x = [int(a * 32767) for a in _x]
16    _x = b''.join([b''.join([struct.pack('h', v)
17                             for _ in range(nchannels)])
18                   for v in _x])
19    return _x
20
21
22def create_test_sound(pathname, freq=441, duration=None,
23                      framerate=44100, nchannels=2):
24    """ create a sound file at pathname, overwriting exiting file """
25    sampwidth = 2
26    nframes = duration or framerate  # defaults to 1 second duration
27    fid = wave.open(pathname, 'w')
28    fid.setnchannels(nchannels)
29    fid.setsampwidth(sampwidth)
30    fid.setframerate(framerate)
31    fid.setnframes(nframes)
32    frames = create_sine_wave(freq, framerate, nframes, nchannels)
33    fid.writeframes(frames)
34    fid.close()
35    return 0
36
37
38if __name__ == '__main__':
39    import sys
40    if len(sys.argv) < 2:
41        sys.exit(2)
42    sys.exit(create_test_sound(sys.argv[1]))
Note: See TracBrowser for help on using the repository browser.