def test_new_durs():
    basesettings = {
        "num_voices": 1,
        "pattern_len": 2,
        "overlap": True,
    }
    onsets = np.arange(4)
    iois = np.array((0.75, 0.125, 0.75, 0.375))

    for density in (0, 0.25, 0.5, 0.75, 1.0):
        basesettings["dur_density"] = density
        er = er_preprocess.preprocess_settings(basesettings, silent=True)

        # We do it a few times to get some randomnesss
        for _ in range(3):
            durs = er_rhythm.make.get_durs(er, 0, iois, onsets, ())
            try:
                assert (abs(density - (sum(durs) / er.rhythm_len[0])) <=
                        er.dur_subdivision[0] / 2) or np.all(
                            np.equal(durs, np.minimum(iois, er.min_dur[0])))
                assert np.all(np.less_equal(durs, iois))
            except:  # pylint: disable=bare-except

                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type,
                                          exc_value,
                                          exc_traceback,
                                          file=sys.stdout)
                breakpoint()

    basesettings = {
        "num_voices": 2,
        "pattern_len": 4,
        "min_dur": 1 / 8,
        "dur_subdivision": 1 / 8,
        "rhythmic_quasi_unison": True,
        "rhythmic_quasi_unison_constrain": True,
        "onset_density": 0.5,
    }
    leader_onsets = np.array([0.0, 1.0, 2.25, 3.5])
    leader_durs = np.array([0.25, 0.75, 0.5, 0.5])
    leader_density = 0.5
    for density in (0, 0.25, 0.5, 0.75, 1.0):
        basesettings["dur_density"] = density
        er = er_preprocess.preprocess_settings(basesettings, silent=True)
        l_rhythm = er_rhythm.rhythm.Rhythm.from_er_settings(
            er, 0, leader_onsets, leader_durs)
        try:
            onsets = er_rhythm.make.get_onsets(er, 1, (l_rhythm, ))
            iois = er_rhythm.make.get_iois_from_er(er, 1, onsets)
            durs = er_rhythm.make.get_durs(er, 1, iois, onsets, (l_rhythm, ))
            actual_density = sum(durs) / er.rhythm_len[1]
            # TODO write a function to test that:
            #   - if actual_density is less than leader_density, all durations
            #       lie within leader durations
            #   - otherwise, leader_density is "full"
            warnings.warn(
                "test_new_durs() with rhythmic_quasi_unison_constrain is not complete"
            )
        except:  # pylint: disable=bare-except
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      file=sys.stdout)
            breakpoint()
def test_hocketing_indices():
    basesettings = {
        "num_voices": 3,
        "hocketing": True,
        "rhythmic_quasi_unison": True,
        "rhythm_len": 2,
    }
    er = er_preprocess.preprocess_settings(basesettings, silent=True)
    empty_prev_rhythm1 = er_rhythm.rhythm.Rhythm.from_er_settings(
        er, 0, (), ())
    nonempty_prev_rhythm1 = er_rhythm.rhythm.Rhythm.from_er_settings(
        er,
        0,
        np.array([0.0, 1.0, 1.5, 1.875]),
        np.array([0.25, 0.25, 0.25, 0.125]),
    )
    empty_prev_rhythm2 = er_rhythm.rhythm.Rhythm.from_er_settings(
        er, 1, (), ())
    onset_positions = er_rhythm.make._onset_positions(er, 2)
    oblig_indices = [0, 1, 5, 7]
    try:
        # w/ empty previous rhythms, no oblig onsets
        hocket_indices = er_rhythm.make._hocketing_indices(
            er, 2, onset_positions, (empty_prev_rhythm1, empty_prev_rhythm2),
            ())
        qu_indices, constrain_indices = er_rhythm.make._quasi_unison_indices(
            er, 2, onset_positions, (empty_prev_rhythm1, empty_prev_rhythm2),
            ())
        assert set(hocket_indices) == {i for i in range(8)}
        assert len(qu_indices) == 0
        assert len(constrain_indices) == 0
        # w/ empty previous rhythms, oblig onsets
        hocket_indices = er_rhythm.make._hocketing_indices(
            er,
            2,
            onset_positions,
            (empty_prev_rhythm1, empty_prev_rhythm2),
            (oblig_indices),
        )
        qu_indices, constrain_indices = er_rhythm.make._quasi_unison_indices(
            er,
            2,
            onset_positions,
            (empty_prev_rhythm1, empty_prev_rhythm2),
            (oblig_indices),
        )
        assert set(hocket_indices) == {2, 3, 4, 6}
        assert len(qu_indices) == 0
        assert len(constrain_indices) == 0
        # w/ one non-empty previous rhythms
        hocket_indices = er_rhythm.make._hocketing_indices(
            er,
            2,
            onset_positions,
            (nonempty_prev_rhythm1, empty_prev_rhythm2),
            (),
        )
        qu_indices, constrain_indices = er_rhythm.make._quasi_unison_indices(
            er,
            2,
            onset_positions,
            (nonempty_prev_rhythm1, empty_prev_rhythm2),
            (),
        )
        assert set(hocket_indices) == {1, 2, 3, 5, 7}
        assert set(qu_indices) == {0, 4, 6}
        # w/ one non-empty previous rhythm, oblig onsets
        hocket_indices = er_rhythm.make._hocketing_indices(
            er,
            2,
            onset_positions,
            (nonempty_prev_rhythm1, empty_prev_rhythm2),
            (oblig_indices),
        )
        qu_indices, constrain_indices = er_rhythm.make._quasi_unison_indices(
            er,
            2,
            onset_positions,
            (nonempty_prev_rhythm1, empty_prev_rhythm2),
            (oblig_indices),
        )
        assert set(hocket_indices), {2, 3}
        assert set(qu_indices), {4, 6}
    except:  # pylint: disable=bare-except

        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type,
                                  exc_value,
                                  exc_traceback,
                                  file=sys.stdout)
        breakpoint()
