Exemple #1
0
def mcep2spec(MCEP, alpha, dftlen):
    misc.check_executable(
        'mgc2sp',
        'You can find it in SPTK (https://sourceforge.net/projects/sp-tk/)')

    order = MCEP.shape[1] - 1

    tmpspecfile = sp.gentmpfile('mcep2spec.mcep')
    outspecfile = sp.gentmpfile('mcep2spec.spec')

    try:
        MCEP.astype(np.float32).tofile(tmpspecfile)
        cmd = os.path.join(
            sp.BINPATH, 'mgc2sp') + ' -a ' + str(alpha) + ' -g 0 -m ' + str(
                int(order)) + ' -l ' + str(
                    dftlen) + ' -o 2 ' + tmpspecfile + ' > ' + outspecfile
        ret = os.system(cmd)
        if ret > 0: raise ValueError('ERROR during execution of mgc2sp')
        SPEC = np.fromfile(outspecfile, dtype=np.float32)
        SPEC = SPEC.reshape((-1, int(dftlen / 2) + 1))
    except:
        if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
        if os.path.exists(outspecfile): os.remove(outspecfile)
        raise

    if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
    if os.path.exists(outspecfile): os.remove(outspecfile)

    return SPEC
Exemple #2
0
def analysis(wav, fs, f0s, shift, dftlen):
    '''
    Compute features using the STRAIGHT vocoder.
    (STRAIGHT's Analysis stage)
    
    fs     : [Hz]
    f0s    : [s,Hz]
    shift  : [s]
    dftlen : 
    '''

    #f0s = np.interp(np.arange(f0s[0,0], f0s[-1,0], shift), f0s[:,0], np.log(f0s[:,1])
    f0shift = np.median(np.diff(f0s[:,0]))

    tmprawwavfile = sigproc.gentmpfile('straight-analysis.raw')
    tmpf0file = sigproc.gentmpfile('straight-analysis.f0')
    tmpspecfile = sigproc.gentmpfile('straight-analysis.spec')
    tmpaperfile = sigproc.gentmpfile('straight-analysis.aper')

    try:
        ((wav.copy()*np.iinfo(np.int16).max).astype(np.int16)).tofile(tmprawwavfile)

        np.savetxt(tmpf0file, f0s[:,1], fmt='%f')

        #print('STRAIGHTPATH='+STRAIGHTPATH)

        # Aperiodicity estimation
        cmd = STRAIGHTPATH+'straight_bndap -float -nmsg -f '+str(fs)+' -shift '+str(shift*1000)+' -f0shift '+str(f0shift*1000)+' -f0file '+tmpf0file+' -fftl '+str(dftlen)+' -apord '+str(dftlen/2+1)+' -raw '+tmprawwavfile+' '+tmpaperfile # -nmsg
        os.system(cmd)

        # Spectral envelope estimation
        cmd = STRAIGHTPATH+'straight_mcep -float -nmsg -f '+str(fs)+' -fftl '+str(dftlen)+' -apord '+str(dftlen/2+1)+' -shift '+str(shift*1000)+' -f0shift '+str(f0shift*1000)+' -f0file '+tmpf0file+' -pow -raw '+tmprawwavfile+' '+tmpspecfile # -nmsg
        os.system(cmd)

        SPEC = np.fromfile(tmpspecfile, dtype='float32')
        SPEC = SPEC.reshape((-1, dftlen/2+1))
        APER = np.fromfile(tmpaperfile, dtype='float32')
        APER = APER.reshape((-1, dftlen/2+1))
    except:
        if os.path.exists(tmprawwavfile): os.remove(tmprawwavfile)
        if os.path.exists(tmpf0file): os.remove(tmpf0file)
        if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
        if os.path.exists(tmpaperfile): os.remove(tmpaperfile)
        raise

    if os.path.exists(tmprawwavfile): os.remove(tmprawwavfile)
    if os.path.exists(tmpf0file): os.remove(tmpf0file)
    if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
    if os.path.exists(tmpaperfile): os.remove(tmpaperfile)

    if 0:
        import matplotlib.pyplot as plt
        plt.ion()

        f, axs = plt.subplots(2, 1, sharex=True, sharey=True)
        axs[0].imshow(sp.mag2db(SPEC.T), origin='lower', aspect='auto', interpolation='none', extent=(0, SPEC.shape[0]*shift, 0, fs/2))
        axs[1].imshow(APER.T, origin='lower', aspect='auto', interpolation='none', extent=(0, SPEC.shape[0]*shift, 0, fs/2))
        from IPython.core.debugger import  Pdb; Pdb().set_trace()

    return SPEC, APER
