source: python/aubiopitch @ 5e491b3b

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 5e491b3b was 5e491b3b, checked in by Paul Brossier <piem@altern.org>, 19 years ago

massive changes from cam

  • Property mode set to 100755
File size: 5.7 KB
Line 
1#!/usr/bin/python
2
3""" this file was written by Paul Brossier
4  it is released under the GNU/GPL license.
5"""
6
7import sys
8from aubio.tasks import *
9
10usage = "usage: %s [options] -i soundfile" % sys.argv[0]
11
12
13def parse_args():
14        from optparse import OptionParser
15        parser = OptionParser(usage=usage)
16        parser.add_option("-i","--input",
17                          action="store", dest="filename", 
18                          help="input sound file")
19        parser.add_option("-m","--mode", action="callback", 
20                          callback=check_pitch_mode, dest="mode", 
21                          default=aubio_pitch_mcomb,
22                          help="pitch detection mode [default=mcomb] \
23                          mcomb|yin|fcomb|schmitt")
24        parser.add_option("-u","--units", action="callback", 
25                          callback=check_pitchm_mode, dest="omode",
26                          default=aubio_pitchm_freq,
27                          help="output pitch in units [default=Hz] \
28                          freq|midi|cent|bin")
29        parser.add_option("-B","--bufsize",
30                          action="store", dest="bufsize", default=None, 
31                          help="buffer size [default=1024]")
32        parser.add_option("-H","--hopsize",
33                          action="store", dest="hopsize", default=None, 
34                          help="overlap size [default=512]")
35        parser.add_option("-t","--threshold",
36                          action="store", dest="threshold", default=0.1, 
37                          help="pitch threshold (for yin) [default=0.1]")
38        parser.add_option("-s","--silence",
39                          action="store", dest="silence", default=-70, 
40                          help="silence threshold [default=-70]")
41        parser.add_option("-D","--delay",
42                          action="store", dest="delay", 
43                          help="number of seconds to take back [default=system]\
44                          default system delay is 2*hopsize/samplerate")
45        parser.add_option("-L","--localmin",
46                          action="store_true", dest="localmin", default=False, 
47                          help="use local minima after peak detection")
48        parser.add_option("-c","--cut",
49                          action="store_true", dest="cut", default=False,
50                          help="cut input sound file at detected labels \
51                          best used with option -L")
52        # to be implemented
53        parser.add_option("-n","--note",
54                          action="store_true", dest="note", default=False,
55                          help="NOT IMPLEMENTED output notes")
56        # plotting functions
57        parser.add_option("-p","--plot",
58                          action="store_true", dest="plot", default=False, 
59                          help="NOT IMPLEMENTED draw plot")
60        parser.add_option("-O","--outplot",
61                          action="store", dest="outplot", default=None, 
62                          help="NOT IMPLEMENTED save plot to output.{ps,png}")
63        parser.add_option("-v","--verbose",
64                          action="store_true", dest="verbose", default=False,
65                          help="make lots of noise [default]")
66        parser.add_option("-q","--quiet",
67                          action="store_false", dest="verbose", default=False, 
68                          help="be quiet")
69        (options, args) = parser.parse_args()
70        if not options.bufsize:
71                if options.mode == aubio_pitch_yin:     options.bufsize = 1024
72                if options.mode == aubio_pitch_schmitt: options.bufsize = 2048
73                if options.mode == aubio_pitch_mcomb:   options.bufsize = 4096
74                if options.mode == aubio_pitch_fcomb:   options.bufsize = 4096 
75                else: options.bufsize = 2048
76        if not options.hopsize:
77                options.hopsize = float(options.bufsize) / 2
78        if not options.filename: 
79                print "no file name given\n", usage
80                sys.exit(1)
81        return options, args
82
83options, args = parse_args()
84
85#print options.bufsize, options.hopsize
86
87filename   = options.filename
88samplerate = float(sndfile(filename).samplerate())
89hopsize    = int(options.hopsize)
90bufsize    = int(options.bufsize)
91step       = float(samplerate)/float(hopsize)
92threshold  = float(options.threshold)
93silence    = float(options.silence)
94mode       = options.mode
95#mintol     = float(options.mintol)*step
96# default take back system delay
97if options.delay: delay = float(options.delay)
98else:             delay = 2./step
99
100if options.note:
101        exit("not implemented yet")
102else:
103        pitch = []
104        for i in range(len(mode)):
105                pitch.append(getpitch(filename, #threshold,
106                        mode=mode[i],
107                        omode=options.omode,
108                        bufsize=bufsize,hopsize=hopsize,
109                        silence=silence))
110
111## take back system delay
112#if delay != 0:
113#        for i in range(len(onsets)):
114#                onsets[i] -= delay*step
115#
116## prune doubled
117#if mintol > 0:
118#        last = -2*mintol
119#        newonsets = []
120#        for new in onsets:
121#                if (new - last > mintol):
122#                        newonsets.append(new)
123#                last = new
124#        onsets = newonsets
125
126# print times in second
127if options.verbose:
128        for j in range(len(pitch[0])): 
129                print "%f\t" % (j/step),
130                for i in range(len(pitch)): 
131                        print "%f\t" % pitch[i][j],
132                print
133
134if options.plot:
135        from aubio.gnuplot import plot_pitch
136        plot_pitch(filename, pitch, 
137                samplerate=samplerate, hopsize=hopsize, outplot=options.outplot)
Note: See TracBrowser for help on using the repository browser.