def test_new_onsets():
    # presently, if the number of notes implied by onset_density is smaller than
    #   the number of notes implied by obligatory onsets, excess obligatory
    #   onsets will be ignored in an undefined order
    basesettings = {
        "num_voices": 1,
        "pattern_len": 2,
        "onset_subdivision": 0.25,
    }
    settingsdict1 = {
        "obligatory_onsets": (0, ),
        "obligatory_onsets_modulo": 0.25,
        "onset_density": 1.0,
    }
    test1 = lambda x: np.all(np.equal(x, np.arange(8) * 0.25))

    settingsdict2 = {
        "obligatory_onsets": (0, 0.75),
        "obligatory_onsets_modulo": 1,
        "onset_density": 0.75,
    }
    test2 = lambda x: len(x) == 6 and all([t in x for t in (0, 0.75, 1, 1.75)])

    settingsdict3 = {
        "onset_density": 0.8,
    }
    test3 = lambda x: len(x) == 6
    settingsdict4 = {
        "onset_density": 0.0,
    }
    test4 = lambda x: len(x) == 1
    tests = [
        (settingsdict1, test1),
        (settingsdict2, test2),
        (settingsdict3, test3),
        (settingsdict4, test4),
    ]
    for settingsdict, test_func in tests:
        settingsdict.update(basesettings)
        er = er_preprocess.preprocess_settings(settingsdict, silent=True)
        onsets = er_rhythm.make.get_onsets(er, 0, ())
        try:
            assert test_func(onsets)
            assert all([t for t in onsets >= 0])
            assert all([t for t in onsets < 2])
        except:  # pylint: disable=bare-except
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      file=sys.stdout)
            breakpoint()

    basesettings = {
        "num_voices": 4,
        "hocketing": ((0, 2), (0, 3)),
        "rhythmic_quasi_unison": ((0, 1, 3), ),
        "pattern_len": 2,
        "truncate_patterns": True,
        "onset_density": 0.75,
    }
    oblig_settings = {
        "obligatory_onsets": (0, 0.75, 1.75),
        "obligatory_onsets_modulo": 2,
    }
    oblig_settings.update(basesettings)
    prev_onsets = np.array([0.25, 1.0])
    prev_durs = np.array([0.5, 0.25])
    try:
        er = er_preprocess.preprocess_settings(basesettings, silent=True)
        prev_rhythm = er_rhythm.rhythm.Rhythm.from_er_settings(
            er, 0, prev_onsets, prev_durs)
        for _ in range(5):
            onsets = er_rhythm.make.get_onsets(er, 1, (prev_rhythm, ))
            assert np.all(np.isin(prev_onsets, onsets))
        for _ in range(5):
            onsets = er_rhythm.make.get_onsets(er, 2, (prev_rhythm, ))
            assert np.all(np.isin(prev_onsets, onsets, invert=True))
        for _ in range(5):
            onsets = er_rhythm.make.get_onsets(er, 3, (prev_rhythm, ))
            assert np.all(np.isin(prev_onsets, onsets))
    except:  # pylint: disable=bare-except
        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type,
                                  exc_value,
                                  exc_traceback,
                                  file=sys.stdout)
        breakpoint()
