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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! Host specific structures.

use std::path::Path;
use std::sync::{Arc, Mutex};
use std::error::Error;
use std::{fmt, mem, ptr, slice};
use std::ffi::CString;

use libloading::Library;
use libc::c_void;

use interfaces;
use plugin::{self, Plugin, Info, Category};
use api::{self, AEffect, PluginMain, Supported};
use api::consts::*;
use buffer::AudioBuffer;
use event::Event;
use channels::ChannelInfo;

#[repr(usize)]
#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub enum OpCode {
    /// [index]: parameter index
    /// [opt]: parameter value
    Automate = 0,
    /// [return]: host vst version (e.g. 2400 for VST 2.4)
    Version,
    /// [return]: current plugin ID (useful for shell plugins to figure out which plugin to load in
    ///           `VSTPluginMain()`).
    CurrentId,
    /// No arguments. Give idle time to Host application, e.g. if plug-in editor is doing mouse
    /// tracking in a modal loop.
    Idle,
    /// Deprecated.
    _PinConnected = 4,

    /// Deprecated.
    _WantMidi = 6, // Not a typo
    /// [value]: request mask. see `VstTimeInfoFlags`
    /// [return]: `VstTimeInfo` pointer or null if not supported.
    GetTime,
    /// Inform host that the plugin has MIDI events ready to be processed. Should be called at the
    /// end of `Plugin::process`.
    /// [ptr]: `VstEvents*` the events to be processed.
    /// [return]: 1 if supported and processed OK.
    ProcessEvents,
    /// Deprecated.
    _SetTime,
    /// Deprecated.
    _TempoAt,
    /// Deprecated.
    _GetNumAutomatableParameters,
    /// Deprecated.
    _GetParameterQuantization,

    /// Notifies the host that the input/output setup has changed. This can allow the host to check
    /// numInputs/numOutputs or call `getSpeakerArrangement()`.
    /// [return]: 1 if supported.
    IOChanged,

    /// Deprecated.
    _NeedIdle,

    /// Request the host to resize the plugin window.
    /// [index]: new width.
    /// [value]: new height.
    SizeWindow,
    /// [return]: the current sample rate.
    GetSampleRate,
    /// [return]: the current block size.
    GetBlockSize,
    /// [return]: the input latency in samples.
    GetInputLatency,
    /// [return]: the output latency in samples.
    GetOutputLatency,

    /// Deprecated.
    _GetPreviousPlug,
    /// Deprecated.
    _GetNextPlug,
    /// Deprecated.
    _WillReplaceOrAccumulate,

    /// [return]: the current process level, see `VstProcessLevels`
    GetCurrentProcessLevel,
    /// [return]: the current automation state, see `VstAutomationStates`
    GetAutomationState,

    /// The plugin is ready to begin offline processing.
    /// [index]: number of new audio files.
    /// [value]: number of audio files.
    /// [ptr]: `AudioFile*` the host audio files. Flags can be updated from plugin.
    OfflineStart,
    /// Called by the plugin to read data.
    /// [index]: (bool)
    ///    VST offline processing allows a plugin to overwrite existing files. If this value is
    ///    true then the host will read the original file's samples, but if it is false it will
    ///    read the samples which the plugin has written via `OfflineWrite`
    /// [value]: see `OfflineOption`
    /// [ptr]: `OfflineTask*` describing the task.
    /// [return]: 1 on success
    OfflineRead,
    /// Called by the plugin to write data.
    /// [value]: see `OfflineOption`
    /// [ptr]: `OfflineTask*` describing the task.
    OfflineWrite,
    /// Unknown. Used in offline processing.
    OfflineGetCurrentPass,
    /// Unknown. Used in offline processing.
    OfflineGetCurrentMetaPass,

    /// Deprecated.
    _SetOutputSampleRate,
    /// Deprecated.
    _GetOutputSpeakerArrangement,

    /// Get the vendor string.
    /// [ptr]: `char*` for vendor string, limited to `MAX_VENDOR_STR_LEN`.
    GetVendorString,
    /// Get the product string.
    /// [ptr]: `char*` for vendor string, limited to `MAX_PRODUCT_STR_LEN`.
    GetProductString,
    /// [return]: vendor-specific version
    GetVendorVersion,
    /// Vendor specific handling.
    VendorSpecific,

    /// Deprecated.
    _SetIcon,

    /// Check if the host supports a feature.
    /// [ptr]: `char*` can do string
    /// [return]: 1 if supported
    CanDo,
    /// Get the language of the host.
    /// [return]: `VstHostLanguage`
    GetLanguage,

    /// Deprecated.
    _OpenWindow,
    /// Deprecated.
    _CloseWindow,

    /// Get the current directory.
    /// [return]: `FSSpec` on OS X, `char*` otherwise
    GetDirectory,
    /// No arguments. TODO: Figure out what this does.
    UpdateDisplay,
    /// Tell the host that if needed, it should record automation data for a control.
    ///
    /// Typically called when the plugin editor begins changing a control.
    ///
    /// [index]: index of the control.
    /// [return]: true on success.
    BeginEdit,
    /// A control is no longer being changed.
    ///
    /// Typically called after the plugin editor is done.
    ///
    /// [index]: index of the control.
    /// [return]: true on success.
    EndEdit,
    /// Open the host file selector.
    /// [ptr]: `VstFileSelect*`
    /// [return]: true on success.
    OpenFileSelector,
    /// Close the host file selector.
    /// [ptr]: `VstFileSelect*`
    /// [return]: true on success.
    CloseFileSelector,

    /// Deprecated.
    _EditFile,
    /// Deprecated.
    /// [ptr]: char[2048] or sizeof (FSSpec).
    /// [return]: 1 if supported.
    _GetChunkFile,
    /// Deprecated.
    _GetInputSpeakerArrangement
}
impl_clike!(OpCode);

