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
//! Wrapper for the [JACK] backend (behind the `backend-jack` feature).
//!
//! Support is only enabled if you compile with the "backend-jack" feature, see
//! [the cargo reference] for more information on setting cargo features.
//!
//! For an example, see `jack_synth.rs` in the `examples` folder.
//! `examples/example_synth` contains the code that is shared for all backends and
//! `examples/jack_synth.rs` contains the jack-specific code.
//!
//! # Usage
//! See the documentation of the [`run`] function.
//!
//! [JACK]: http://www.jackaudio.org/
//! [the cargo reference]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
//! [`run`]: ./fn.run.html
use crate::backend::{HostInterface, Stop};
use crate::buffer::AudioBufferInOut;
use crate::event::{
    ContextualEventHandler, EventHandler, Indexed, RawMidiEvent, SysExEvent, Timed,
};
use crate::{
    AudioHandler, CommonAudioPortMeta, CommonMidiPortMeta, CommonPluginMeta,
    ContextualAudioRenderer,
};
use std::io;
use vecstorage::VecStorage;

/// Re-exports of the [`jack`](https://crates.io/crates/jack) crate.
/// Use this so that your code doesn't break when `rsynth` upgrades its dependency on `jack`.
pub mod jack {
    pub use jack::*;
}

use self::jack::{AudioIn, AudioOut, MidiIn, MidiOut, Port, ProcessScope, RawMidi};
use self::jack::{Client, ClientOptions, Control, ProcessHandler};

/// Used to communicate with `Jack`.
///
/// You don't need to instantiate this yourself: it is passed as the `context`
/// parameter to the [`render_audio`] method when using the [`run`] function.
///
/// [`render_audio`]: ../../trait.ContextualAudioRenderer.html#tymethod.render_buffer
/// [`run`]: ./fn.run.html
pub struct JackHost<'c, 'mp, 'mw> {
    client: &'c Client,
    midi_out_ports: &'mp mut [jack::MidiWriter<'mw>],
    control: jack::Control,
}

impl<'c, 'mp, 'mw> JackHost<'c, 'mp, 'mw> {
    /// Get access to the underlying [`Client`] so that you can use Jack-specific features.
    ///
    /// ['Client`]: ./jack/struct.Client.html
    pub fn client(&self) -> &'c Client {
        self.client
    }
}

impl<'c, 'mp, 'mw> HostInterface for JackHost<'c, 'mp, 'mw> {
    fn output_initialized(&self) -> bool {
        false
    }

    fn stop(&mut self) {
        self.control = jack::Control::Quit
    }
}

impl<'c, 'mp, 'mw> Stop for JackHost<'c, 'mp, 'mw> {}

impl<'c, 'mp, 'mw> EventHandler<Indexed<Timed<RawMidiEvent>>> for JackHost<'c, 'mp, 'mw> {
    fn handle_event(&mut self, event: Indexed<Timed<RawMidiEvent>>) {
        let Indexed { index, event } = event;
        if let Some(ref mut midi_out_port) = self.midi_out_ports.get_mut(index).as_mut() {
            let raw_midi = RawMidi {
                time: event.time_in_frames,
                bytes: event.event.bytes(),
            };
            midi_out_port.write(&raw_midi); // TODO: error handling.
        } else {
            error!(
                "midi port out of bounds: port index is {}, but only {} ports are available",
                index,
                self.midi_out_ports.len()
            );
        }
    }
}

impl<'c, 'mp, 'mw, 'e> EventHandler<Indexed<Timed<SysExEvent<'e>>>> for JackHost<'c, 'mp, 'mw> {
    fn handle_event(&mut self, event: Indexed<Timed<SysExEvent>>) {
        let Indexed { index, event } = event;
        if let Some(ref mut midi_out_port) = self.midi_out_ports.get_mut(index).as_mut() {
            let raw_midi = RawMidi {
                time: event.time_in_frames,
                bytes: event.event.data(),
            };
            midi_out_port.write(&raw_midi); // TODO: error handling.
        } else {
            error!(
                "midi port out of bounds: port index is {}, but only {} ports are available",
                index,
                self.midi_out_ports.len()
            );
        }
    }
}

