예제 #1
0
def test_read_features_wrong_fun():
    my_file_struct = FileStruct("01_-_Come_Together.wav")
    my_file_struct.features_file = os.path.join(
        "fixtures", "01_-_Come_Together.json")
    mfcc = MFCC(my_file_struct, FeatureTypes.est_beatsync, sr=22050)
    mfcc.ref_power = np.mean
    mfcc.read_features()
예제 #2
0
def test_read_features_wrong_fun():
    my_file_struct = FileStruct("01_-_Come_Together.wav")
    my_file_struct.features_file = os.path.join(
        "fixtures", "01_-_Come_Together.json")
    mfcc = MFCC(my_file_struct, FeatureTypes.est_beatsync, sr=22050)
    mfcc.ref_power = np.mean
    mfcc.read_features()
예제 #3
0
def test_save_estimations_existing():
    # Copy estimations file temporarily
    est_file = "tmp.jams"
    shutil.copy(os.path.join("fixtures", "01-Sargon-Mindless-ests.jams"),
                est_file)

    # First, find estimation
    jam = jams.load(est_file)
    params = {"hier": False}
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == 21

    # Add to estimation which will replace it
    file_struct = FileStruct("dummy")
    file_struct.est_file = est_file
    file_struct.features_file = os.path.join("fixtures",
                                             "01_-_Come_Together.json")
    times = np.array([0, 10, 20, 30])
    labels = np.array([-1] * (len(times) - 1))
    msaf.io.save_estimations(file_struct, times, labels, "sf", None, **params)
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == len(times) - 1

    # Add to estimation which will add a new one
    times2 = np.array([0, 10, 20, 30, 40])
    labels2 = np.array([-1] * (len(times2) - 1))
    params2 = {"sf_param": 0.1, "hier": False}
    msaf.io.save_estimations(file_struct, times2, labels2, "sf", None,
                             **params2)

    # Make sure the old one is the same
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == len(times) - 1

    # Make sure the new one is the same
    ann = msaf.io.find_estimation(jam, "sf", None, params2)
    assert len(ann.data) == len(times2) - 1

    # Add hierarchical
    times3 = [np.array([0, 40]), np.array([0, 10, 20, 30, 40])]
    labels3 = [
        np.array([-1] * (len(times3[0]) - 1)),
        np.array([-1] * (len(times3[1]) - 1))
    ]
    params3 = {"sf_param": 0.1, "hier": True}
    msaf.io.save_estimations(file_struct, times3, labels3, "sf", None,
                             **params3)
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params3)
    assert len(ann.data) == 5
    assert ann.data[0].value["level"] == 0
    assert ann.data[1].value["level"] == 1
    assert ann.data[2].value["level"] == 1
    assert ann.data[3].value["level"] == 1
    assert ann.data[4].value["level"] == 1

    # Cleanup
    os.remove(est_file)
예제 #4
0
def test_wrong_type_frame_times():
    """Trying to use custom type for frame times."""
    my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3"))
    my_file_struct.features_file = os.path.join("features", "no_file.json")
    FeatureTypes2 = Enum('FeatureTypes',
                         'framesync1 est_beatsync ann_beatsync')
    cqt = CQT(my_file_struct, FeatureTypes2.framesync1, sr=11025)
    cqt.frame_times
예제 #5
0
def test_no_audio_no_params():
    """The features should raise a NoFileAudioError if different parameters
    want to be explored and no audio file is found."""
    # This file doesn't exist
    no_audio_file_struct = FileStruct("fixtures/chirp_noaudio.mp3")
    no_audio_file_struct.features_file = "features/chirp_noaudio.json"
    feat_type = FeatureTypes.framesync
    CQT(no_audio_file_struct, feat_type, sr=11025).features
예제 #6
0
def test_wrong_type_frame_times():
    """Trying to use custom type for frame times."""
    my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3"))
    my_file_struct.features_file = os.path.join("features", "no_file.json")
    FeatureTypes2 = Enum('FeatureTypes',
                         'framesync1 est_beatsync ann_beatsync')
    cqt = CQT(my_file_struct, FeatureTypes2.framesync1, sr=11025)
    cqt.frame_times
