Changeset b610be5


Ignore:
Timestamp:
Nov 29, 2018, 4:08:24 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
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
Message:

[tests] pure python create_sine_wave

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/create_tests_source.py

    r60384e9 rb610be5  
    22
    33""" Create a simple stereo file containing a sine tone at 441 Hz, using only
    4 numpy and python's native wave module. """
     4python's built-in modules. """
    55
    66import wave
    7 import numpy as np
     7import math
     8import struct
    89
    910
    1011def 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
    1820
    1921
    2022def create_test_sound(pathname, freq=441, duration=None,
    21                       sampwidth=2, framerate=44100, nchannels=2):
     23                      framerate=44100, nchannels=2):
    2224    """ create a sound file at pathname, overwriting exiting file """
     25    sampwidth = 2
    2326    nframes = duration or framerate  # defaults to 1 second duration
    2427    fid = wave.open(pathname, 'w')
     
    2831    fid.setnframes(nframes)
    2932    frames = create_sine_wave(freq, framerate, nframes, nchannels)
    30     fid.writeframes(frames.tobytes())
     33    fid.writeframes(frames)
    3134    fid.close()
    3235    return 0
Note: See TracChangeset for help on using the changeset viewer.