Esempio n. 1
0
def loudnessSignal(y, sr):
    """Calculates the loudness of a signal.

    Args:
        y (list of float): The signal.
        sr (int, optional): The sample rate of the signal.

    Returns:
        float: The negative replay gain as the loudness in dB of the signal.

    """

    # Only these samplerates are accepted by ReplayGain()
    supportedSamplerates = [8000, 32000, 44100, 48000]

    if sr in supportedSamplerates:
        # Sample rate is okay. No need to change signal
        yNew = y
        srNew = sr
    else:
        # Samplerate is not okay
        # Resample the signal to fit

        # Find next higher supported sample rate
        idx = bisect_left(sorted(supportedSamplerates), sr)
        idx = min(idx, len(supportedSamplerates) - 1)
        srNew = supportedSamplerates[idx]
        # Resample signal
        fResample = Resample(
            inputSampleRate=sr, outputSampleRate=srNew, quality=0)
        yNew = fResample(y)

    fReplayGain = ReplayGain(sampleRate=srNew)
    loudness = -(fReplayGain(yNew) + 14)  # Offset replay gain by 14 dB
    return loudness
Esempio n. 2
0
 def __getitem__(self, item):
     left, right = self / 2
     resample_rate = self.freq / item
     res_func = Resample(outputSampleRate=resample_rate, quality=0)
     new_left = left.new(res_func(left.audio))
     new_right = left.new(res_func(right.audio))
     return new_left * new_right
Esempio n. 3
0
 def __getitem__(self, item):
     left, right = self / 2
     resample_rate = self.freq / item
     res_func = Resample(outputSampleRate=resample_rate, quality=0)
     left = list(map(lambda frame: left.new(res_func(frame.audio)), left))
     right = list(
         map(lambda frame: right.new(res_func(frame.audio)), right))
     return sum(left) * sum(right)
Esempio n. 4
0
    def handle(self, audio: np.array) -> np.array:
        left, right = self.to_stereo(audio)

        r = Resample(outputSampleRate=self._resample_freq, quality=0)

        left_new = [r(frame) for frame in self.to_frames(left)]
        right_new = [r(frame) for frame in self.to_frames(right)]

        return self.to_mono(self.to_audio(left_new), self.to_audio(right_new))
Esempio n. 5
0
def get(inputDir, descExt):
    exception = {}
    output = {}

    for path, dname, fnames in os.walk(
            inputDir
    ):  # dname directories names in current directory, fnames file names in current directory.
        for fname in fnames:
            if descExt in fname.lower():
                new_pid = os.fork()
                if new_pid == 0:  # Si new_pid == 0 > forked process.
                    try:  # uso un try..except..finally para asegurarme de _siempre_ terminar el proceso
                        file_name = path + "/" + fname
                        [Sound, Fs, nChannels, md5, bit_rate,
                         codec] = AudioLoader(filename=file_name)()  # lo cargo
                        Sound = Sound[:,
                                      0]  # El algoritmo siempre tira dos canales

                        print file_name

                        impulse, FsImpulse = LoadImpulse.get_impulse()
                        impulse = impulse.astype('float32',
                                                 casting='same_kind')

                        if Fs != FsImpulse:
                            Rs = Resample(inputSampleRate=FsImpulse,
                                          outputSampleRate=Fs)
                            impulse = Rs(impulse)

                        final = np.convolve(Sound, impulse)

                        if descExt == '.aif': descExt = '.aiff'

                        mw = MonoWriter(filename=path + '/R1_' + fname,
                                        sampleRate=Fs,
                                        format=descExt.split('.')[1])
                        mw(final)
                        print 'Done!'

                    except Exception:
                        exception[fname] = [
                            'oops'
                        ]  # De esta forma puedo fijarme si hubo alguna excepcion
                        # pass
                    finally:
                        os._exit(0)  # Cierro el fork y vuelvo al proceso padre
                else:
                    child = new_pid
                os.waitpid(
                    child, 0
                )  # evito crear procesos paralelos, lo que limita la performance de mi programa

    return exception
