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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! # Rsynth
//! An API abstraction for API's for audio plugins and applications.
//! Use it to write real-time audio effects, software synthesizers, ... and target different platforms
//! (vst, jack, offline audio rendering, ...).
//! It is currently most suitable for real-time or "streaming" audio processing.
//! E.g. you cannot use it to reverse audio in time.
//!
//! ## Back-ends
//! `rsynth` currently supports the following back-ends:
//!
//! * [`jack`] (behind the `backend-jack` feature)
//! * [`vst`] (behind the `backend-vst` feature)
//! * [`combined`] combine different back-ends for audio input, audio output, midi input and
//!     midi output, mostly for offline rendering and testing (behind various features)
//!
//! See the documentation of each back-end for more information.
//!
//! ## Features and how to use them
//!
//! `rsynth` puts common functionality of the different backends behind common traits.
//! Conversely, a plugin can be used for different backends by implementing common traits.
//! A mix-and-match approach is used: if a backend doesn't require a certain functionality,
//! you don't need the corresponding trait.
//!
//! ### Starting the backend/entry point for the host
//!
//! * Jack: [`run()`](./backend/jack_backend/fn.run.html)
//! * Offline : [`run()`](backend/combined/fn.run.html)
//! * VST 2.4:  [`vst_init!`]
//!
//! ### Meta-data
//! There are a number of traits that an application or plugin needs to implement in order to define
//! meta-data. Every plugin or application should implement these, but it can be tedious, so you can
//! implement these traits in a more straightforward way by implementing the [`Meta`] trait.
//! However, you can also implement these trait "by hand".
//!
//! **Meta-data for Jack**
//!
//! Applications need to implement
//! * [`CommonPluginMeta`] (name of the plugin etc)
//! * [`AudioHandlerMeta`] (number of audio ports)
//! * [`CommonAudioPortMeta`] (names of the audio in and out ports)
//! * [`MidiHandlerMeta`] (number of midi ports)
//! * [`CommonMidiPortMeta`] (names of the audio in and out ports)
//!
//! **No meta-data for offline rendering**
//!
//! Applications do not need to implement special traits describing meta-data.
//!
//! **Meta-data for VST 2.4**
//!
//! Plugins need to implement
//! * [`CommonPluginMeta`] (name of the plugin etc)
//! * [`AudioHandlerMeta`] (number of audio ports)
//! * [`CommonAudioPortMeta`] (names of the audio in and out ports)
//! * [`VstPluginMeta`] (vst-specific meta-data)
//!
//! ### Rendering audio
//! All backends require the plugin/application to implement the [`ContextualAudioRenderer`] trait.
//! [`ContextualAudioRenderer`] has two type parameters and the type parameter depends on the
//! backends to use.
//! One type parameter is the data type used to represent a sample.
//! The other type parameter is called the "context" and can be used to access functionality of
//! the backend in the audio rendering itself.
//! Common functionality of the context is defined in the [`HostInterface`] trait.
//! The application or plugin can have either a generic implementation of the [`ContextualAudioRenderer`]
//! or choose to use different, specialized implementations if different behaviour is needed.
//!
//! **Rendering audio with Jack**
//!
//! Applications need to implement
//! * [`AudioHandler`]
//! * [`ContextualAudioRenderer`]`<f32,`[`JackHost`]`>`
//!
//! **Rendering audio offline**
//!
//! Applications need to implement
//! * [`ContextualAudioRenderer`]`<S, `[`MidiWriterWrapper`]`<`[`Timed`]`<`[`RawMidiEvent`]`>>>` Note: the type parameter `S`, which represents the sample data type, is free.
//!
//! **Rendering audio with VST 2.4**
//!
//! Plugins need to implement
//! * [`AudioHandler`]
//! * [`ContextualAudioRenderer`]`<f32,`[`HostCallback`]`>`
//! * [`ContextualAudioRenderer`]`<f64,`[`HostCallback`]`>`
//!
//! _Note_: [`HostCallback`] is re-exported from the vst crate, but implements `rsynth`'s
//! [`HostInterface`], which defines functionality shared by all backends.
//!
//! ### Handling (midi) events
//! A plugin or application can handle events (typically midi events) by implementing the
//! [`ContextualEventHandler`] trait. This trait is generic over the event type. It also has
//! a second type parameter, the context, which typically corresponds to the host, so that
//! plugins or applications can have access to the host while handling events.
//!
//! **Handling events with Jack**
//!
//! Applications need to implement
//! * [`ContextualEventHandler`]`<`[`Indexed`]`<`[`Timed`]`<`[`RawMidiEvent`]`>>, `[`JackHost`]`>`,
//! * [`ContextualEventHandler`]`<`[`Indexed`]`<`[`Timed`]`<`[`SysExEvent`]`>>, `[`JackHost`]`>`
//!
//! **Handling events with the "offline" backend**
//!
//! Applications need to implement
//! * [`EventHandler`]`<`[`Timed`]`<`[`RawMidiEvent`]`>>`
//!
//! _Note_: [`EventHandler`] is similar to [`ContextualEventHandler`], but without the context.
//! We would like to make this more uniform in a future version and also require
//! [`ContextualEventHandler`] here.
//!
//! **Handling events with VST 2.4**
//! Plugins need to implement
//!
//! * [`ContextualEventHandler`]`<`[`Timed`]`<`[`RawMidiEvent`]`>, `[`HostCallback`]`>` and
//! * [`ContextualEventHandler`]`<`[`Timed`]`<`[`SysExEvent`]`>, `[`HostCallback`]`>`.
//!
//! _Note_: VST 2.4 does not support sample-accurate events; a dummy timestamp of `0` is always added.
//!
//! _Note_: [`HostCallback`] is re-exported from the vst crate, but implements `rsynth`'s
//! [`HostInterface`], which defines functionality shared by all backends.
//!
//! ### Generating midi events
//! The "context" parameter passed in the methods from the [`ContextualAudioRenderer`] and
//! [`ContextualEventHandler`] traits allows to access features from the host/backend, such as
//! generating midi events.
//!
//! **Generating midi events with Jack**
//!
//! [`JackHost`] implements the following traits:
//!
//! * [`EventHandler`]`<`[`Indexed`]`<`[`Timed`]`<`[`RawMidiEvent`]`>>>`
//! * [`EventHandler`]`<`[`Indexed`]`<`[`Timed`]`<`[`SysExEvent`]`>>>`
//!
//! **Generating midi events with offline rendering**
//!
//! [`MidiWriterWrapper`] implements
//! * [`EventHandler`]`<`[`Timed`]`<`[`RawMidiEvent`]`>>`
//!
//! **Generating midi events with VST 2.4 is not possible**
//!
//! ### Stopping the backend
//! The "context" parameter passed in the methods from the [`ContextualAudioRenderer`] and
//! [`ContextualEventHandler`] traits allows to access features from the host/backend, such as
//! stopping the backend.
//! All "backends" implement the [`HostInterface`] trait, which defines a [`stop`] method.
//! The [`stop`] method only actually does something if the backend additionally implements
//! the [`Stop`] trait.
//!
//! **Stopping Jack**
//!
//! Stopping Jack is possible: [`JackHost`] implements the [`Stop`] trait.
//!
//! **Stopping offline rendering**
//!
//! Stopping offline rendering is possible: [`MidiWriterWrapper`] implements the [`Stop`] trait.
//! Additionally, offline rendering automatically stops when the [`fill_buffer`] method of the
//! [`AudioReader`] indicates that no frames are to be expected anymore.
//!
//! **Stopping VST 2.4 is not possible**
//!
//! [`jack`]: ./backend/jack_backend/index.html
//! [`vst`]: ./backend/vst_backend/index.html
//! [`combined`]: ./backend/combined/index.html
//! [`EventHandler`]: ./event/trait.EventHandler.html
//! [`RawMidiEvent`]: ./event/struct.RawMidiEvent.html
//! [`SysExEvent`]: ./event/struct.SysExEvent.html
//! [`Timed<T>`]: ./event/struct.Timed.html
//! [`Timed`]: ./event/struct.Timed.html
//! [`Indexed<T>`]: ./event/struct.Indexed.html
//! [`Indexed`]: ./event/struct.Indexed.html
//! [`CommonPluginMeta`]: ./trait.CommonPluginMeta.html
//! [`AudioHandlerMeta`]: ./trait.AudioHandlerMeta.html
//! [`MidiHandlerMeta`]: ./trait.MidiHandlerMeta.html
//! [`CommonAudioPortMeta`]: ./trait.CommonAudioPortMeta.html
//! [`Meta`]: ./meta/trait.Meta.html
//! [`AudioRenderer`]: ./trait.AudioRenderer.html
//! [`ContextualAudioRenderer`]: trait.ContextualAudioRenderer.html
//! [`ContextualEventHandler`]: ./event/trait.ContextualEventHandler.html
//! [`EventHandler`]: ./event/trait.EventHandler.html
//! [`vst_init!`]: ./macro.vst_init.html
//! [`jack_backend::run()`]:  ./backend/jack_backend/fn.run.html
//! [`combined::run()`]: backend/combined/fn.run.html
//! [`HostCallback`]: ./backend/vst_backend/vst/plugin/struct.HostCallback.html
//! [`HostInterface`]: ./backend/trait.HostInterface.html
//! [`JackHost`]: ./backend/jack_backend/struct.JackHost.html
//! [`AudioHandler`]: ./trait.AudioHandler.html
//! [`MidiWriterWrapper`]: ./backend/combined/struct.MidiWriterWrapper.html
//! [`CommonMidiPortMeta`]: ./trait.CommonMidiPortMeta.html
//! [`VstPluginMeta`]: ./backend/vst_backend/trait.VstPluginMeta.html
//! [`MidiWriterWrapper`]: ./backend/combined/struct.MidiWriterWrapper.html
//! [`stop`]: ./backend/trait.HostInterface.html#method.stop
//! [`Stop`]: ./backend/trait.Stop.html
//! [`fill_buffer`]: ./backend/combined/trait.AudioReader.html#tymethod.fill_buffer
//! [`AudioReader`]: ./backend/combined/trait.AudioReader.html

