예제 #1
0
파일: gen_noise.py 프로젝트: Pezz89/BPLabs
def gen_noise(OutDir, b, fs):
    print("Generating noise...")
    # Generate 10 minutes of white noise
    x = np.random.randn(int(fs * 60. * 5.))
    x /= x.max()
    noiseDir = os.path.join(OutDir, 'wav')
    noiseRMSDir = os.path.join(OutDir, 'rms')
    dir_must_exist(noiseDir)
    noiseDir = os.path.join(noiseDir, 'noise')
    dir_must_exist(noiseDir)
    y, y_max = block_lfilter_wav(b, [1.0], x,
                                 os.path.join(noiseDir, 'noise.wav'), 65538,
                                 44100)
    block_process_wav(os.path.join(noiseDir, 'noise.wav'),
                      os.path.join(noiseDir, 'noise_norm.wav'), lambda x: x /
                      (y_max * 1.05))
    noise_norm_wav = PySndfile(os.path.join(noiseDir, 'noise_norm.wav'), 'r')
    noise_rms_path = os.path.join(noiseRMSDir, 'noise_rms.npy')
    y = noise_norm_wav.read_frames(fs * 60)
    y = y / (np.abs(y).max() * 0.95)
    # rms = np.sqrt(np.mean(y**2))
    # rms, _, _ = asl_P56(y, fs, 16)
    rms = rms_no_silences(y, fs, -30.)
    print(f"Noise level: {rms}")

    peak = np.abs(y).max()
    np.save(noise_rms_path, rms)
    np.save('./stimulus/peak/noise_peak.npy', peak)
    return y
예제 #2
0
def read(name, end=None, start=0, dtype=np.float64, return_format=False) :
    """read samples from arbitrary sound files.
    return data, samplerate and encoding string

    returns subset of samples as specified by start and end arguments (Def all samples)
    normalizes samples to [-1,1] if the datatype is a floating point type
    """
    sf  = PySndfile(name)
    enc = sf.encoding_str()

    nf = sf.seek(start, 0)
    if not nf == start:
        raise IOError("sndio.read::error:: while seeking at starting position")
    
    if end == None:
        ff = sf.read_frames(dtype=dtype)
    else:
        ff = sf.read_frames(end-start, dtype=dtype)
        
    # if norm and (enc not in ["float32" , "float64"]) :
    #     if enc in enc_norm_map :
    #         ff = ff / enc_norm_map[sf.encoding_str()]
    #     else :
    #         raise IOError("sndio.read::error::normalization of compressed pcm data is not supported")

    if return_format:
        return ff, sf.samplerate(), enc, sf.major_format_str()
    return ff, sf.samplerate(), enc
예제 #3
0
def main():
    wavs = globDir('./out/stim/', '*.wav')
    rmss = globDir('./out/stim/', 'stim_*_env.npy')
    outDir = "./out/stim/"
    for wav, rms in zip(wavs, rmss):
        print("Detecting silence in wav file: {}".format(wav))
        snd = PySndfile(wav, 'r')
        fs = int(snd.samplerate())
        silences = detect_silences(rms, fs)

        head, tail = os.path.split(wav)
        tail = os.path.splitext(tail)[0]
        tail = tail + "_silence.npy"
        silence_filepath = os.path.join(outDir, tail)
        np.save(silence_filepath, silences)
예제 #4
0
 def loadNoise(self, noiseFilepath, noiseRMSFilepath):
     '''
     Read noise samples and calculate the RMS of the signal
     '''
     noise = PySndfile(noiseFilepath, 'r')
     noise_rms = np.load(noiseRMSFilepath)
     for ind, _ in enumerate(self.adaptiveTracks):
         self.adaptiveTracks[ind].setNoise(noise, noise_rms)
