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()
def test_process_track(): @raises(AssertionError) def __process_track_wrong_names(file_struct): E.process_track(file_struct, None, None, None) @raises(NoReferencesError) def __process_track_no_refs(file_struct): E.process_track(file_struct, None, None, None) def __process_track_correct(file_struct, bounds_id, labels_id, config): res = E.process_track(file_struct, bounds_id, labels_id, config) assert "HitRate_3F" in res.keys() # Wrong match no_match_fs = FileStruct("udontexist.mp3") no_match_fs.ref_file = "idontexist.mp3" yield (__process_track_wrong_names, no_match_fs) # No References no_ref_fs = FileStruct("udontexist.mp3") no_ref_fs.ref_file = "udontexist.jams" yield (__process_track_no_refs, no_ref_fs) # Correct audio_path = "fixtures/Sargon_test/audio/Mindless_cut.mp3" correct_fs = FileStruct(audio_path) config = {"hier": False} yield (__process_track_correct, correct_fs, "sf", None, config) yield (__process_track_correct, audio_path, "sf", None, config)
def test_read_ann_beats_old_jams(): """Trying to read an old jams file.""" my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) my_file_struct.ref_file = os.path.join("fixtures", "old_jams.jams") pcp = PCP(my_file_struct, FeatureTypes.ann_beatsync, sr=11025) times, frames = pcp.read_ann_beats() assert times is None assert frames is None
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
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
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)
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)
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())
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") feat_type = FeatureTypes.framesync CQT(no_audio_file_struct, feat_type, sr=11025).features
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)
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)
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 )
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") 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())
def test_select_features(): my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) feature = Features.select_features("pcp", my_file_struct, False, True) assert isinstance(feature, PCP) assert feature.feat_type == FeatureTypes.framesync feature = Features.select_features("mfcc", my_file_struct, False, False) assert isinstance(feature, MFCC) assert feature.feat_type == FeatureTypes.est_beatsync feature = Features.select_features("cqt", my_file_struct, True, False) assert isinstance(feature, CQT) assert feature.feat_type == FeatureTypes.ann_beatsync
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
import librosa from nose.tools import raises import numpy as np import os # 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())
def test_frame_times_old_jams(): """Trying to use invalid jams file.""" my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) my_file_struct.ref_file = os.path.join("fixtures", "old_jams.jams") pcp = PCP(my_file_struct, FeatureTypes.ann_beatsync, sr=11025) pcp.frame_times
def test_frame_times_framesync(): """Checking frame times of framesync type of features""" my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) pcp = PCP(my_file_struct, FeatureTypes.framesync, sr=11025) times = pcp.frame_times assert(isinstance(times, np.ndarray))
import librosa from nose.tools import raises import numpy as np import os # 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())
def test_frame_times_no_annotations(): """Checking frame times when there are no beat annotations.""" my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) my_file_struct.ref_file = os.path.join("fixtures", "old_jams.jams") pcp = PCP(my_file_struct, FeatureTypes.ann_beatsync, sr=11025) pcp.frame_times
def test_wrong_frame_times(): my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) pcp = PCP(my_file_struct, FeatureTypes.ann_beatsync, sr=11025) pcp.feat_types = "wrong" pcp.frame_times
def test_global_compute_features(): my_file_struct = FileStruct(os.path.join("fixtures", "chirp.mp3")) feats = Features(my_file_struct, 11025, 1024, FeatureTypes.framesync) feats.compute_features()