forked from ideoforms/isobar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.ex-midifile-markov.py
More file actions
executable file
·55 lines (46 loc) · 2.11 KB
/
32.ex-midifile-markov.py
File metadata and controls
executable file
·55 lines (46 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
#------------------------------------------------------------------------
# ex-midifile-markov:
#
# Apply first-order Markov chains to an input MIDI file.
#------------------------------------------------------------------------
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(iso.PInt(iso.PRound(iso.PScalar(pattern["amplitude"]), -1)))
#------------------------------------------------------------------------
# The markov property of a learner is a PMarkov, which generates
# outputs by traversing the Markov chain stochastically.
#------------------------------------------------------------------------
timeline = iso.Timeline(90)
timeline.schedule({
"note": note_learner.markov,
"duration": dur_learner.markov,
"amplitude": amp_learner.markov
})
try:
timeline.run()
except KeyboardInterrupt:
timeline.output_device.all_notes_off()