#[macro_use]
extern crate log;

use crate::buffer::AudioBufferInOut;
use crate::meta::{AudioPort, General, Meta, MidiPort, Name, Port};
use std::fmt::{Error, Write};

#[macro_use]
pub mod buffer;
pub mod backend;
pub mod envelope;
pub mod event;
pub mod meta;
pub mod test_utilities;
pub mod utilities;

/// Re-exports from the [`vecstorage`](https://crates.io/crates/vecstorage) crate.
pub mod vecstorage {
    pub use vecstorage::VecStorage;
}

#[cfg(feature = "rsor-0-1")]
/// Re-exports from the [`rsor`](https://crates.io/crates/rsor) crate.
pub mod rsor {
    pub use rsor::Slice;
}

/// Define the maximum number of audio inputs and the maximum number of audio outputs.
///
/// Backends that require the plugin to implement this trait ensure that when calling the
/// [`render_buffer`] method of the [`AudioRenderer`] trait
/// *  the number of inputs channels (`buffer.number_of_input_channels()`) is smaller than or equal to
///    `Self::max_number_of_audio_inputs()` and
/// * the number of outputs (`buffer.number_of_output_channels()`) is smaller than or equal to
///    `Self::max_number_of_audio_outputs()`.
///
/// # Remark
/// This trait can be more conveniently implemented by implementing the [`Meta`] trait.
///
/// [`Meta`]: ./meta/trait.Meta.html
/// [`render_buffer`]: ./trait.AudioRenderer.html#tymethod.render_buffer
/// [`AudioRenderer`]: ./trait.AudioRenderer.html
pub trait AudioHandlerMeta {
    /// The maximum number of audio inputs supported.
    /// This method should return the same value every time it is called.
    fn max_number_of_audio_inputs(&self) -> usize;

