feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change
on this file since be94d24 was
b610be5,
checked in by Paul Brossier <piem@piem.org>, 6 years ago
|
[tests] pure python create_sine_wave
|
-
Property mode set to
100755
|
File size:
1.2 KB
|
Rev | Line | |
---|
[703cc2b] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ Create a simple stereo file containing a sine tone at 441 Hz, using only |
---|
[b610be5] | 4 | python's built-in modules. """ |
---|
[703cc2b] | 5 | |
---|
| 6 | import wave |
---|
[b610be5] | 7 | import math |
---|
| 8 | import struct |
---|
[703cc2b] | 9 | |
---|
| 10 | |
---|
| 11 | def create_sine_wave(freq, samplerate, nframes, nchannels): |
---|
[b610be5] | 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 |
---|
[703cc2b] | 20 | |
---|
| 21 | |
---|
| 22 | def create_test_sound(pathname, freq=441, duration=None, |
---|
[b610be5] | 23 | framerate=44100, nchannels=2): |
---|
[703cc2b] | 24 | """ create a sound file at pathname, overwriting exiting file """ |
---|
[b610be5] | 25 | sampwidth = 2 |
---|
[703cc2b] | 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) |
---|
[b610be5] | 33 | fid.writeframes(frames) |
---|
[703cc2b] | 34 | fid.close() |
---|
| 35 | return 0 |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | if __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.