예제 #1
0
def load_melody(f0_path):
    """Load cante100 f0 annotations

    Args:
        f0_path (str): path to audio file

    Returns:
        F0Data: predominant melody

    """
    if not os.path.exists(f0_path):
        raise IOError("f0_path {} does not exist".format(f0_path))

    times = []
    freqs = []
    with open(f0_path, "r") as fhandle:
        reader = csv.reader(fhandle, delimiter=",")
        for line in reader:
            times.append(float(line[0]))
            freqs.append(float(line[1]))

    times = np.array(times)
    freqs = np.array(freqs)
    confidence = (freqs > 0).astype(float)

    return annotations.F0Data(times, freqs, confidence)
예제 #2
0
def load_melody(melody_path):
    """Load an Orchset melody annotation file

    Args:
        melody_path (str): path to melody annotation file

    Raises:
        IOError: if melody_path doesn't exist

    Returns:
        F0Data: melody annotation data
    """
    if not os.path.exists(melody_path):
        raise IOError("melody_path {} does not exist".format(melody_path))

    times = []
    freqs = []
    confidence = []
    with open(melody_path, "r") as fhandle:
        reader = csv.reader(fhandle, delimiter="\t")
        for line in reader:
            times.append(float(line[0]))
            freqs.append(float(line[1]))
            confidence.append(0.0 if line[1] == "0" else 1.0)

    melody_data = annotations.F0Data(np.array(times), np.array(freqs),
                                     np.array(confidence))
    return melody_data
예제 #3
0
def load_pitch(fhandle: TextIO) -> annotations.F0Data:
    """load a MedleyDB pitch annotation file

    Args:
        pitch_path (str): path to pitch annotation file

    Raises:
        IOError: if pitch_path doesn't exist

    Returns:
        F0Data: pitch annotation

    """

    times = []
    freqs = []
    reader = csv.reader(fhandle, delimiter=",")
    for line in reader:
        times.append(float(line[0]))
        freqs.append(float(line[1]))

    times = np.array(times)
    freqs = np.array(freqs)
    confidence = (cast(np.ndarray, freqs) > 0).astype(float)
    pitch_data = annotations.F0Data(times, freqs, confidence)
    return pitch_data
예제 #4
0
def load_pitch(pitch_path):
    """Load automatic extracted pitch or melody

    Args:
        pitch path (str): Local path where the pitch annotation is stored.
            If `None`, returns None.

    Returns:
        F0Data: pitch annotation

    """
    if pitch_path is None:
        return None

    if not os.path.exists(pitch_path):
        raise IOError("pitch_path {} does not exist".format(pitch_path))

    times = []
    freqs = []
    with open(pitch_path, "r") as fhandle:
        reader = csv.reader(fhandle, delimiter="\t")
        for line in reader:
            times.append(float(line[0]))
            freqs.append(float(line[1]))

    if not times:
        return None

    times = np.array(times)
    freqs = np.array(freqs)
    confidence = (freqs > 0).astype(float)
    return annotations.F0Data(times, freqs, confidence)
예제 #5
0
def load_pitch(pitch_path):
    """load a MedleyDB pitch annotation file

    Args:
        pitch_path (str): path to pitch annotation file

    Raises:
        IOError: if pitch_path doesn't exist

    Returns:
        F0Data: pitch annotation

    """
    if not os.path.exists(pitch_path):
        raise IOError("pitch_path {} does not exist".format(pitch_path))

    times = []
    freqs = []
    with open(pitch_path, "r") as fhandle:
        reader = csv.reader(fhandle, delimiter=",")
        for line in reader:
            times.append(float(line[0]))
            freqs.append(float(line[1]))

    times = np.array(times)
    freqs = np.array(freqs)
    confidence = (freqs > 0).astype(float)
    pitch_data = annotations.F0Data(times, freqs, confidence)
    return pitch_data
예제 #6
0
def load_melody(fhandle: TextIO) -> annotations.F0Data:
    """Load an Orchset melody annotation file

    Args:
        fhandle (str or file-like): File-like object or path to melody annotation file

    Raises:
        IOError: if melody_path doesn't exist

    Returns:
        F0Data: melody annotation data
    """

    times = []
    freqs = []
    confidence = []
    reader = csv.reader(fhandle, delimiter="\t")
    for line in reader:
        times.append(float(line[0]))
        freqs.append(float(line[1]))
        confidence.append(0.0 if line[1] == "0" else 1.0)

    melody_data = annotations.F0Data(np.array(times), np.array(freqs),
                                     np.array(confidence))
    return melody_data