    /// The maximum number of audio outputs supported.
    /// This method should return the same value every time it is called.
    fn max_number_of_audio_outputs(&self) -> usize;
}

/// Define how sample-rate changes are handled.
pub trait AudioHandler {
    /// Called when the sample-rate changes.
    /// The backend should ensure that this function is called before
    /// any other method.
    ///
    /// # Parameters
    /// `sample_rate`: The new sample rate in frames per second (Hz).
    /// Common sample rates are 44100 Hz (CD quality) and 48000 Hz.
    // TODO: Looking at the WikiPedia list https://en.wikipedia.org/wiki/Sample_rate, it seems that
    // TODO: there are no fractional sample rates. Maybe change the data type into u32?
    fn set_sample_rate(&mut self, sample_rate: f64);
}

/// Define the maximum number of midi inputs and the maximum number of midi outputs.
/// This trait can be more conveniently implemented by implementing the [`Meta`] trait.
///
/// [`Meta`]: ./meta/trait.Meta.html
pub trait MidiHandlerMeta {
    /// The maximum number of midi inputs supported.
    /// This method should return the same value for subsequent calls.
    fn max_number_of_midi_inputs(&self) -> usize;

    /// The maximum number of midi outputs supported.
    /// This method should return the same value for subsequent calls.
    fn max_number_of_midi_outputs(&self) -> usize;
}

// TODO: Is this trait actually used?
/// Defines how audio is rendered.
///
/// The type parameter `S` refers to the data type of a sample.
/// It is typically `f32` or `f64`.
pub trait AudioRenderer<S>
where
    S: Copy,
{
    /// This method is called repeatedly for subsequent audio buffers.
    fn render_buffer(&mut self, buffer: &mut AudioBufferInOut<S>);
}

