コード例 #1
0
def parser(wavefile,num_mfcc,env_noise=None):
    try:
        y, sr = librosa.load(wavefile, res_type= 'kaiser_fast')
        y = prep_data.normalize(y)
        #remove the silence at beginning and end of recording
        y = prep_data.get_speech_samples(y,sr)
        
        rand_scale = 0.0
        #randomly assigns speaker data to 1 (train) 2 (validation) or 3 (test)
        if env_noise is not None:
            #at random apply varying amounts of environment noise
            rand_scale = random.choice([0.0,0.25,0.5,0.75,1.0,1.25])
            logging.info("Scale of noise applied: {}".format(rand_scale))
            if rand_scale:
                #apply *known* environemt noise to signal
                total_length = len(y)/sr
                envnoise_normalized = prep_data.normalize(env_noise)
                envnoise_scaled = prep_data.scale_noise(envnoise_normalized,rand_scale)
                envnoise_matched = prep_data.match_length(envnoise_scaled,sr,total_length)
                if len(envnoise_matched) != len(y):
                    diff = int(len(y) - len(envnoise_matched))
                    if diff < 0:
                        envnoise_matched = envnoise_matched[:diff]
                    else:
                        envnoise_matched = np.append(envnoise_matched,np.zeros(diff,))
                y += envnoise_matched
        mfccs = librosa.feature.mfcc(y, sr, n_mfcc=num_mfcc,hop_length=int(0.010*sr),n_fft=int(0.025*sr))
        return mfccs, sr, rand_scale
    except EOFError as error:
        logging.exception('def parser() resulted in {} for the file: {}'.format(error,wavefile))
    except ValueError as ve:
        logging.exception("Error occured ({}) with the file {}".format(ve,wavefile))
    
    return None, None, None
コード例 #2
0
def apply_noise(y, sr, wavefile):
    #at random apply varying amounts of environment noise
    rand_scale = random.choice([0.0, 0.25, 0.5, 0.75])
    if rand_scale > 0.0:
        #apply *known* environemt noise to signal
        total_length = len(y) / sr
        y_noise, sr = librosa.load(wavefile, sr=16000)
        envnoise_normalized = prep_data_vad_noise.normalize(y_noise)
        envnoise_scaled = prep_data_vad_noise.scale_noise(
            envnoise_normalized, rand_scale)
        envnoise_matched = prep_data_vad_noise.match_length(
            envnoise_scaled, sr, total_length)
        if len(envnoise_matched) != len(y):
            diff = int(len(y) - len(envnoise_matched))
            if diff < 0:
                envnoise_matched = envnoise_matched[:diff]
            else:
                envnoise_matched = np.append(envnoise_matched,
                                             np.zeros(diff, ))
        y += envnoise_matched
    return y