[2d88f41a] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | from aubio import source, sink, pvoc |
---|
| 5 | from numpy import arange, exp, hstack, zeros, cos |
---|
| 6 | from math import pi |
---|
| 7 | |
---|
| 8 | def gauss(size): |
---|
| 9 | return exp(- 1.0 / (size * size) * pow(2.0* arange(size) - 1. *size, 2.)); |
---|
| 10 | |
---|
| 11 | def hanningz(size): |
---|
| 12 | return 0.5 * (1. - cos(2.*pi*arange(size) / size)) |
---|
| 13 | |
---|
| 14 | if __name__ == '__main__': |
---|
| 15 | if len(sys.argv) < 2: |
---|
[4120fbc] | 16 | print('usage: %s <inputfile> <outputfile>' % sys.argv[0]) |
---|
[2d88f41a] | 17 | sys.exit(1) |
---|
| 18 | samplerate = 0 |
---|
| 19 | if len(sys.argv) > 3: samplerate = int(sys.argv[3]) |
---|
| 20 | f = source(sys.argv[1], samplerate, 256) |
---|
| 21 | samplerate = f.samplerate |
---|
| 22 | g = sink(sys.argv[2], samplerate) |
---|
| 23 | |
---|
| 24 | win_s = 512 # fft size |
---|
[4120fbc] | 25 | hop_s = win_s // 2 # hop size |
---|
[2d88f41a] | 26 | pv = pvoc(win_s, hop_s) # phase vocoder |
---|
| 27 | |
---|
| 28 | # spectral weighting vector |
---|
| 29 | spec_weight = hstack ( [ |
---|
| 30 | .8 * hanningz(80)[40:], |
---|
| 31 | zeros( 50 ), |
---|
| 32 | 1.3 * hanningz(100), |
---|
[4120fbc] | 33 | zeros (win_s // 2 + 1 - 40 - 50 - 100), |
---|
[2d88f41a] | 34 | ] ) |
---|
| 35 | |
---|
| 36 | if 0: |
---|
| 37 | from pylab import plot, show |
---|
| 38 | plot(spec_weight) |
---|
| 39 | show() |
---|
| 40 | |
---|
| 41 | total_frames, read = 0, hop_s |
---|
| 42 | while read: |
---|
| 43 | # get new samples |
---|
| 44 | samples, read = f() |
---|
| 45 | # compute spectrum |
---|
| 46 | spectrum = pv(samples) |
---|
| 47 | # apply weight to spectral amplitudes |
---|
| 48 | spectrum.norm *= spec_weight |
---|
| 49 | # resynthesise modified samples |
---|
| 50 | new_samples = pv.rdo(spectrum) |
---|
| 51 | # write to output |
---|
| 52 | g(new_samples, read) |
---|
| 53 | total_frames += read |
---|
| 54 | |
---|
[4120fbc] | 55 | duration = total_frames / float(samplerate) |
---|
| 56 | print("read {:.3f}s from {:s}".format(duration, f.uri)) |
---|