summaryrefslogtreecommitdiff
path: root/python/demos/demo_simple_spectral_weighting.py
blob: dd4abbe48b702cf8b5b37a0b855145c4390171c8 (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
51
52
53
54
55
56
#! /usr/bin/env python

import sys
from aubio import source, sink, pvoc
from numpy import arange, exp, hstack, zeros, cos
from math import pi

def gauss(size):
    return exp(- 1.0 / (size * size) * pow(2.0* arange(size) - 1. *size, 2.));

def hanningz(size):
    return 0.5 * (1. - cos(2.*pi*arange(size) / size))

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('usage: %s <inputfile> <outputfile>' % sys.argv[0])
        sys.exit(1)
    samplerate = 0 
    if len(sys.argv) > 3: samplerate = int(sys.argv[3])
    f = source(sys.argv[1], samplerate, 256)
    samplerate = f.samplerate
    g = sink(sys.argv[2], samplerate)

    win_s = 512 # fft size
    hop_s = win_s // 2 # hop size
    pv = pvoc(win_s, hop_s) # phase vocoder

    # spectral weighting vector
    spec_weight = hstack ( [
        .8 * hanningz(80)[40:],
        zeros( 50 ),
        1.3 * hanningz(100),
        zeros (win_s // 2 + 1 - 40 - 50 - 100),
        ] )

    if 0:
        from pylab import plot, show
        plot(spec_weight) 
        show()

    total_frames, read = 0, hop_s
    while read:
        # get new samples
        samples, read = f()
        # compute spectrum
        spectrum = pv(samples)
        # apply weight to spectral amplitudes
        spectrum.norm *= spec_weight
        # resynthesise modified samples
        new_samples = pv.rdo(spectrum)
        # write to output
        g(new_samples, read)
        total_frames += read

    duration = total_frames / float(samplerate)
    print("read {:.3f}s from {:s}".format(duration, f.uri))