Ejemplo n.º 1
0
def write_wav_skip_existing(path, y, sr):
    if not os.path.exists(path):
        if not os.path.exists(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path), exists_ok=True)
        sndio.write(path, data=y, rate=sr, format="wav", enc='pcm16')
        #soundfile.write(path, y, sr, "PCM_16")
    else:
        print("WARNING: Tried writing audio to " + path +
              ", but audio file exists already. Skipping file!")
    return Sample.from_array(path, y, sr)
Ejemplo n.º 2
0
def write_wav_skip_existing(path, y, sr):
    if not os.path.exists(path):
        soundfile.write(path, y, sr, "PCM_16")
    else:
        print("WARNING: Tried writing audio to " + path + ", but audio file exists already. Skipping file!")
    return Sample.from_array(path, y, sr)
Ejemplo n.º 3
0
def getMUSDB_augmented(database_path):

    subsets = list()

    rate = None
    for subset in ['train', 'test']:
        for root, _, files in os.path.walk(os.path.join("database", sub_set)):
            if "voice.wav" in files:
                bass_audio = drums_audio = vocal_audio = mix_audio = None
                for file in files:
                    if file == "bass.wav":
                        bass_path = os.path.join(root, file)
                        bass_audio, sr, _ = sndio.read(bass_path)
                    elif file == "drums.wav":
                        drums_path = os.path.join(root, file)
                        drums_audio, sr, _ = sndio.read(drums_path)
                    elif file == "rest.wav":
                        other_path = os.path.join(root, file)
                        other_audio, sr, _ = sndio.read(other_path)
                    elif file == "mix.wav":
                        mix_path = os.path.join(root, file)
                        mix_audio, sr, _ = sndio.read(mix_path)
                    elif file == "voice.wav":
                        vocal_path = os.path.join(root, file)
                        vocal_audio, sr, _ = sndio.read(vocal_path)

                        if rate is None:
                            rate = sr
                        else:
                            if rate != sr:
                                raise RuntimeError(
                                    "getMUSDB_augmented::error::inconsistent sample rate in {} - {} != {}"
                                    .fromat(root, rate, sr))
                # Add other instruments to form accompaniment
                acc_audio = drums_audio + bass_audio + other_audio
                acc_path = os.path.join(local_path, os.path.basename(root),
                                        "accompaniment.wav")
                acc = write_wav_skip_existing(acc_path, acc_audio, rate)

                # Create mixture
                if mix_audio is None:
                    mix_path = os.path.join(local_path, os.path.basename(root),
                                            "mix.wav")
                    mix_audio = acc_audio + vocal_audio
                    mix = write_wav_skip_existing(mix_path, mix_audio, rate)
                else:
                    mix = Sample.from_array(mix_path, mix_audio, rate)

            diff_signal = np.abs(mix_audio - bass_audio - drums_audio -
                                 other_audio - vocal_audio)
            print(
                "Maximum absolute deviation from source additivity constraint: "
                + str(np.max(diff_signal)))  # Check if acc+vocals=mix
            print(
                "Mean absolute deviation from source additivity constraint:    "
                + str(np.mean(diff_signal)))

            # Collect all sources for now. Later on for
            # SVS: [mix, acc, vocal]
            # Multi-instrument: [mix, bass, drums, other, vocals]
            samples.append((mix, acc, bass, drums, other, vocal))

        subsets.append(samples)

    return subsets
Ejemplo n.º 4
0
def process_track(track__is_wav_tuple):

    track, is_wav = track__is_wav_tuple
    rate = track.rate
    # Get mix and instruments
    # Bass
    bass_audio = track.targets["bass"].audio
    if is_wav:
        bass_path = track.targets["bass"].sources[0]
        bass = Sample.from_array(bass_path, bass_audio, rate)
    else:
        bass_path = track.path[:-4] + "_bass.wav"
        bass = write_wav_skip_existing(bass_path, bass_audio, rate)

    # Drums
    drums_audio = track.targets["drums"].audio
    if is_wav:
        drums_path = track.targets["drums"].sources[0]
        drums = Sample.from_array(drums_path, drums_audio, rate)
    else:
        drums_path = track.path[:-4] + "_drums.wav"
        drums = write_wav_skip_existing(drums_path, drums_audio, rate)

    # Other
    other_audio = track.targets["other"].audio
    if is_wav:
        other_path = track.targets["other"].sources[0]
        other = Sample.from_array(other_path, other_audio, rate)
    else:
        other_path = track.path[:-4] + "_other.wav"
        other = write_wav_skip_existing(other_path, other_audio, rate)

    # Vocals
    vocal_audio = track.targets["vocals"].audio
    if is_wav:
        vocal_path = track.targets["vocals"].sources[0]
        vocal = Sample.from_array(vocal_path, vocal_audio, rate)
    else:
        vocal_path = track.path[:-4] + "_vocals.wav"
        vocal = write_wav_skip_existing(vocal_path, vocal_audio, rate)

    # Add other instruments to form accompaniment
    acc_audio = drums_audio + bass_audio + other_audio
    if is_wav:
        acc_path = os.path.join(os.path.dirname(track.path),
                                "accompaniment.wav")
    else:
        acc_path = track.path[:-4] + "_accompaniment.wav"
    acc = write_wav_skip_existing(acc_path, acc_audio, rate)

    # Create mixture
    mix_audio = track.audio
    if is_wav:
        mix_path = track.path
        mix = Sample.from_array(mix_path, mix_audio, rate)
    else:
        mix_path = track.path[:-4] + "_mix.wav"
        mix = write_wav_skip_existing(mix_path, mix_audio, rate)

    diff_signal = np.abs(mix_audio - bass_audio - drums_audio - other_audio -
                         vocal_audio)

    with _map_process_progress.get_lock():
        _map_process_progress.value += 1

    # Collect all sources for now. Later on for SVS: [mix, acc, vocal] Multi-instrument: [mix, bass, drums, other, vocals]
    return track.name, rate, np.max(diff_signal), np.mean(diff_signal), (mix,
                                                                         acc,
                                                                         bass,
                                                                         drums,
                                                                         other,
                                                                         vocal)