/// Implemented by all VST hosts.
#[allow(unused_variables)]
pub trait Host {
    /// Automate a parameter; the value has been changed.
    fn automate(&mut self, index: i32, value: f32) {}

    /// Get the plugin ID of the currently loading plugin.
    ///
    /// This is only useful for shell plugins where this value will change the plugin returned.
    /// `TODO: implement shell plugins`
    fn get_plugin_id(&self) -> i32 {
        // TODO: Handle this properly
        0
    }

    /// An idle call.
    ///
    /// This is useful when the plugin is doing something such as mouse tracking in the UI.
    fn idle(&self) {}

    /// Get vendor and product information.
    ///
    /// Returns a tuple in the form of `(version, vendor_name, product_name)`.
    fn get_info(&self) -> (isize, String, String) {
        (1, "vendor string".to_owned(), "product string".to_owned())
    }
}

/// All possible errors that can occur when loading a VST plugin.
#[derive(Debug)]
pub enum PluginLoadError {
    /// Could not load given path.
    InvalidPath,

    /// Given path is not a VST plugin.
    NotAPlugin,

    /// Failed to create an instance of this plugin.
    ///
    /// This can happen for many reasons, such as if the plugin requires a different version of
    /// the VST API to be used, or due to improper licensing.
    InstanceFailed,

    /// The API version which the plugin used is not supported by this library.
    InvalidApiVersion,
}

impl fmt::Display for PluginLoadError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.description())
    }
}

impl Error for PluginLoadError {
    fn description(&self) -> &str {
        use self::PluginLoadError::*;

        match *self {
            InvalidPath => "Could not open the requested path",
            NotAPlugin => "The given path does not contain a VST2.4 compatible library",
            InstanceFailed => "Failed to create a plugin instance",
            InvalidApiVersion => "The plugin API version is not compatible with this library"
        }
    }
}

/// Wrapper for an externally loaded VST plugin.
///
/// The only functionality this struct provides is loading plugins, which can be done via the
/// [`load`](#method.load) method.
pub struct PluginLoader<T: Host> {
    main: PluginMain,
    lib: Arc<Library>,
    host: Arc<Mutex<T>>,
}

/// An instance of an externally loaded VST plugin.
#[allow(dead_code)] // To keep `lib` around.
pub struct PluginInstance {
    effect: *mut AEffect,
    lib: Arc<Library>,
    info: Info,
}

impl Drop for PluginInstance {
    fn drop(&mut self) {
        self.dispatch(plugin::OpCode::Shutdown, 0, 0, ptr::null_mut(), 0.0);
    }
}


