Example #1
0
def test_get():
    signal = Signal(["test", "test2"], [[1, 2], [3, 4]])
    audio = signal.get("test")
    assert audio == [1, 2]

    with pytest.raises(ValueError) as pe:
        audio = signal.get("test3")
Example #2
0
def test_overlay():
    dummy_signal = Signal(["test"], [np.array(range(100), dtype=np.float32)])
    augment = Augment()

    augment.overlay((5, 10), 17, 1)
    augment.augment(dummy_signal, "test")

    audio = dummy_signal.get("test")
    actual = np.array([11, 12, 13, 14, 15], dtype=np.float32)
    assert np.allclose(audio[17: 22], actual)
Example #3
0
def test_set():
    signal = Signal(["test", "test2"], [[1, 2], [3, 4]])
    signal.set("test", [1, 2.5])

    with pytest.raises(ValueError) as pe:
        signal.set("test3", [1, 2.5])

    signal.set("tesT2", [3, 4.5])

    with pytest.raises(ValueError) as pe:
        signal.set("tesT2", [3, 4.5, 4])
Example #4
0
def augmented():
    music_id = request.args.get("musicID")
    music = Music.query.get_or_404(music_id)
    session_id = session["session_id"]

    file_path = Path(music.file_path)
    file_stem = file_path.stem
    sr = music.sample_rate
    signal = Signal.load_from_path(file_path.parent / file_stem / "augmented",
                                   file_path.parent / file_stem / "original",
                                   sr=sr)

    json_data = request.get_json()
    signal= gen_storage(session, session_id, music_id, json_data, Augment(), signal)

    store_combined_signal(signal, session["dir"], sr)
    return {"augmentation": "success"}
Example #5
0
    def separate(self, audio: Union[str, np.ndarray], sample_rate=44_100):
        """Separate audio into specified stems.

            Note: Spleeter uses tensorflow backend. Hence, corresponding
            installed device will automatically be used (CPU/GPU).
            Minimum VRAM/RAM requirement: 4GB (for small audio, <6 minutes).

            Args:
                audio_file (str, array): path to the original signal or the signal itself.
                sample_rate (int): sampling rate of the file.

            Returns:
                signal (Signal): separated signals.

            Raises:
                tf.errors.ResourceExhaustedError: When memory gets exhausted.

        """
        if isinstance(audio, np.ndarray):
            waveform = audio
        else:
            waveform, _ = self._audio_adapter.load(audio,
                                                   sample_rate=sample_rate)

        print(waveform.shape)
        #predict in chunks
        prediction = {}
        for chunk in self._chunk(waveform, sample_rate):
            chunk_prediction = self._separator.separate(chunk)

            for chunk_key, chunk_value in chunk_prediction.items():
                if chunk_key not in prediction:
                    prediction[chunk_key] = []
                prediction.get(chunk_key).append(chunk_value)

        #merge chunk prediction
        prediction = {k: np.vstack(v) for k, v in prediction.items()}
        print(list(v.shape for v in prediction.values()))
        signal = Signal(prediction.keys(), prediction.values())

        return signal
Example #6
0
def shift_music(music_id, stem_name, session, augment, delta):
    undo = Undo.query.filter_by(music_id=music_id, stem_name=stem_name).all()
    assert len(undo) == 1, undo
    undo = undo[0]
    undo.shift_augmentation(delta)

    current_augmentations = undo.current_augmentations

    storages = Storage.query.filter_by(music_id=music_id).all()
    command_ids = [s.command.cmd_id for s in storages]

    #apply command only upto first `current_augmentations`
    #to the original signal, which acts as undo
    command_ids = command_ids[:current_augmentations]
    commands = get_command_list(command_ids, stem_name)

    #get original signal
    music = Music.query.get(music_id)
    sr = music.sample_rate
    file_path = Path(music.file_path)
    file_stem = file_path.stem
    signal = Signal.load_from_path(file_path.parent / file_stem / "original",
                                   alt_path=None,
                                   sr=sr)

    for command_name, command in commands.items():
        store_attr = CMD_MAP[command_name]
        augment = store_attr(False, sr, augment, command)

    print("Re-augmenting...")
    augment.augment(signal, stem_name)
    print("Done")
    _save_all(signal, session, file_stem, augmented=True)
    store_combined_signal(signal, session["dir"], sr)

    db.session.commit()
Example #7
0
def test_create():
    signal = Signal(["test"], [[1, 2]])
    assert signal is not None
Example #8
0
def test_len():
    signal = Signal(["test", "test2"], [[1, 2, 3], [4, 5, 6]])
    assert len(signal) == 2
    assert signal.get_signal_length() == 3
Example #9
0
def test_n_stem():
    signal = Signal(["test", "test2"], [[1, 2], [3, 4]])
    stem = signal.stems
    assert stem == 2
Example #10
0
def test_diff_len():
    with pytest.raises(Exception) as pe:
        signal = Signal(["test"], [[1], [2, 3]])
Example #11
0
import pytest
import numpy as np

from separator.main import Signal
from separator.main.augment import Augment

dummy_signal = Signal(["test"], [np.array(range(100), dtype=np.float32)])

def test_command():
    augment = Augment()

    augment.amplitude((3, 10), 0, 1)
    assert len(augment.command) == 1

    augment.augment(dummy_signal, "test")
    assert len(augment.command) == 1

    augment.amplitude((3, 10), 0, 1)
    assert len(augment.command) == 2

    augment.clear()
    assert len(augment.command) == 0

def test_amplitude():
    augment = Augment()
    augment.amplitude((3, 10), 0, 1)
    augment.augment(dummy_signal, "test")

    audio = dummy_signal.get("test")
    assert not any(audio[3: 10])