예제 #7
0
def test_save_estimations_existing():
    # Copy estimations file temporarily
    est_file = "tmp.jams"
    shutil.copy(os.path.join("fixtures", "01-Sargon-Mindless-ests.jams"),
                est_file)

    # First, find estimation
    jam = jams.load(est_file)
    params = {"hier": False}
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == 21

    # Add to estimation which will replace it
    file_struct = FileStruct("dummy")
    file_struct.est_file = est_file
    file_struct.features_file = os.path.join("fixtures",
                                             "01_-_Come_Together.json")
    times = np.array([0, 10, 20, 30])
    labels = np.array([-1] * (len(times) - 1))
    msaf.io.save_estimations(file_struct, times, labels, "sf", None, **params)
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == len(times) - 1

    # Add to estimation which will add a new one
    times2 = np.array([0, 10, 20, 30, 40])
    labels2 = np.array([-1] * (len(times2) - 1))
    params2 = {"sf_param": 0.1, "hier": False}
    msaf.io.save_estimations(file_struct, times2, labels2, "sf", None,
                             **params2)

    # Make sure the old one is the same
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params)
    assert len(ann.data) == len(times) - 1

    # Make sure the new one is the same
    ann = msaf.io.find_estimation(jam, "sf", None, params2)
    assert len(ann.data) == len(times2) - 1

    # Add hierarchical
    times3 = [np.array([0, 40]), np.array([0, 10, 20, 30, 40])]
    labels3 = [np.array([-1] * (len(times3[0]) - 1)),
               np.array([-1] * (len(times3[1]) - 1))]
    params3 = {"sf_param": 0.1, "hier": True}
    msaf.io.save_estimations(file_struct, times3, labels3,
                             "sf", None, **params3)
    jam = jams.load(est_file)
    ann = msaf.io.find_estimation(jam, "sf", None, params3)
    assert len(ann.data) == 5
    assert ann.data[0].value["level"] == 0
    assert ann.data[1].value["level"] == 1
    assert ann.data[2].value["level"] == 1
    assert ann.data[3].value["level"] == 1
    assert ann.data[4].value["level"] == 1

    # Cleanup
    os.remove(est_file)
예제 #8
0
def test_save_estimations_hier_wrong():
    file_struct = FileStruct("dummy")
    file_struct.features_file = os.path.join("fixtures", "01_-_Come_Together.json")

    # Wrong times and labels (don't match)
    times = [np.arange(0, 10, 2), np.arange(0, 10, 1)]
    labels = [['A', 'B'], ['a', 'a', 'b']]

    # Should raise assertion error
    msaf.io.save_estimations(file_struct, times, labels, None, None)
예제 #9
0
def test_save_estimations_hier_wrong():
    file_struct = FileStruct("dummy")
    file_struct.features_file = os.path.join("fixtures",
                                             "01_-_Come_Together.json")

    # Wrong times and labels (don't match)
    times = [np.arange(0, 10, 2), np.arange(0, 10, 1)]
    labels = [['A', 'B'], ['a', 'a', 'b']]

    # Should raise assertion error
    msaf.io.save_estimations(file_struct, times, labels, None, None)
예제 #10
0
def test_no_audio():
    """The features should be returned even without having an audio file if
    they have been previously been computed."""
    # This file doesn't exist
    no_audio_file_struct = FileStruct("fixtures/chirp_noaudio.mp3")
    no_audio_file_struct.features_file = "features/chirp_noaudio.json"
    feat_type = FeatureTypes.framesync
    CQT(no_audio_file_struct, feat_type, sr=22050).features
    assert (os.path.isfile(no_audio_file_struct.features_file))
    with open(no_audio_file_struct.features_file) as f:
        data = json.load(f)
    assert(CQT.get_id() in data.keys())