예제 #5
0
def main():
    wavs = globDir('./out/stim/', '*.wav')
    silences = globDir('./out/stim/', 'stim_*_silence.npy')
    outDir = "./out/stim/"
    for wav, sil in zip(wavs, silences):
        snd = PySndfile(wav, 'r')
        fs = int(snd.samplerate())
        s = np.load(sil)
        sil_bool = slice_to_bool(s, snd.frames())

        rms = np.sqrt(np.mean(np.abs(snd.read_frames()[~sil_bool]**2)))

        head, tail = os.path.split(wav)
        tail = os.path.splitext(tail)[0]
        tail = tail + "_rms.npy"
        rms_filepath = os.path.join(outDir, tail)
        np.save(rms_filepath, rms)
예제 #6
0
def main():
    wavs = globDir('./out/stim/', '*.wav')
    envs = globDir('./out/stim/', 'stim_*_env.npy')
    silences = globDir('./out/stim/', 'stim_*_silence.npy')
    for wavfp, envfp, silfp in zip(wavs, envs, silences):
        snd = PySndfile(wavfp, 'r')
        fs = int(snd.samplerate())
        env = np.load(envfp)
        sil_slices = np.load(silfp)
        sil = np.zeros(env.size)
        for sil_slice in sil_slices:
            sil[sil_slice[0]:sil_slice[1]] = 1
        pdb.set_trace()

        plt.plot(snd.read_frames(fs*60))
        plt.plot(sil[:fs*60])
        plt.show()
예제 #7
0
def write(name, vec, rate=44100, format="aiff", enc='pcm16'):
    """
    Write datavector to aiff file using amplerate and encoding as specified
    """
    nchans = len(vec.shape)
    if nchans != 1:
        nchans = vec.shape[1]
    sf = PySndfile(name,
                   "w",
                   format=construct_format(formt, enc),
                   channels=nchans,
                   samplerate=rate)

    nf = sf.write_frames(vec)

    if nf != vec.shape[0]:
        raise IOError("sndio.write::error::writing of samples failed")
    return nf
예제 #8
0
def open(filename, mode=None, format=None, channels=None,
         framerate=None):
    """Factory method to generate PySndfile objects with wav file defaults."""
    if not mode:
        mode = 'w'
    if not format:
        fmt = wav_format_code()
    if not channels:
        channels = defaults.channels
    if not framerate:
        framerate = defaults.framerate
    return PySndfile(filename, mode, fmt, channels, framerate)
예제 #9
0
 def __init__(self,
              fn,
              samplerate,
              filefmt='wav',
              datafmt='pcm16',
              channels=1):
     fmt = construct_format(filefmt, datafmt)
     self.sf = PySndfile(fn,
                         mode='w',
                         format=fmt,
                         channels=channels,
                         samplerate=samplerate)
예제 #10
0
파일: gen_noise.py 프로젝트: Pezz89/BPLabs
def synthesize_trial(wavFileMatrix, indexes):
    '''
    Using the matrix of alternative words and the selected words for each
    column, generate samples from audio files
    Returns an array of samples generated by concatenating the selected audio
    files
    '''
    columnNames = ['a', 'b', 'c', 'd', 'e']
    indexes = np.pad(indexes, ((0, 1)), 'constant', constant_values=0)
    indexes = rolling_window_lastaxis(indexes, 2)
    offset = 10
    y = np.array([])
    filenames = []
    for name, ind in zip(columnNames, indexes):
        if name == 'e':
            offset = 1
        wavFilename, wavFilepath = wavFileMatrix[name][(ind[0] * offset) +
                                                       ind[1]]
        wav = PySndfile(wavFilepath)
        fs = wav.samplerate()
        x = wav.read_frames()
        y = np.append(y, x)
        filenames.append(wavFilename)
    return (y, {
        'rate': fs,
        'format': wav.major_format_str(),
        'enc': wav.encoding_str()
    }, filenames)
