def _load_chords(chords_path): """Private function to load Beatles format chord data from a file Args: chords_path (str): """ if chords_path is None or not os.path.exists(chords_path): return None start_times, end_times, chords = [], [], [] with open(chords_path, 'r') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) reader = csv.reader(f, dialect) for line in reader: start_times.append(float(line[0])) end_times.append(float(line[1])) chords.append(line[2]) chord_data = utils.ChordData( np.array(start_times), np.array(end_times), np.array(chords) ) return chord_data
def load_chords(chords_path): """Load Beatles format chord data from a file Args: chords_path (str): path to chord annotation file Returns: (utils.ChordData): loaded chord data """ if chords_path is None: return None if not os.path.exists(chords_path): raise IOError("chords_path {} does not exist".format(chords_path)) start_times, end_times, chords = [], [], [] with open(chords_path, "r") as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) reader = csv.reader(f, dialect) for line in reader: start_times.append(float(line[0])) end_times.append(float(line[1])) chords.append(line[2]) chord_data = utils.ChordData(np.array([start_times, end_times]).T, chords) return chord_data
def _load_chords(jams_path, leadsheet_version=True): """ Args: jams_path (str): Path of the jams annotation file leadsheet_version (Bool) Whether or not to load the leadsheet version of the chord annotation If False, load the infered version. """ jam = jams.load(jams_path) if leadsheet_version: anno = jam.search(namespace='chord')[0] else: anno = jam.search(namespace='chord')[1] intervals, values = anno.to_interval_values() return utils.ChordData(intervals, values)
def _load_chords(chords_path): if not os.path.exists(chords_path): return None begs = [] # timestamps of chord beginnings ends = [] # timestamps of chord endings chords = [] # chord labels if os.path.exists(chords_path): with open(chords_path, 'r') as fhandle: reader = csv.reader(fhandle, delimiter='\t') for line in reader: begs.append(float(line[0])) ends.append(float(line[1])) chords.append(line[2]) return utils.ChordData(np.array([begs, ends]).T, chords)
def load_chords(chords_path): if not os.path.exists(chords_path): raise IOError("chords_path {} does not exist".format(chords_path)) begs = [] # timestamps of chord beginnings ends = [] # timestamps of chord endings chords = [] # chord labels if os.path.exists(chords_path): with open(chords_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter="\t") for line in reader: begs.append(float(line[0])) ends.append(float(line[1])) chords.append(line[2]) return utils.ChordData(np.array([begs, ends]).T, chords)
def load_chords(jams_path, leadsheet_version=True): """ Args: jams_path (str): Path of the jams annotation file leadsheet_version (Bool) Whether or not to load the leadsheet version of the chord annotation If False, load the infered version. Returns: (ChordData): Chord data """ if not os.path.exists(jams_path): raise IOError("jams_path {} does not exist".format(jams_path)) jam = jams.load(jams_path) if leadsheet_version: anno = jam.search(namespace="chord")[0] else: anno = jam.search(namespace="chord")[1] intervals, values = anno.to_interval_values() return utils.ChordData(intervals, values)
def test_chords(): chord_data_1 = [ ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "A", "E"]), ), None, ) ] chord_data_2 = [ ( utils.ChordData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "B", "C"]), ), "chords_2", ) ] chord_data_3 = [ ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "A", "E"]), ), "chords_1", ), ( utils.ChordData( np.array([[0.0, 0.7, 1.0], [0.7, 1.0, 1.5]]).T, np.array(["A", "B", "C"]), ), "chords_2", ), ] chord_data_4 = ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "A", "E"]) ), None, ) chord_data_5 = [ [ utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "A", "E"]), ), None, ], ( utils.ChordData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array(["A", "B", "C"]), ), "chords_2", ), ] chord_data_6 = [(None, None)] chord_data_7 = [ ( utils.EventData( np.array([0.2, 0.3]), np.array([0.3, 0.4]), np.array(["event A", "event B"]), ), None, ) ] jam_1 = jams_utils.jams_converter(chord_data=chord_data_1) jam_2 = jams_utils.jams_converter(chord_data=chord_data_2) jam_3 = jams_utils.jams_converter(chord_data=chord_data_3) jam_6 = jams_utils.jams_converter(chord_data=chord_data_6) time, duration, value, confidence = get_jam_data(jam_1, "chord", 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == ["A", "A", "E"] assert confidence == [None, None, None] assert jam_2.annotations[0]["sandbox"]["name"] == "chords_2" time, duration, value, confidence = get_jam_data(jam_3, "chord", 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == ["A", "A", "E"] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_3, "chord", 1) assert time == [0.0, 0.7, 1.0] assert duration == [0.7, 0.3, 0.5] assert value == ["A", "B", "C"] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_6, "chord", 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_7)
def test_chords(): chord_data_1 = [ ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'A', 'E']), ), None, ) ] chord_data_2 = [ ( utils.ChordData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'B', 'C']), ), 'chords_2', ) ] chord_data_3 = [ ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'A', 'E']), ), 'chords_1', ), ( utils.ChordData( np.array([[0.0, 0.7, 1.0], [0.7, 1.0, 1.5]]).T, np.array(['A', 'B', 'C']), ), 'chords_2', ), ] chord_data_4 = ( utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'A', 'E']) ), None, ) chord_data_5 = [ [ utils.ChordData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'A', 'E']), ), None, ], ( utils.ChordData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array(['A', 'B', 'C']), ), 'chords_2', ), ] chord_data_6 = [(None, None)] chord_data_7 = [ ( utils.EventData( np.array([0.2, 0.3]), np.array([0.3, 0.4]), np.array(['event A', 'event B']), ), None, ) ] jam_1 = jams_utils.jams_converter(chord_data=chord_data_1) jam_2 = jams_utils.jams_converter(chord_data=chord_data_2) jam_3 = jams_utils.jams_converter(chord_data=chord_data_3) jam_6 = jams_utils.jams_converter(chord_data=chord_data_6) time, duration, value, confidence = get_jam_data(jam_1, 'chord', 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == ['A', 'A', 'E'] assert confidence == [None, None, None] assert jam_2.annotations[0]['sandbox']['name'] == 'chords_2' time, duration, value, confidence = get_jam_data(jam_3, 'chord', 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == ['A', 'A', 'E'] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_3, 'chord', 1) assert time == [0.0, 0.7, 1.0] assert duration == [0.7, 0.3, 0.5] assert value == ['A', 'B', 'C'] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_6, 'chord', 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(chord_data=chord_data_7)