peterf@2: #!/usr/bin/env python peterf@2: '''CREATED:2013-12-09 00:02:54 by Brian McFee peterf@2: peterf@2: Estimate the tuning (deviation from A440) of a recording. peterf@2: peterf@2: Usage: ./tuning.py [-h] input_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 estimate_tuning(input_file): peterf@2: '''Load an audio file and estimate tuning (in cents)''' peterf@2: 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: 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 estimation 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: return vars(parser.parse_args(args)) peterf@2: peterf@2: peterf@2: if __name__ == '__main__': peterf@2: # Get the parameters peterf@2: parameters = process_arguments(sys.argv[1:]) peterf@2: peterf@2: # Run the beat tracker peterf@2: estimate_tuning(parameters['input_file'])