fn audio_in_ports<P>(client: &Client, plugin: &P) -> Vec<Port<AudioIn>>
where
    P: CommonAudioPortMeta,
{
    let mut in_ports = Vec::with_capacity(plugin.max_number_of_audio_inputs());
    for index in 0..plugin.max_number_of_audio_inputs() {
        let mut name = String::new();
        if let Err(e) = plugin.input_name(&mut name, index) {
            error!(
                "Failed to get the name of audio input port with index {}: {}.",
                index, e
            );
            // TODO: Maybe instead of skipping, it is better to provide a "dummy" audio input port?
            continue;
        }
        info!("Registering audio input port with name {}", name);
        let port = client.register_port(&name, AudioIn::default());
        match port {
            Ok(p) => {
                in_ports.push(p);
            }
            Err(e) => {
                // TODO: Maybe instead of skipping, it is better to provide a "dummy" audio input
                // TODO: port that always contains silence?
                error!("Failed to open audio input port with index {} and name {}: {:?}. Skipping this port.", index, name, e);
            }
        }
    }
    in_ports
}

fn audio_out_ports<P>(client: &Client, plugin: &P) -> Vec<Port<AudioOut>>
where
    P: CommonAudioPortMeta,
{
    let mut out_ports = Vec::with_capacity(plugin.max_number_of_audio_outputs());
    for index in 0..plugin.max_number_of_audio_outputs() {
        let mut name = String::new();
        if let Err(e) = plugin.output_name(&mut name, index) {
            error!(
                "Failed to get the name of audio output port with index {}: {}.",
                index, e
            );
            // TODO: Maybe instead of skipping, it is better to provide a "dummy" audio output port?
            continue;
        }
        info!("Registering audio output port with name {}", name);
        let port = client.register_port(&name, AudioOut::default());
        match port {
            Ok(p) => {
                out_ports.push(p);
            }
            Err(e) => {
                // TODO: Maybe instead of skipping, it is better to provide a "dummy" audio output
                // TODO: port that is in fact unused?
                error!("Failed to open audio output port with index {} and name {}: {:?}. Skipping this port.", index, name, e);
            }
        }
    }
    out_ports
}

fn midi_in_ports<P>(client: &Client, plugin: &P) -> Vec<Port<MidiIn>>
where
    P: CommonMidiPortMeta,
{
    let mut in_ports = Vec::with_capacity(plugin.max_number_of_midi_inputs());
    for index in 0..plugin.max_number_of_midi_inputs() {
        let mut name = String::new();
        if let Err(e) = plugin.input_name(&mut name, index) {
            error!(
                "Failed to get the name of midi input port with index {}: {}.",
                index, e
            );
            // TODO: Maybe instead of skipping, it is better to provide a "dummy" midi input port?
            continue;
        }
        info!("Registering midi input port with name {}", name);
        let port = client.register_port(&name, MidiIn::default());
        match port {
            Ok(p) => {
                in_ports.push(p);
            }
            Err(e) => {
                // TODO: Maybe instead of skipping, it is better to provide a "dummy" midi input port?
                error!("Failed to open midi input port with index {} and name {}: {:?}. Skipping this port.", index, name, e);
            }
        }
    }
    in_ports
}