/// Defines how audio is rendered, similar to the [`AudioRenderer`] trait.
/// The extra parameter `context` can be used by the backend to provide extra information.
///
/// The type parameter `S` refers to the data type of a sample.
/// It is typically `f32` or `f64`.
///
/// [`AudioRenderer`]: ./trait.AudioRenderer.html
pub trait ContextualAudioRenderer<S, Context>
where
    S: Copy,
{
    /// This method called repeatedly for subsequent buffers.
    ///
    /// It is similar to the [`render_buffer`] from the [`AudioRenderer`] trait,
    /// see its documentation for more information.
    ///
    /// [`AudioRenderer`]: ./trait.AudioRenderer.html
    /// [`render_buffer`]: ./trait.AudioRenderer.html#tymethod.render_buffer
    fn render_buffer(&mut self, buffer: &mut AudioBufferInOut<S>, context: &mut Context);
}

/// Provides common meta-data of the plugin or application to the host.
/// This trait is common for all backends that need this info.
/// This trait can be more conveniently implemented by implementing the [`Meta`] trait.
///
/// [`Meta`]: ./meta/trait.Meta.html
pub trait CommonPluginMeta {
    /// The name of the plugin or application.
    ///
    #[deprecated(since = "0.1.2", note = "Use or implement `plugin_name` instead.")]
    fn name(&self) -> &str {
        "plugin_or_application"
    }

    /// The name of the plugin or application.
    ///
    /// # Compatibility note
    /// The default implementation of this method will likely be removed in a future release.
    fn plugin_name<W: std::fmt::Write>(&self, buffer: &mut W) -> Result<(), std::fmt::Error> {
        buffer.write_str(self.name())
    }
}

/// Provides some meta-data of the audio-ports used by the plugin or application to the host.
/// This trait can be more conveniently implemented by implementing the [`Meta`] trait.
///
/// [`Meta`]: ./meta/trait.Meta.html
pub trait CommonAudioPortMeta: AudioHandlerMeta {
    /// The name of the audio input with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_audio_inputs()`].
    ///
    /// # Note
    /// When using the Jack backend, this function should not return an empty string.
    ///
    /// [`Self::max_number_of_audio_inputs()`]: trait.AudioHandlerMeta.html#tymethod.max_number_of_audio_inputs
    #[deprecated(since = "0.1.2", note = "Use or implement `input_name` instead.")]
    fn audio_input_name(&self, index: usize) -> String {
        let mut result = String::new();
        match self.input_name(&mut result, index) {
            Ok(_) => result,
            Err(_) => format!("audio in {}", index),
        }
    }

    /// The name of the audio input with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_audio_inputs()`].
    ///
    /// # Note
    /// When using the Jack backend, the name should not be an empty string.
    ///
    /// [`Self::max_number_of_audio_inputs()`]: trait.AudioHandlerMeta.html#tymethod.max_number_of_audio_inputs
    fn input_name<W: std::fmt::Write>(
        &self,
        buffer: &mut W,
        index: usize,
    ) -> Result<(), std::fmt::Error> {
        write!(buffer, "audio in {}", index)
    }

    /// The name of the audio output with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_audio_outputs()`].
    ///
    /// # Note
    /// When using the Jack backend, this function should not return an empty string.
    ///
    /// [`Self::max_number_of_audio_outputs()`]: ./trait.AudioHandlerMeta.html#tymethod.max_number_of_audio_outputs
    #[deprecated(since = "0.1.2", note = "Use or implement `output_name` instead.")]
    fn audio_output_name(&self, index: usize) -> String {
        let mut result = String::new();
        match self.output_name(&mut result, index) {
            Ok(_) => result,
            Err(_) => format!("audio out {}", index),
        }
    }

    /// The name of the audio output with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_audio_outputs()`].
    ///
    /// # Note
    /// When using the Jack backend, the name should not be an empty string.
    ///
    /// [`Self::max_number_of_audio_outputs()`]: ./trait.AudioHandlerMeta.html#tymethod.max_number_of_audio_outputs
    fn output_name<W: std::fmt::Write>(
        &self,
        buffer: &mut W,
        index: usize,
    ) -> Result<(), std::fmt::Error> {
        write!(buffer, "audio out {}", index)
    }
}

