def cpitch(argv=None, cout=None, cerr=None, **kwargs): """ The script front-end """ import sys from chirp.version import version if argv is None: argv = sys.argv[1:] if cout is None: cout = sys.stdout if cerr is None: cerr = sys.stderr import getopt from chirp.common.config import configoptions config = configoptions() maskfile = None opts, args = getopt.getopt(argv, 'hvc:m:') for o, a in opts: if o == '-h': print _scriptdoc return -1 elif o == '-v': print "cpitch version %s" % version return -1 elif o == '-c': if not os.path.exists(a): print >> cout, "ERROR: config file %s doesn't exist" % a return -1 config.read(a) elif o == '-m': maskfile = a if len(args) < 1: print _scriptdoc return -1 print >> cout, "* Program: cpitch" print >> cout, "** Version: %s" % version print >> cout, "* Input: %s" % args[0] from ewave import wavfile try: fp = wavfile(args[0]) except IOError: print >> cerr, "No such file %s" % args[0] return -2 except: print >> cerr, "Input file %s must be in WAV format" % args[0] return -2 pcm = fp.read() samplerate = fp.sampling_rate / 1000. print >> cout, "** Samples:", pcm.size print >> cout, "** Samplerate: %.2f (kHz)" % samplerate pt = tracker(configfile=config, samplerate=samplerate * 1000, **kwargs) print >> cout, pt.spectrogram_options_str() print >> cout, pt.template_options_str() print >> cout, "* DLFT spectrogram:" spec, tgrid, fgrid = pt.matched_spectrogram(pcm, samplerate) print >> cout, "** Dimensions:", spec.shape print >> cout, pt.particle_options_str() if maskfile is not None and os.path.exists(maskfile): from chirp.common.geom import elementlist, masker print >> cout, "* Mask file:", maskfile elems = elementlist.read(maskfile) mask = masker(configfile=config, **kwargs) for startcol, mspec, imask in mask.split(spec, elems, tgrid, fgrid, cout=cout): try: startframe, pitch_mmse, pitch_var, pitch_map, stats = pt.track(mspec, cout=cout, mask=imask) stats['p.map'] = None if pitch_map is None else pitch_map * samplerate T = tgrid[startframe + startcol:startframe + startcol + pitch_mmse.shape[0]] # + tracker.options['winsize'] / (samplerate * 1000) ?? ptrace = pitchtrace(T, pitch_mmse * samplerate, pitch_var * samplerate * samplerate, **stats) print >> cout, "*** Pitch calculations:" ptrace.write(cout) except ValueError, e: print >> cout, "*** Pitch calculation error: %s" % e continue
startframe, pitch_mmse, pitch_var, pitch_map, stats = pt.track(mspec, cout=cout, mask=imask) stats['p.map'] = None if pitch_map is None else pitch_map * samplerate T = tgrid[startframe + startcol:startframe + startcol + pitch_mmse.shape[0]] # + tracker.options['winsize'] / (samplerate * 1000) ?? ptrace = pitchtrace(T, pitch_mmse * samplerate, pitch_var * samplerate * samplerate, **stats) print >> cout, "*** Pitch calculations:" ptrace.write(cout) except ValueError, e: print >> cout, "*** Pitch calculation error: %s" % e continue else: print >> cout, "* No mask file; calculating pitch for entire signal" print >> cout, "** Element 0, interval (%.2f, %.2f)" % (tgrid[0], tgrid[-1]) try: startframe, pitch_mmse, pitch_var, pitch_map, stats = pt.track(spec, cout=cout) stats['p.map'] = None if pitch_map is None else pitch_map * samplerate T = tgrid[startframe:startframe + pitch_mmse.shape[0]] ptrace = pitchtrace(T, pitch_mmse * samplerate, pitch_var * samplerate * samplerate, **stats) print >> cout, "*** Pitch calculations:" ptrace.write(cout) except ValueError, e: print >> cout, "*** Pitch calculation error: %s" % e return 0 # Variables: # End: