def fradosify(path, outfp, delta, pitchmin=70, pitchmax=400, threshold=0.7): print >> sys.stderr, 'reading: %r' % path ratio = pow(2, delta / 12.0) src = WaveReader(path) if src.nchannels != 1: raise ValueError('invalid number of channels') if src.sampwidth != 2: raise ValueError('invalid sampling width') contour = PitchContour(src.framerate, pitchmin=pitchmin, pitchmax=pitchmax, threshold=threshold) dst = WaveWriter(outfp, framerate=src.framerate) nframes = src.nframes buf = src.readraw(nframes) contour.reset() contour.load(buf, nframes) def f(t): x = contour.getavg(t) if x != 0: x = int(x * ratio) return x dst.writeraw(psola(buf, src.framerate, contour.getsrc, f, contour.wmin)) src.close() dst.close() return
def fradosify(path, outfp, delta, pitchmin=70, pitchmax=400, threshold=0.7): print >>sys.stderr, 'reading: %r' % path ratio = pow(2, delta/12.0) src = WaveReader(path) if src.nchannels != 1: raise ValueError('invalid number of channels') if src.sampwidth != 2: raise ValueError('invalid sampling width') contour = PitchContour( src.framerate, pitchmin=pitchmin, pitchmax=pitchmax, threshold=threshold) dst = WaveWriter(outfp, framerate=src.framerate) nframes = src.nframes buf = src.readraw(nframes) contour.reset() contour.load(buf, nframes) def f(t): x = contour.getavg(t) if x != 0: x = int(x*ratio) return x dst.writeraw(psola(buf, src.framerate, contour.getsrc, f, contour.wmin)) src.close() dst.close() return
def main(argv): import getopt from wavestream import WaveReader def usage(): print ('usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] ' '[-o out.mid] [-w wsize] [-p instru] wav ...' % argv[0]) return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:o:w:p:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.95 outpath = 'out.mid' wsize = 50 instru = 0 attack = 70 release = 70 for (k, v) in opts: if k == '-M': (pitchmin,pitchmax) = (75,200) # male voice elif k == '-F': (pitchmin,pitchmax) = (150,300) # female voice elif k == '-n': pitchmin = int(v) elif k == '-m': pitchmax = int(v) elif k == '-t': threshold = float(v) elif k == '-o': outpath = v elif k == '-w': wsize = int(v) elif k == '-p': instru = int(v) contour = None for path in args: src = WaveReader(path) if contour is None: contour = PitchContour(src.framerate, pitchmin=pitchmin, pitchmax=pitchmax, threshold=threshold) contour.load(src.readraw(), src.nframes) src.close() # events = [midi.ProgramChangeEvent(tick=0, channel=0, data=[instru])] window = [] km0 = 0 kt = 0 kd = 0 for p in contour.segments: if p == 0: k = 0 else: i = getpt(FRANGES, p) (_,k) = FRANGES[i-1] #print '%d/%d' % (p,k), window.append(k) if len(window) < wsize: continue window = window[1:] km1 = majority(window) if km0 == km1: kd += 1 else: print km0, kd if km0 == 0: kt += kd else: events.append(midi.NoteOnEvent(tick=kt, channel=0, data=[km0, attack])) events.append(midi.NoteOffEvent(tick=kd, channel=0, data=[km0, release])) kt = 0 kd = 0 km0 = km1 events.append(midi.EndOfTrackEvent(tick=0, data=[])) pat = midi.Pattern(tracks=[events]) midi.write_midifile(outpath, pat) return
def main(argv): import getopt from wavestream import WaveReader def usage(): print( 'usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] ' '[-o out.mid] [-w wsize] [-p instru] wav ...' % argv[0]) return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:o:w:p:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.97 outpath = 'out.mid' wsize = 50 instru = 0 attack = 70 release = 70 for (k, v) in opts: if k == '-M': (pitchmin, pitchmax) = (75, 200) # male voice elif k == '-F': (pitchmin, pitchmax) = (150, 300) # female voice elif k == '-n': pitchmin = int(v) elif k == '-m': pitchmax = int(v) elif k == '-t': threshold = float(v) elif k == '-o': outpath = v elif k == '-w': wsize = int(v) elif k == '-p': instru = int(v) contour = None for path in args: src = WaveReader(path) if contour is None: contour = PitchContour(src.framerate, pitchmin=pitchmin, pitchmax=pitchmax, threshold=threshold) contour.load(src.readraw(), src.nframes) src.close() events = [midi.ProgramChangeEvent(tick=0, channel=0, data=[instru])] window = [] km0 = 0 kt = 0 kd = 0 piano = open("../server/piano.txt", "w") for p in contour.segments: if p == 0: k = 0 else: i = getpt(FRANGES, p) (_, k) = FRANGES[i - 1] window.append(k) if len(window) < wsize: continue window = window[1:] km1 = majority(window) if km0 == km1: kd += 1 else: print km0, kd piano.write(str(km0) + ' ' + str(kd) + '\r\n') if km0 == 0: kt += kd else: events.append( midi.NoteOnEvent(tick=kt, channel=0, data=[km0, attack])) events.append( midi.NoteOffEvent(tick=kd, channel=0, data=[km0, release])) kt = 0 kd = 0 km0 = km1 piano.write('EOF') piano.close() events.append(midi.EndOfTrackEvent(tick=0, data=[])) pat = midi.Pattern(tracks=[events]) midi.write_midifile(outpath, pat) return