File: 06-midi-out.html

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 (182 lines) | stat: -rw-r--r-- 13,531 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/><meta content="Docutils 0.17.1: http://docutils.sourceforge.net/" name="generator"/>
<title>06-midi-out.py - Sending MIDI events from pyo. — Pyo 1.0.5 documentation</title>
<link href="../../_static/pygments.css" rel="stylesheet" type="text/css"/>
<link href="../../_static/agogo.css" rel="stylesheet" type="text/css"/>
<link href="../../_static/sphinx-codeautolink.css" rel="stylesheet" type="text/css"/>
<link href="../../_static/autoclasstoc.css" rel="stylesheet" type="text/css"/>
<script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script>
<script src="../../_static/underscore.js"></script>
<script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
<script src="../../_static/doctools.js"></script>
<script src="../../_static/sphinx_highlight.js"></script>
<link href="../../_static/E-PyoIcon.ico" rel="shortcut icon"/>
<link href="../../about.html" rel="author" title="About these documents"/>
<link href="../../genindex.html" rel="index" title="Index"/>
<link href="../../search.html" rel="search" title="Search"/>
<link href="07-midifile-with-mido.html" rel="next" title="07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo."/>
<link href="05-control-change.html" rel="prev" title="05-control-change.py - Adding control changes to our MIDI synthesizer class."/>
</head><body>
<div class="header-wrapper" role="banner">
<div class="header">
<div class="headertitle"><a href="../../index.html">Pyo 1.0.5 documentation</a></div>
<div aria-label="related navigation" class="rel" role="navigation">
<a accesskey="P" href="05-control-change.html" title="05-control-change.py - Adding control changes to our MIDI synthesizer class.">previous</a> |
          <a accesskey="N" href="07-midifile-with-mido.html" title="07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo.">next</a> |
          <a accesskey="I" href="../../genindex.html" title="General Index">index</a>
</div>
</div>
</div>
<div class="content-wrapper">
<div class="content">
<div class="sidebar">
<h3>Table of Contents</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../about.html">About pyo</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../download.html">Installing pyo with pip</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../compiling.html">Compiling pyo from sources</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../structure.html">Structure of the library</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../gettingstarted.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../winaudioinspect.html">Configuring the audio output (Windows)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../perftips.html">Improve performance of pyo programs</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../api/index.html">API documentation</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../index.html">Examples</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../01-intro/index.html">First steps</a></li>
<li class="toctree-l2"><a class="reference internal" href="../02-controls/index.html">Parameter control</a></li>
<li class="toctree-l2"><a class="reference internal" href="../03-generators/index.html">Synthesis generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../04-soundfiles/index.html">Playing with soundfiles</a></li>
<li class="toctree-l2"><a class="reference internal" href="../05-envelopes/index.html">Amplitude envelopes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../06-filters/index.html">Filtering</a></li>
<li class="toctree-l2"><a class="reference internal" href="../07-effects/index.html">Creating sound effects</a></li>
<li class="toctree-l2"><a class="reference internal" href="../08-dynamics/index.html">Dynamic range of audio signals</a></li>
<li class="toctree-l2"><a class="reference internal" href="../09-callbacks/index.html">Calling python functions from audio objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="../10-tables/index.html">Using tables</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="index.html">How to use MIDI with pyo</a></li>
<li class="toctree-l2"><a class="reference internal" href="../17-osc/index.html">How to use OSC with pyo</a></li>
<li class="toctree-l2"><a class="reference internal" href="../19-multirate/index.html">Multirate audio processing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../20-multicore/index.html">Multicore audio programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="../21-utilities/index.html">Utilities</a></li>
<li class="toctree-l2"><a class="reference internal" href="../22-events/index.html">Events framework</a></li>
<li class="toctree-l2"><a class="reference internal" href="../23-expression/index.html">Evaluating prefix expression</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorials/index.html">Advanced tutorials</a></li>
</ul>
<div role="search">
<h3 style="margin-top: 1.5em;">Search</h3>
<form action="../../search.html" class="search" method="get">
<input name="q" type="text"/>
<input type="submit" value="Go"/>
</form>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="midi-out-py-sending-midi-events-from-pyo">
<h1>06-midi-out.py - Sending MIDI events from pyo.<a class="headerlink" href="#midi-out-py-sending-midi-events-from-pyo" title="Permalink to this heading">¶</a></h1>
<p>This program gives an example of how to build an algorithm with python/pyo and
then send MIDI events from the Server to a connected MIDI synthesizer (physical
or virtual).</p>
<p>Available MIDI output methods (see the Server documentation for the details):</p>
<ul class="simple">
<li><p>afterout(pitch, velocity, channel=0, timestamp=0)</p></li>
<li><p>bendout(value, channel=0, timestamp=0)</p></li>
<li><p>ctlout(ctlnum, value, channel=0, timestamp=0)</p></li>
<li><p>makenote(pitch, velocity, duration, channel=0)</p></li>
<li><p>noteout(pitch, velocity, channel=0, timestamp=0)</p></li>
<li><p>pressout(value, channel=0, timestamp=0)</p></li>
<li><p>programout(value, channel=0, timestamp=0)</p></li>
<li><p>sysexout(msg, timestamp=0)</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</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>