def test_get_i():
    # settingsdict = {
    #     "num_voices": 2,
    #     "onset_density": 0.5,
    #     "rhythm_len": [1.5, 7],
    #     "pattern_len": [4, 7],
    # }
    # er = er_preprocess.preprocess_settings(settingsdict)
    # rhythm = er_rhythm.rhythms_handler(er)[0]
    # num_reps = 2
    # rhythm_onsets = []
    # for rep in range(num_reps):
    #     rhythm_onsets.extend(
    #         [a + rep * rhythm.total_rhythm_len for a in rhythm.onsets]
    #     )
    # try:
    #     for i in range(settingsdict["pattern_len"][0] * 4 * num_reps):
    #         time = i / 4
    #         j = rhythm.get_i_at_or_after(time)
    #         onsets = [a for a in rhythm_onsets if a >= time]
    #         assert rhythm.get_onset_and_dur(j)[0] == min(onsets)
    #         j = rhythm.get_i_before(time)
    #         try:
    #             onset = max([a for a in rhythm_onsets if a < time])
    #         except ValueError:
    #             # max() got an empty iterable because time is < all onset times
    #             assert j == -1
    #         else:
    #             assert rhythm.get_onset_and_dur(j)[0] == onset
    #         j = rhythm.get_i_at_or_before(time)
    #         try:
    #             onset = max([a for a in rhythm_onsets if a <= time])
    #         except ValueError:
    #             # max() got an empty iterable because time is < all onset times
    #             assert j == -1
    #         else:
    #             assert rhythm.get_onset_and_dur(j)[0] == onset

    # except:  # pylint: disable=bare-except

    #     exc_type, exc_value, exc_traceback = sys.exc_info()
    #     traceback.print_exception(
    #         exc_type, exc_value, exc_traceback, file=sys.stdout
    #     )
    #     breakpoint()
    settingsdict = {
        "pattern_len": [1.5, 4],
        "truncate_patterns": True,
        "harmony_len": 4,
        "num_harmonies": 4,
        "num_voices": 2,
        "onset_density": 0.7,
    }
    onsets_and_durs = {
        Fraction(0, 1): Fraction(1, 4),
        Fraction(1, 4): Fraction(1, 4),
        Fraction(1, 2): Fraction(1, 4),
        Fraction(3, 4): Fraction(3, 4),
    }
    initial_onsets = np.array(list(onsets_and_durs.keys()))
    initial_durs = np.array(list(onsets_and_durs.values()))
    er = er_preprocess.preprocess_settings(settingsdict, silent=True)
    rhythm = er_rhythm.rhythm.Rhythm.from_er_settings(er, 0, initial_onsets,
                                                      initial_durs)
    max_onset = max(rhythm.onsets)
    try:
        for i in range(50):
            time = i / 8
            before = rhythm.get_i_before(time)
            at_or_after = rhythm.get_i_at_or_after(time)
            at_or_before = rhythm.get_i_at_or_before(time)
            after = rhythm.get_i_after(time)
            assert before + 1 == at_or_after
            assert at_or_before + 1 == after
            # if there is a note at time, then both comparisons should be false;
            # otherwise, they should both be true
            assert (before == at_or_before) == (at_or_after == after)
            assert (before == at_or_before) or (at_or_before == at_or_after)
            assert (after == at_or_after) or (at_or_before == at_or_after)
            assert rhythm.get_onset_and_dur(at_or_after) == rhythm.at_or_after(
                time)

    except:  # pylint: disable=bare-except

        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type,
                                  exc_value,
                                  exc_traceback,
                                  file=sys.stdout)
        breakpoint()
