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 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 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 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 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
class WavEd(object): def __init__(self): self._wav = None self._cur = None self._curs = {} self._player = None return def stop(self): if self._player is not None: self._player.stop() self._player = None return def close(self): self.stop() if self._wav is not None: self._wav.close() self._wav = None self._cur = None self._curs = {} return def read(self, path): self.close() self._wav = WaveReader(path) self._cur = WavCursor(self._wav) self._cur.set_length('1.0') self._curs = {} print('Read: rate=%d, frames=%d, duration=%.3f' % (self._wav.framerate, self._wav.nframes, self._wav.nframes / float(self._wav.framerate))) return def write(self, cur, path, force=False): if not force and os.path.exists(path): raise WavEdError('File exists: %r' % path) nframes = cur.get_length() fp = open(path, 'wb') writer = WaveWriter(fp, nchannels=self._wav.nchannels, sampwidth=self._wav.sampwidth, framerate=self._wav.framerate) self._wav.seek(cur.start) (_, data) = self._wav.readraw(nframes) writer.writeraw(data) writer.close() fp.close() print('Written: %r, rate=%d, frames=%d, duration=%.3f' % (path, writer.framerate, nframes, nframes / float(writer.framerate))) return def play(self, cur): nframes = cur.get_length() self.stop() self._player = WavePlayer(nchannels=self._wav.nchannels, sampwidth=self._wav.sampwidth, framerate=self._wav.framerate) self._wav.seek(cur.start) (_, data) = self._wav.readraw(nframes) self._player.writeraw(data) self._player.flush() return def show(self, cur): print cur return def run(self): self.exec_command('p') while 1: try: s = raw_input('> ') self.stop() s = s.strip() if s: self.exec_command(s) except (EOFError, WavEdExit): break return def show_help(self): print 'commands: q)uit, r)ead, w)rite, p)lay, s)tart, e)nd, l)ength' print ' C)reate, D)elete, R)ename, J)ump, L)ist, load, save, export' return def exec_command(self, s): try: if s[0].isdigit() or s[0] in '+-': self.cmd_s(s) return (c, _, v) = s.partition(' ') c = 'cmd_' + c.replace('!', '_f') if hasattr(self, c): getattr(self, c)(v) else: self.show_help() except WavEdExit, e: raise except WavNoFileError, e: print 'no file'
class WavEd(object): def __init__(self): self._wav = None self._cur = None self._curs = {} self._player = None return def stop(self): if self._player is not None: self._player.stop() self._player = None return def close(self): self.stop() if self._wav is not None: self._wav.close() self._wav = None self._cur = None self._curs = {} return def read(self, path): self.close() self._wav = WaveReader(path) self._cur = WavCursor(self._wav) self._cur.set_length('1.0') self._curs = {} print ('Read: rate=%d, frames=%d, duration=%.3f' % (self._wav.framerate, self._wav.nframes, self._wav.nframes/float(self._wav.framerate))) return def write(self, cur, path, force=False): if not force and os.path.exists(path): raise WavEdError('File exists: %r' % path) nframes = cur.get_length() fp = open(path, 'wb') writer = WaveWriter(fp, nchannels=self._wav.nchannels, sampwidth=self._wav.sampwidth, framerate=self._wav.framerate) self._wav.seek(cur.start) (_,data) = self._wav.readraw(nframes) writer.writeraw(data) writer.close() fp.close() print ('Written: %r, rate=%d, frames=%d, duration=%.3f' % (path, writer.framerate, nframes, nframes/float(writer.framerate))) return def play(self, cur): nframes = cur.get_length() self.stop() self._player = WavePlayer(nchannels=self._wav.nchannels, sampwidth=self._wav.sampwidth, framerate=self._wav.framerate) self._wav.seek(cur.start) (_,data) = self._wav.readraw(nframes) self._player.writeraw(data) self._player.flush() return def show(self, cur): print cur return def run(self): self.exec_command('p') while 1: try: s = raw_input('> ') self.stop() s = s.strip() if s: self.exec_command(s) except (EOFError, WavEdExit): break return def show_help(self): print 'commands: q)uit, r)ead, w)rite, p)lay, s)tart, e)nd, l)ength' print ' C)reate, D)elete, R)ename, J)ump, L)ist, load, save, export' return def exec_command(self, s): try: if s[0].isdigit() or s[0] in '+-': self.cmd_s(s) return (c,_,v) = s.partition(' ') c = 'cmd_'+c.replace('!','_f') if hasattr(self, c): getattr(self, c)(v) else: self.show_help() except WavEdExit, e: raise except WavNoFileError, e: print 'no file'