summaryrefslogtreecommitdiff
path: root/python/demos/demo_create_test_sounds.py
blob: 3d2f2d061e74f9c042f1ba718bf8456490a7d058 (plain)
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
43
44
45
46
47
48
49
50
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
import numpy as np
from aubio import fvec, sink, float_type

if __name__ == '__main__':
    if len(sys.argv) < 1:
        print('usage: %s' % sys.argv[0])
        sys.exit(1)

    samplerate = 44100
    hop_size = 256

    # create python/tests/sounds if needed
    output_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    output_dir = os.path.join(output_dir, 'tests', 'sounds')
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    filenames = ['44100Hz_1f_silence.wav',
                 '22050Hz_5s_brownnoise.wav',
                 '32000Hz_127f_sine440.wav',
                ]
    samplerates = [44100, 22050, 32000]
    durations = [1, 5*22050, 127]

    for fname, samplerate, duration in zip(filenames, samplerates, durations):
        output_name = os.path.join(output_dir, fname)
        g = sink(output_name, samplerate)
        total_frames = 0
        while total_frames < duration:
            write = min(hop_size, duration - total_frames)
            if 'brownnoise' in fname:
                vec = np.random.rand(write).astype(float_type) * 2. - 1.
            elif 'sine' in fname:
                freq = 440
                t = np.arange(write).astype(float_type) + total_frames
                vec = np.sin(2. * np.pi * freq * t / float(samplerate))
            else:
                # silence
                vec = fvec(write)
            g(vec, write)
            total_frames += write
        outstr = "wrote {:2f}s".format(total_frames / float(samplerate))
        outstr += " ({:d} frames".format(total_frames)
        outstr += " at {:d}Hz)".format(g.samplerate)
        outstr += " to {:s}".format(g.uri)
        print(outstr)