summaryrefslogtreecommitdiff
path: root/python/demos/demo_timestretch_online.py
blob: e682e8e2c1caeda7ff9816a4ac31355b40988856 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python

# Implementation of the timescale algorithm according to Dan Ellis, *A Phase
# Vocoder in Matlab*.  http://www.ee.columbia.edu/~dpwe/resources/matlab/pvoc/

# This file performs both analysis and synthesis in a single pass. See also
# `demo_timestretch.py` for a version following the original implementation.

import sys
from aubio import source, sink, pvoc, mfcc, cvec
from aubio import unwrap2pi, float_type
import numpy as np

win_s = 1024
hop_s = win_s / 8 # 87.5 % overlap

warmup = win_s // hop_s - 1

if len(sys.argv) < 3:
    print("Usage: %s <source_filename> <output_filename> <rate> [samplerate]".format(sys.argv[0]))
    print("""Examples:
    # twice faster
    {0} track_01.mp3 track_01_faster.wav 2.0
    # twice slower
    {0} track_02.flac track_02_slower.wav 0.5
    # one and a half time faster, resampling first the input to 22050
    {0} track_02.flac track_02_slower.wav 1.5 22050""".format(sys.argv[0]))
    sys.exit(1)

source_filename = sys.argv[1]
output_filename = sys.argv[2]
rate = float(sys.argv[3])

samplerate = 0 if len(sys.argv) < 5 else int(sys.argv[4])
source_in = source(source_filename, samplerate, hop_s)
samplerate = source_in.samplerate
p = pvoc(win_s, hop_s)

sink_out = sink(output_filename, samplerate)

# excepted phase advance in each bin
phi_advance = np.linspace(0, np.pi * hop_s, win_s / 2 + 1).astype (float_type)

old_grain = cvec(win_s)
new_grain = cvec(win_s)

block_read = 0
interp_read = 0
interp_block = 0
while True:

    samples, read = source_in()
    cur_grain = p(samples)

    if block_read == 1:
        phas_acc = old_grain.phas

    #print "block_read", block_read
    while True and (block_read > 0):
        if interp_read >= block_read:
            break
        #print "`--- interp_block:", interp_block,
        #print 'at orig_block', interp_read, '<- from', block_read - 1, block_read,
        #print 'old_grain', old_grain, 'cur_grain', cur_grain
        # time to compute interp grain
        frac = 1. - np.mod(interp_read, 1.0)

        # compute interpolated frame
        new_grain.norm = frac * old_grain.norm + (1. - frac) * cur_grain.norm
        new_grain.phas = phas_acc

        # psola
        samples = p.rdo(new_grain)
        if interp_read > warmup: # skip the first frames to warm up phase vocoder
            # write to sink
            sink_out(samples, hop_s)

        # calculate phase advance
        dphas = cur_grain.phas - old_grain.phas - phi_advance
        # unwrap angle to [-pi; pi]
        dphas = unwrap2pi(dphas)
        # cumulate phase, to be used for next frame
        phas_acc += phi_advance + dphas

        # prepare for next interp block
        interp_block += 1
        interp_read = interp_block * rate
        if interp_read >= block_read:
            break

    # copy cur_grain to old_grain
    old_grain.norm = np.copy(cur_grain.norm)
    old_grain.phas = np.copy(cur_grain.phas)

    block_read += 1
    if read < hop_s: break

for t in range(warmup + 2): # purge the last frames from the phase vocoder
    new_grain.norm[:] = 0
    new_grain.phas[:] = 0
    samples = p.rdo(new_grain)
    sink_out(samples, read if t == warmup + 1 else hop_s)

# just to make sure
source_in.close()
sink_out.close()

format_out = "read {:d} blocks from {:s} at {:d}Hz and rate {:f}, wrote {:d} blocks to {:s}"
print (format_out.format(block_read, source_filename, samplerate, rate,
    interp_block, output_filename))