Beispiel #1
0
def main(argv):
    import getopt, fileinput, os.path
    from pycdb import TCDBReader, CDBReader
    from wavestream import WaveWriter
    def usage():
        print 'usage: %s [-d] [-c codec] [-D dictpath] [file ...]' % argv[0]
        return 100
    try:
        (opts, args) = getopt.getopt(argv[1:], 'dc:C:D:')
    except getopt.GetoptError:
        return usage()
    debug = 0
    codec = 'utf-8'
    dictcodec = 'euc-jp'
    dictpath = os.path.join(os.path.dirname(__file__), 'yomi.tcdb')
    phonepath = os.path.join(os.path.dirname(__file__), 'diphone.cdb')
    for (k, v) in opts:
        if k == '-d': debug += 1
        elif k == '-c': codec = v
        elif k == '-C': dictcodec = v
        elif k == '-D': dictpath = v
    langdb = TCDBReader(dictpath)
    phonedb = CDBReader(phonepath)
    synth = Synthesizer(langdb, phonedb, dictcodec=dictcodec)
    for line in fileinput.input(args):
        line = line.decode(codec, 'ignore')
        fp = open('out.wav', 'wb')
        writer = WaveWriter(fp)
        synth.synth(writer, line)
        writer.close()
        fp.close()
    return 0
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
 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
Beispiel #5
0
 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
Beispiel #6
0
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
Beispiel #7
0
def main(argv):
    import getopt
    def usage():
        print 'usage: %s [-f] [-o out.wav] [-m maxlength] [expr]' % argv[0]
        return 100
    try:
        (opts, args) = getopt.getopt(argv[1:], 'fo:m:')
    except getopt.GetoptError:
        return usage()
    force = False
    maxlength = 10
    path = 'out.wav'
    for (k, v) in opts:
        if k == '-f': force = True
        elif k == '-o': path = v
        elif k == '-m': maxlength = float(v)
    if not args: return usage()
    if not force and os.path.exists(path): raise IOError(path)
    fp = open(path, 'wb')
    stream = WaveWriter(fp)
    expr = args.pop(0)
    gen = WaveGenerator(stream.framerate)
    vars = {
        'add': gen.add,
        'mult': gen.mult,
        'amp': gen.amp,
        'concat': gen.concat,
        'mix': gen.mix,
        'clip': gen.clip,
        'env': gen.env,
        'sine': gen.sine,
        'rect': gen.rect,
        'saw': gen.saw,
        'noise': gen.noise,
    }
    wav = eval(expr, vars, {})
    stream.write(gen.clip(wav, maxlength))
    stream.close()
    fp.close()
    return 0
Beispiel #8
0
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
Beispiel #9
0
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