File: create_tests_source.py

package info (click to toggle)
aubio 0.4.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,720 kB
  • sloc: python: 20,447; ansic: 20,127; makefile: 348; sh: 232
file content (42 lines) | stat: -rwxr-xr-x 1,263 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env python

""" Create a simple stereo file containing a sine tone at 441 Hz, using only
python's built-in modules. """

import wave
import math
import struct


def create_sine_wave(freq, samplerate, nframes, nchannels):
    """ create a pure tone (without numpy) """
    _x = [0.7 * math.sin(2. * math.pi * freq * t / float(samplerate))
          for t in range(nframes)]
    _x = [int(a * 32767) for a in _x]
    _x = b''.join([b''.join([struct.pack('h', v)
                             for _ in range(nchannels)])
                   for v in _x])
    return _x


def create_test_sound(pathname, freq=441, duration=None,
                      framerate=44100, nchannels=2):
    """ create a sound file at pathname, overwriting exiting file """
    sampwidth = 2
    nframes = duration or framerate  # defaults to 1 second duration
    fid = wave.open(pathname, 'w')
    fid.setnchannels(nchannels)
    fid.setsampwidth(sampwidth)
    fid.setframerate(framerate)
    fid.setnframes(nframes)
    frames = create_sine_wave(freq, framerate, nframes, nchannels)
    fid.writeframes(frames)
    fid.close()
    return 0


if __name__ == '__main__':
    import sys
    if len(sys.argv) < 2:
        sys.exit(2)
    sys.exit(create_test_sound(sys.argv[1]))