fn midi_out_ports<P>(client: &Client, plugin: &P) -> Vec<Port<MidiOut>>
where
    P: CommonMidiPortMeta,
{
    let mut out_ports = Vec::with_capacity(plugin.max_number_of_midi_outputs());
    for index in 0..plugin.max_number_of_midi_outputs() {
        let mut name = String::new();
        if let Err(e) = plugin.output_name(&mut name, index) {
            error!(
                "Failed to get the name of midi output port with index {}: {}.",
                index, e
            );
            // TODO: Maybe instead of skipping, it is better to provide a "dummy" midi output port?
            continue;
        }
        let port = client.register_port(&name, MidiOut::default());
        match port {
            Ok(p) => {
                out_ports.push(p);
            }
            Err(e) => {
                // TODO: Maybe instead of skipping, it is better to provide a "dummy" midi output port?
                error!("Failed to open midi output port with index {} and name {}: {:?}. Skipping this port.", index, name, e);
            }
        }
    }
    out_ports
}

// `MidiWriter` does not implement `Send`, but we do want `JackProcessHandler` to implement `Send`.
// `JackProcessHandler` contains only `VecStorage` of `MidiWriter`s, not a real `MidiWriter`.
// So we solve this by creating a data type that is guaranteed to have the same alignment and
// size as a `MidiWriter`.
struct MidiWriterWrapper {
    _inner: jack::MidiWriter<'static>,
}

unsafe impl Send for MidiWriterWrapper {}
unsafe impl Sync for MidiWriterWrapper {}

struct JackProcessHandler<P> {
    audio_in_ports: Vec<Port<AudioIn>>,
    audio_out_ports: Vec<Port<AudioOut>>,
    midi_in_ports: Vec<Port<MidiIn>>,
    midi_out_ports: Vec<Port<MidiOut>>,
    plugin: P,
    inputs: VecStorage<&'static [f32]>,
    outputs: VecStorage<&'static [f32]>,
    midi_writer: VecStorage<MidiWriterWrapper>, // We cannot use rsor for this one.
}

impl<P> JackProcessHandler<P>
where
    P: CommonAudioPortMeta + CommonMidiPortMeta + CommonPluginMeta + Send,
    for<'c, 'mp, 'mw> P: ContextualAudioRenderer<f32, JackHost<'c, 'mp, 'mw>>
        + ContextualEventHandler<Indexed<Timed<RawMidiEvent>>, JackHost<'c, 'mp, 'mw>>,
    for<'c, 'mp, 'mw, 'a> P:
        ContextualEventHandler<Indexed<Timed<SysExEvent<'a>>>, JackHost<'c, 'mp, 'mw>>,
{
    fn new(client: &Client, plugin: P) -> Self {
        trace!("JackProcessHandler::new()");
        let audio_in_ports = audio_in_ports::<P>(&client, &plugin);
        let audio_out_ports = audio_out_ports::<P>(&client, &plugin);

        let midi_in_ports = midi_in_ports::<P>(&client, &plugin);
        let midi_out_ports = midi_out_ports::<P>(&client, &plugin);

        let inputs = VecStorage::with_capacity(plugin.max_number_of_audio_inputs());
        let outputs = VecStorage::with_capacity(plugin.max_number_of_audio_outputs());

        let midi_writer = VecStorage::with_capacity(plugin.max_number_of_midi_outputs());

        JackProcessHandler {
            audio_in_ports,
            audio_out_ports,
            midi_in_ports,
            midi_out_ports,
            plugin,
            inputs,
            outputs,
            midi_writer,
        }
    }

    fn handle_events<'c, 'mp, 'mw>(
        midi_in_ports: &[Port<MidiIn>],
        plugin: &mut P,
        process_scope: &ProcessScope,
        jack_host: &mut JackHost<'c, 'mp, 'mw>,
    ) {
        // No tracing here, because this is called in the `process` function,
        // and we do not want to trace that.
        for (index, midi_in_port) in midi_in_ports.iter().enumerate() {
            trace!("handle_events for input port {}", index);
            for input_event in midi_in_port.iter(process_scope) {
                trace!("handle_events found event: {:?}", &input_event.bytes);
                if input_event.bytes.len() <= 3 {
                    if let Some(raw_event) = RawMidiEvent::try_new(&input_event.bytes) {
                        let event = Indexed {
                            index,
                            event: Timed {
                                time_in_frames: input_event.time,
                                event: raw_event,
                            },
                        };
                        plugin.handle_event(event, jack_host);
                    } else {
                        warn!(
                            "Strange event of length {}; ignoring this event.",
                            input_event.bytes.len()
                        );
                    }
                } else {
                    let event = Indexed {
                        index,
                        event: Timed {
                            time_in_frames: input_event.time,
                            event: SysExEvent::new(input_event.bytes),
                        },
                    };
                    plugin.handle_event(event, jack_host);
                }
            }
        }
    }
}

