ovos-phal-plugin-midi 0.1.0a1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.4
2
+ Name: ovos-phal-plugin-midi
3
+ Version: 0.1.0a1
4
+ Summary: OpenVoiceOS PHAL plugin: a MIDI instrument on the bus - a General MIDI synth skills can play note by note or give a MIDI file, MIDI keyboards in, real instruments out
5
+ Author: Andreas Lorensen
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/emkay-voice/ovos-phal-plugin-midi
8
+ Project-URL: Issues, https://github.com/emkay-voice/ovos-phal-plugin-midi/issues
9
+ Project-URL: Emkay, https://github.com/emkay-voice/emkay
10
+ Keywords: ovos,openvoiceos,plugin,phal,midi,synthesizer,music,fluidsynth,emkay
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Multimedia :: Sound/Audio :: MIDI
15
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Sound Synthesis
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: ovos-plugin-manager>=0.0.26
20
+ Requires-Dist: ovos-utils>=0.0.38
21
+ Requires-Dist: ovos-bus-client>=0.0.8
22
+ Requires-Dist: numpy
23
+ Requires-Dist: mido>=1.3
24
+ Provides-Extra: ports
25
+ Requires-Dist: python-rtmidi; extra == "ports"
26
+ Provides-Extra: sounddevice
27
+ Requires-Dist: sounddevice; extra == "sounddevice"
28
+ Provides-Extra: fluidsynth
29
+ Requires-Dist: pyfluidsynth; extra == "fluidsynth"
30
+ Provides-Extra: all
31
+ Requires-Dist: python-rtmidi; extra == "all"
32
+ Requires-Dist: sounddevice; extra == "all"
33
+ Requires-Dist: pyfluidsynth; extra == "all"
34
+ Provides-Extra: test
35
+ Requires-Dist: pytest; extra == "test"
36
+ Dynamic: license-file
37
+
38
+ # ovos-phal-plugin-midi
39
+
40
+ An [OpenVoiceOS](https://openvoiceos.org) PHAL plugin that gives the device a
41
+ MIDI instrument on its message bus.
42
+
43
+ It is part of [Emkay](https://github.com/emkay-voice/emkay), and works on any
44
+ OpenVoiceOS.
45
+
46
+ Skills can play music on it note by note, timed to the millisecond and
47
+ changed while it plays, or give it a MIDI file. It plays them on:
48
+
49
+ - **a real instrument plugged into the device**: a USB keyboard, a synth, a
50
+ drum module, or anything else that speaks MIDI;
51
+ - **FluidSynth** with a SoundFont (real instrument samples), when it is
52
+ installed;
53
+ - otherwise **its own synthesizer**: every General MIDI instrument and a drum
54
+ kit, made in numpy, with a little room reverb. It needs nothing installed
55
+ and nothing downloaded.
56
+
57
+ A keyboard plugged into the device works the other way too. What you play
58
+ goes on the bus, so a skill can follow it, and with `thru` the device plays
59
+ it.
60
+
61
+ ## Why a plugin, and why software?
62
+
63
+ Sound cards used to have a synthesizer chip, but that has been gone from
64
+ ordinary computers for twenty years. A Raspberry Pi or a Mark II never had
65
+ one. Today a computer plays MIDI with software: Windows and macOS have
66
+ one built in, Linux uses FluidSynth or TiMidity. MIDI itself is alive and
67
+ well as the language of keyboards and synths, over USB.
68
+
69
+ So this plugin is the device's synth and MIDI port in one. It runs once, in
70
+ OVOS' PHAL service, and every skill can use it over the bus. That is what
71
+ makes music that **changes while it plays** possible. A skill sends the next
72
+ bar a moment ahead. When it changes its mind ("add strings"), it takes back
73
+ what it sent (`ovos.midi.cancel`) and sends the bar again. The sound never
74
+ stops or restarts, which a skill handing finished audio files to the player
75
+ cannot do.
76
+
77
+ ## Install
78
+
79
+ ```bash
80
+ pip install --pre ovos-phal-plugin-midi
81
+ ```
82
+
83
+ That is all the built-in synth needs. Restart OVOS' PHAL service
84
+ (`systemctl --user restart ovos-phal` on ovos-installer and raspOVOS).
85
+
86
+ Optional, for more:
87
+
88
+ | | |
89
+ |---|---|
90
+ | `pip install --pre "ovos-phal-plugin-midi[ports]"` | python-rtmidi: MIDI keyboards and instruments plugged into the device |
91
+ | `pip install --pre "ovos-phal-plugin-midi[sounddevice]"` | PortAudio: the lowest latency from the built-in synth |
92
+ | `pip install --pre "ovos-phal-plugin-midi[fluidsynth]"` + `sudo apt install fluidsynth fluid-soundfont-gm` | FluidSynth with the FluidR3 SoundFont: real instrument samples |
93
+
94
+ ## Configure
95
+
96
+ Nothing has to be set. To change something, in `mycroft.conf`:
97
+
98
+ ```json
99
+ {
100
+ "PHAL": {
101
+ "ovos-phal-plugin-midi": {
102
+ "synth": "auto",
103
+ "audio": "auto",
104
+ "output_port": "",
105
+ "inputs": ["*"],
106
+ "thru": false
107
+ }
108
+ }
109
+ }
110
+ ```
111
+
112
+ | Setting | |
113
+ |---|---|
114
+ | `synth` | `auto` (the default) uses a MIDI port if `output_port` names one that is plugged in, else FluidSynth if it and a SoundFont are there, else the built-in synth. You can also choose `port`, `fluidsynth` or `builtin`. |
115
+ | `audio` | where the built-in synth's sound goes. `auto` (the default) tries `sounddevice`, then `paplay` (PulseAudio and PipeWire), `pw-cat`, `aplay` (plain ALSA), in that order. In a browser it uses `bus`. |
116
+ | `audio_device` | a PortAudio device for `sounddevice` (name or number) |
117
+ | `soundfont` | a `.sf2` file for FluidSynth (default: the first of the usual places, e.g. `/usr/share/sounds/sf2/FluidR3_GM.sf2`) |
118
+ | `output_port` | a MIDI output port: its name or part of it, e.g. `"Roland"` |
119
+ | `inputs` | the input ports to listen to, as names or globs (default `["*"]`: all; `[]`: none) |
120
+ | `thru` | play what comes in from a keyboard on the device's own synth (default `false`) |
121
+ | `gain`, `reverb` | the built-in synth's level (0.8) and room (`true`) |
122
+ | `sample_rate` | the built-in synth's sample rate (24000) |
123
+
124
+ ## Permissions
125
+
126
+ The plugin runs inside OVOS' PHAL service, as the same user. What that user
127
+ needs depends on what you use:
128
+
129
+ - **Sound (the built-in synth or FluidSynth)**: the same access as OVOS' own
130
+ speech.
131
+ - On ovos-installer, raspOVOS and a Mark II, PHAL runs as the desktop or
132
+ `ovos` user in its session, and PipeWire or PulseAudio is there.
133
+ - If PHAL runs as a system service, it needs `XDG_RUNTIME_DIR` (or
134
+ `PULSE_SERVER`) pointing at the user's sound server, or the user in the
135
+ `audio` group for plain ALSA.
136
+ - `aplay -l` or `paplay --version` as that user shows whether it can.
137
+ - **MIDI keyboards and instruments**: ALSA's sequencer (`/dev/snd/seq`) and
138
+ the USB MIDI devices (`/dev/snd/midiC*`). These are open to the `audio`
139
+ group (`sudo usermod -aG audio ovos`, then log in again) and to the user at
140
+ the desk (systemd-logind). `aconnect -l` as that user lists what it can
141
+ see.
142
+ - **Docker** (ovos-docker): the PHAL container needs the sound server's
143
+ socket, as the audio container has it, and for MIDI devices
144
+ `--device /dev/snd` and the `audio` group (`group_add: [audio]`).
145
+ - **Emkay** (a browser): no device access at all. The sound comes over the
146
+ bus to Emkay's page, which plays it. Emkay sees no keyboards yet.
147
+
148
+ The plugin opens no network port and sends nothing off the device.
149
+
150
+ Anyone who can put messages on the bus can make the device play music, as
151
+ they can make it speak. What you play on a keyboard goes on the bus too
152
+ (`ovos.midi.input`), but nowhere else. Set `"inputs": []` to keep the
153
+ keyboards to yourself.
154
+
155
+ ## Rights
156
+
157
+ The plugin is Apache-2.0. Its built-in instruments are made by its own code,
158
+ not recorded, so there is nothing else to license. A SoundFont you install
159
+ for FluidSynth has its own license. Debian's and Fedora's `fluid-soundfont-gm`
160
+ (FluidR3_GM) is MIT, and `timgm6mb-soundfont` is GPL-2. Both are fine to
161
+ use. The music is whoever's music it is: the plugin plays the files it is
162
+ given and keeps nothing.
163
+
164
+ ## The bus
165
+
166
+ All times are wall-clock seconds (`time.time()`) on the device. All
167
+ channels are 0-15, with channel 9 as the drum kit, as in General MIDI.
168
+
169
+ | Message | Data | |
170
+ |---|---|---|
171
+ | `ovos.midi.events` | `source`, `at` (default: now + latency), `events` | play events (below) at `at` + their own time |
172
+ | `ovos.midi.cancel` | `source`, `after` (default: now) | take back that source's events from then on; answers `cancelled` (how many) |
173
+ | `ovos.midi.play` | `uri` / `path` / `data` (base64), `source`, `loop`, `transpose`, `tempo`, `mute` (channels) | play a `.mid` or `.kar` file. Answers `ovos.midi.play.started` (`at`, `duration`) or `.failed` (`error`); `ovos.midi.play.ended` when it ends |
174
+ | `ovos.midi.stop` | `source` | stop that source's file and notes; with no `source`, everything |
175
+ | `ovos.midi.panic` | | stop everything, all notes off |
176
+ | `ovos.midi.status` | | answers with the engine, `latency`, `sample_rate`, `time` (the device's clock), what is playing, the ports |
177
+ | `ovos.midi.ports` | | answers `inputs`, `outputs` |
178
+ | `ovos.midi.input` | `port`, `type`, `channel`, `note`, `velocity`, `control`, `value`, `program` | sent by the plugin: what a keyboard played |
179
+ | `ovos.midi.pcm` | `seq`, `sample_rate`, `at`, `pcm` (base64 s16le mono) | sent by the plugin with `"audio": "bus"`: the sound, for a client that plays it |
180
+
181
+ `mycroft.stop` also stops what plays from files. A skill's own notes are its
182
+ skill's to stop, with `ovos.midi.stop`.
183
+
184
+ Events, each with `t` as seconds after `at`:
185
+
186
+ ```
187
+ ["note", t, channel, note, velocity, seconds] a note with its length
188
+ ["on", t, channel, note, velocity] note on (velocity 0: off)
189
+ ["off", t, channel, note]
190
+ ["program", t, channel, program] a General MIDI instrument, 0-127
191
+ ["cc", t, channel, controller, value] 7 volume, 11 expression, 64 sustain pedal, 123 all notes off
192
+ ["all_off", t, channel] channel -1: every channel
193
+ ```
194
+
195
+ A bar of blues in D, starting in half a second:
196
+
197
+ ```python
198
+ import time
199
+ bus.emit(Message("ovos.midi.events", {"source": "my-skill", "at": time.time() + 0.5, "events": [
200
+ ["program", 0, 0, 0], ["program", 0, 1, 32], # piano, acoustic bass
201
+ ["note", 0.0, 0, 62, 80, 1.8], ["note", 0.0, 0, 66, 80, 1.8], ["note", 0.0, 0, 72, 80, 1.8],
202
+ ["note", 0.0, 1, 38, 100, 0.45], ["note", 0.5, 1, 42, 90, 0.45],
203
+ ["note", 1.0, 1, 45, 90, 0.45], ["note", 1.5, 1, 47, 90, 0.45],
204
+ ["note", 0.0, 9, 36, 110, 0.1], ["note", 0.5, 9, 38, 100, 0.1],
205
+ ["note", 1.0, 9, 36, 110, 0.1], ["note", 1.5, 9, 38, 100, 0.1],
206
+ ]}))
207
+ ```
208
+
209
+ To find out how far ahead to send, ask `ovos.midi.status` for `latency` and
210
+ send at least that far ahead. A skill that sends a bar or two ahead and uses
211
+ `ovos.midi.cancel` when the music changes is always on time and still
212
+ answers quickly.
213
+
214
+ ## How it works
215
+
216
+ - `synth.py` is the built-in synth: 16 channels, program, volume,
217
+ expression and sustain pedal. A note's samples are made once
218
+ (`instruments.py`, the same instruments as
219
+ [ovos-skill-karaoke](https://github.com/emkay-voice/ovos-skill-karaoke)'s
220
+ band) and kept, so playing is cheap.
221
+ - `engine.py` keeps the timeline of events and has three engines: the
222
+ built-in synth, which places each event on its exact sample; FluidSynth;
223
+ and a MIDI port.
224
+ - `audio.py` holds the sound outputs.
225
+ - `player.py` plays files: it hands their events to the engine a couple of
226
+ seconds ahead, so stop is immediate.
227
+ - `ports.py` handles keyboards and instruments, and looks for newly plugged
228
+ ones every few seconds.
229
+
230
+ ## Development
231
+
232
+ ```bash
233
+ pip install -e ".[test]"
234
+ pytest -q tests
235
+ ```
@@ -0,0 +1,198 @@
1
+ # ovos-phal-plugin-midi
2
+
3
+ An [OpenVoiceOS](https://openvoiceos.org) PHAL plugin that gives the device a
4
+ MIDI instrument on its message bus.
5
+
6
+ It is part of [Emkay](https://github.com/emkay-voice/emkay), and works on any
7
+ OpenVoiceOS.
8
+
9
+ Skills can play music on it note by note, timed to the millisecond and
10
+ changed while it plays, or give it a MIDI file. It plays them on:
11
+
12
+ - **a real instrument plugged into the device**: a USB keyboard, a synth, a
13
+ drum module, or anything else that speaks MIDI;
14
+ - **FluidSynth** with a SoundFont (real instrument samples), when it is
15
+ installed;
16
+ - otherwise **its own synthesizer**: every General MIDI instrument and a drum
17
+ kit, made in numpy, with a little room reverb. It needs nothing installed
18
+ and nothing downloaded.
19
+
20
+ A keyboard plugged into the device works the other way too. What you play
21
+ goes on the bus, so a skill can follow it, and with `thru` the device plays
22
+ it.
23
+
24
+ ## Why a plugin, and why software?
25
+
26
+ Sound cards used to have a synthesizer chip, but that has been gone from
27
+ ordinary computers for twenty years. A Raspberry Pi or a Mark II never had
28
+ one. Today a computer plays MIDI with software: Windows and macOS have
29
+ one built in, Linux uses FluidSynth or TiMidity. MIDI itself is alive and
30
+ well as the language of keyboards and synths, over USB.
31
+
32
+ So this plugin is the device's synth and MIDI port in one. It runs once, in
33
+ OVOS' PHAL service, and every skill can use it over the bus. That is what
34
+ makes music that **changes while it plays** possible. A skill sends the next
35
+ bar a moment ahead. When it changes its mind ("add strings"), it takes back
36
+ what it sent (`ovos.midi.cancel`) and sends the bar again. The sound never
37
+ stops or restarts, which a skill handing finished audio files to the player
38
+ cannot do.
39
+
40
+ ## Install
41
+
42
+ ```bash
43
+ pip install --pre ovos-phal-plugin-midi
44
+ ```
45
+
46
+ That is all the built-in synth needs. Restart OVOS' PHAL service
47
+ (`systemctl --user restart ovos-phal` on ovos-installer and raspOVOS).
48
+
49
+ Optional, for more:
50
+
51
+ | | |
52
+ |---|---|
53
+ | `pip install --pre "ovos-phal-plugin-midi[ports]"` | python-rtmidi: MIDI keyboards and instruments plugged into the device |
54
+ | `pip install --pre "ovos-phal-plugin-midi[sounddevice]"` | PortAudio: the lowest latency from the built-in synth |
55
+ | `pip install --pre "ovos-phal-plugin-midi[fluidsynth]"` + `sudo apt install fluidsynth fluid-soundfont-gm` | FluidSynth with the FluidR3 SoundFont: real instrument samples |
56
+
57
+ ## Configure
58
+
59
+ Nothing has to be set. To change something, in `mycroft.conf`:
60
+
61
+ ```json
62
+ {
63
+ "PHAL": {
64
+ "ovos-phal-plugin-midi": {
65
+ "synth": "auto",
66
+ "audio": "auto",
67
+ "output_port": "",
68
+ "inputs": ["*"],
69
+ "thru": false
70
+ }
71
+ }
72
+ }
73
+ ```
74
+
75
+ | Setting | |
76
+ |---|---|
77
+ | `synth` | `auto` (the default) uses a MIDI port if `output_port` names one that is plugged in, else FluidSynth if it and a SoundFont are there, else the built-in synth. You can also choose `port`, `fluidsynth` or `builtin`. |
78
+ | `audio` | where the built-in synth's sound goes. `auto` (the default) tries `sounddevice`, then `paplay` (PulseAudio and PipeWire), `pw-cat`, `aplay` (plain ALSA), in that order. In a browser it uses `bus`. |
79
+ | `audio_device` | a PortAudio device for `sounddevice` (name or number) |
80
+ | `soundfont` | a `.sf2` file for FluidSynth (default: the first of the usual places, e.g. `/usr/share/sounds/sf2/FluidR3_GM.sf2`) |
81
+ | `output_port` | a MIDI output port: its name or part of it, e.g. `"Roland"` |
82
+ | `inputs` | the input ports to listen to, as names or globs (default `["*"]`: all; `[]`: none) |
83
+ | `thru` | play what comes in from a keyboard on the device's own synth (default `false`) |
84
+ | `gain`, `reverb` | the built-in synth's level (0.8) and room (`true`) |
85
+ | `sample_rate` | the built-in synth's sample rate (24000) |
86
+
87
+ ## Permissions
88
+
89
+ The plugin runs inside OVOS' PHAL service, as the same user. What that user
90
+ needs depends on what you use:
91
+
92
+ - **Sound (the built-in synth or FluidSynth)**: the same access as OVOS' own
93
+ speech.
94
+ - On ovos-installer, raspOVOS and a Mark II, PHAL runs as the desktop or
95
+ `ovos` user in its session, and PipeWire or PulseAudio is there.
96
+ - If PHAL runs as a system service, it needs `XDG_RUNTIME_DIR` (or
97
+ `PULSE_SERVER`) pointing at the user's sound server, or the user in the
98
+ `audio` group for plain ALSA.
99
+ - `aplay -l` or `paplay --version` as that user shows whether it can.
100
+ - **MIDI keyboards and instruments**: ALSA's sequencer (`/dev/snd/seq`) and
101
+ the USB MIDI devices (`/dev/snd/midiC*`). These are open to the `audio`
102
+ group (`sudo usermod -aG audio ovos`, then log in again) and to the user at
103
+ the desk (systemd-logind). `aconnect -l` as that user lists what it can
104
+ see.
105
+ - **Docker** (ovos-docker): the PHAL container needs the sound server's
106
+ socket, as the audio container has it, and for MIDI devices
107
+ `--device /dev/snd` and the `audio` group (`group_add: [audio]`).
108
+ - **Emkay** (a browser): no device access at all. The sound comes over the
109
+ bus to Emkay's page, which plays it. Emkay sees no keyboards yet.
110
+
111
+ The plugin opens no network port and sends nothing off the device.
112
+
113
+ Anyone who can put messages on the bus can make the device play music, as
114
+ they can make it speak. What you play on a keyboard goes on the bus too
115
+ (`ovos.midi.input`), but nowhere else. Set `"inputs": []` to keep the
116
+ keyboards to yourself.
117
+
118
+ ## Rights
119
+
120
+ The plugin is Apache-2.0. Its built-in instruments are made by its own code,
121
+ not recorded, so there is nothing else to license. A SoundFont you install
122
+ for FluidSynth has its own license. Debian's and Fedora's `fluid-soundfont-gm`
123
+ (FluidR3_GM) is MIT, and `timgm6mb-soundfont` is GPL-2. Both are fine to
124
+ use. The music is whoever's music it is: the plugin plays the files it is
125
+ given and keeps nothing.
126
+
127
+ ## The bus
128
+
129
+ All times are wall-clock seconds (`time.time()`) on the device. All
130
+ channels are 0-15, with channel 9 as the drum kit, as in General MIDI.
131
+
132
+ | Message | Data | |
133
+ |---|---|---|
134
+ | `ovos.midi.events` | `source`, `at` (default: now + latency), `events` | play events (below) at `at` + their own time |
135
+ | `ovos.midi.cancel` | `source`, `after` (default: now) | take back that source's events from then on; answers `cancelled` (how many) |
136
+ | `ovos.midi.play` | `uri` / `path` / `data` (base64), `source`, `loop`, `transpose`, `tempo`, `mute` (channels) | play a `.mid` or `.kar` file. Answers `ovos.midi.play.started` (`at`, `duration`) or `.failed` (`error`); `ovos.midi.play.ended` when it ends |
137
+ | `ovos.midi.stop` | `source` | stop that source's file and notes; with no `source`, everything |
138
+ | `ovos.midi.panic` | | stop everything, all notes off |
139
+ | `ovos.midi.status` | | answers with the engine, `latency`, `sample_rate`, `time` (the device's clock), what is playing, the ports |
140
+ | `ovos.midi.ports` | | answers `inputs`, `outputs` |
141
+ | `ovos.midi.input` | `port`, `type`, `channel`, `note`, `velocity`, `control`, `value`, `program` | sent by the plugin: what a keyboard played |
142
+ | `ovos.midi.pcm` | `seq`, `sample_rate`, `at`, `pcm` (base64 s16le mono) | sent by the plugin with `"audio": "bus"`: the sound, for a client that plays it |
143
+
144
+ `mycroft.stop` also stops what plays from files. A skill's own notes are its
145
+ skill's to stop, with `ovos.midi.stop`.
146
+
147
+ Events, each with `t` as seconds after `at`:
148
+
149
+ ```
150
+ ["note", t, channel, note, velocity, seconds] a note with its length
151
+ ["on", t, channel, note, velocity] note on (velocity 0: off)
152
+ ["off", t, channel, note]
153
+ ["program", t, channel, program] a General MIDI instrument, 0-127
154
+ ["cc", t, channel, controller, value] 7 volume, 11 expression, 64 sustain pedal, 123 all notes off
155
+ ["all_off", t, channel] channel -1: every channel
156
+ ```
157
+
158
+ A bar of blues in D, starting in half a second:
159
+
160
+ ```python
161
+ import time
162
+ bus.emit(Message("ovos.midi.events", {"source": "my-skill", "at": time.time() + 0.5, "events": [
163
+ ["program", 0, 0, 0], ["program", 0, 1, 32], # piano, acoustic bass
164
+ ["note", 0.0, 0, 62, 80, 1.8], ["note", 0.0, 0, 66, 80, 1.8], ["note", 0.0, 0, 72, 80, 1.8],
165
+ ["note", 0.0, 1, 38, 100, 0.45], ["note", 0.5, 1, 42, 90, 0.45],
166
+ ["note", 1.0, 1, 45, 90, 0.45], ["note", 1.5, 1, 47, 90, 0.45],
167
+ ["note", 0.0, 9, 36, 110, 0.1], ["note", 0.5, 9, 38, 100, 0.1],
168
+ ["note", 1.0, 9, 36, 110, 0.1], ["note", 1.5, 9, 38, 100, 0.1],
169
+ ]}))
170
+ ```
171
+
172
+ To find out how far ahead to send, ask `ovos.midi.status` for `latency` and
173
+ send at least that far ahead. A skill that sends a bar or two ahead and uses
174
+ `ovos.midi.cancel` when the music changes is always on time and still
175
+ answers quickly.
176
+
177
+ ## How it works
178
+
179
+ - `synth.py` is the built-in synth: 16 channels, program, volume,
180
+ expression and sustain pedal. A note's samples are made once
181
+ (`instruments.py`, the same instruments as
182
+ [ovos-skill-karaoke](https://github.com/emkay-voice/ovos-skill-karaoke)'s
183
+ band) and kept, so playing is cheap.
184
+ - `engine.py` keeps the timeline of events and has three engines: the
185
+ built-in synth, which places each event on its exact sample; FluidSynth;
186
+ and a MIDI port.
187
+ - `audio.py` holds the sound outputs.
188
+ - `player.py` plays files: it hands their events to the engine a couple of
189
+ seconds ahead, so stop is immediate.
190
+ - `ports.py` handles keyboards and instruments, and looks for newly plugged
191
+ ones every few seconds.
192
+
193
+ ## Development
194
+
195
+ ```bash
196
+ pip install -e ".[test]"
197
+ pytest -q tests
198
+ ```