def _load_lyrics(lyrics_path): if not os.path.exists(lyrics_path): return None # input: start time (ms), end time (ms), lyric, [pronunciation] with open(lyrics_path, 'r') as fhandle: reader = csv.reader(fhandle, delimiter=' ') start_times = [] end_times = [] lyrics = [] pronunciations = [] for line in reader: start_times.append(float(line[0]) / 1000.0) end_times.append(float(line[1]) / 1000.0) lyrics.append(line[2]) if len(line) > 2: pronunciation = ' '.join(line[3:]) pronunciations.append( pronunciation if pronunciation != '' else None) else: pronunciations.append(None) lyrics_data = utils.LyricData( np.array(start_times), np.array(end_times), np.array(lyrics), np.array(pronunciations), ) return lyrics_data
def load_annotations_granularity(annotations_path, granularity): """Load annotations at the specified level of granularity Args: annotations_path (str): path to a DALI annotation file granularity (str): one of 'notes', 'words', 'lines', 'paragraphs' Returns: NoteData for granularity='notes' or LyricData otherwise """ if not os.path.exists(annotations_path): return None try: with gzip.open(annotations_path, 'rb') as f: output = pickle.load(f) except Exception as e: with gzip.open(annotations_path, 'r') as f: output = pickle.load(f) text = [] notes = [] begs = [] ends = [] for annot in output.annotations['annot'][granularity]: notes.append(round(annot['freq'][0], 3)) begs.append(round(annot['time'][0], 3)) ends.append(round(annot['time'][1], 3)) text.append(annot['text']) if granularity == 'notes': annotation = utils.NoteData( np.array([begs, ends]).T, np.array(notes), None) else: annotation = utils.LyricData(np.array(begs), np.array(ends), np.array(text), None) return annotation
def _load_annotations_granularity(annotations_path, granularity): if not os.path.exists(annotations_path): return None try: with gzip.open(annotations_path, 'rb') as f: output = pickle.load(f) except Exception as e: with gzip.open(annotations_path, 'r') as f: output = pickle.load(f) text = [] notes = [] begs = [] ends = [] for annot in output.annotations['annot'][granularity]: notes.append(round(annot['freq'][0], 3)) begs.append(round(annot['time'][0], 3)) ends.append(round(annot['time'][1], 3)) text.append(annot['text']) if granularity == 'notes': annotation = utils.NoteData(np.array(begs), np.array(ends), np.array(notes), None) else: annotation = utils.LyricData(np.array(begs), np.array(ends), np.array(text), None) return annotation
def load_lyrics(lyrics_path): if not os.path.exists(lyrics_path): raise IOError("lyrics_path {} does not exist".format(lyrics_path)) # input: start time (ms), end time (ms), lyric, [pronunciation] with open(lyrics_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter=" ") start_times = [] end_times = [] lyrics = [] pronunciations = [] for line in reader: start_times.append(float(line[0]) / 1000.0) end_times.append(float(line[1]) / 1000.0) lyrics.append(line[2]) if len(line) > 2: pronunciation = " ".join(line[3:]) pronunciations.append(pronunciation if pronunciation != "" else None) else: pronunciations.append(None) lyrics_data = utils.LyricData( np.array(start_times), np.array(end_times), np.array(lyrics), np.array(pronunciations), ) return lyrics_data
def test_lyrics(): lyrics_data_1 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(["The", "Test"]), np.array([None, None]), ), None, ) ] lyrics_data_2 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(["The", "Test"]), np.array([None, None]), ), "lyrics_1", ) ] lyrics_data_3 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(["The", "Test"]), np.array([None, None]), ), "lyrics_1", ), ( utils.LyricData( np.array([0.0, 0.232]), np.array([0.227, 0.742]), np.array(["is", "cool"]), np.array([None, None]), ), "lyrics_2", ), ] lyrics_data_4 = ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(["The", "Test"]), np.array([None, None]), ), "lyrics_1", ) lyrics_data_5 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(["The", "Test"]), np.array([None, None]), ), "lyrics_1", ), [ utils.LyricData( np.array([0.0, 0.232]), np.array([0.227, 0.742]), np.array(["is", "cool"]), np.array([None, None]), ), "lyrics_2", ], ] lyrics_data_6 = [(None, None)] lyrics_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(lyrics_data=lyrics_data_1) jam_2 = jams_utils.jams_converter(lyrics_data=lyrics_data_2) jam_3 = jams_utils.jams_converter(lyrics_data=lyrics_data_3) jam_6 = jams_utils.jams_converter(lyrics_data=lyrics_data_6) time, duration, value, confidence = get_jam_data(jam_1, "lyrics", 0) assert time == [0.027, 0.232] assert duration == [0.2, 0.51] assert value == ["The", "Test"] assert confidence == [None, None] assert jam_2.annotations[0]["sandbox"]["name"] == "lyrics_1" time, duration, value, confidence = get_jam_data(jam_3, "lyrics", 0) assert time == [0.027, 0.232] assert duration == [0.2, 0.51] assert value == ["The", "Test"] assert confidence == [None, None] time, duration, value, confidence = get_jam_data(jam_3, "lyrics", 1) assert time == [0.0, 0.232] assert duration == [0.227, 0.51] assert value == ["is", "cool"] assert confidence == [None, None] time, duration, value, confidence = get_jam_data(jam_6, "lyrics", 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_7)
def test_lyrics(): lyrics_data_1 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(['The', 'Test']), np.array([None, None]), ), None, ) ] lyrics_data_2 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(['The', 'Test']), np.array([None, None]), ), 'lyrics_1', ) ] lyrics_data_3 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(['The', 'Test']), np.array([None, None]), ), 'lyrics_1', ), ( utils.LyricData( np.array([0.0, 0.232]), np.array([0.227, 0.742]), np.array(['is', 'cool']), np.array([None, None]), ), 'lyrics_2', ), ] lyrics_data_4 = ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(['The', 'Test']), np.array([None, None]), ), 'lyrics_1', ) lyrics_data_5 = [ ( utils.LyricData( np.array([0.027, 0.232]), np.array([0.227, 0.742]), np.array(['The', 'Test']), np.array([None, None]), ), 'lyrics_1', ), [ utils.LyricData( np.array([0.0, 0.232]), np.array([0.227, 0.742]), np.array(['is', 'cool']), np.array([None, None]), ), 'lyrics_2', ], ] lyrics_data_6 = [(None, None)] lyrics_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(lyrics_data=lyrics_data_1) jam_2 = jams_utils.jams_converter(lyrics_data=lyrics_data_2) jam_3 = jams_utils.jams_converter(lyrics_data=lyrics_data_3) jam_6 = jams_utils.jams_converter(lyrics_data=lyrics_data_6) time, duration, value, confidence = get_jam_data(jam_1, 'lyrics', 0) assert time == [0.027, 0.232] assert duration == [0.2, 0.51] assert value == ['The', 'Test'] assert confidence == [None, None] assert jam_2.annotations[0]['sandbox']['name'] == 'lyrics_1' time, duration, value, confidence = get_jam_data(jam_3, 'lyrics', 0) assert time == [0.027, 0.232] assert duration == [0.2, 0.51] assert value == ['The', 'Test'] assert confidence == [None, None] time, duration, value, confidence = get_jam_data(jam_3, 'lyrics', 1) assert time == [0.0, 0.232] assert duration == [0.227, 0.51] assert value == ['is', 'cool'] assert confidence == [None, None] time, duration, value, confidence = get_jam_data(jam_6, 'lyrics', 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(lyrics_data=lyrics_data_7)