impl<T: Host> PluginLoader<T> {
    /// Load a plugin at the given path with the given host.
    ///
    /// Because of the possibility of multi-threading problems that can occur when using plugins,
    /// the host must be passed in via an `Arc<Mutex<T>>` object. This makes sure that even if the
    /// plugins are multi-threaded no data race issues can occur.
    ///
    /// Upon success, this method returns a [`PluginLoader`](.) object which you can use to call
    /// [`instance`](#method.instance) to create a new instance of the plugin.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use std::path::Path;
    /// # use std::sync::{Arc, Mutex};
    /// # use vst2::host::{Host, PluginLoader};
    /// # let path = Path::new(".");
    /// # struct MyHost;
    /// # impl MyHost { fn new() -> MyHost { MyHost } }
    /// # impl Host for MyHost {
    /// #     fn automate(&mut self, _: i32, _: f32) {}
    /// #     fn get_plugin_id(&self) -> i32 { 0 }
    /// # }
    /// // ...
    /// let host = Arc::new(Mutex::new(MyHost::new()));
    ///
    /// let mut plugin = PluginLoader::load(path, host.clone()).unwrap();
    ///
    /// let instance = plugin.instance().unwrap();
    /// // ...
    /// ```
    ///
    /// # Linux/Windows
    ///   * This should be a path to the library, typically ending in `.so`/`.dll`.
    ///   * Possible full path: `/home/overdrivenpotato/.vst/u-he/Zebra2.64.so`
    ///   * Possible full path: `C:\Program Files (x86)\VSTPlugins\iZotope Ozone 5.dll`
    ///
    /// # OS X
    ///   * This should point to the mach-o file within the `.vst` bundle.
    ///   * Plugin: `/Library/Audio/Plug-Ins/VST/iZotope Ozone 5.vst`
    ///   * Possible full path:
    ///     `/Library/Audio/Plug-Ins/VST/iZotope Ozone 5.vst/Contents/MacOS/PluginHooksVST`
    pub fn load(path: &Path, host: Arc<Mutex<T>>) -> Result<PluginLoader<T>, PluginLoadError> {
        // Try loading the library at the given path
        let lib = match Library::new(path) {
            Ok(l) => l,
            Err(_) => return Err(PluginLoadError::InvalidPath)
        };

        Ok(PluginLoader {
            main: unsafe {
                      // Search the library for the VSTAPI entry point
                      match lib.get(b"VSTPluginMain") {
                          Ok(s) => *s,
                          _ => return Err(PluginLoadError::NotAPlugin)
                      }
                  },
            lib: Arc::new(lib),
            host: host,
        })
    }

    /// Call the VST entry point and retrieve a (possibly null) pointer.
    unsafe fn call_main(&mut self) -> *mut AEffect {
        load_pointer = Box::into_raw(Box::new(self.host.clone())) as *mut c_void;
        (self.main)(callback_wrapper::<T>)
    }

    /// Try to create an instance of this VST plugin.
    ///
    /// If the instance is successfully created, a [`PluginInstance`](struct.PluginInstance.html)
    /// is returned. This struct implements the [`Plugin` trait](../plugin/trait.Plugin.html).
    pub fn instance(&mut self) -> Result<PluginInstance, PluginLoadError> {
        // Call the plugin main function. This also passes the plugin main function as the closure
        // could not return an error if the symbol wasn't found
        let effect = unsafe { self.call_main() };

        if effect.is_null() {
            return Err(PluginLoadError::InstanceFailed);
        }

        unsafe {
            // Move the host to the heap and add it to the `AEffect` struct for future reference
            (*effect).reserved1 = Box::into_raw(Box::new(self.host.clone())) as isize;
        }

        let instance = PluginInstance::new(
            effect,
            self.lib.clone()
        );

        let api_ver = instance.dispatch(plugin::OpCode::GetApiVersion, 0, 0, ptr::null_mut(), 0.0);
        if api_ver >= 2400 {
            Ok(instance)
        } else {
            trace!("Could not load plugin with api version {}", api_ver);
            Err(PluginLoadError::InvalidApiVersion)
        }
    }
}

impl PluginInstance {
    fn new(effect: *mut AEffect, lib: Arc<Library>) -> PluginInstance {
        use plugin::OpCode as op;

        let mut plug = PluginInstance {
            effect: effect,
            lib: lib,
            info: Default::default()
        };

        unsafe {
            use api::flags::*;

            let effect: &AEffect = &*effect;
            let flags = Plugin::from_bits_truncate(effect.flags);

            plug.info = Info {
                name: plug.read_string(op::GetProductName, MAX_PRODUCT_STR_LEN),
                vendor: plug.read_string(op::GetVendorName, MAX_VENDOR_STR_LEN),

                presets: effect.numPrograms,
                parameters: effect.numParams,
                inputs: effect.numInputs,
                outputs: effect.numOutputs,

                unique_id: effect.uniqueId,
                version: effect.version,

                category: Category::from(plug.opcode(op::GetCategory)),

                initial_delay: effect.initialDelay,

                preset_chunks: flags.intersects(PROGRAM_CHUNKS),
                f64_precision: flags.intersects(CAN_DOUBLE_REPLACING),
                silent_when_stopped: flags.intersects(NO_SOUND_IN_STOP),
            };
        }

        plug
    }

