Changeset b610be5
- Timestamp:
- Nov 29, 2018, 4:08:24 PM (6 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master
- Children:
- da67922
- Parents:
- 60384e9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/create_tests_source.py
r60384e9 rb610be5 2 2 3 3 """ Create a simple stereo file containing a sine tone at 441 Hz, using only 4 numpy and python's native wave module. """4 python's built-in modules. """ 5 5 6 6 import wave 7 import numpy as np 7 import math 8 import struct 8 9 9 10 10 11 def create_sine_wave(freq, samplerate, nframes, nchannels): 11 """ create a pure tone """ 12 # samples indices 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() 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 18 20 19 21 20 22 def create_test_sound(pathname, freq=441, duration=None, 21 sampwidth=2,framerate=44100, nchannels=2):23 framerate=44100, nchannels=2): 22 24 """ create a sound file at pathname, overwriting exiting file """ 25 sampwidth = 2 23 26 nframes = duration or framerate # defaults to 1 second duration 24 27 fid = wave.open(pathname, 'w') … … 28 31 fid.setnframes(nframes) 29 32 frames = create_sine_wave(freq, framerate, nframes, nchannels) 30 fid.writeframes(frames .tobytes())33 fid.writeframes(frames) 31 34 fid.close() 32 35 return 0
Note: See TracChangeset
for help on using the changeset viewer.