<a class="sphinx-codeautolink-a" href="../../api/functions/midi.html#pyo.pm_list_devices" title="pyo.pm_list_devices"><span class="n">pm_list_devices</span></a><span class="p">()</span>

<a class="sphinx-codeautolink-a" href="../../api/classes/server.html#pyo.Server" title="pyo.lib.server.Server"><span class="n">s</span></a> <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="c1"># Open all MIDI output devices.</span>
<span class="n">s</span><span class="o">.</span><span class="n">setMidiOutputDevice</span><span class="p">(</span><span class="mi">99</span><span class="p">)</span>

<span class="c1"># Then boot the Server.</span>
<span class="n">s</span><span class="o">.</span><span class="n">boot</span><span class="p">()</span>

<span class="c1"># Generates an audio ramp from 36 to 84, from</span>
<span class="c1"># which MIDI pitches will be extracted.</span>
<a class="sphinx-codeautolink-a" href="../../api/classes/generators.html#pyo.Phasor" title="pyo.lib.generators.Phasor"><span class="n">pitch</span></a> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/generators.html#pyo.Phasor" title="pyo.lib.generators.Phasor"><span class="n">Phasor</span></a><span class="p">(</span><span class="n">freq</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">mul</span><span class="o">=</span><span class="mi">48</span><span class="p">,</span> <span class="n">add</span><span class="o">=</span><span class="mi">36</span><span class="p">)</span>

<span class="c1"># Global variable to count the down and up beats.</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>


<span class="k">def</span> <span class="nf">midi_event</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">count</span>
    <span class="c1"># Retrieve the value of the pitch audio stream and convert it to an int.</span>
    <span class="n">pit</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">pitch</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>

    <span class="c1"># If the count is 0 (down beat), play a louder and longer event, otherwise</span>
    <span class="c1"># play a softer and shorter one.</span>
    <span class="k">if</span> <span class="n">count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">vel</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">90</span><span class="p">,</span> <span class="mi">110</span><span class="p">)</span>
        <span class="n">dur</span> <span class="o">=</span> <span class="mi">500</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">vel</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">70</span><span class="p">)</span>
        <span class="n">dur</span> <span class="o">=</span> <span class="mi">125</span>

    <span class="c1"># Increase and wrap the count to generate a 4 beats sequence.</span>
    <span class="n">count</span> <span class="o">=</span> <span class="p">(</span><span class="n">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">4</span>

    <span class="nb">print</span><span class="p">(</span><span class="s2">"pitch: </span><span class="si">%d</span><span class="s2">, velocity: </span><span class="si">%d</span><span class="s2">, duration: </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">pit</span><span class="p">,</span> <span class="n">vel</span><span class="p">,</span> <span class="n">dur</span><span class="p">))</span>

    <span class="c1"># The Server's `makenote` method generates a noteon event immediately</span>
    <span class="c1"># and the correponding noteoff event after `duration` milliseconds.</span>
    <span class="n">s</span><span class="o">.</span><span class="n">makenote</span><span class="p">(</span><span class="n">pitch</span><span class="o">=</span><span class="n">pit</span><span class="p">,</span> <span class="n">velocity</span><span class="o">=</span><span class="n">vel</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="n">dur</span><span class="p">)</span>


<span class="c1"># Generates a MIDI event every 125 milliseconds.</span>
<span class="n">pat</span> <span class="o">=</span> <a class="sphinx-codeautolink-a" href="../../api/classes/pattern.html#pyo.Pattern" title="pyo.lib.pattern.Pattern"><span class="n">Pattern</span></a><span class="p">(</span><span class="n">midi_event</span><span class="p">,</span> <span class="mf">0.125</span><span class="p">)</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>

<span class="n">s</span><span class="o">.</span><span class="n">gui</span><span class="p">(</span><span class="nb">locals</span><span class="p">())</span>
</pre></div>
</div>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
<div class="footer-wrapper">
<div class="footer">
<div class="left">
<div aria-label="related navigaton" role="navigation">
<a href="05-control-change.html" title="05-control-change.py - Adding control changes to our MIDI synthesizer class.">previous</a> |
            <a href="07-midifile-with-mido.html" title="07-midifile-with-mido.py - Reading a MIDI file with mido and sending the events to pyo.">next</a> |
            <a href="../../genindex.html" title="General Index">index</a>
</div>
<div aria-label="source link" role="note">
</div>
</div>
<div class="right">
<div class="footer" role="contentinfo">
        © Copyright 2021, Olivier Bélanger.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.3.0.
    </div>
</div>
<div class="clearer"></div>
</div>
</div>
</body>
</html>