def test_pdict(): a = iso.PDict({ "a": iso.PSequence([1, 2, 3], 1), "b": 4, "c": None }) assert list(a) == [ {"a": 1, "b": 4, "c": None}, {"a": 2, "b": 4, "c": None}, {"a": 3, "b": 4, "c": None} ] a = iso.PDict([{"a": 1}, {"a": 2, }, {"a": 3}]) b = a["a"].copy() assert list(b) == [1, 2, 3] assert list(a) == [{"a": 1}, {"a": 2}, {"a": 3}]
def pattern(v): """ Patternify a value, turning it into an object with a next() method to obtain its next value. Pattern subclasses remain untouched. Scalars and other objects are turned into PConst objects. """ if isinstance(v, Pattern): return v elif isinstance(v, dict): return isobar.PDict(v) else: return isobar.PConstant(v)
def test_io_midifile_pdict_save(dummy_timeline): events = { iso.EVENT_NOTE: iso.PSequence([60, 62, 64, 67], 1), iso.EVENT_DURATION: iso.PSequence([0.5, 1.5, 1, 1], 1), iso.EVENT_GATE: iso.PSequence([2, 0.5, 1, 1], 1), iso.EVENT_AMPLITUDE: iso.PSequence([64, 32, 16, 8], 1) } pdict = iso.PDict(events) pdict.save("output.mid") d = MidiFileInputDevice("output.mid").read() for key in events.keys(): assert isinstance(d[key], iso.PSequence) assert list(d[key]) == list(events[key]) os.unlink("output.mid")
import argparse import isobar as iso from isobar.io.midifile.input import MidiFileInputDevice import logging logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] %(message)s") parser = argparse.ArgumentParser(description="Read and play a .mid file") parser.add_argument("filename", type=str, help="File to load (.mid)") args = parser.parse_args() #------------------------------------------------------------------------ # Quantize durations to the nearest 1/8th note. #------------------------------------------------------------------------ pattern = MidiFileInputDevice(args.filename).read(quantize=1 / 8) pattern = iso.PDict(pattern) #------------------------------------------------------------------------ # Learn note, duration and amplitude series separately. #------------------------------------------------------------------------ note_learner = iso.MarkovLearner() note_learner.learn_pattern(pattern["note"]) dur_learner = iso.MarkovLearner() dur_learner.learn_pattern(pattern["duration"]) #------------------------------------------------------------------------ # Quantize velocities to the nearest 10 to make chains easier to # learn with a small sample set. #------------------------------------------------------------------------ amp_learner = iso.MarkovLearner() amp_learner.learn_pattern(