impl<P> ProcessHandler for JackProcessHandler<P>
where
    P: CommonAudioPortMeta + CommonMidiPortMeta + CommonPluginMeta + Send,
    for<'c, 'mp, 'mw> P: ContextualAudioRenderer<f32, JackHost<'c, 'mp, 'mw>>
        + ContextualEventHandler<Indexed<Timed<RawMidiEvent>>, JackHost<'c, 'mp, 'mw>>,
    for<'c, 'mp, 'mw, 'a> P:
        ContextualEventHandler<Indexed<Timed<SysExEvent<'a>>>, JackHost<'c, 'mp, 'mw>>,
{
    fn process(&mut self, client: &Client, process_scope: &ProcessScope) -> Control {
        let mut midi_writer_guard = self.midi_writer.vec_guard();
        for midi_output in self.midi_out_ports.iter_mut() {
            midi_writer_guard.push(midi_output.writer(process_scope));
        }
        let mut jack_host: JackHost = JackHost {
            client,
            midi_out_ports: midi_writer_guard.as_mut_slice(),
            control: jack::Control::Continue,
        };
        Self::handle_events(
            &self.midi_in_ports,
            &mut self.plugin,
            process_scope,
            &mut jack_host,
        );

        let mut inputs = self.inputs.vec_guard();
        for port in self.audio_in_ports.iter().take(inputs.capacity()) {
            inputs.push(port.as_slice(process_scope));
        }

        let mut outputs = self.outputs.vec_guard();
        for port in self.audio_out_ports.iter_mut().take(outputs.capacity()) {
            outputs.push(port.as_mut_slice(process_scope));
        }

        let mut buffer = AudioBufferInOut::new(
            inputs.as_slice(),
            outputs.as_mut_slice(),
            client.buffer_size() as usize,
        );
        self.plugin.render_buffer(&mut buffer, &mut jack_host);
        jack_host.control
    }
}

/// Run the plugin until the user presses a key on the computer keyboard or the plugin
/// requests the `JackHost` to stop.
pub fn run<P>(mut plugin: P) -> Result<P, jack::Error>
where
    P: CommonPluginMeta
        + AudioHandler
        + CommonAudioPortMeta
        + CommonMidiPortMeta
        + Send
        + Sync
        + 'static,
    for<'c, 'mp, 'mw> P: ContextualAudioRenderer<f32, JackHost<'c, 'mp, 'mw>>,
    for<'c, 'mp, 'mw> P:
        ContextualEventHandler<Indexed<Timed<RawMidiEvent>>, JackHost<'c, 'mp, 'mw>>,
    for<'c, 'mp, 'mw, 'a> P:
        ContextualEventHandler<Indexed<Timed<SysExEvent<'a>>>, JackHost<'c, 'mp, 'mw>>,
{
    let (client, _status) = Client::new(plugin.name(), ClientOptions::NO_START_SERVER)?;

    let sample_rate = client.sample_rate();
    plugin.set_sample_rate(sample_rate as f64);

    let jack_process_handler = JackProcessHandler::new(&client, plugin);
    let active_client = client.activate_async((), jack_process_handler)?;

    println!("Press any key to quit");
    let mut user_input = String::new();
    io::stdin().read_line(&mut user_input).ok();

    info!("Deactivating client...");

    let (_, _, plugin) = active_client.deactivate()?;
    return Ok(plugin.plugin);
}