Exemple #3
0
def spec2mcep(SPEC, alpha, order):
    misc.check_executable(
        'mcep',
        'You can find it in SPTK (https://sourceforge.net/projects/sp-tk/)')

    dftlen = 2 * (SPEC.shape[1] - 1)

    tmpspecfile = sp.gentmpfile('spec2mcep.spec')
    outspecfile = sp.gentmpfile('spec2mcep.mcep')

    try:
        SPEC.astype(np.float32).tofile(tmpspecfile)
        cmd = os.path.join(
            sp.BINPATH, 'mcep'
        ) + ' -a ' + str(alpha) + ' -m ' + str(int(order)) + ' -l ' + str(
            dftlen
        ) + ' -e 1.0E-8 -j 0 -f 0.0 -q 3 ' + tmpspecfile + ' > ' + outspecfile
        ret = os.system(cmd)
        if ret > 0: raise ValueError('ERROR during execution of mcep')
        MCEP = np.fromfile(outspecfile, dtype=np.float32)
        MCEP = MCEP.reshape((-1, 1 + int(order)))
    except:
        if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
        if os.path.exists(outspecfile): os.remove(outspecfile)
        raise

    if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
    if os.path.exists(outspecfile): os.remove(outspecfile)

    return MCEP
Exemple #4
0
def analysis_aper(wav, fs, f0s, shift, dftlen):
    '''
    Compute features using the STRAIGHT vocoder.
    (STRAIGHT's Analysis stage)
    
    fs     : [Hz]
    f0s    : [s,Hz]
    shift  : [s]
    dftlen : 
    '''

    #f0s = np.interp(np.arange(f0s[0,0], f0s[-1,0], shift), f0s[:,0], np.log(f0s[:,1])
    f0shift = np.median(np.diff(f0s[:,0]))

    tmprawwavfile = sigproc.gentmpfile('straight-analysis-aper.raw')
    tmpf0file = sigproc.gentmpfile('straight-analysis-aper.f0')
    tmpaperfile = sigproc.gentmpfile('straight-analysis-aper.aper')

    try:
        ((wav.copy()*np.iinfo(np.int16).max).astype(np.int16)).tofile(tmprawwavfile)

        np.savetxt(tmpf0file, f0s[:,1], fmt='%f')

        # Aperiodicity estimation
        cmd = STRAIGHTPATH+'straight_bndap -float -nmsg -f '+str(fs)+' -shift '+str(shift*1000)+' -f0shift '+str(f0shift*1000)+' -f0file '+tmpf0file+' -fftl '+str(dftlen)+' -apord '+str(dftlen/2+1)+' -raw '+tmprawwavfile+' '+tmpaperfile # -nmsg
        ret = os.system(cmd)
        if ret>0: raise ValueError('ERROR during execution of straight_bndap')

        APER = np.fromfile(tmpaperfile, dtype='float32')
        APER = APER.reshape((-1, dftlen/2+1))
    except:
        if os.path.exists(tmprawwavfile): os.remove(tmprawwavfile)
        if os.path.exists(tmpf0file): os.remove(tmpf0file)
        if os.path.exists(tmpaperfile): os.remove(tmpaperfile)
        raise

    if os.path.exists(tmprawwavfile): os.remove(tmprawwavfile)
    if os.path.exists(tmpf0file): os.remove(tmpf0file)
    if os.path.exists(tmpaperfile): os.remove(tmpaperfile)

    if 0:
        import matplotlib.pyplot as plt
        plt.ion()

        plt.imshow(APER.T, origin='lower', aspect='auto', interpolation='none', extent=(0, APER.shape[0]*shift, 0, fs/2))
        from IPython.core.debugger import  Pdb; Pdb().set_trace()

    return APER