예제 #11
0
def process(in_path,
            sonify_beats=False,
            n_jobs=1,
            overwrite=False,
            out_file="out.json",
            out_beats="out_beats.wav",
            ds_name="*"):
    """Main process to compute features.

    Parameters
    ----------
    in_path: str
        Path to the file or dataset to compute the features.
    sonify_beats: bool
        Whether to sonify the beats on top of the audio file
        (single file mode only).
    n_jobs: int
        Number of threads (collection mode only).
    overwrite: bool
        Whether to overwrite the previously computed features.
    out_file: str
        Path to the output json file (single file mode only).
    out_beats: str
        Path to the new file containing the sonified beats.
    ds_name: str
        Name of the prefix of the dataset (e.g., Beatles)
    """

    # If in_path it's a file, we only compute one file
    if os.path.isfile(in_path):
        file_struct = FileStruct(in_path)
        file_struct.features_file = out_file
        compute_all_features(file_struct, sonify_beats, overwrite, out_beats)

    elif os.path.isdir(in_path):
        # Check that in_path exists
        utils.ensure_dir(in_path)

        # Get files
        file_structs = io.get_dataset_files(in_path, ds_name=ds_name)

        # Compute features using joblib
        Parallel(n_jobs=n_jobs)(delayed(compute_all_features)(
            file_struct, sonify_beats, overwrite, out_beats)
                                for file_struct in file_structs)
예제 #12
0
def test_compute_all_features():
    # Create file struct
    file_struct = FileStruct(audio_file)

    # Set output file
    feat_file = "tmp.json"
    beats_file = "beats.wav"
    file_struct.features_file = feat_file

    # Remove previously computed outputs if exist
    if os.path.isfile(feat_file):
        os.remove(feat_file)
    if os.path.isfile(beats_file):
        os.remove(beats_file)

    # Call main function
    msaf.featextract.compute_all_features(file_struct,
                                          sonify_beats=False,
                                          overwrite=False)
    assert os.path.isfile(feat_file)

    # Call again main function (should do nothing, since feat_file exists)
    msaf.featextract.compute_all_features(file_struct,
                                          sonify_beats=False,
                                          overwrite=False)
    assert os.path.isfile(feat_file)

    # Overwrite
    msaf.featextract.compute_all_features(file_struct,
                                          sonify_beats=False,
                                          overwrite=True)
    assert os.path.isfile(feat_file)

    # Sonify
    msaf.featextract.compute_all_features(file_struct,
                                          sonify_beats=True,
                                          overwrite=True,
                                          out_beats=beats_file)
    assert os.path.isfile(feat_file) and os.path.isfile(beats_file)

    # Clean up
    os.remove(feat_file)
    os.remove(beats_file)
예제 #13
0
파일: featextract.py 프로젝트: beckgom/msaf
def process(
    in_path, sonify_beats=False, n_jobs=1, overwrite=False, out_file="out.json", out_beats="out_beats.wav", ds_name="*"
):
    """Main process to compute features.

    Parameters
    ----------
    in_path: str
        Path to the file or dataset to compute the features.
    sonify_beats: bool
        Whether to sonify the beats on top of the audio file
        (single file mode only).
    n_jobs: int
        Number of threads (collection mode only).
    overwrite: bool
        Whether to overwrite the previously computed features.
    out_file: str
        Path to the output json file (single file mode only).
    out_beats: str
        Path to the new file containing the sonified beats.
    ds_name: str
        Name of the prefix of the dataset (e.g., Beatles)
    """

    # If in_path it's a file, we only compute one file
    if os.path.isfile(in_path):
        file_struct = FileStruct(in_path)
        file_struct.features_file = out_file
        compute_all_features(file_struct, sonify_beats, overwrite, out_beats)

    elif os.path.isdir(in_path):
        # Check that in_path exists
        utils.ensure_dir(in_path)

        # Get files
        file_structs = io.get_dataset_files(in_path, ds_name=ds_name)

        # Compute features using joblib
        Parallel(n_jobs=n_jobs)(
            delayed(compute_all_features)(file_struct, sonify_beats, overwrite, out_beats)
            for file_struct in file_structs
        )