예제 #11
0
def write(name, data, rate=44100, format="aiff", enc='pcm16') :
    """
    Write datavector to sndfile using samplerate, format and encoding as specified
    valid format strings are all the keys in the dict pysndfile.fileformat_name_to_id
    valid encodings are those that are supported by the selected format
    from the list of keys in pysndfile.encoding_name_to_id.
    """
    nchans = len(data.shape)
    if nchans == 2 :
        nchans = data.shape[1]
    elif nchans != 1:
        raise RuntimeError("error:sndio.write:can only be called with vectors or matrices ")

    sf  = PySndfile(name, "w",
                    format=construct_format(format, enc),
                    channels = nchans, samplerate = rate)
    
    nf = sf.write_frames(data)

    if nf != data.shape[0]:
        raise IOError("sndio.write::error::writing of samples failed")
    return nf
예제 #12
0
파일: gen_noise.py 프로젝트: Pezz89/BPLabs
def block_lfilter_wav(b, a, x, outfile, fmt, fs, blocksize=8192):
    '''
    Filter 1D signal in blocks. For use with large signals
    '''
    new_state = np.zeros(b.size - 1)
    sndfile = PySndfile(outfile, 'w', fmt, 1, fs)
    i = 0
    y_out = np.zeros(x.size)
    y_max = 0.0
    while i < x.size:
        print("Filtering {0} to {1} of {2}".format(i, i + blocksize, x.size))
        if i + blocksize > x.size:
            y, new_state = sgnl.lfilter(b, a, x[i:-1], zi=new_state)
            sndfile.write_frames(y)
            y_out[i:i + y.size] = y
        else:
            y, new_state = sgnl.lfilter(b, a, x[i:i + blocksize], zi=new_state)
            sndfile.write_frames(y)
            y_out[i:i + y.size] = y
        y_max = np.max([y_max, np.abs(y).max()])
        i += blocksize
    return y_out, y_max
예제 #13
0
def read_audio(name_audio):
    soundIO = PySndfile(name_audio)

    frames = soundIO.read_frames()
    s_rate = soundIO.samplerate()
    return frames, s_rate
