File: 07-midifile-with-mido.rst.txt

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (40 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download | duplicates (2)
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
07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo.
============================================================================================================================================


This example shows how simple it is to play a MIDI file with mido and send the events
to an audio synth build with pyo.

.. code-block:: python

    from pyo import *
    
    # Try to import MidiFile from the mido module. You can install mido with pip:
    #   pip install mido
    
    try:
        from mido import MidiFile
    except:
        print("The `mido` module must be installed to run this example!")
        exit()
    
    s = Server().boot().start()
    
    # A little audio synth to play the MIDI events.
    mid = Notein()
    amp = MidiAdsr(mid["velocity"])
    pit = MToF(mid["pitch"])
    osc = Osc(SquareTable(), freq=pit, mul=amp).mix(1)
    rev = STRev(osc, revtime=1, cutoff=4000, bal=0.2).out()
    
    # Opening the MIDI file...
    mid = MidiFile("../snds/mapleleafrag.mid")
    
    # ... and reading its content.
    for message in mid.play():
        # For each message, we convert it to integer data with the bytes()
        # method and send the values to pyo's Server with the addMidiEvent()
        # method. This method programmatically adds a MIDI message to the
        # server's internal MIDI event buffer.
        s.addMidiEvent(*message.bytes())