Esempio n. 6
0
def VCTK(model_config):
    print("Preprocessing VCTK dataset")
    VCTK_path = model_config["raw_data_path"] + "/VCTK"
    VCTK_preprocessed_path = model_config[
        "preprocessed_data_path"] + "/VCTK_8k_DBE"

    clean_dirs = ["/clean_trainset_wav", "/clean_testset_wav"]
    noisy_dirs = ["/noisy_trainset_wav", "/noisy_testset_wav"]

    # copy clean dirs
    for clean_dir in clean_dirs:
        shutil.copytree(VCTK_path + clean_dir,
                        VCTK_preprocessed_path + clean_dir)

    # create dirs
    for noisy_dir in noisy_dirs:
        noisy8k_dir = VCTK_preprocessed_path + noisy_dir.replace(
            "noisy", "noisy8k")
        os.makedirs(noisy8k_dir)

        # preprocessing
        for root, dirs, files in os.walk(VCTK_path + noisy_dir):
            for file in files:
                if file.endswith('.wav'):
                    # read audio
                    file_name = os.path.join(root, file)
                    noisy = MonoLoader(filename=file_name, sampleRate=44100)()
                    noisy8k = MonoLoader(filename=file_name, sampleRate=8000)()

                    # resample audio
                    noisy8k_resampled = Resample(
                        inputSampleRate=8000, outputSampleRate=44100)(noisy8k)

                    # lengths
                    len_noisy = len(noisy)
                    len_noisy8k_resampled = len(noisy8k_resampled)

                    # trimming/appending
                    len_diff = len_noisy8k_resampled - len_noisy
                    if len_diff > 0:
                        noisy8k_resampled = noisy8k_resampled[:len_noisy]
                    elif len_diff < 0:
                        noisy8k_resampled = np.pad(noisy8k_resampled,
                                                   (0, abs(len_diff)),
                                                   'constant',
                                                   constant_values=(0, 0))

                    # write audio
                    output_name = noisy8k_dir + "/" + file.split(
                        ".")[0] + "_8k.wav"
                    MonoWriter(filename=output_name,
                               sampleRate=44100)(noisy8k_resampled)
Esempio n. 7
0
    def get_mix(self, idx, sr=None):
        """
        Returns the audio array of the mixed song

        Arguments
        ---------
        idx : int
            the index of the wanted item
        sr : int or None
            the sampling rate at which the audio will be returned
            (if needed, a resampling is performed). If `None`, no
            resampling is performed

        Returns
        -------
        mix : numpy.ndarray
            the audio waveform of the mixed song
        int :
            The sampling rate of the audio array
        """
        recordings_fn = self.get_mix_paths(idx)

        recordings = []
        for recording_fn in recordings_fn:
            audio, in_sr = utils.open_audio(
                joinpath(self.install_dir, recording_fn))
            recordings.append(audio)

        if len(recordings) > 1:
            L = max(len(rec) for rec in recordings)
            mix = np.zeros(L, dtype=np.float32)
            for rec in recordings:
                mix[:rec.shape[0]] += rec
            mix /= len(rec)
        else:
            mix = recordings[0]

        if sr is not None:
            resampler = Resample(inputSampleRate=in_sr, outputSampleRate=sr)
            mix = resampler(mix)
        else:
            sr = in_sr
        return mix, sr
Esempio n. 8
0
def produce_estimate(model_config, model_path, input_path, output_path):
    print("Producing estimate for file " + input_path)

    # Read audio
    audio = MonoLoader(filename=input_path,
                       sampleRate=model_config['expected_sr'])()
    audio_8k = MonoLoader(filename=input_path, sampleRate=8000)()

    # Resample audio
    audio_nb = Resample(inputSampleRate=8000, outputSampleRate=44100)(audio_8k)

    # Lengths
    len_audio = len(audio)
    len_audio_nb = len(audio_nb)

    # Trimming/appending
    len_diff = len_audio_nb - len_audio
    if len_diff > 0:
        audio_nb = audio_nb[:len_audio]
    elif len_diff < 0:
        audio_nb = np.pad(audio_nb, (0, abs(len_diff)),
                          'constant',
                          constant_values=(0, 0))

    # Prediction
    audio_nb = np.expand_dims(audio_nb, axis=0).T  #(n_frames, n_channels)
    prediction_audio = predict(audio_nb, model_config,
                               model_path)  # Get estimate
    prediction_file_name = os.path.join(
        output_path,
        input_path.split("/")[-1]) + "_prediction.wav"

    # Save estimate as audio file
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    librosa.output.write_wav(prediction_file_name, prediction_audio,
                             model_config['expected_sr'])
