annotate gmm_baseline_experiments/external_libs/librosa/librosa-0.3.1/examples/time_stretch.py @ 5:b523456082ca tip

Update path to dataset and reflect modified chunk naming convention.
author peterf
date Mon, 01 Feb 2016 21:35:27 +0000
parents cb535b80218a
children
rev   line source
peterf@2 1 #!/usr/bin/env python
peterf@2 2 '''CREATED:2013-12-08 14:28:34 by Brian McFee <brm2132@columbia.edu>
peterf@2 3
peterf@2 4 Demonstration of phase vocoder time stretching.
peterf@2 5 '''
peterf@2 6 from __future__ import print_function
peterf@2 7
peterf@2 8 import argparse
peterf@2 9 import sys
peterf@2 10 import librosa
peterf@2 11
peterf@2 12
peterf@2 13 def stretch_demo(input_file, output_file, speed):
peterf@2 14 '''Phase-vocoder time stretch demo function.
peterf@2 15
peterf@2 16 :parameters:
peterf@2 17 - input_file : str
peterf@2 18 path to input audio
peterf@2 19 - output_file : str
peterf@2 20 path to save output (wav)
peterf@2 21 - speed : float > 0
peterf@2 22 speed up by this factor
peterf@2 23 '''
peterf@2 24
peterf@2 25 # 1. Load the wav file, resample
peterf@2 26 print('Loading ', input_file)
peterf@2 27
peterf@2 28 y, sr = librosa.load(input_file)
peterf@2 29
peterf@2 30 # 2. Time-stretch through effects module
peterf@2 31 print('Playing back at {:3.0f}% speed'.format(speed * 100))
peterf@2 32
peterf@2 33 y_stretch = librosa.effects.time_stretch(y, speed)
peterf@2 34
peterf@2 35 print('Saving stretched audio to: ', output_file)
peterf@2 36 librosa.output.write_wav(output_file, y_stretch, sr)
peterf@2 37
peterf@2 38
peterf@2 39 def process_arguments(args):
peterf@2 40 '''Argparse function to get the program parameters'''
peterf@2 41
peterf@2 42 parser = argparse.ArgumentParser(description='Time stretching example')
peterf@2 43
peterf@2 44 parser.add_argument('input_file',
peterf@2 45 action='store',
peterf@2 46 help='path to the input file (wav, mp3, etc)')
peterf@2 47
peterf@2 48 parser.add_argument('output_file',
peterf@2 49 action='store',
peterf@2 50 help='path to the stretched output (wav)')
peterf@2 51
peterf@2 52 parser.add_argument('-s', '--speed',
peterf@2 53 action='store',
peterf@2 54 type=float,
peterf@2 55 default=2.0,
peterf@2 56 required=False,
peterf@2 57 help='speed')
peterf@2 58
peterf@2 59 return vars(parser.parse_args(args))
peterf@2 60
peterf@2 61
peterf@2 62 if __name__ == '__main__':
peterf@2 63 # get the parameters
peterf@2 64 parameters = process_arguments(sys.argv[1:])
peterf@2 65
peterf@2 66 # Run the HPSS code
peterf@2 67 stretch_demo(parameters['input_file'],
peterf@2 68 parameters['output_file'],
peterf@2 69 parameters['speed'])