def main(argv): import getopt from wavestream import WaveReader def usage(): print 'usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] wav ...' % argv[0] return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.9 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) 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() for (t,p) in enumerate(contour.segments): print t, p return
def load_wav(self, wavpath, pitchpath): src = WaveReader(wavpath) fp = open(pitchpath) for line in fp: line = line.strip() if not line: print (line, _, _) = line.partition('#') if not line: continue (f, _, pitch) = line.partition(' ') f = int(f) pitch = int(pitch) src.seek(f) r = [] (nframes, data) = src.readraw(int(src.framerate / pitch)) for (name, pat) in self.pats: s = wavcorr.matchs16(pat, 0, nframes, data) if self.threshold <= s: r.append((s, name)) if r: r.sort(reverse=True) print f, ' '.join('%.04f:%s' % (s, name) for (s, name) in r) fp.close() src.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 load_wav(self, wavpath, pitchpath): src = WaveReader(wavpath) fp = open(pitchpath) for line in fp: line = line.strip() if not line: print (line,_,_) = line.partition('#') if not line: continue (f, _, pitch) = line.partition(' ') f = int(f) pitch = int(pitch) src.seek(f) r = [] (nframes, data) = src.readraw(int(src.framerate/pitch)) for (name,pat) in self.pats: s = wavcorr.matchs16(pat, 0, nframes, data) if self.threshold <= s: r.append((s, name)) if r: r.sort(reverse=True) print f, ' '.join( '%.04f:%s' % (s,name) for (s,name) in r ) fp.close() src.close() return
def main(argv): import getopt from wavestream import WaveReader def usage(): print 'usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] wav ...' % argv[ 0] return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.9 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) 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() for (t, p) in enumerate(contour.segments): print t, p 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 def usage(): print 'usage: %s [-v] [-o out.wav] [-t f0-f1] wav ...' % argv[0] return 100 def getv(v): if '.' in v: return float(v) else: return int(v) try: (opts, args) = getopt.getopt(argv[1:], 'vo:t:') except getopt.GetoptError: return usage() verbose = 0 outfp = None ranges = [] for (k, v) in opts: if k == '-v': verbose += 1 elif k == '-o': outfp = open(v, 'wb') elif k == '-t': (f0,_,f1) = v.partition('-') ranges.append((getv(f0), getv(f1))) dst = None for path in args: src = WaveReader(path) if verbose: print >>sys.stderr, \ ('%s: nchannels=%r, sampwidth=%r, framerate=%r, nframes=%r' % (path, src.nchannels, src.sampwidth, src.framerate, src.nframes)) if dst is None: if outfp is None: dst = WavePlayer(nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) else: dst = WaveWriter(outfp, nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) for (f0,f1) in ranges: if isinstance(f0, float): f0 = int(f0*src.framerate) if isinstance(f1, float): f1 = int(f1*src.framerate) src.seek(f0) if f1 < f0: f1 = src.nframes (_,buf) = src.readraw(f1-f0) dst.writeraw(buf) src.close() if dst is not None: dst.close() if outfp is not None: outfp.close() return
def main(argv): import getopt def usage(): print 'usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] wav ...' % argv[ 0] return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.9 bufsize = 10000 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) detector = None for path in args: src = WaveReader(path) if detector is None: detector = PitchDetector(src.framerate, pitchmin=pitchmin, pitchmax=pitchmax) i = 0 while 1: (nframes, buf) = src.readraw(bufsize) if not nframes: break pitches = detector.feed(buf, nframes) for (n, t, freq, mag, data) in pitches: if threshold <= t: print i, n, t, freq, mag else: print i, n, t i += n src.close() return
def fradosify(path, outfp, delta): 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') dst = WaveWriter(outfp, framerate=src.framerate) contour = PitchContour(src.framerate) contour.load(src) def f(t): x = contour.getavg(t) if x is not None: x = int(x*ratio) return x src.seek(0) dst.write(psola(src.read(), src.framerate, contour.getsrc, f, contour.wmin)) src.close() dst.close() return
def main(argv): import getopt def usage(): print 'usage: %s [-M|-F] [-n pitchmin] [-m pitchmax] [-t threshold] wav ...' % argv[0] return 100 try: (opts, args) = getopt.getopt(argv[1:], 'MFn:m:t:') except getopt.GetoptError: return usage() pitchmin = 70 pitchmax = 400 threshold = 0.9 bufsize = 10000 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) detector = None for path in args: src = WaveReader(path) if detector is None: detector = PitchDetector(src.framerate, pitchmin=pitchmin, pitchmax=pitchmax) i = 0 while 1: (nframes,buf) = src.readraw(bufsize) if not nframes: break pitches = detector.feed(buf, nframes) for (n,t,freq,mag,data) in pitches: if threshold <= t: print i,n,t,freq,mag else: print i,n,t i += n src.close() return
def load_pat(self, path, name=None): fp = WaveReader(path) (_, pat) = fp.readraw() self.pats.append((name or path, pat)) fp.close() return
def main(argv): import getopt import fileinput def usage(): print 'usage: %s [-v] [-o out.wav] [script ...]' % argv[0] return 100 def getv(v): try: if '.' in v: return float(v) else: return int(v) except ValueError: return 0 try: (opts, args) = getopt.getopt(argv[1:], 'vo:') except getopt.GetoptError: return usage() verbose = 0 outfp = None for (k, v) in opts: if k == '-v': verbose += 1 elif k == '-o': outfp = open(v, 'wb') # if not args: return usage() path = args.pop(0) src = WaveReader(path) # waves = [] for line in fileinput.input(args): (line,_,_) = line.partition('#') line = line.strip() if not line: continue (t,dur,path) = line.split('\t') t = getv(t) dur = getv(dur) wav = WaveReader(path) assert wav.nchannels == src.nchannels assert wav.sampwidth == src.sampwidth #assert wav.framerate == src.framerate if isinstance(t, float): t = int(t*src.framerate) if isinstance(dur, float): dur = int(dur*src.framerate) buf = wav.read(dur) wav.close() waves.append((t, buf)) waves.append((src.nframes, [])) # if outfp is not None: dst = WaveWriter(outfp, nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) else: dst = WavePlayer(nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) # t0 = 0 bufs = [] for (t1,buf1) in sorted(waves, key=lambda (t,_): t): dt = (t1-t0)*dst.nchannels tmp = [src.read(t1-t0)] assert len(tmp[0]) == dt for (i,b) in enumerate(bufs): if dt <= len(b): tmp.append(b[:dt]) else: tmp.append(b+[0]*(dt-len(b))) bufs[i] = b[dt:] bufs.append(buf1) bufs = [ b for b in bufs if b ] dst.write(mix(tmp)) t0 = t1 # dst.close() if outfp is not None: outfp.close() return
def main(argv): import getopt from wavestream import WaveReader def usage(): print( 'usage: %s [-d] [-M|-F] [-n pitchmin] [-m pitchmax]' ' [-T threshold_sim] [-S threshold_mag] wav ...' % argv[0]) return 100 def parse_range(x): (b, _, e) = x.partition('-') try: b = int(b) except ValueError: b = 0 try: e = int(e) except ValueError: e = 0 return (b, e) try: (opts, args) = getopt.getopt(argv[1:], 'dMFn:m:T:S:') except getopt.GetoptError: return usage() debug = 0 pitchmin = 70 pitchmax = 400 threshold_sim = 0.75 threshold_mag = 0.025 bufsize = 10000 for (k, v) in opts: if k == '-d': debug += 1 elif 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_sim = float(v) elif k == '-S': threshold_mag = float(v) detector = None smoother = None for arg1 in args: (path, _, ranges) = arg1.partition(':') ranges = [parse_range(x) for x in ranges.split(',') if x] if not ranges: ranges.append((0, 0)) print '#', path, ranges src = WaveReader(path) if src.nchannels != 1: raise ValueError('invalid number of channels') if src.sampwidth != 2: raise ValueError('invalid sampling width') framerate = src.framerate if detector is None: detector = PitchDetector(wmin=framerate / pitchmax, wmax=framerate / pitchmin, threshold_sim=threshold_sim) smoother = PitchSmoother(2 * framerate / pitchmin, threshold_sim=threshold_sim, threshold_mag=threshold_mag) for (b, e) in ranges: if e == 0: e = src.nframes src.seek(b) length = e - b i0 = b while length: (nframes, buf) = src.readraw(min(bufsize, length)) if not nframes: break length -= nframes seq = detector.feed(buf, nframes) for (n0, pitches, data) in seq: if debug: print('# %d: %r' % (n0, pitches)) for streak in smoother.feed(i0, n0, pitches): for (i1, n1, spitches) in streak: print i1, n1, ' '.join('%d:%.4f' % (w, sim) for (_, w, sim) in spitches) print i0 += n0 src.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 import fileinput def usage(): print 'usage: %s [-v] [-o out.wav] [script ...]' % argv[0] return 100 def getv(v): try: if '.' in v: return float(v) else: return int(v) except ValueError: return 0 try: (opts, args) = getopt.getopt(argv[1:], 'vo:') except getopt.GetoptError: return usage() verbose = 0 outfp = None for (k, v) in opts: if k == '-v': verbose += 1 elif k == '-o': outfp = open(v, 'wb') # if not args: return usage() path = args.pop(0) src = WaveReader(path) # waves = [] for line in fileinput.input(args): (line, _, _) = line.partition('#') line = line.strip() if not line: continue (t, dur, path) = line.split('\t') t = getv(t) dur = getv(dur) wav = WaveReader(path) assert wav.nchannels == src.nchannels assert wav.sampwidth == src.sampwidth #assert wav.framerate == src.framerate if isinstance(t, float): t = int(t * src.framerate) if isinstance(dur, float): dur = int(dur * src.framerate) buf = wav.read(dur) wav.close() waves.append((t, buf)) waves.append((src.nframes, [])) # if outfp is not None: dst = WaveWriter(outfp, nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) else: dst = WavePlayer(nchannels=src.nchannels, sampwidth=src.sampwidth, framerate=src.framerate) # t0 = 0 bufs = [] for (t1, buf1) in sorted(waves, key=lambda (t, _): t): dt = (t1 - t0) * dst.nchannels tmp = [src.read(t1 - t0)] assert len(tmp[0]) == dt for (i, b) in enumerate(bufs): if dt <= len(b): tmp.append(b[:dt]) else: tmp.append(b + [0] * (dt - len(b))) bufs[i] = b[dt:] bufs.append(buf1) bufs = [b for b in bufs if b] dst.write(mix(tmp)) t0 = t1 # dst.close() if outfp is not None: outfp.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.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
def load_pat(self, path, name=None): fp = WaveReader(path) (_, pat) = fp.readraw() self.pats.append((name or path, pat)) fp.close() return