def test_note_data(): intervals = np.array([[1.0, 2.0], [1.5, 3.0], [2.0, 3.0]]) notes = np.array([100.0, 150.0, 120.0]) confidence = np.array([0.1, 0.4, 0.2]) note_data = annotations.NoteData(intervals, notes) assert np.allclose(note_data.intervals, intervals) assert np.allclose(note_data.notes, notes) assert note_data.confidence is None note_data2 = annotations.NoteData(intervals, notes, confidence) assert np.allclose(note_data2.intervals, intervals) assert np.allclose(note_data2.notes, notes) assert np.allclose(note_data2.confidence, confidence) with pytest.raises(ValueError): annotations.NoteData(None, notes) with pytest.raises(ValueError): annotations.NoteData(intervals, None) with pytest.raises(TypeError): annotations.NoteData([1.0, 2.0], notes) with pytest.raises(TypeError): annotations.NoteData(intervals, [3.0, 4.0]) with pytest.raises(TypeError): annotations.NoteData(intervals.astype(int), notes) with pytest.raises(ValueError): annotations.NoteData(intervals, np.array([1.0]))
def load_notes(fhandle: TextIO) -> annotations.NoteData: """Load note data from the annotation files Args: fhandle (str or file-like): path or file-like object pointing to a notes annotation file Returns: NoteData: note annotations """ intervals = [] pitches = [] confidence = [] reader = csv.reader(fhandle, delimiter=",") for line in reader: intervals.append([line[0], float(line[0]) + float(line[1])]) # Convert midi value to frequency pitches.append((440 / 32) * (2**((int(line[2]) - 9) / 12))) confidence.append(1.0) return annotations.NoteData( np.array(intervals, dtype="float"), np.array(pitches, dtype="float"), np.array(confidence, dtype="float"), )
def load_notes(midi_path, midi=None): """Load note data from the midi file. Args: midi_path (str): path to midi file midi (pretty_midi.PrettyMIDI): pre-loaded midi object or None if None, the midi object is loaded using midi_path Returns: NoteData: note annotations """ if midi is None: midi = load_midi(midi_path) intervals = [] pitches = [] confidence = [] for note in midi.instruments[0].notes: intervals.append([note.start, note.end]) pitches.append(librosa.midi_to_hz(note.pitch)) confidence.append(note.velocity / 127.0) return annotations.NoteData( np.array(intervals), np.array(pitches), np.array(confidence) )
def load_notes(notes_path): """Load note data from the annotation files Args: notes_path (str): path to notes file Returns: NoteData: note annotations """ if not os.path.exists(notes_path): raise IOError("notes_path {} does not exist".format(notes_path)) intervals = [] pitches = [] confidence = [] with open(notes_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter=",") for line in reader: intervals.append([line[0], float(line[0]) + float(line[1])]) # Convert midi value to frequency pitches.append((440 / 32) * (2**((int(line[2]) - 9) / 12))) confidence.append(1.0) return annotations.NoteData( np.array(intervals, dtype="float"), np.array(pitches, dtype="float"), np.array(confidence, dtype="float"), )
def get_notes_target(self, track_keys, notes_property="notes"): """Get the notes for all the tracks Args: track_keys (list): list of track keys to get the NoteData for notes_property (str): the attribute associated with NoteData, notes or notes_original Returns: NoteData: Note data for the tracks """ values = [] intervals = [] for k in track_keys: score = getattr(self.tracks[k], notes_property) values.append(score.notes) intervals.append(score.intervals) values = np.hstack(values) intervals = np.vstack(intervals) #### sort on the start time ind = np.argsort(intervals[:, 0], axis=0) start_times = np.take_along_axis(intervals[:, 0], ind, axis=0) end_times = np.take_along_axis(intervals[:, 1], ind, axis=0) intervals = np.vstack([start_times, end_times]).T values = np.take_along_axis(values, ind, axis=0) return annotations.NoteData(intervals, values)
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 """ 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 = annotations.NoteData( np.array([begs, ends]).T, np.array(notes), None) else: annotation = annotations.LyricData( np.array([begs, ends]).T, text, None) return annotation
def load_score(fhandle: TextIO) -> annotations.NoteData: """Load a Dagstuhl ChoirSet time-aligned score representation. Args: fhandle (str or file-like): File-like object or path to score representation file Returns: NoteData Object - the time-aligned score representation """ intervals = np.empty((0, 2)) notes = [] reader = csv.reader(fhandle, delimiter=",") for line in reader: intervals = np.vstack([intervals, [float(line[0]), float(line[1])]]) notes.append(float(line[2])) return annotations.NoteData(intervals, librosa.midi_to_hz(notes), None)
def load_notes(jams_path, string_num): """Load a guitarset note annotation for a given string Args: jams_path (str): Path of the jams annotation file string_num (int), in range(6): Which string to load. 0 is the Low E string, 5 is the high e string. Returns: NoteData: Note data for the given string """ if not os.path.exists(jams_path): raise IOError("jams_path {} does not exist".format(jams_path)) jam = jams.load(jams_path) anno_arr = jam.search(namespace="note_midi") anno = anno_arr.search(data_source=str(string_num))[0] intervals, values = anno.to_interval_values() if len(values) == 0: return None return annotations.NoteData(intervals, np.array(values))
def load_score(fhandle: TextIO) -> annotations.NoteData: """Load a Phenicx-Anechoic score file. Args: fhandle (str or file-like): File-like object or path to score file Returns: NoteData: Note data for the given track """ #### read start, end times intervals = np.loadtxt(fhandle, delimiter=",", usecols=[0, 1], dtype=np.float_) #### read notes as string fhandle.seek(0) content = fhandle.readlines() values = np.array([ librosa.note_to_hz(line.split(",")[2].strip("\n")) for line in content ]) return annotations.NoteData(intervals, values)
def test_notes(): note_data_1 = [ ( annotations.NoteData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), None, ) ] note_data_2 = [ ( annotations.NoteData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), "notes_2", ) ] note_data_3 = [ ( annotations.NoteData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), "notes_1", ), ( annotations.NoteData( np.array([[0.0, 0.7, 1.0], [0.7, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), "notes_2", ), ] note_data_4 = ( annotations.NoteData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), None, ) note_data_5 = [ [ annotations.NoteData( np.array([[0.0, 0.5, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), None, ], ( annotations.NoteData( np.array([[0.0, 0.8, 1.0], [0.5, 1.0, 1.5]]).T, np.array([1108.731, 1108.731, 1108.731]), np.array([1.0, 1.0, 1.0]), ), "notes_2", ), ] note_data_6 = [(None, None)] note_data_7 = [ ( annotations.EventData( np.array([[0.2, 0.3], [0.3, 0.4]]).T, ["event A", "event B"], ), None, ) ] jam_1 = jams_utils.jams_converter(note_data=note_data_1) jam_2 = jams_utils.jams_converter(note_data=note_data_2) jam_3 = jams_utils.jams_converter(note_data=note_data_3) jam_6 = jams_utils.jams_converter(note_data=note_data_6) time, duration, value, confidence = get_jam_data(jam_1, "note_hz", 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == [1108.731, 1108.731, 1108.731] assert confidence == [None, None, None] assert jam_2.annotations[0]["sandbox"]["name"] == "notes_2" time, duration, value, confidence = get_jam_data(jam_3, "note_hz", 0) assert time == [0.0, 0.5, 1.0] assert duration == [0.5, 0.5, 0.5] assert value == [1108.731, 1108.731, 1108.731] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_3, "note_hz", 1) assert time == [0.0, 0.7, 1.0] assert duration == [0.7, 0.3, 0.5] assert value == [1108.731, 1108.731, 1108.731] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_6, "note_hz", 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(note_data=note_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(note_data=note_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(note_data=note_data_7)