Esempio n. 9
0
def DAPS(model_config):
    print("Preprocessing DAPS dataset")
    DAPS_path = model_config["raw_data_path"] + "/DAPS/"
    DAPS_preprocessed_path = model_config[
        "preprocessed_data_path"] + "/DAPS_8k_DBE/"

    # create processed dir
    if not os.path.exists(DAPS_preprocessed_path):
        os.makedirs(DAPS_preprocessed_path)

    devices_and_rooms = [
        'ipad_bedroom1', 'ipad_confroom1', 'ipad_confroom2',
        'ipad_livingroom1', 'ipad_office1', 'ipad_office2',
        'ipadflat_confroom1', 'ipadflat_office1', 'iphone_bedroom1',
        'iphone_livingroom1'
    ]  #'ipad_balcony1' and 'iphone_balcony1' excluded

    duration = 5
    target = "clean"

    # Train samples
    for i in np.arange(9):
        subject = str(i + 1)
        scripts = [str(i % 5 + 1),
                   str((i + 1) % 5 + 1)]  # 2 scripts per subject

        for script in scripts:
            # Female
            target_file = DAPS_path + target + "/f" + subject + "_script" + script + "_" + target + ".wav"
            target_audio = MonoLoader(filename=target_file, sampleRate=44100)()

            starts = np.arange(0, len(target_audio), 44100 * duration)
            for j, start in enumerate(starts):
                # output dir
                output_dir = DAPS_preprocessed_path + target
                if not os.path.exists(output_dir):
                    os.makedirs(output_dir)

                if len(target_audio[start:]) > (
                        44100 *
                        duration):  # check if there is a complete excerpt
                    audio_to_write = target_audio[starts[j]:starts[j + 1]]
                    output_name = output_dir + "/f" + subject + "_script" + script + "_" + target + "_" + str(
                        j) + ".wav"
                    MonoWriter(filename=output_name,
                               sampleRate=44100)(audio_to_write)

            for device_and_room in devices_and_rooms:
                device_path = DAPS_path + device_and_room
                device_file = device_path + "/f" + subject + "_script" + script + "_" + device_and_room + ".wav"
                device_audio = MonoLoader(filename=device_file,
                                          sampleRate=44100)()

                starts = np.arange(0, len(device_audio), 44100 * duration)
                for j, start in enumerate(starts):
                    # output dir
                    output_dir = DAPS_preprocessed_path + device_and_room
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)

                    if len(device_audio[start:]) > (44100 * duration):
                        audio_to_write = device_audio[starts[j]:starts[j + 1]]

                        audio_44k_to_8k = Resample(
                            inputSampleRate=44100,
                            outputSampleRate=8000)(audio_to_write)
                        audio_8k_to_44k = Resample(
                            inputSampleRate=8000,
                            outputSampleRate=44100)(audio_44k_to_8k)

                        len_audio_44k = len(audio_to_write)
                        len_audio_44k_resampled = len(audio_8k_to_44k)

                        # trimming/appending
                        len_diff = len_audio_44k_resampled - len_audio_44k
                        if len_diff > 0:
                            audio_8k_to_44k = audio_8k_to_44k[:len_audio_44k]
                        elif len_diff < 0:
                            audio_8k_to_44k = np.pad(audio_8k_to_44k,
                                                     (0, abs(len_diff)),
                                                     'constant',
                                                     constant_values=(0, 0))

                        output_name = output_dir + "/f" + subject + "_script" + script + "_" + device_and_room + "_" + str(
                            j) + ".wav"
                        MonoWriter(filename=output_name,
                                   sampleRate=44100)(audio_8k_to_44k)

            # Male
            target_file = DAPS_path + target + "/m" + subject + "_script" + script + "_" + target + ".wav"
            target_audio = MonoLoader(filename=target_file, sampleRate=44100)()

            starts = np.arange(0, len(target_audio), 44100 * duration)
            for j, start in enumerate(starts):
                # output dir
                output_dir = DAPS_preprocessed_path + target
                if not os.path.exists(output_dir):
                    os.makedirs(output_dir)

                if len(target_audio[start:]) > (
                        44100 *
                        duration):  # check if there is a complete excerpt
                    audio_to_write = target_audio[starts[j]:starts[j + 1]]
                    output_name = output_dir + "/m" + subject + "_script" + script + "_" + target + "_" + str(
                        j) + ".wav"
                    MonoWriter(filename=output_name,
                               sampleRate=44100)(audio_to_write)

            for device_and_room in devices_and_rooms:
                device_path = DAPS_path + device_and_room
                device_file = device_path + "/m" + subject + "_script" + script + "_" + device_and_room + ".wav"
                device_audio = MonoLoader(filename=device_file,
                                          sampleRate=44100)()

                starts = np.arange(0, len(device_audio), 44100 * duration)
                for j, start in enumerate(starts):
                    # output dir
                    output_dir = DAPS_preprocessed_path + device_and_room
                    if not os.path.exists(output_dir):
                        os.makedirs(output_dir)

                    if len(device_audio[start:]) > (44100 * duration):
                        audio_to_write = device_audio[starts[j]:starts[j + 1]]

                        audio_44k_to_8k = Resample(
                            inputSampleRate=44100,
                            outputSampleRate=8000)(audio_to_write)
                        audio_8k_to_44k = Resample(
                            inputSampleRate=8000,
                            outputSampleRate=44100)(audio_44k_to_8k)

                        len_audio_44k = len(audio_to_write)
                        len_audio_44k_resampled = len(audio_8k_to_44k)

                        # trimming/appending
                        len_diff = len_audio_44k_resampled - len_audio_44k
                        if len_diff > 0:
                            audio_8k_to_44k = audio_8k_to_44k[:len_audio_44k]
                        elif len_diff < 0:
                            audio_8k_to_44k = np.pad(audio_8k_to_44k,
                                                     (0, abs(len_diff)),
                                                     'constant',
                                                     constant_values=(0, 0))

                        output_name = output_dir + "/m" + subject + "_script" + script + "_" + device_and_room + "_" + str(
                            j) + ".wav"
                        MonoWriter(filename=output_name,
                                   sampleRate=44100)(audio_8k_to_44k)

    # Test samples
    # Female
    target_file = DAPS_path + target + "/f10_script5_" + target + ".wav"
    target_audio = MonoLoader(filename=target_file, sampleRate=44100)()

    output_name = DAPS_preprocessed_path + target + "/f10_script5_" + target + ".wav"
    MonoWriter(filename=output_name, sampleRate=44100)(target_audio)

    for device_and_room in devices_and_rooms:
        device_path = DAPS_path + device_and_room
        device_file = device_path + "/f10_script5_" + device_and_room + ".wav"
        device_audio = MonoLoader(filename=device_file, sampleRate=8000)()
        audio_8k_to_44k = Resample(inputSampleRate=8000,
                                   outputSampleRate=44100)(device_audio)

        len_audio_44k = len(target_audio)
        len_audio_44k_resampled = len(audio_8k_to_44k)

        # trimming/appending
        len_diff = len_audio_44k_resampled - len_audio_44k
        if len_diff > 0:
            audio_8k_to_44k = audio_8k_to_44k[:len_audio_44k]
        elif len_diff < 0:
            audio_8k_to_44k = np.pad(audio_8k_to_44k, (0, abs(len_diff)),
                                     'constant',
                                     constant_values=(0, 0))

        output_name = DAPS_preprocessed_path + device_and_room + "/f10_script5_" + device_and_room + ".wav"
        MonoWriter(filename=output_name, sampleRate=44100)(audio_8k_to_44k)

    # Male
    target_file = DAPS_path + target + "/m10_script5_" + target + ".wav"
    target_audio = MonoLoader(filename=target_file, sampleRate=44100)()

    output_name = DAPS_preprocessed_path + target + "/m10_script5_" + target + ".wav"
    MonoWriter(filename=output_name, sampleRate=44100)(target_audio)

    for device_and_room in devices_and_rooms:
        device_path = DAPS_path + device_and_room
        device_file = device_path + "/m10_script5_" + device_and_room + ".wav"
        device_audio = MonoLoader(filename=device_file, sampleRate=8000)()
        audio_8k_to_44k = Resample(inputSampleRate=8000,
                                   outputSampleRate=44100)(device_audio)

        len_audio_44k = len(target_audio)
        len_audio_44k_resampled = len(audio_8k_to_44k)

        # trimming/appending
        len_diff = len_audio_44k_resampled - len_audio_44k
        if len_diff > 0:
            audio_8k_to_44k = audio_8k_to_44k[:len_audio_44k]
        elif len_diff < 0:
            audio_8k_to_44k = np.pad(audio_8k_to_44k, (0, abs(len_diff)),
                                     'constant',
                                     constant_values=(0, 0))

        output_name = DAPS_preprocessed_path + device_and_room + "/m10_script5_" + device_and_room + ".wav"
        MonoWriter(filename=output_name, sampleRate=44100)(audio_8k_to_44k)