예제 #7
0
def load_melody(melody_path):
    """Load a MedleyDB melody1 or melody2 annotation file

    Args:
        melody_path (str): path to a melody annotation file

    Raises:
        IOError: if melody_path does not exist

    Returns:
        F0Data: melody data

    """
    if not os.path.exists(melody_path):
        raise IOError("melody_path {} does not exist".format(melody_path))

    times = []
    freqs = []
    with open(melody_path, "r") as fhandle:
        reader = csv.reader(fhandle, delimiter=",")
        for line in reader:
            times.append(float(line[0]))
            freqs.append(float(line[1]))

    times = np.array(times)
    freqs = np.array(freqs)
    confidence = (freqs > 0).astype(float)
    melody_data = annotations.F0Data(times, freqs, confidence)
    return melody_data
예제 #8
0
def test_f0_data():
    times = np.array([1.0, 2.0, 3.0])
    frequencies = np.array([100.0, 150.0, 120.0])
    confidence = np.array([0.1, 0.4, 0.2])
    f0_data = annotations.F0Data(times, frequencies, confidence)
    assert np.allclose(f0_data.times, times)
    assert np.allclose(f0_data.frequencies, frequencies)
    assert np.allclose(f0_data.confidence, confidence)
def load_pitch(fhandle: TextIO) -> annotations.F0Data:
    """Load pitch

    Args:
        fhandle (str or file-like): path or file-like object pointing to a pitch annotation file

    Returns:
        F0Data: pitch annotation

    """
    time_step = 128 / 44100  # hop-size / fs

    reader = csv.reader(fhandle, delimiter=",")
    freqs = np.array([float(line[0]) for line in reader])
    times = np.array(np.arange(len(freqs)) * time_step)
    confidence = np.array((freqs > 0.0).astype(float))

    return annotations.F0Data(times, freqs, confidence)
예제 #10
0
def load_f0(fhandle: TextIO) -> annotations.F0Data:
    """Load an ikala f0 annotation

    Args:
        fhandle (str or file-like): File-like object or path to f0 annotation file

    Raises:
        IOError: If f0_path does not exist

    Returns:
        F0Data: the f0 annotation data

    """
    lines = fhandle.readlines()
    f0_midi = np.array([float(line) for line in lines])
    f0_hz = librosa.midi_to_hz(f0_midi) * (f0_midi > 0)
    confidence = (f0_hz > 0).astype(float)
    times = (np.arange(len(f0_midi)) * TIME_STEP) + (TIME_STEP / 2.0)
    f0_data = annotations.F0Data(times, f0_hz, confidence)
    return f0_data
def load_f0(fhandle: TextIO) -> annotations.F0Data:
    """Load a Dagstuhl ChoirSet F0-trajectory.

    Args:
        fhandle (str or file-like): File-like object or path to F0 file

    Returns:
        F0Data Object - the F0-trajectory
    """
    times = []
    freqs = []
    confs = []
    reader = csv.reader(fhandle, delimiter=",")
    for line in reader:
        times.append(float(line[0]))
        freqs.append(float(line[1]))
        if len(line) == 3:
            confs.append(float(line[2]))
        else:
            confs.append(float(1.0))

    return annotations.F0Data(np.array(times), np.array(freqs), np.array(confs))
예제 #12
0
def load_melody(fhandle: TextIO) -> Optional[annotations.F0Data]:
    """Load cante100 f0 annotations

    Args:
        fhandle (str or file-like): path or file-like object pointing to melody annotation file

    Returns:
        F0Data: predominant melody

    """
    times = []
    freqs = []
    reader = csv.reader(fhandle, delimiter=",")
    for line in reader:
        times.append(float(line[0]))
        freqs.append(float(line[1]))

    times = np.array(times)  # type: ignore
    freqs = np.array(freqs)  # type: ignore
    confidence = (cast(np.ndarray, freqs) > 0).astype(float)

    return annotations.F0Data(times, freqs, confidence)