예제 #14
0
def test_compute_all_features():
    # Create file struct
    file_struct = FileStruct(audio_file)

    # Set output file
    feat_file = "tmp.json"
    beats_file = "beats.wav"
    file_struct.features_file = feat_file

    # Remove previously computed outputs if exist
    if os.path.isfile(feat_file):
        os.remove(feat_file)
    if os.path.isfile(beats_file):
        os.remove(beats_file)

    # Call main function
    msaf.featextract.compute_all_features(file_struct, sonify_beats=False,
                                          overwrite=False)
    assert os.path.isfile(feat_file)

    # Call again main function (should do nothing, since feat_file exists)
    msaf.featextract.compute_all_features(file_struct, sonify_beats=False,
                                          overwrite=False)
    assert os.path.isfile(feat_file)

    # Overwrite
    msaf.featextract.compute_all_features(file_struct, sonify_beats=False,
                                          overwrite=True)
    assert os.path.isfile(feat_file)

    # Sonify
    msaf.featextract.compute_all_features(file_struct, sonify_beats=True,
                                          overwrite=True, out_beats=beats_file)
    assert os.path.isfile(feat_file) and os.path.isfile(beats_file)

    # Clean up
    os.remove(feat_file)
    os.remove(beats_file)
예제 #15
0
def test_wrong_ann_frame_times():
    """Trying to get annotated frametimes when no annotated beats are found."""
    my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3"))
    my_file_struct.features_file = os.path.join("features", "no_file.json")
    cqt = CQT(my_file_struct, FeatureTypes.ann_beatsync, sr=11025)
    cqt.frame_times
예제 #16
0
# Msaf imports
import msaf
from msaf.base import FeatureTypes
from msaf.exceptions import (NoAudioFileError, FeatureParamsError,
                             FeatureTypeNotFound)
from msaf.features import CQT, PCP, Tonnetz, MFCC, Tempogram
from msaf.input_output import FileStruct

# Global vars
audio_file = os.path.join("fixtures", "chirp.mp3")
file_struct = FileStruct(audio_file)
file_struct.ref_file = os.path.join("fixtures", "chirp.jams")
msaf.utils.ensure_dir("features")
features_file = os.path.join("features", "chirp.json")
file_struct.features_file = features_file
try:
    os.remove(features_file)
except OSError:
    pass


def test_registry():
    """All the features should be in the features register."""
    assert(CQT.get_id() in msaf.base.features_registry.keys())
    assert(PCP.get_id() in msaf.base.features_registry.keys())
    assert(Tonnetz.get_id() in msaf.base.features_registry.keys())
    assert(MFCC.get_id() in msaf.base.features_registry.keys())
    assert(Tempogram.get_id() in msaf.base.features_registry.keys())

예제 #17
0
def test_wrong_ann_frame_times():
    """Trying to get annotated frametimes when no annotated beats are found."""
    my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3"))
    my_file_struct.features_file = os.path.join("features", "no_file.json")
    cqt = CQT(my_file_struct, FeatureTypes.ann_beatsync, sr=11025)
    cqt.frame_times
예제 #18
0
# Msaf imports
import msaf
from msaf.base import FeatureTypes
from msaf.exceptions import (NoAudioFileError, FeatureParamsError,
                             FeatureTypeNotFound)
from msaf.features import CQT, PCP, Tonnetz, MFCC, Tempogram, Features
from msaf.input_output import FileStruct

# Global vars
audio_file = os.path.join("fixtures", "chirp.mp3")
file_struct = FileStruct(audio_file)
file_struct.ref_file = os.path.join("fixtures", "chirp.jams")
msaf.utils.ensure_dir("features")
features_file = os.path.join("features", "chirp.json")
file_struct.features_file = features_file
try:
    os.remove(features_file)
except OSError:
    pass


def test_registry():
    """All the features should be in the features register."""
    assert(CQT.get_id() in msaf.base.features_registry.keys())
    assert(PCP.get_id() in msaf.base.features_registry.keys())
    assert(Tonnetz.get_id() in msaf.base.features_registry.keys())
    assert(MFCC.get_id() in msaf.base.features_registry.keys())
    assert(Tempogram.get_id() in msaf.base.features_registry.keys())