Debian Codegraph @

docs/examples/16-midi/07-midifile-with-mido.html

raw
1
2 <!DOCTYPE html>
3
4 <html lang="en">
5 <head>
6 <meta charset="utf-8"/>
7 <meta content="width=device-width, initial-scale=1.0" name="viewport"/><meta content="Docutils 0.17.1: http://docutils.sourceforge.net/" name="generator"/>
8 <title>07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo. — Pyo 1.0.5 documentation</title>
9 <link href="../../_static/pygments.css" rel="stylesheet" type="text/css"/>
10 <link href="../../_static/agogo.css" rel="stylesheet" type="text/css"/>
11 <link href="../../_static/sphinx-codeautolink.css" rel="stylesheet" type="text/css"/>
12 <link href="../../_static/autoclasstoc.css" rel="stylesheet" type="text/css"/>
13 <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
14 <script src="../../_static/jquery.js"></script>
15 <script src="../../_static/underscore.js"></script>
16 <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
17 <script src="../../_static/doctools.js"></script>
18 <script src="../../_static/sphinx_highlight.js"></script>
19 <link href="../../_static/E-PyoIcon.ico" rel="shortcut icon"/>
20 <link href="../../about.html" rel="author" title="About these documents"/>
21 <link href="../../genindex.html" rel="index" title="Index"/>
22 <link href="../../search.html" rel="search" title="Search"/>
23 <link href="../17-osc/index.html" rel="next" title="17-osc"/>
24 <link href="06-midi-out.html" rel="prev" title="06-midi-out.py - Sending MIDI events from pyo."/>
25 </head><body>
26 <div class="header-wrapper" role="banner">
27 <div class="header">
28 <div class="headertitle"><a href="../../index.html">Pyo 1.0.5 documentation</a></div>
29 <div aria-label="related navigation" class="rel" role="navigation">
30 <a accesskey="P" href="06-midi-out.html" title="06-midi-out.py - Sending MIDI events from pyo.">previous</a> |
31 <a accesskey="N" href="../17-osc/index.html" title="17-osc">next</a> |
32 <a accesskey="I" href="../../genindex.html" title="General Index">index</a>
33 </div>
34 </div>
35 </div>
36 <div class="content-wrapper">
37 <div class="content">
38 <div class="sidebar">
39 <h3>Table of Contents</h3>
40 <ul>
41 <li class="toctree-l1"><a class="reference internal" href="../../about.html">About pyo</a></li>
42 <li class="toctree-l1"><a class="reference internal" href="../../download.html">Installing pyo with pip</a></li>
43 <li class="toctree-l1"><a class="reference internal" href="../../compiling.html">Compiling pyo from sources</a></li>
44 <li class="toctree-l1"><a class="reference internal" href="../../structure.html">Structure of the library</a></li>
45 <li class="toctree-l1"><a class="reference internal" href="../../gettingstarted.html">Getting started</a></li>
46 <li class="toctree-l1"><a class="reference internal" href="../../winaudioinspect.html">Configuring the audio output (Windows)</a></li>
47 <li class="toctree-l1"><a class="reference internal" href="../../perftips.html">Improve performance of pyo programs</a></li>
48 </ul>
49 <ul class="current">
50 <li class="toctree-l1"><a class="reference internal" href="../../api/index.html">API documentation</a></li>
51 <li class="toctree-l1 current"><a class="reference internal" href="../index.html">Examples</a><ul class="current">
52 <li class="toctree-l2"><a class="reference internal" href="../01-intro/index.html">First steps</a></li>
53 <li class="toctree-l2"><a class="reference internal" href="../02-controls/index.html">Parameter control</a></li>
54 <li class="toctree-l2"><a class="reference internal" href="../03-generators/index.html">Synthesis generators</a></li>
55 <li class="toctree-l2"><a class="reference internal" href="../04-soundfiles/index.html">Playing with soundfiles</a></li>
56 <li class="toctree-l2"><a class="reference internal" href="../05-envelopes/index.html">Amplitude envelopes</a></li>
57 <li class="toctree-l2"><a class="reference internal" href="../06-filters/index.html">Filtering</a></li>
58 <li class="toctree-l2"><a class="reference internal" href="../07-effects/index.html">Creating sound effects</a></li>
59 <li class="toctree-l2"><a class="reference internal" href="../08-dynamics/index.html">Dynamic range of audio signals</a></li>
60 <li class="toctree-l2"><a class="reference internal" href="../09-callbacks/index.html">Calling python functions from audio objects</a></li>
61 <li class="toctree-l2"><a class="reference internal" href="../10-tables/index.html">Using tables</a></li>
62 <li class="toctree-l2 current"><a class="reference internal" href="index.html">How to use MIDI with pyo</a></li>
63 <li class="toctree-l2"><a class="reference internal" href="../17-osc/index.html">How to use OSC with pyo</a></li>
64 <li class="toctree-l2"><a class="reference internal" href="../19-multirate/index.html">Multirate audio processing</a></li>
65 <li class="toctree-l2"><a class="reference internal" href="../20-multicore/index.html">Multicore audio programming</a></li>
66 <li class="toctree-l2"><a class="reference internal" href="../21-utilities/index.html">Utilities</a></li>
67 <li class="toctree-l2"><a class="reference internal" href="../22-events/index.html">Events framework</a></li>
68 <li class="toctree-l2"><a class="reference internal" href="../23-expression/index.html">Evaluating prefix expression</a></li>
69 </ul>
70 </li>
71 <li class="toctree-l1"><a class="reference internal" href="../../tutorials/index.html">Advanced tutorials</a></li>
72 </ul>
73 <div role="search">
74 <h3 style="margin-top: 1.5em;">Search</h3>
75 <form action="../../search.html" class="search" method="get">
76 <input name="q" type="text"/>
77 <input type="submit" value="Go"/>
78 </form>
79 </div>
80 </div>
81 <div class="document">
82 <div class="documentwrapper">
83 <div class="bodywrapper">
84 <div class="body" role="main">
85 <section id="midifile-with-mido-py-reading-a-midi-file-with-mido-and-sending-the-events-to-pyo">
86 <h1>07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo.<a class="headerlink" href="#midifile-with-mido-py-reading-a-midi-file-with-mido-and-sending-the-events-to-pyo" title="Permalink to this heading">¶</a></h1>
87 <p>This example shows how simple it is to play a MIDI file with mido and send the events
88 to an audio synth build with pyo.</p>
89 <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <a class="sphinx-codeautolink-a" href="../../api/alphabetical.html#module-pyo" title="pyo"><span class="nn">pyo</span></a> <span class="kn">import</span> <span class="o">*</span>
90
91 <span class="c1"># Try to import MidiFile from the mido module. You can install mido with pip:</span>
92 <span class="c1"># pip install mido</span>
93
94 <span class="k">try</span><span class="p">:</span>
95 <span class="kn">from</span> <span class="nn">mido</span> <span class="kn">import</span> <span class="n">MidiFile</span>
96 <span class="k">except</span><span class="p">:</span>
97 <span class="nb">print</span><span class="p">(</span><span class="s2">"The `mido` module must be installed to run this example!"</span><span class="p">)</span>
98 <span class="n">exit</span><span class="p">()</span>
99
100 <span class="n">s</span> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/server.html#pyo.Server" title="pyo.lib.server.Server"><span class="n">Server</span></a><span class="p">()</span><span class="o">.</span><span class="n">boot</span><span class="p">()</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
101
102 <span class="c1"># A little audio synth to play the MIDI events.</span>
103 <a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.Notein" title="pyo.lib.midi.Notein"><span class="n">mid</span></a> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.Notein" title="pyo.lib.midi.Notein"><span class="n">Notein</span></a><span class="p">()</span>
104 <a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.MidiAdsr" title="pyo.lib.midi.MidiAdsr"><span class="n">amp</span></a> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.MidiAdsr" title="pyo.lib.midi.MidiAdsr"><span class="n">MidiAdsr</span></a><span class="p">(</span><a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.Notein" title="pyo.lib.midi.Notein"><span class="n">mid</span></a><span class="p">[</span><span class="s2">"velocity"</span><span class="p">])</span>
105 <a class="sphinx-codeautolink-a" href="../../api/classes/utils.html#pyo.MToF" title="pyo.lib.utils.MToF"><span class="n">pit</span></a> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/utils.html#pyo.MToF" title="pyo.lib.utils.MToF"><span class="n">MToF</span></a><span class="p">(</span><a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.Notein" title="pyo.lib.midi.Notein"><span class="n">mid</span></a><span class="p">[</span><span class="s2">"pitch"</span><span class="p">])</span>
106 <span class="n">osc</span> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/tableprocess.html#pyo.Osc" title="pyo.lib.tableprocess.Osc"><span class="n">Osc</span></a><span class="p">(</span><a class="sphinx-codeautolink-a" href="../../api/classes/tables.html#pyo.SquareTable" title="pyo.lib.tables.SquareTable"><span class="n">SquareTable</span></a><span class="p">(),</span> <span class="n">freq</span><span class="o">=</span><a class="sphinx-codeautolink-a" href="../../api/classes/utils.html#pyo.MToF" title="pyo.lib.utils.MToF"><span class="n">pit</span></a><span class="p">,</span> <span class="n">mul</span><span class="o">=</span><a class="sphinx-codeautolink-a" href="../../api/classes/midi.html#pyo.MidiAdsr" title="pyo.lib.midi.MidiAdsr"><span class="n">amp</span></a><span class="p">)</span><span class="o">.</span><span class="n">mix</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
107 <span class="n">rev</span> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/effects.html#pyo.STRev" title="pyo.lib.effects.STRev"><span class="n">STRev</span></a><span class="p">(</span><span class="n">osc</span><span class="p">,</span> <span class="n">revtime</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">cutoff</span><span class="o">=</span><span class="mi">4000</span><span class="p">,</span> <span class="n">bal</span><span class="o">=</span><span class="mf">0.2</span><span class="p">)</span><span class="o">.</span><span class="n">out</span><span class="p">()</span>
108
109 <span class="c1"># Opening the MIDI file...</span>
110 <span class="n">mid</span> <span class="o">=</span> <span class="n">MidiFile</span><span class="p">(</span><span class="s2">"../snds/mapleleafrag.mid"</span><span class="p">)</span>
111
112 <span class="c1"># ... and reading its content.</span>
113 <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="n">mid</span><span class="o">.</span><span class="n">play</span><span class="p">():</span>
114 <span class="c1"># For each message, we convert it to integer data with the bytes()</span>
115 <span class="c1"># method and send the values to pyo's Server with the addMidiEvent()</span>
116 <span class="c1"># method. This method programmatically adds a MIDI message to the</span>
117 <span class="c1"># server's internal MIDI event buffer.</span>
118 <span class="n">s</span><span class="o">.</span><span class="n">addMidiEvent</span><span class="p">(</span><span class="o">*</span><span class="n">message</span><span class="o">.</span><span class="n">bytes</span><span class="p">())</span>
119 </pre></div>
120 </div>
121 </section>
122 <div class="clearer"></div>
123 </div>
124 </div>
125 </div>
126 </div>
127 <div class="clearer"></div>
128 </div>
129 </div>
130 <div class="footer-wrapper">
131 <div class="footer">
132 <div class="left">
133 <div aria-label="related navigaton" role="navigation">
134 <a href="06-midi-out.html" title="06-midi-out.py - Sending MIDI events from pyo.">previous</a> |
135 <a href="../17-osc/index.html" title="17-osc">next</a> |
136 <a href="../../genindex.html" title="General Index">index</a>
137 </div>
138 <div aria-label="source link" role="note">
139 </div>
140 </div>
141 <div class="right">
142 <div class="footer" role="contentinfo">
143 © Copyright 2021, Olivier Bélanger.
144 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.3.0.
145 </div>
146 </div>
147 <div class="clearer"></div>
148 </div>
149 </div>
150 </body>
151 </html>