forked from librosa/librosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_stretch.py
executable file
·69 lines (49 loc) · 1.86 KB
/
time_stretch.py
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
#!/usr/bin/env python
'''CREATED:2013-12-08 14:28:34 by Brian McFee <brm2132@columbia.edu>
Demonstration of phase vocoder time stretching.
'''
from __future__ import print_function
import argparse
import sys
import librosa
def stretch_demo(input_file, output_file, speed):
'''Phase-vocoder time stretch demo function.
:parameters:
- input_file : str
path to input audio
- output_file : str
path to save output (wav)
- speed : float > 0
speed up by this factor
'''
# 1. Load the wav file, resample
print('Loading ', input_file)
y, sr = librosa.load(input_file)
# 2. Time-stretch through effects module
print('Playing back at {:3.0f}% speed'.format(speed * 100))
y_stretch = librosa.effects.time_stretch(y, speed)
print('Saving stretched audio to: ', output_file)
librosa.output.write_wav(output_file, y_stretch, sr)
def process_arguments(args):
'''Argparse function to get the program parameters'''
parser = argparse.ArgumentParser(description='Time stretching example')
parser.add_argument('input_file',
action='store',
help='path to the input file (wav, mp3, etc)')
parser.add_argument('output_file',
action='store',
help='path to the stretched output (wav)')
parser.add_argument('-s', '--speed',
action='store',
type=float,
default=2.0,
required=False,
help='speed')
return vars(parser.parse_args(args))
if __name__ == '__main__':
# get the parameters
parameters = process_arguments(sys.argv[1:])
# Run the HPSS code
stretch_demo(parameters['input_file'],
parameters['output_file'],
parameters['speed'])