Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
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 = []

    parsed_notes = np.genfromtxt(notes_path, delimiter=',')
    for row in parsed_notes:
        intervals.append([row[0], float(row[0]) + float(row[1])])
        # Convert midi value to frequency
        pitches.append((440 / 32) * (2**((int(row[2]) - 9) / 12)))
        confidence.append(1.0)

    return utils.NoteData(
        np.array(intervals, dtype='float'),
        np.array(pitches, dtype='float'),
        np.array(confidence, dtype='float'),
    )
Beispiel #4
0
def _load_note_ann(jams_path, string_num):
    """
    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.
    """
    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()
    return utils.NoteData(intervals, values, np.ones_like(values))
Beispiel #5
0
def load_note_ann(jams_path, string_num):
    """
    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.
    """
    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()
    return utils.NoteData(intervals, values, np.ones_like(values))
Beispiel #6
0
def _load_note_ann(jams_path, string_num):
    '''
    Parameters:
    -----------
    jams_path : str
        path of the jams annotation file
    string_num : int, in range(6)
        Which string to load.
        0 being the Low E string, 5 is the high e string.
    '''
    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()
    return utils.NoteData(intervals[:, 0], intervals[:, 1], values,
                          np.ones_like(values))
Beispiel #7
0
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:
        note_data (NoteData)

    """
    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)
    return utils.NoteData(np.array(intervals), np.array(pitches), np.array(confidence))
Beispiel #8
0
def test_notes():
    note_data_1 = [
        (
            utils.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, 1, 1]),
            ),
            None,
        )
    ]
    note_data_2 = [
        (
            utils.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, 1, 1]),
            ),
            "notes_2",
        )
    ]
    note_data_3 = [
        (
            utils.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, 1, 1]),
            ),
            "notes_1",
        ),
        (
            utils.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, 1, 1]),
            ),
            "notes_2",
        ),
    ]
    note_data_4 = (
        utils.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, 1, 1]),
        ),
        None,
    )
    note_data_5 = [
        [
            utils.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, 1, 1]),
            ),
            None,
        ],
        (
            utils.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, 1, 1]),
            ),
            "notes_2",
        ),
    ]
    note_data_6 = [(None, None)]
    note_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(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)