    /// Send a dispatch message to the plugin.
    fn dispatch(&self,
                opcode: plugin::OpCode,
                index: i32,
                value: isize,
                ptr: *mut c_void,
                opt: f32)
                -> isize {
        let dispatcher = unsafe {
            (*self.effect).dispatcher
        };
        if (dispatcher as *mut u8).is_null() {
            panic!("Plugin was not loaded correctly.");
        }
        dispatcher(self.effect, opcode.into(), index, value, ptr, opt)
    }

    /// Send a lone opcode with no parameters.
    fn opcode(&self, opcode: plugin::OpCode) -> isize {
        self.dispatch(opcode, 0, 0, ptr::null_mut(), 0.0)
    }

    /// Like `dispatch`, except takes a `&str` to send via `ptr`.
    fn write_string(&self,
                    opcode: plugin::OpCode,
                    index: i32,
                    value: isize,
                    string: &str,
                    opt: f32)
                    -> isize {
        let string = CString::new(string).expect("Invalid string data");
        self.dispatch(opcode, index, value, string.as_bytes().as_ptr() as *mut c_void, opt)
    }

    fn read_string(&self, opcode: plugin::OpCode, max: usize) -> String {
        self.read_string_param(opcode, 0, 0, 0.0, max)
    }

    fn read_string_param(&self,
                         opcode: plugin::OpCode,
                         index: i32,
                         value: isize,
                         opt: f32,
                         max: usize)
                         -> String {
        let mut buf = vec![0; max];
        self.dispatch(opcode, index, value, buf.as_mut_ptr() as *mut c_void, opt);
        String::from_utf8_lossy(&buf).chars().take_while(|c| *c != '\0').collect()
    }
}

impl Plugin for PluginInstance {
    fn get_info(&self) -> plugin::Info {
        self.info.clone()
    }

    fn init(&mut self) {
        self.opcode(plugin::OpCode::Initialize);
    }


    fn change_preset(&mut self, preset: i32) {
        self.dispatch(plugin::OpCode::ChangePreset, 0, preset as isize, ptr::null_mut(), 0.0);
    }

    fn get_preset_num(&self) -> i32 {
        self.opcode(plugin::OpCode::GetCurrentPresetNum) as i32
    }

    fn set_preset_name(&mut self, name: String) {
        self.write_string(plugin::OpCode::SetCurrentPresetName, 0, 0, &name, 0.0);
    }

    fn get_preset_name(&self, preset: i32) -> String {
        self.read_string_param(plugin::OpCode::GetPresetName, preset, 0, 0.0, MAX_PRESET_NAME_LEN)
    }


    fn get_parameter_label(&self, index: i32) -> String {
        self.read_string_param(plugin::OpCode::GetParameterLabel, index, 0, 0.0, MAX_PARAM_STR_LEN)
    }

    fn get_parameter_text(&self, index: i32) -> String {
        self.read_string_param(plugin::OpCode::GetParameterDisplay, index, 0, 0.0, MAX_PARAM_STR_LEN)
    }

    fn get_parameter_name(&self, index: i32) -> String {
        self.read_string_param(plugin::OpCode::GetParameterName, index, 0, 0.0, MAX_PARAM_STR_LEN)
    }

    fn get_parameter(&self, index: i32) -> f32 {
        unsafe {
            ((*self.effect).getParameter)(self.effect, index)
        }
    }

    fn set_parameter(&mut self, index: i32, value: f32) {
        unsafe {
            ((*self.effect).setParameter)(self.effect, index, value)
        }
    }

    fn can_be_automated(&self, index: i32) -> bool {
        self.dispatch(plugin::OpCode::CanBeAutomated, index, 0, ptr::null_mut(), 0.0) > 0
    }

    fn string_to_parameter(&mut self, index: i32, text: String) -> bool {
        self.write_string(plugin::OpCode::StringToParameter, index, 0, &text, 0.0) > 0
    }


    fn set_sample_rate(&mut self, rate: f32) {
        self.dispatch(plugin::OpCode::SetSampleRate, 0, 0, ptr::null_mut(), rate);
    }