def sv56demo(wav, fs, level=-26):
    '''
    Normalize the waveform in amplitude
    '''
    misc.check_executable('sv56demo', 'source?')

    tmpinfname = sigproc.gentmpfile('interface-sv56demo-in.raw')
    tmpoutfname = sigproc.gentmpfile('interface-sv56demo-out.raw')

    try:
        (wav.copy() * np.iinfo(np.int16).max).astype(
            np.int16).tofile(tmpinfname)

        levelstr = ''
        if level != None:
            levelstr = ' -lev ' + str(level)
        cmd = os.path.join(
            sigproc.BINPATH, 'sv56demo') + ' ' + levelstr + ' -sf ' + str(
                fs) + ' ' + tmpinfname + ' ' + tmpoutfname + ' 640'
        out = subprocess.check_output(cmd,
                                      stderr=subprocess.STDOUT,
                                      shell=True)
        #print(out)

        # Put outputs in a dictionary
        m = re.findall(r'\n?\s*(.*): .* ([-+]?[0-9]+\.?[0-9]*).*', out)
        meta = dict(m)
        meta.pop('Input file', None)
        for key in meta:
            meta[key] = float(meta[key])

        wav = np.fromfile(tmpoutfname, dtype=np.int16)
        wav = wav / float(np.iinfo(np.int16).max)

    except:
        if os.path.exists(tmpinfname): os.remove(tmpinfname)
        if os.path.exists(tmpoutfname): os.remove(tmpoutfname)
        raise

    if os.path.exists(tmpinfname): os.remove(tmpinfname)
    if os.path.exists(tmpoutfname): os.remove(tmpoutfname)

    return wav, meta
Exemple #6
0
def avread(filename):
    '''
    Read any audio
    '''
    tmpoutfname = sp.gentmpfile('avread.wav')
    try:
        os.system('avconv -i ' + filename + ' ' + tmpoutfname +
                  ' >/dev/null 2>&1')
        #os.system('avconv -i '+filename+' '+tmpoutfname)

        wav, fs, enc = wavread(tmpoutfname)
    except:
        if os.path.exists(tmpoutfname): os.remove(tmpoutfname)
        raise

    if os.path.exists(tmpoutfname): os.remove(tmpoutfname)

    return wav, fs, enc
def reaper(wav,
           fs,
           shift,
           f0min,
           f0max,
           rmsteps=False,
           outpm=False,
           prehp_cutoff=None):
    '''
    Estimate the f0

    prehp_cutoff: High-pass the signal before estimating f0 (def. None). Value is [Hz].

    Source:
        https://github.com/google/REAPER
    '''
    misc.check_executable(
        'reaper',
        'You can download the source from https://github.com/google/REAPER')

    if not prehp_cutoff is None:
        b, a = scipy.signal.butter(8,
                                   prehp_cutoff / (0.5 / shift),
                                   btype='high')
        wav = scipy.signal.filtfilt(b, a, wav)

    tmpwavfile = sigproc.gentmpfile('interface-reaper-in.wav')
    tmpf0file = sigproc.gentmpfile('interface-reaper-out.f0')
    tmppmfile = sigproc.gentmpfile('interface-reaper-out.pm')

    try:
        fileio.wavwrite(tmpwavfile, wav, fs, np.int16)

        cmd = os.path.join(
            sigproc.BINPATH, 'reaper') + ' -i ' + tmpwavfile + ' -m ' + str(
                f0min) + ' -x ' + str(f0max) + ' -e ' + str(
                    shift) + ' -a -f ' + tmpf0file + ' -p ' + tmppmfile
        os.system(cmd + ' 2>&1 >/dev/null')  # Run it silent

        # Read the f0 values and the corresponding analysis times
        f0s = list()
        with open(tmpf0file) as f0file:
            n = 1
            for line in f0file:
                if n > 7:  # Skip the EST-file header TODO Neater way to do this!
                    values = line.split()
                    f0s.append((float(values[0]), float(values[2])))
                n += 1
        f0s = np.array(f0s)
        f0s[f0s[:, 1] < 0, 1] = 0

        if rmsteps:
            f0s = resampling.f0s_rmsteps(f0s)

        # Read the f0 values and the corresponding analysis times
        pms = list()
        with open(tmppmfile) as pmfile:
            n = 1
            for line in pmfile:
                if n > 7:  # Skip the EST-file header TODO Neater way to do this!
                    values = line.split()
                    pms.append(float(values[0]))
                n += 1
        pms = np.array(pms)

    except:
        if os.path.exists(tmpwavfile): os.remove(tmpwavfile)
        if os.path.exists(tmpf0file): os.remove(tmpf0file)
        if os.path.exists(tmppmfile): os.remove(tmppmfile)
        raise

    if os.path.exists(tmpwavfile): os.remove(tmpwavfile)
    if os.path.exists(tmpf0file): os.remove(tmpf0file)
    if os.path.exists(tmppmfile): os.remove(tmppmfile)

    if outpm:
        return f0s, pms
    else:
        return f0s
