Skip to content

Commit 42e10e0

Browse files
committed
Examples: MIDI file read/write
1 parent 61f4547 commit 42e10e0

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

examples/30.ex-midifile-read.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
#------------------------------------------------------------------------
4+
# ex-midifile-read:
5+
#
6+
# Example of reading from a MIDI file in real time.
7+
#------------------------------------------------------------------------
8+
9+
import isobar as iso
10+
from isobar.io import MidiFileIn
11+
12+
import argparse
13+
import logging
14+
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(message)s")
15+
16+
parser = argparse.ArgumentParser(description="Read and play a .mid file")
17+
parser.add_argument("filename", type=str, help="File to load (.mid)")
18+
args = parser.parse_args()
19+
20+
pattern = iso.PDict()
21+
pattern.load(args.filename)
22+
print(pattern["note"])
23+
24+
timeline = iso.Timeline()
25+
timeline.schedule(pattern)
26+
timeline.run()

examples/31.ex-midifile-write.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@
1010
from isobar.io import MidiFileOut
1111

1212
import logging
13+
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(message)s")
1314

14-
logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] %(message)s")
15-
16-
chords = iso.PSequence([ [0, 2, 4], [1, 3, 5], [2, 4, 6], [3, 5, 7], [4, 6, 8] ], 1)
17-
chords = iso.PSequence([0, 1, 2, 3], 1)
15+
key = iso.Key("C", "major")
1816

1917
filename = "output.mid"
2018
output = MidiFileOut(filename)
2119

22-
timeline = iso.Timeline(120, output)
20+
timeline = iso.Timeline(iso.MAX_CLOCK_RATE, output_device=output)
2321
timeline.sched({
24-
iso.EVENT_NOTE: chords,
25-
iso.EVENT_DURATION: 0.25,
26-
iso.EVENT_GATE: 1.0
22+
iso.EVENT_NOTE: iso.PDegree(iso.PSequence([ 0, 1, 2, 4 ], 4), key),
23+
iso.EVENT_OCTAVE: 5,
24+
iso.EVENT_GATE: iso.PSequence([ 0.5, 1, 2, 1 ]),
25+
iso.EVENT_AMPLITUDE: iso.PSequence([ 100, 80, 60, 40], 4),
26+
iso.EVENT_DURATION: 1.0
2727
})
28+
timeline.sched({
29+
iso.EVENT_NOTE: iso.PDegree(iso.PSequence([ 7, 6, 4, 2 ], 4), key),
30+
iso.EVENT_OCTAVE: 6,
31+
iso.EVENT_GATE: 1,
32+
iso.EVENT_AMPLITUDE: iso.PSequence([ 80, 70, 60, 50], 4),
33+
iso.EVENT_DURATION: 1.0
34+
}, delay=0.5)
2835
timeline.run()
2936
output.write()
3037

0 commit comments

Comments
 (0)