    fn set_block_size(&mut self, size: i64) {
        self.dispatch(plugin::OpCode::SetBlockSize, 0, size as isize, ptr::null_mut(), 0.0);
    }


    fn resume(&mut self) {
        self.dispatch(plugin::OpCode::StateChanged, 0, 1, ptr::null_mut(), 0.0);
    }

    fn suspend(&mut self) {
        self.dispatch(plugin::OpCode::StateChanged, 0, 0, ptr::null_mut(), 0.0);
    }


    fn vendor_specific(&mut self, index: i32, value: isize, ptr: *mut c_void, opt: f32) -> isize {
        self.dispatch(plugin::OpCode::VendorSpecific, index, value, ptr, opt)
    }


    fn can_do(&self, can_do: plugin::CanDo) -> Supported {
        let s: String = can_do.into();
        Supported::from(
            self.write_string(plugin::OpCode::CanDo, 0, 0, &s, 0.0)
        ).expect("Invalid response received when querying plugin CanDo")
    }

    fn get_tail_size(&self) -> isize {
        self.opcode(plugin::OpCode::GetTailSize)
    }


    fn process(&mut self, buffer: AudioBuffer<f32>) {
        let (inputs, outputs) = buffer.split();
        let frames = inputs[0].len();
        let mut inputs: Vec<*mut f32> = inputs.into_iter().map(|s| s.as_mut_ptr()).collect();
        let mut outputs: Vec<*mut f32> = outputs.into_iter().map(|s| s.as_mut_ptr()).collect();

        unsafe {
            ((*self.effect).processReplacing)
                (self.effect, inputs.as_mut_ptr(), outputs.as_mut_ptr(), frames as i32)
        }
    }

    fn process_f64(&mut self, buffer: AudioBuffer<f64>) {
        let (inputs, outputs) = buffer.split();
        let frames = inputs[0].len();
        let mut inputs: Vec<*mut f64> = inputs.into_iter().map(|s| s.as_mut_ptr()).collect();
        let mut outputs: Vec<*mut f64> = outputs.into_iter().map(|s| s.as_mut_ptr()).collect();

        unsafe {
            ((*self.effect).processReplacingF64)
                (self.effect, inputs.as_mut_ptr(), outputs.as_mut_ptr(), frames as i32)
        }
    }

    fn process_events(&mut self, events: Vec<Event>) {
        use api::flags::REALTIME_EVENT;
        use api;

        let len = events.len();

        // To send this array to the plugin, we need to convert events to the equivalent VST
        // structures. Most of what's happening here is just copying data but the key thing to
        // notice is that each event is boxed and casted to (*mut api::Event). This way we can let
        // the plugin handle the event, and then later create the box again from the raw pointer so
        // that it can be properly dropped.
        let mut events: Vec<*mut api::Event> = events.iter().map(|event| {
            match *event {
                Event::Midi { data, delta_frames, live,
                              note_length, note_offset,
                              detune, note_off_velocity } => {
                    Box::into_raw(Box::new(api::MidiEvent {
                        event_type: api::EventType::Midi,
                        byte_size: mem::size_of::<api::MidiEvent>() as i32,
                        delta_frames: delta_frames,
                        flags: if live { REALTIME_EVENT.bits() } else { 0 },
                        note_length: note_length.unwrap_or(0),
                        note_offset: note_offset.unwrap_or(0),
                        midi_data: data,
                        _midi_reserved: 0,
                        detune: detune,
                        note_off_velocity: note_off_velocity,
                        _reserved1: 0,
                        _reserved2: 0
                    })) as *mut api::Event
                }
                Event::SysEx { payload, delta_frames } => {
                    Box::into_raw(Box::new(api::SysExEvent {
                        event_type: api::EventType::SysEx,
                        byte_size: mem::size_of::<api::SysExEvent>() as i32,
                        delta_frames: delta_frames,
                        _flags: 0,
                        data_size: payload.len() as i32,
                        _reserved1: 0,
                        system_data: payload.as_ptr() as *const u8 as *mut u8,
                        _reserved2: 0,
                    })) as *mut api::Event
                }
                Event::Deprecated(e) => Box::into_raw(Box::new(e))
            }
        }).collect();

        let mut send = api::Events {
            num_events: len as i32,
            _reserved: 0,
            events: events.as_mut_ptr()
        };

        self.dispatch(plugin::OpCode::ProcessEvents, 0, 0, &mut send as *mut api::Events as *mut c_void, 0.0);

        // Clean up the created events
        unsafe {
            for event in events {
                match (*event).event_type {
                    api::EventType::Midi => {
                        drop(Box::from_raw(event as *mut api::MidiEvent));
                    }
                    api::EventType::SysEx => {
                        drop(Box::from_raw(event as *mut api::SysExEvent));
                    }
                    _ => {
                        drop(Box::from_raw(event));
                    }
                }
            }
        }
    }