/// Provides some meta-data of the midi-ports used by the plugin or application to the host.
/// This trait can be more conveniently implemented by implementing the [`Meta`] trait.
///
/// [`Meta`]: ./meta/trait.Meta.html
pub trait CommonMidiPortMeta: MidiHandlerMeta {
    /// The name of the midi input with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_midi_inputs()`].
    ///
    /// # Note
    /// When using the Jack backend, this function should not return an empty string.
    ///
    /// # Note
    /// The default implementation of this method uses `input_name`.
    ///
    /// [`Self::max_number_of_midi_inputs()`]: trait.MidiHandlerMeta.html#tymethod.max_number_of_midi_inputs
    #[deprecated(since = "0.1.2", note = "Use or implement `input_name` instead.")]
    fn midi_input_name(&self, index: usize) -> String {
        let mut result = String::new();
        match self.input_name(&mut result, index) {
            Ok(_) => result,
            Err(_) => format!("midi in {}", index),
        }
    }

    /// The name of the midi input with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_midi_inputs()`].
    ///
    /// # Note
    /// When using the Jack backend, the name should not be an empty string.
    ///
    /// [`Self::max_number_of_midi_inputs()`]: trait.MidiHandlerMeta.html#tymethod.max_number_of_midi_inputs
    fn input_name<W: std::fmt::Write>(
        &self,
        buffer: &mut W,
        index: usize,
    ) -> Result<(), std::fmt::Error> {
        write!(buffer, "midi in {}", index)
    }

    /// The name of the midi output with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_midi_outputs()`]
    ///
    /// # Note
    /// When using the Jack backend, this function should not return an empty string.
    ///
    /// [`Self::max_number_of_midi_outputs()`]: ./trait.MidiHandlerMeta.html#tymethod.max_number_of_midi_outputs
    #[deprecated(since = "0.1.2", note = "Use or implement `output_name` instead.")]
    fn midi_output_name(&self, index: usize) -> String {
        let mut result = String::new();
        match self.output_name(&mut result, index) {
            Ok(_) => result,
            Err(_) => format!("midi out {}", index),
        }
    }

    /// The name of the midi output with the given index.
    /// You can assume that `index` is strictly smaller than [`Self::max_number_of_midi_outputs()`]
    ///
    /// # Note
    /// When using the Jack backend, the name should not be an empty string.
    ///
    /// [`Self::max_number_of_midi_outputs()`]: ./trait.MidiHandlerMeta.html#tymethod.max_number_of_midi_outputs
    fn output_name<W: std::fmt::Write>(
        &self,
        buffer: &mut W,
        index: usize,
    ) -> Result<(), std::fmt::Error> {
        write!(buffer, "midi out {}", index)
    }
}

impl<T> CommonPluginMeta for T
where
    T: Meta,
    T::MetaData: General,
    <<T as Meta>::MetaData as General>::GeneralData: Name,
{
    fn name(&self) -> &str {
        self.meta().general().name()
    }

    fn plugin_name<W: std::fmt::Write>(&self, buffer: &mut W) -> Result<(), Error> {
        self.meta().general().write_name(buffer)
    }
}

impl<T> AudioHandlerMeta for T
where
    T: Meta,
    T::MetaData: Port<AudioPort>,
{
    fn max_number_of_audio_inputs(&self) -> usize {
        self.meta().in_ports().len()
    }

    fn max_number_of_audio_outputs(&self) -> usize {
        self.meta().out_ports().len()
    }
}

impl<T> CommonAudioPortMeta for T
where
    T: Meta,
    T::MetaData: Port<AudioPort>,
    <<T as Meta>::MetaData as Port<AudioPort>>::PortData: Name,
{
    fn input_name<W: std::fmt::Write>(&self, buffer: &mut W, index: usize) -> Result<(), Error> {
        self.meta().in_ports()[index].write_name(buffer)
    }

    fn output_name<W: std::fmt::Write>(&self, buffer: &mut W, index: usize) -> Result<(), Error> {
        self.meta().out_ports()[index].write_name(buffer)
    }
}

impl<T> MidiHandlerMeta for T
where
    T: Meta,
    T::MetaData: Port<MidiPort>,
{
    fn max_number_of_midi_inputs(&self) -> usize {
        self.meta().in_ports().len()
    }

    fn max_number_of_midi_outputs(&self) -> usize {
        self.meta().out_ports().len()
    }
}

impl<T> CommonMidiPortMeta for T
where
    T: Meta,
    T::MetaData: Port<MidiPort>,
    <<T as Meta>::MetaData as Port<MidiPort>>::PortData: Name,
{
    fn input_name<W: std::fmt::Write>(&self, buffer: &mut W, index: usize) -> Result<(), Error> {
        self.meta().in_ports()[index].write_name(buffer)
    }

    fn output_name<W: std::fmt::Write>(&self, buffer: &mut W, index: usize) -> Result<(), Error> {
        self.meta().out_ports()[index].write_name(buffer)
    }
}