예제 #14
0
    def __init__(self, fn, sr=None, chns=None, blksz=2**16, dtype=np.float32):
        fnd = False

        if not fnd and (PySndfile is not None):
            try:
                sf = PySndfile(fn, mode='r')
            except IOError:
                pass
            else:
                if (sr is None or sr == sf.samplerate()) and (
                        chns is None or chns == sf.channels()):
                    # no resampling required
                    self.channels = sf.channels()
                    self.samplerate = sf.samplerate()
                    self.frames = sf.frames()

                    self.rdr = sndreader(sf, blksz, dtype=dtype)
                    fnd = True

        if not fnd:
            ffmpeg = findfile('ffmpeg') or findfile('avconv')
            if ffmpeg is not None:
                pipe = sp.Popen([ffmpeg, '-i', fn, '-'],
                                stdin=sp.PIPE,
                                stdout=sp.PIPE,
                                stderr=sp.PIPE)
                fmtout = pipe.stderr.read()
                if (sys.version_info > (3, 0)):
                    fmtout = fmtout.decode()
                m = re.match(
                    r"^(ffmpeg|avconv) version.*Duration: (\d\d:\d\d:\d\d.\d\d),.*Audio: (.+), (\d+) Hz, (.+), (.+), (\d+) kb/s",
                    " ".join(fmtout.split('\n')))
                if m is not None:
                    self.samplerate = int(m.group(4)) if not sr else int(sr)
                    chdef = m.group(5)
                    if chdef.endswith(" channels") and len(chdef.split()) == 2:
                        self.channels = int(chdef.split()[0])
                    else:
                        try:
                            self.channels = {
                                'mono': 1,
                                '1 channels (FL+FR)': 1,
                                'stereo': 2,
                                'hexadecagonal': 16
                            }[chdef] if not chns else chns
                        except:
                            print(f"Channel definition '{chdef}' unknown")
                            raise
                    dur = reduce(lambda x, y: x * 60 + y,
                                 list(map(float,
                                          m.group(2).split(':'))))
                    self.frames = int(
                        dur * self.samplerate
                    )  # that's actually an estimation, because of potential resampling with round-off errors
                    pipe = sp.Popen(
                        [
                            ffmpeg, '-i', fn, '-f', 'f32le', '-acodec',
                            'pcm_f32le', '-ar',
                            str(self.samplerate), '-ac',
                            str(self.channels), '-'
                        ],
                        #                    bufsize=self.samplerate*self.channels*4*50,
                        stdin=sp.PIPE,
                        stdout=sp.PIPE,
                        stderr=sp.PIPE)

                    def rdr():
                        bufsz = (blksz // self.channels) * self.channels * 4
                        while True:
                            data = pipe.stdout.read(bufsz)
                            if len(data) == 0:
                                break
                            data = np.fromstring(data, dtype=dtype)
                            yield data.reshape((-1, self.channels)).T

                    self.rdr = rdr()
                    fnd = True

        if not fnd:
            raise IOError("Format not usable")
예제 #15
0
def main():
    stim_dir = "../behavioural_stim/stimulus"
    wav_dir = "../behavioural_stim/stimulus/wav"
    base_dir = "../behavioural_stim/stimulus/wav/sentence-lists/"
    noise_dir = "../behavioural_stim/stimulus/wav/noise/"
    out_dir = "./out"
    dir_must_exist(base_dir)
    dir_must_exist(out_dir)
    dir_must_exist(wav_dir)
    dir_must_exist(noise_dir)

    noise_filepath = "../behavioural_stim/stimulus/wav/noise/noise_norm.wav"

    folders = os.listdir(base_dir)
    folders = natsorted(folders)[1:15]
    folders = list(zip(folders[::2], folders[1::2]))
    calc_potential_max(base_dir, noise_filepath, out_dir)
    n_questions = 4
    fs = 44100

    for ind, (list_folder_1, list_folder_2) in enumerate(folders):
        out_folder_name = 'Stim_{}'.format(ind)
        out_folder = os.path.join(out_dir, out_folder_name)
        delete_if_exists(out_folder)
        dir_must_exist(out_folder)
        out_wav_path = os.path.join(out_folder, "stim.wav")
        out_csv_path = os.path.join(out_folder, "markers.csv")
        out_rms_path = os.path.join(out_folder, "rms.npy")
        out_q_path = [
            os.path.join(out_folder, "questions_{}.csv".format(x))
            for x in range(n_questions)
        ]
        out_wav = PySndfile(out_wav_path, 'w',
                            construct_format('wav', 'pcm16'), 3, 44100)
        list_1_wav = globDir(os.path.join(base_dir, list_folder_1), '*.wav')
        list_2_wav = globDir(os.path.join(base_dir, list_folder_2), '*.wav')
        list_1_csv = globDir(os.path.join(base_dir, list_folder_1), '*.csv')
        list_2_csv = globDir(os.path.join(base_dir, list_folder_2), '*.csv')
        merged_wavs = list_1_wav + list_2_wav
        merged_csvs = list_1_csv + list_2_csv
        words = []
        for c in merged_csvs:
            with open(c, 'r') as csvfile:
                for line in csv.reader(csvfile):
                    words.append(line)
        c = list(zip(merged_wavs, words))
        shuffle(c)
        merged_wavs, words = zip(*c)
        sum_sqrd = 0.
        n = 0
        with open(out_csv_path, 'w') as csvfile, ExitStack() as stack:
            # Open all question files
            qfiles = [
                stack.enter_context(open(qfile, 'w')) for qfile in out_q_path
            ]
            writer = csv.writer(csvfile)
            qwriters = [csv.writer(qfile) for qfile in qfiles]

            counter = 0
            stim_count = len(merged_wavs)
            stim_count_half = stim_count // 2
            q_inds = np.array([
                sample(range(0, stim_count_half), n_questions),
                sample(range(stim_count_half, stim_count - 1), n_questions)
            ]).T
            a = 0
            silence = np.zeros((88200, 3))
            idx = np.arange(0, silence.shape[0])
            trigger = gen_trigger(idx, 2., 0.01, fs)
            silence[:, 2] = trigger
            out_wav.write_frames(silence)
            for ind, (wav, txt) in enumerate(zip(merged_wavs, words)):
                csv_line = [counter]
                silence = np.zeros((int(
                    np.random.uniform(int(0.3 * 44100), int(0.4 * 44100),
                                      1)), 3))
                idx = np.arange(counter, counter + silence.shape[0])
                trigger = gen_trigger(idx, 2., 0.01, fs)
                silence[:, 2] = trigger
                out_wav.write_frames(silence)
                counter += silence.shape[0]
                csv_line.append(counter)
                csv_line.append("#")
                writer.writerow(csv_line)
                csv_line = [counter]
                x, fs, enc = sndio.read(wav)
                sum_sqrd += np.sum(x**2)
                n += x.size

                y = np.vstack([x, x, np.zeros(x.size)]).T
                idx = np.arange(counter, counter + y.shape[0])
                trigger = gen_trigger(idx, 2., 0.01, fs)
                y[:, 2] = trigger
                out_wav.write_frames(y)
                counter += y.shape[0]
                csv_line.append(counter)
                csv_line.append(" ".join(txt))
                writer.writerow(csv_line)
                if ind in q_inds:
                    writer_ind = int(np.where(ind == q_inds)[0])
                    blank_ind = randint(0, len(txt) - 1)
                    q_list = copy(txt)
                    q_list[blank_ind] = '_'
                    qwriters[writer_ind].writerow(
                        [" ".join(q_list), txt[blank_ind]])
                    a += 1
            if a != 8:
                pdb.set_trace()

            csv_line = [counter]
            silence = np.zeros(
                (int(np.random.uniform(int(0.3 * 44100), int(0.4 * 44100),
                                       1)), 3))
            idx = np.arange(counter, counter + silence.shape[0])
            trigger = gen_trigger(idx, 2., 0.01, fs)
            silence[:, 2] = trigger
            out_wav.write_frames(silence)
            counter += silence.size
            csv_line.append(counter)
            csv_line.append("#")
            writer.writerow(csv_line)
            rms = np.sqrt(sum_sqrd / n)
            np.save(out_rms_path, rms)

            x, fs, enc = sndio.read(out_wav_path)
예제 #16
0
def block_mix_wavs(wavpath_a,
                   wavpath_b,
                   out_wavpath,
                   a_gain=1.,
                   b_gain=1.,
                   block_size=4096,
                   mute_left=False):
    '''
    Mix two wav files, applying gains to each
    '''
    wav_a = PySndfile(wavpath_a, 'r')
    wav_b = PySndfile(wavpath_b, 'r')

    out_wav = PySndfile(out_wavpath, 'w', construct_format('wav', 'pcm16'),
                        wav_a.channels(), wav_a.samplerate())

    i = 0
    while i < wav_a.frames():
        if i + block_size > wav_a.frames():
            block_size = wav_a.frames() - i
        x1 = wav_a.read_frames(block_size)
        x2 = wav_b.read_frames(block_size)
        x1[:, :2] *= a_gain
        x2 *= b_gain
        if x1.shape[1] == 3:
            y = np.zeros(x1.shape)
            y[:, 0] = x1[:, 0] + x2
            y[:, 1] = x1[:, 1] + x2
            y[:, 2] = x1[:, 2]
            if mute_left:
                y[:, 0] = 0.0
        else:
            y = x1 + x2
        out_wav.write_frames(y)
        i += block_size
예제 #17
0
    def loadStimulus(self):
        '''
        '''
        self.participant.load('mat_test')
        try:
            srt_50 = self.participant.data['mat_test']['srt_50']
            s_50 = self.participant.data['mat_test']['s_50']
        except KeyError:
            raise KeyError(
                "Behavioural matrix test results not available, make "
                "sure the behavioural test has been run before "
                "running this test.")
        save_dir = self.participant.data_paths['eeg_test/stimulus']
        '''
        # Estimate speech intelligibility thresholds using predicted
        # psychometric function
        s_50 *= 0.01
        x = logit(self.si * 0.01)
        snrs = (x/(4*s_50))+srt_50
        snrs = np.append(snrs, np.inf)
        snr_map = pd.DataFrame({"speech_intel" : np.append(self.si, 0.0), "snr": snrs})
        snr_map_path = os.path.join(save_dir, "snr_map.csv")
        snr_map.to_csv(snr_map_path)
        snrs = np.repeat(snrs[np.newaxis], 4, axis=0)
        snrs = roll_independant(snrs, np.array([0,-1,-2,-3]))
        stim_dirs = [x for x in os.listdir(self.listDir) if os.path.isdir(os.path.join(self.listDir, x))]
        shuffle(stim_dirs)
        '''
        snrs = self.participant.data['parameters']['decoder_test_SNRs'] + srt_50
        stim_dirs = [
            x for x in os.listdir(self.listDir)
            if os.path.isdir(os.path.join(self.listDir, x))
        ]

        ordered_stim_dirs = []
        for ind in self.participant_parameters['decoder_test_lists']:
            for folder in stim_dirs:
                if re.match(f'Stim_({int(ind)})', folder):
                    ordered_stim_dirs.append(folder)

        # ordered_stim_dirs *= int(len(snrs))
        noise_file = PySndfile(self.noise_path, 'r')
        wav_files = []
        wav_metas = []
        question = []
        marker_files = []
        self.socketio.emit('test_stim_load', namespace='/main')
        for ind, dir_name in enumerate(ordered_stim_dirs[:snrs.shape[1]]):
            logger.debug(
                f"Processing list directory {ind+1} of {snrs.shape[1]}")
            stim_dir = os.path.join(self.listDir, dir_name)
            wav = globDir(stim_dir, "*.wav")[0]
            csv_files = natsorted(globDir(stim_dir, "*.csv"))
            marker_file = csv_files[0]
            question_files = csv_files[1:]
            # rms_file = globDir(stim_dir, "*.npy")[0]
            # speech_rms = float(np.load(rms_file))
            snr = snrs[:, ind]
            audio, fs, enc, fmt = sndio.read(wav, return_format=True)

            speech = audio[:, :2]
            triggers = audio[:, 2]
            #speech_rms, _, _ = asl_P56(speech, fs, 16.)
            rms_no_silences(speech, fs, -30.)

            wf = []
            wm = []
            for ind2, s in enumerate(snr):
                start = randint(0, noise_file.frames() - speech.shape[0])
                noise_file.seek(start)
                noise = noise_file.read_frames(speech.shape[0])
                noise_rms = np.sqrt(np.mean(noise**2))
                # noise_rms = asl_P56(noise, fs, 16)
                snr_fs = 10**(-s / 20)
                if snr_fs == np.inf:
                    snr_fs = 0.
                elif snr_fs == -np.inf:
                    raise ValueError(
                        "Noise infinitely louder than signal at snr: {}".
                        format(snr))
                noise = noise * (speech_rms / noise_rms)
                out_wav_path = os.path.join(
                    save_dir, "Stim_{0}_{1}.wav".format(ind, ind2))
                out_meta_path = os.path.join(
                    save_dir, "Stim_{0}_{1}.npy".format(ind, ind2))
                with np.errstate(divide='raise'):
                    try:
                        out_wav = (speech + (np.stack([noise, noise], axis=1) *
                                             snr_fs)) * self.reduction_coef
                    except:
                        set_trace()
                out_wav = np.concatenate([out_wav, triggers[:, np.newaxis]],
                                         axis=1)
                sndio.write(out_wav_path, out_wav, fs, fmt, enc)
                np.save(out_meta_path, s)
                wf.append(out_wav_path)
                wm.append(out_meta_path)
            wav_metas.append(wm)
            wav_files.append(wf)
            out_marker_path = os.path.join(save_dir,
                                           "Marker_{0}.csv".format(ind))
            marker_files.append(out_marker_path)
            copyfile(marker_file, out_marker_path)
            for q_file in question_files:
                out_q_path = os.path.join(
                    save_dir, "Questions_{0}_{1}.csv".format(ind, ind2))
                self.question_files.append(out_q_path)
                copyfile(q_file, out_q_path)

            for q_file_path in question_files:
                q = []
                with open(q_file_path, 'r') as q_file:
                    q_reader = csv.reader(q_file)
                    for line in q_reader:
                        q.append(line)
                question.append(q)

        self.wav_files = [item for sublist in wav_files for item in sublist]
        self.wav_metas = [item for sublist in wav_metas for item in sublist]

        self.question.extend(question)

        for item in marker_files:
            self.marker_files.extend([item] * 4)

        self.answers = np.empty(np.shape(self.question)[:2])
        self.answers[:] = np.nan
예제 #18
0
def block_process_wav(wavpath, out_wavpath, func, block_size=4096, **args):
    '''
    Mix two wav files, applying gains to each
    '''
    wav = PySndfile(wavpath, 'r')

    out_wav = PySndfile(out_wavpath, 'w', construct_format('wav', 'pcm16'),
                        wav.channels(), wav.samplerate())

    i = 0
    while i < wav.frames():
        if i + block_size > wav.frames():
            block_size = wav.frames() - i
        x = wav.read_frames(block_size)
        y = func(x, **args)
        out_wav.write_frames(y)
        i += block_size
    del out_wav
예제 #19
0
def get_info(name) :
    """
    retrieve samplerate, encoding (str) and format informationfor sndfile name
    """
    sf  = PySndfile(name)
    return sf.samplerate(), sf.encoding_str(), sf.major_format_str()
예제 #20
0
parser.add_argument('-m', "--matrixform", action='store_true', help="use regular time division (matrix form)")
parser.add_argument('-l', "--reducedform", action='count', default=0, help="if real==1: omit bins for f=0 and f=fs/2 (lossy=1), or also the transition bands (lossy=2)")
parser.add_argument('-t', "--time", type=int, default=1, help="timing calculation n-fold (default=%(default)s)")
parser.add_argument('-p', "--plot", action='store_true', help="plot results (needs installed matplotlib and scipy packages)")

args = parser.parse_args()
if not os.path.exists(args.input):
    parser.error("Input file '%s' not found"%args.input)

# Read audio data
if Sndfile is not None:
    sf = Sndfile(args.input)
    fs = sf.samplerate
    samples = sf.nframes
else:
    sf = PySndfile(args.input)
    fs = sf.samplerate()
    samples = sf.frames()
s = sf.read_frames(samples)

if s.ndim > 1: 
    s = np.mean(s, axis=1)
    
scales = {'log':LogScale, 'lin':LinScale, 'mel':MelScale, 'oct':OctScale}
try:
    scale = scales[args.scale]
except KeyError:
    parser.error('scale unknown')

scl = scale(args.fmin, args.fmax, args.bins)
예제 #21
0
from optparse import OptionParser

parser = OptionParser(usage="Usage: %prog [options]")
parser.add_option("-i", dest="infile", help="Input file name")
parser.add_option("-o", dest="outfile", help="Output file name")
(opts, args) = parser.parse_args()

if opts.infile == None or opts.outfile == None:
    parser.print_help()
    exit(1)

if not os.path.isfile(opts.infile):
    print "Error: {} does not exists".format(opts.infile)
    exit(1)

wav = PySndfile(opts.infile)

parts = opts.infile.split("/")
filename = parts[-1]
name = filename.replace(".wav", "").replace("-", "_").replace(".", "_")
mic = parts[-2].replace("-", "_").replace(" ", "_")
vendor = parts[-3].replace("-", "_").replace(" ", "_")
nsname = vendor + "_" + mic + "_" + name

outfile = vendor + "_" + mic + "_" + opts.outfile
print "Creating {} ...".format(outfile)

with open(outfile, "w") as f:
    f.write("/// Impulse response header, generated by ir_wav2h.py\n")
    f.write("/// Input file: {}\n\n".format(filename))
    f.write("#ifndef IR_{}_H\n#define IR_{}_H\n\n".format(
예제 #22
0
def concatenateStimuli(MatrixDir, OutDir, Length, n):
    # Get matrix wav file paths
    wavFiles = globDir(MatrixDir, '*.wav')

    stim_parts = os.path.join(MatrixDir, "stim_parts.csv")
    stim_words = os.path.join(MatrixDir, "stim_words.csv")
    stim_part_rows = []
    with open(stim_parts, 'r') as csvfile:
        stim_part_rows = [line for line in csv.reader(csvfile)]
    with open(stim_words, 'r') as csvfile:
        stim_word_rows = [line for line in csv.reader(csvfile)]

    wavFiles = natsorted(wavFiles)
    totalSize = 0
    y = []
    parts = []
    questions = []
    i = 0
    gapSize = np.uniform(0.8, 1.2, len(wavFiles))
    for wav, gap in zip(wavFiles, gapSize):
        if i == n:
            break
        wavObj = PySndfile(wav)
        fs = wavObj.samplerate()
        size = wavObj.frames()
        totalSize += size
        totalSize += int(gap * fs)
        if (totalSize / fs) > Length:
            # total size + 2 second silence at start
            y.append(np.zeros((totalSize + 2 * fs, 3)))
            parts.append([])
            questions.append([])
            i += 1
            totalSize = 0

    writePtr = 2 * fs
    idx = np.arange(0, writePtr)
    chunk = np.zeros(idx.size)
    chunk = np.vstack([chunk, chunk, chunk]).T
    trigger = gen_trigger(idx, 2., 0.01, fs)
    chunk[:, 2] = trigger
    for i, _ in enumerate(y):
        y[i][0:writePtr, :] = chunk

    i = 0
    for wav, word, part in zip(wavFiles, stim_word_rows, stim_part_rows):
        if writePtr >= y[i].shape[0]:
            i += 1
            writePtr = fs * 2
        if i == n:
            break
        x, fs, encStr, fmtStr = sndio.read(wav, return_format=True)
        threeMs = int(0.1 * fs)
        silence = np.zeros(threeMs)
        chunk = np.append(x, silence)

        idx = np.arange(writePtr, writePtr + chunk.shape[0])
        chunk = np.vstack([chunk, chunk, np.zeros(chunk.shape[0])]).T
        trigger = gen_trigger(idx, 2., 0.01, fs)
        chunk[:, 2] = trigger

        y[i][writePtr:writePtr + chunk.shape[0], :] = chunk
        questions[i].append(word)
        parts[i].append(part)

        writePtr += chunk.shape[0]

    for ind, (data, q, p) in enumerate(zip(y, questions, parts)):
        pysndfile.sndio.write(os.path.join(OutDir, 'stim_{}.wav'.format(ind)),
                              data,
                              format=fmtStr,
                              enc=encStr)
        with open('./out/stim/stim_words_{}.csv'.format(ind), 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerows(q)
        with open('./out/stim/stim_parts_{}.csv'.format(ind), 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerows(p)