    // TODO: Editor

    fn get_preset_data(&mut self) -> Vec<u8> {
        // Create a pointer that can be updated from the plugin.
        let mut ptr: *mut u8 = ptr::null_mut();
        let len = self.dispatch(plugin::OpCode::GetData,
                                1 /*preset*/, 0,
                                &mut ptr as *mut *mut u8 as *mut c_void, 0.0);
        let slice = unsafe { slice::from_raw_parts(ptr, len as usize) };
        slice.to_vec()
    }

    fn get_bank_data(&mut self) -> Vec<u8> {
        // Create a pointer that can be updated from the plugin.
        let mut ptr: *mut u8 = ptr::null_mut();
        let len = self.dispatch(plugin::OpCode::GetData,
                                0 /*bank*/, 0,
                                &mut ptr as *mut *mut u8 as *mut c_void, 0.0);
        let slice = unsafe { slice::from_raw_parts(ptr, len as usize) };
        slice.to_vec()
    }

    fn load_preset_data(&mut self, data: &[u8]) {
        self.dispatch(plugin::OpCode::SetData, 1, data.len() as isize,
                      data.as_ptr() as *mut c_void, 0.0);
    }

    fn load_bank_data(&mut self, data: &[u8]) {
        self.dispatch(plugin::OpCode::SetData, 0, data.len() as isize,
                      data.as_ptr() as *mut c_void, 0.0);
    }

    fn get_input_info(&self, input: i32) -> ChannelInfo {
        let mut props = unsafe { mem::uninitialized() };
        let ptr = &mut props as *mut api::ChannelProperties as *mut c_void;

        self.dispatch(plugin::OpCode::GetInputInfo, input, 0, ptr, 0.0);

        ChannelInfo::from(props)
    }

    fn get_output_info(&self, output: i32) -> ChannelInfo {
        let mut props = unsafe { mem::uninitialized() };
        let ptr = &mut props as *mut api::ChannelProperties as *mut c_void;

        self.dispatch(plugin::OpCode::GetOutputInfo, output, 0, ptr, 0.0);

        ChannelInfo::from(props)
    }
}

/// HACK: a pointer to store the host so that it can be accessed from the `callback_wrapper`
/// function passed to the plugin.
///
/// When the plugin is being loaded, a `Box<Arc<Mutex<T>>>` is transmuted to a *mut c_void pointer
/// and placed here. When the plugin calls the callback during initialization, the host refers to
/// this pointer to get a handle to the Host. After initialization, this pointer is invalidated and
/// the host pointer is placed into a [reserved field] in the instance `AEffect` struct.
///
/// The issue with this approach is that if 2 plugins are simultaneously loaded with 2 different
/// host instances, this might fail as one host may receive a pointer to the other one. In practice
/// this is a rare situation as you normally won't have 2 seperate host instances loading at once.
///
/// [reserved field]: ../api/struct.AEffect.html#structfield.reserved1
static mut load_pointer: *mut c_void = 0 as *mut c_void;

/// Function passed to plugin to handle dispatching host opcodes.
fn callback_wrapper<T: Host>(effect: *mut AEffect, opcode: i32, index: i32,
                             value: isize, ptr: *mut c_void, opt: f32) -> isize {
    unsafe {
        // If the effect pointer is not null and the host pointer is not null, the plugin has
        // already been initialized
        if !effect.is_null() && (*effect).reserved1 != 0 {
            let reserved = (*effect).reserved1 as *const Arc<Mutex<T>>;
            let host = &*reserved;

            let host = &mut *host.lock().unwrap();

            interfaces::host_dispatch(host, effect, opcode, index, value, ptr, opt)
        // In this case, the plugin is still undergoing initialization and so `load_pointer` is
        // dereferenced
        } else {
            // Used only during the plugin initialization
            let host = load_pointer as *const Arc<Mutex<T>>;
            let host = &*host;
            let host = &mut *host.lock().unwrap();

            interfaces::host_dispatch(host, effect, opcode, index, value, ptr, opt)
        }
    }
}