Debian Codegraph @

docs/_sources/examples/16-midi/07-midifile-with-mido.rst.txt

raw
1 07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo.
2 ============================================================================================================================================
3
4
5 This example shows how simple it is to play a MIDI file with mido and send the events
6 to an audio synth build with pyo.
7
8 .. code-block:: python
9
10 from pyo import *
11
12 # Try to import MidiFile from the mido module. You can install mido with pip:
13 # pip install mido
14
15 try:
16 from mido import MidiFile
17 except:
18 print("The `mido` module must be installed to run this example!")
19 exit()
20
21 s = Server().boot().start()
22
23 # A little audio synth to play the MIDI events.
24 mid = Notein()
25 amp = MidiAdsr(mid["velocity"])
26 pit = MToF(mid["pitch"])
27 osc = Osc(SquareTable(), freq=pit, mul=amp).mix(1)
28 rev = STRev(osc, revtime=1, cutoff=4000, bal=0.2).out()
29
30 # Opening the MIDI file...
31 mid = MidiFile("../snds/mapleleafrag.mid")
32
33 # ... and reading its content.
34 for message in mid.play():
35 # For each message, we convert it to integer data with the bytes()
36 # method and send the values to pyo's Server with the addMidiEvent()
37 # method. This method programmatically adds a MIDI message to the
38 # server's internal MIDI event buffer.
39 s.addMidiEvent(*message.bytes())
40