Exemple #8
0
def synthesis(fs, f0s, SPEC, shift, APER=None, f0_forcecont=False, verbose=0):

    if f0s.shape[0]!=SPEC.shape[0] or (APER!=None and (SPEC.shape[0]!=APER.shape[0])):
        raise ValueError('Features dont have the same length (F0={}, SPEC={}, APER={}).'.format(f0s.shape, SPEC.shape, APER.shape))
    if APER!=None and (SPEC.shape[1]!=APER.shape[1]):
        raise ValueError('Spectral features dont have the same width (SPEC={}, APER={}).'.format(SPEC.shape, APER.shape))

    dftlen = (SPEC.shape[1]-1)*2

    if APER==None:
        APER = np.zeros(SPEC.shape)
        APER[f0s[:,1]>0,:int((float(dftlen)/fs)*4000.0)] = -100.0

    if f0_forcecont:
        # Replace zero values by interpolated values
        idx = f0s[:,1]>0
        f0s[:,1] = np.exp(np.interp(f0s[:,0], f0s[idx,0], np.log(f0s[idx,1])))

    tmpf0file = sigproc.gentmpfile('straight-synthesis.f0')
    tmpaperfile = sigproc.gentmpfile('straight-synthesis.aper')
    tmpspecfile = sigproc.gentmpfile('straight-synthesis.spec')
    tmpwavfile = sigproc.gentmpfile('straight-synthesis.wav')

    try:
        np.savetxt(tmpf0file, f0s[:,1], fmt='%f')
        APER.astype(np.float32).tofile(tmpaperfile)
        SPEC.astype(np.float32).tofile(tmpspecfile)

        cmd = STRAIGHTPATH+'synthesis_fft \
            -f '+str(fs)+' \
            -fftl '+str(dftlen)+' \
            -spec \
            -shift '+str(shift*1000)+' \
            -sigp 1.2 \
            -sd 0.5 \
            -cornf 4000 \
            -bw 70.0 \
            -delfrac 0.2 \
            -apfile '+tmpaperfile+' \
            -float \
            '+tmpf0file+' \
            '+tmpspecfile+' \
            '+tmpwavfile+' ' #>/dev/null 2>&1
        if verbose==0:
            cmd += ' > /dev/null 2>&1'
        #print(cmd)
        os.system(cmd)

        wavfs, wav = scipy.io.wavfile.read(tmpwavfile)
        wavdtype = wav.dtype
        wav = wav / float(np.iinfo(wavdtype).max)

    except:
        if os.path.exists(tmpf0file): os.remove(tmpf0file)
        if os.path.exists(tmpaperfile): os.remove(tmpaperfile)
        if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
        if os.path.exists(tmpwavfile): os.remove(tmpwavfile)
        raise

    if os.path.exists(tmpf0file): os.remove(tmpf0file)
    if os.path.exists(tmpaperfile): os.remove(tmpaperfile)
    if os.path.exists(tmpspecfile): os.remove(tmpspecfile)
    if os.path.exists(tmpwavfile): os.remove(tmpwavfile)

    return wav