def test_pad_truncations():
    # data = {
    #     0: 0.5,
    #     1: 0.25,
    # }
    initial_onsets = np.array([0.0, 1.0])
    initial_durs = np.array([0.5, 0.25])
    settingsdict = {
        "num_voices": 2,
        "rhythm_len": [1.5, 2],
        "pattern_len": [2.5, 3],
        "truncate_patterns": True,
    }
    onsets_ = [
        [0, 1, 1.5, 2.5],
        [0, 1, 2],
    ]
    er = er_preprocess.preprocess_settings(settingsdict, silent=True)
    for k, onsets in enumerate(onsets_):

        rhythm = er_rhythm.rhythm.Rhythm.from_er_settings(
            er, k, initial_onsets, initial_durs)
        try:
            assert len(onsets) == len(rhythm._data)
            for i, j in zip(onsets, rhythm._data):
                assert i == j
        except:  # pylint: disable=bare-except
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      file=sys.stdout)
            breakpoint()

    # data = {
    #     0: 0.25,
    #     0.5: 0.25,
    #     1.0: 0.25,
    #     1.5: 0.25,
    # }
    initial_onsets = np.array([0.0, 0.5, 1.0, 1.5])
    initial_durs = np.array([0.25, 0.25, 0.25, 0.25])
    settingsdict = {
        "num_voices": 3,
        "rhythm_len": [1.75, 2, 3.25],
        "pattern_len": [3, 3.75, 7],
        "truncate_patterns": True,
    }
    onsets_ = [
        [
            0,
            0.5,
            1.0,
            1.5,
            1.75,
            2.25,
            2.75,
            3.0,
            3.5,
            4.0,
            4.5,
            4.75,
            5.25,
            5.75,
            6.0,
            6.5,
        ],
        [
            0,
            0.5,
            1.0,
            1.5,
            2.0,
            2.5,
            3.0,
            3.5,
            3.75,
            4.25,
            4.75,
            5.25,
            5.75,
            6.25,
            6.75,
        ],
        [
            0,
            0.5,
            1.0,
            1.5,
            3.25,
            3.75,
            4.25,
            4.75,
            6.5,
        ],
    ]
    er = er_preprocess.preprocess_settings(settingsdict, silent=True)
    for k, onsets in enumerate(onsets_):

        rhythm = er_rhythm.rhythm.Rhythm.from_er_settings(
            er, k, initial_onsets, initial_durs)
        try:
            assert len(onsets) == len(rhythm._data)
            for i, j in zip(onsets, rhythm._data):
                assert i == j
        except:  # pylint: disable=bare-except
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      file=sys.stdout)
            breakpoint()
Exemplo n.º 6
0
def test_between():
    settingsdict = {"num_voices": 2, "tet": 12}
    er = er_preprocess.preprocess_settings(settingsdict, silent=True)
    score = er_classes.Score(num_voices=er.num_voices, tet=er.tet)
    # voice, pitch, onset, dur
    notes = [
        (0, 57, 0, 0.75),
        (1, 58, 0.25, 0.25),
        (1, 59, 0.5, 0.75),
        (0, 60, 0.75, 0.5),
        (0, 61, 0.75, 0.75),
        (0, 62, 1.5, 0.5),
        (1, 63, 1.5, 0.5),
        (0, 64, 2.0, 1.0),
    ]
    note_objs = [
        er_classes.Note(pitch, onset, dur) for _, pitch, onset, dur in notes
    ]
    voice_is = [v for v, _, _, _ in notes]
    for v, n in zip(voice_is, note_objs):
        score.add_note(v, n)
    more_notes = [n for n in score.between()]
    assert note_objs == more_notes
    more_notes = [n for n in score.between(start_time=0.5)]
    assert note_objs[2:] == more_notes
    more_notes = [n for n in score.between(start_time=0.4)]
    assert note_objs[2:] == more_notes
    more_notes = [n for n in score.between(end_time=2.0)]
    assert note_objs[:7] == more_notes
    more_notes = [n for n in score.between(end_time=1.75)]
    assert note_objs[:7] == more_notes
    more_notes = [n for n in score.between(start_time=0.75, end_time=1.5)]
    assert note_objs[3:5] == more_notes
    more_notes = [n for n in score.between(start_time=0.8, end_time=1.2)]
    assert more_notes == []

    note_objs_by_onset = [
        [note_objs[0]],
        [note_objs[1]],
        [note_objs[2]],
        note_objs[3:5],
        note_objs[5:7],
        [note_objs[7]],
    ]
    more_onsets = [o for o in score.notes_by_onset_between()]
    assert more_onsets == note_objs_by_onset
    more_onsets = [n for n in score.notes_by_onset_between(start_time=0.5)]
    assert note_objs_by_onset[2:] == more_onsets
    more_onsets = [n for n in score.notes_by_onset_between(start_time=0.4)]
    assert note_objs_by_onset[2:] == more_onsets
    more_onsets = [n for n in score.notes_by_onset_between(end_time=2.0)]
    assert note_objs_by_onset[:5] == more_onsets
    more_onsets = [n for n in score.notes_by_onset_between(end_time=1.75)]
    assert note_objs_by_onset[:5] == more_onsets
    more_onsets = [
        n for n in score.notes_by_onset_between(start_time=0.75, end_time=1.5)
    ]
    assert note_objs_by_onset[3:4] == more_onsets
    more_onsets = [
        n for n in score.notes_by_onset_between(start_time=0.8, end_time=1.2)
    ]
    assert more_onsets == []
Exemplo n.º 7
0
def _get_er(settingsdict):
    return er_preprocess.preprocess_settings(settingsdict, silent=True)