peterf@2: #!/usr/bin/env python peterf@2: '''CREATED:2014-05-22 16:43:44 by Brian McFee peterf@2: peterf@2: Pitch-shift a recording to be in A440 tuning. peterf@2: peterf@2: Usage: ./adjust_tuning.py [-h] input_file output_file peterf@2: ''' peterf@2: from __future__ import print_function peterf@2: peterf@2: import argparse peterf@2: import sys peterf@2: import librosa peterf@2: peterf@2: peterf@2: def adjust_tuning(input_file, output_file): peterf@2: '''Load audio, estimate tuning, apply pitch correction, and save.''' peterf@2: print('Loading ', input_file) peterf@2: y, sr = librosa.load(input_file) peterf@2: peterf@2: print('Separating harmonic component ... ') peterf@2: y_harm = librosa.effects.harmonic(y) peterf@2: peterf@2: print('Estimating tuning ... ') peterf@2: # Just track the pitches associated with high magnitude peterf@2: tuning = librosa.feature.estimate_tuning(y=y_harm, sr=sr) peterf@2: peterf@2: print('{:+0.2f} cents'.format(100 * tuning)) peterf@2: print('Applying pitch-correction of {:+0.2f} cents'.format(-100 * tuning)) peterf@2: y_tuned = librosa.effects.pitch_shift(y, sr, -tuning) peterf@2: peterf@2: print('Saving tuned audio to: ', output_file) peterf@2: librosa.output.write_wav(output_file, y_tuned, sr) peterf@2: peterf@2: peterf@2: def process_arguments(args): peterf@2: '''Argparse function to get the program parameters''' peterf@2: peterf@2: parser = argparse.ArgumentParser(description='Tuning adjustment example') peterf@2: peterf@2: parser.add_argument('input_file', peterf@2: action='store', peterf@2: help='path to the input file (wav, mp3, etc)') peterf@2: peterf@2: parser.add_argument('output_file', peterf@2: action='store', peterf@2: help='path to store the output wav') peterf@2: peterf@2: return vars(parser.parse_args(args)) peterf@2: peterf@2: peterf@2: if __name__ == '__main__': peterf@2: # Get the parameters peterf@2: params = process_arguments(sys.argv[1:]) peterf@2: peterf@2: # Run the beat tracker peterf@2: adjust_tuning(params['input_file'], params['output_file'])