예제 #13
0
def load_pitch_contour(jams_path, string_num):
    """Load a guitarset pitch contour 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:
        F0Data: Pitch contour 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="pitch_contour")
    anno = anno_arr.search(data_source=str(string_num))[0]
    times, values = anno.to_event_values()
    if len(times) == 0:
        return None
    frequencies = [v["frequency"] for v in values]
    return annotations.F0Data(times, np.array(frequencies))
예제 #14
0
def load_melody(fhandle: TextIO) -> annotations.F0Data:
    """Load a MedleyDB melody1 or melody2 annotation file

    Args:
        fhandle (str or file-like): File-like object or path to a melody annotation file

    Raises:
        IOError: if melody_path does not exist

    Returns:
        F0Data: melody data

    """
    times = []
    freqs = []
    reader = csv.reader(fhandle, delimiter=",")
    for line in reader:
        times.append(float(line[0]))
        freqs.append(float(line[1]))

    times = np.array(times)  # type: ignore
    freqs = np.array(freqs)  # type: ignore
    confidence = (cast(np.ndarray, freqs) > 0).astype(float)
    return annotations.F0Data(times, freqs, confidence)
예제 #15
0
def test_f0s():
    f0_data_1 = [
        (
            annotations.F0Data(
                np.array([0.016, 0.048]), np.array([0.0, 260.9]), np.array([0.0, 1.0])
            ),
            None,
        )
    ]
    f0_data_2 = [
        (
            annotations.F0Data(
                np.array([0.016, 0.048]), np.array([0.0, 260.9]), np.array([0.0, 1.0])
            ),
            "f0s_1",
        )
    ]
    f0_data_3 = [
        (
            annotations.F0Data(
                np.array([0.016, 0.048]), np.array([0.0, 260.9]), np.array([0.0, 1.0])
            ),
            "f0s_1",
        ),
        (
            annotations.F0Data(
                np.array([0.003, 0.012]), np.array([0.0, 230.5]), np.array([0.0, 1.0])
            ),
            "f0s_2",
        ),
    ]
    f0_data_4 = (
        annotations.F0Data(
            np.array([0.016, 0.048]), np.array([0.0, 260.9]), np.array([0.0, 1.0])
        ),
        "f0s_1",
    )
    f0_data_5 = [
        [
            annotations.F0Data(
                np.array([0.016, 0.048]), np.array([0.0, 260.9]), np.array([0.0, 1.0])
            ),
            "f0s_1",
        ],
        (
            annotations.F0Data(
                np.array([0.003, 0.012]), np.array([0.0, 230.5]), np.array([0.0, 1.0])
            ),
            "f0s_2",
        ),
    ]
    f0_data_6 = [(None, None)]
    f0_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(f0_data=f0_data_1)
    jam_2 = jams_utils.jams_converter(f0_data=f0_data_2)
    jam_3 = jams_utils.jams_converter(f0_data=f0_data_3)
    jam_6 = jams_utils.jams_converter(f0_data=f0_data_6)

    time, duration, value, confidence = get_jam_data(jam_1, "pitch_contour", 0)
    assert time == [0.016, 0.048]
    assert duration == [0.0, 0.0]
    assert value == [
        {"frequency": 0.0, "index": 0, "voiced": False},
        {"frequency": 260.9, "index": 0, "voiced": True},
    ]
    assert confidence == [0.0, 1.0]

    assert jam_2.annotations[0]["sandbox"]["name"] == "f0s_1"

    time, duration, value, confidence = get_jam_data(jam_3, "pitch_contour", 0)
    assert time == [0.016, 0.048]
    assert duration == [0.0, 0.0]
    assert value == [
        {"frequency": 0.0, "index": 0, "voiced": False},
        {"frequency": 260.9, "index": 0, "voiced": True},
    ]
    assert confidence == [0.0, 1.0]

    time, duration, value, confidence = get_jam_data(jam_3, "pitch_contour", 1)
    assert time == [0.003, 0.012]
    assert duration == [0.0, 0.0]
    assert value == [
        {"frequency": 0.0, "index": 0, "voiced": False},
        {"frequency": 230.5, "index": 0, "voiced": True},
    ]
    assert confidence == [0.0, 1.0]

    time, duration, value, confidence = get_jam_data(jam_6, "pitch_contour", 0)
    assert time == []
    assert duration == []
    assert value == []
    assert confidence == []

    assert type(jam_1) == jams.JAMS

    with pytest.raises(TypeError):
        jams_utils.jams_converter(f0_data=f0_data_4)
    with pytest.raises(TypeError):
        jams_utils.jams_converter(f0_data=f0_data_5)
    with pytest.raises(TypeError):
        jams_utils.jams_converter(f0_data=f0_data_7)