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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//! Plugin's host (FL Studio).
pub mod prompt;

use std::collections::HashMap;
use std::ffi::c_void;
use std::os::raw::{c_char, c_int, c_uchar};
use std::slice;
use std::sync::atomic::AtomicPtr;
use std::sync::{Arc, Mutex};

use log::trace;

use crate::plugin::{self, message};
use crate::voice::{self, SendVoiceHandler, Voice};
use crate::{
    intptr_t, AsRawPtr, FlMessage, FromRawPtr, MidiMessage, ProcessModeFlags, TTimeSigInfo,
    TimeSignature, Transport, ValuePtr, WAVETABLE_SIZE,
};

/// Plugin host.
#[derive(Debug)]
pub struct Host {
    voicer: Arc<Mutex<Voicer>>,
    out_voicer: Arc<Mutex<OutVoicer>>,
    pub(crate) host_ptr: AtomicPtr<c_void>,
}

impl Host {
    /// Initializer.
    pub fn new(host_ptr: *mut c_void) -> Self {
        let voicer = Arc::new(Mutex::new(Voicer::new(AtomicPtr::new(host_ptr))));
        let out_voicer = Arc::new(Mutex::new(OutVoicer::new(AtomicPtr::new(host_ptr))));
        Self {
            voicer,
            out_voicer,
            host_ptr: AtomicPtr::new(host_ptr),
        }
    }

    // Get the version of FL Studio. It is stored in one integer. If the version of FL Studio
    // would be 1.2.3 for example, `version` would be 1002003
    // pub fn version(&self) -> i32 {
    // todo!()
    // }

    /// Send message to host.
    ///
    /// See [`plugin::message`](../plugin/message/index.html).
    pub fn on_message<T: message::Message>(&mut self, tag: plugin::Tag, message: T) -> T::Return {
        message.send(tag, self)
    }

    /// Notify the host that a parameter value has changed.
    ///
    /// In order to make your parameters recordable in FL Studio, you have to call this function
    /// whenever a parameter is changed from within your plugin (probably because the user turned a
    /// wheel or something).
    ///
    /// - `tag` - plugin's tag.
    /// - `index` - the parameter index
    /// - `value` - the new parameter value.
    pub fn on_parameter(&mut self, tag: plugin::Tag, index: usize, value: ValuePtr) {
        unsafe {
            host_on_parameter(
                *self.host_ptr.get_mut(),
                tag.0,
                index as c_int,
                value.0 as c_int,
            )
        };
    }

    /// Notify the host that an internal controller has changed.
    ///
    /// Check out [this](https://forum.image-line.com/viewtopic.php?p=26368#p26368) post for more
    /// info about an internal controller implemenation.
    pub fn on_controller(&mut self, tag: plugin::Tag, index: usize, value: u16) {
        unsafe {
            host_on_controller(
                *self.host_ptr.get_mut(),
                tag.0,
                index as intptr_t,
                value.as_raw_ptr(),
            )
        };
    }

    /// Let the host show a hint, as specified by the parameters.
    ///
    /// - `tag` - the plugin's tag.
    /// - `text` - the text to show as a hint.
    ///
    /// There is one extra feature of parameter hints. It is possible to tell FL Studio to show
    /// little icons next to the hint, that have a special meaning. For the moment there are three
    /// of those. Note that these have to be inserted at the BEGINNING of the string.
    ///
    /// - `"^a"` - shows a little icon that informs the user that the parameter that the hint is
    /// about can be linked to a MIDI controller.
    /// - `"^b"` - informs the user that the parameter is recordable.
    /// - `"^c"` - shows a little smiley. No real use, just for fun.
    /// - `"^d"` - shows a mouse with the right button clicked, to denote a control that has a
    /// popup menu.
    /// - `"^e"` - shows an unhappy smiley, to use when something went wrong.
    /// - `"^f"` - shows a left-pointing arrow.
    /// - `"^g"` - shows a double right-pointing arrow, for fast forward.
    /// - `"^h"` - is an exclamation mark, for a warning to the user.
    /// - `"^i"` - is an hourglass.
    /// - `"^j"` - shows a double left-pointing arrow, for fast reverse.
    pub fn on_hint(&mut self, tag: plugin::Tag, text: String) {
        unsafe {
            host_on_hint(
                *self.host_ptr.get_mut(),
                tag.0,
                text.as_raw_ptr() as *mut c_char,
            )
        };
    }

    /// Send a MIDI out message immediately.
    ///
    /// To be able to use this method, you should enable MIDI out for the plugin (see
    /// [`InfoBuilder::midi_out`](../plugin/struct.InfoBuilder.html#method.midi_out)) and send
    /// [`plugin::message::ActivateMidi`](../plugin/message/struct.ActivateMidi.html) to the host.
    pub fn midi_out(&mut self, tag: plugin::Tag, message: MidiMessage) {
        // We could use MidiMessage directly with Box::into_raw, but we can't because of the Rust
        // memory layer. We couldn't free the allocated memory properly, because it's managed by
        // the host. So we just send parameters and instantiate FL's TMIDIOutMsg on the C side.
        //
        // The same is inside midi_out_del.
        unsafe {
            host_midi_out(
                *self.host_ptr.get_mut(),
                tag.0,
                message.status,
                message.data1,
                message.data2,
                message.port,
            )
        };
    }

    /// Send a delayed MIDI out message. This message will actually be sent when the MIDI tick has
    /// reached the current mixer tick.
    ///
    /// To be able to use this method, you should enable MIDI out for the plugin (see
    /// [`InfoBuilder::midi_out`](../plugin/struct.InfoBuilder.html#method.midi_out)).
    pub fn midi_out_del(&mut self, tag: plugin::Tag, message: MidiMessage) {
        unsafe {
            host_midi_out_del(
                *self.host_ptr.get_mut(),
                tag.0,
                message.status,
                message.data1,
                message.data2,
                message.port,
            )
        };
    }

    /// **MAY NOT WORK**
    ///
    /// Ask for a message to be dispatched to itself when the current mixing tick will be played
    /// (to synchronize stuff).
    ///
    /// See [`Plugin::loop_in`](../plugin/trait.Plugin.html#method.loop_in).
    ///
    /// The message is guaranteed to be dispatched, however it could be sent immediately if it
    /// couldn't be buffered (it's only buffered when playing).
    pub fn loop_out(&mut self, tag: plugin::Tag, message: ValuePtr) {
        unsafe { host_loop_out(*self.host_ptr.get_mut(), tag.0, message.0) };
    }

    /// Remove the buffered message scheduled by
    /// [`Host::loop_out`](struct.Host.html#method.loop_out), so that it will never be dispatched.
    pub fn loop_kill(&mut self, tag: plugin::Tag, message: ValuePtr) {
        unsafe { host_loop_kill(*self.host_ptr.get_mut(), tag.0, message.0) };
    }

    /// This is a function for thread synchronization. When this is called, no more voices shall be
    /// created and there will be no more rendering until
    /// [`Host::unlock_mix`](struct.Host.html#method.unlock_mix) has been called.
    pub fn lock_mix(&mut self) {
        unsafe { host_lock_mix(*self.host_ptr.get_mut()) };
    }

    /// Unlocks the mix thread if it was previously locked with
    /// [`Host::lock_mix`](struct.Host.html#method.lock_mix).
    pub fn unlock_mix(&mut self) {
        unsafe { host_unlock_mix(*self.host_ptr.get_mut()) };
    }

    /// **Warning: this function is not very performant, so avoid using it if possible.**
    ///
    /// This is an alternative to [`Host::lock_mix`](struct.Host.html#method.lock_mix).
    /// It won't freeze the audio. This function can only be called from the GUI thread!
    pub fn lock_plugin(&mut self, tag: plugin::Tag) {
        unsafe { host_lock_plugin(*self.host_ptr.get_mut(), tag.0) };
    }

    /// **Warning: this function is not very performant, so avoid using it if possible.**
    ///
    /// Unlocks the mix thread if it was previously locked with
    /// [`Host::lock_plugin`](struct.Host.html#method.lock_plugin).
    pub fn unlock_plugin(&mut self, tag: plugin::Tag) {
        unsafe { host_unlock_plugin(*self.host_ptr.get_mut(), tag.0) };
    }

    /// This is a function for thread synchronization. When this is called, no more voices shall be
    /// created and there will be no more rendering until
    /// [`Host::resume_out`](struct.Host.html#method.resume_out) has been called. Unlike
    /// [`Host::lock_mix`](struct.Host.html#method.lock_mix), this function also stops all sound.
    pub fn suspend_out(&mut self) {
        unsafe { host_suspend_out(*self.host_ptr.get_mut()) };
    }

    /// Unlocks the mixer thread if it was previously locked with
    /// [`Host::suspend_out`](struct.Host.html#method.suspend_out).
    pub fn resume_out(&mut self) {
        unsafe { host_resume_out(*self.host_ptr.get_mut()) };
    }

    /// Get one of the buffers.
    ///
    /// - `kind` the kind of the buffer you want to get 
    ///   (see [`host::Buffer`](../host/enum.Buffer.html)).
    /// - `length` is the buffer length (use the length of the output buffer passed to the render
    ///   function).
    ///
    /// The buffers are valid only during
    /// [`Plugin::render`](../plugin/trait.Plugin.html#method.render) (i.e. it's supposed to be used
    /// inside this method only).
    pub fn buffer(
        &mut self,
        tag: plugin::Tag,
        kind: Buffer,
        length: usize,
    ) -> Option<&mut [[f32; 2]]> {
        match kind {
            Buffer::InputRead(value) => self.input_buf(tag, value, length),
            Buffer::OutputWrite(value) => self.output_buf(tag, value, length),
            Buffer::InsertWrite(value) => self.insert_buf(tag, value, length),
            Buffer::MixWrite(value) => self.mix_buf(value, length),
            Buffer::SendWrite(value) => self.send_buf(value, length),
        }
    }

    fn input_buf(
        &mut self,
        tag: plugin::Tag,
        offset: usize,
        length: usize,
    ) -> Option<&mut [[f32; 2]]> {
        let in_buf =
            unsafe { host_get_input_buf(*self.host_ptr.get_mut(), tag.0, offset as intptr_t) };
        if in_buf.flags == 0 || length == 0 {
            return None;
        }
        Some(unsafe { slice::from_raw_parts_mut(in_buf.buffer as *mut [f32; 2], length) })
    }

    fn output_buf(
        &mut self,
        tag: plugin::Tag,
        offset: usize,
        length: usize,
    ) -> Option<&mut [[f32; 2]]> {
        let out_buf =
            unsafe { host_get_output_buf(*self.host_ptr.get_mut(), tag.0, offset as intptr_t) };
        if out_buf.flags == 0 || length == 0 {
            return None;
        }
        Some(unsafe { slice::from_raw_parts_mut(out_buf.buffer as *mut [f32; 2], length) })
    }

    fn insert_buf(
        &mut self,
        tag: plugin::Tag,
        offset: isize,
        length: usize,
    ) -> Option<&mut [[f32; 2]]> {
        let insert_buf = unsafe { host_get_insert_buf(*self.host_ptr.get_mut(), tag.0, offset) };
        if insert_buf.is_null() || length == 0 {
            return None;
        }
        Some(unsafe { slice::from_raw_parts_mut(insert_buf as *mut [f32; 2], length) })
    }

    fn mix_buf(&mut self, offset: isize, length: usize) -> Option<&mut [[f32; 2]]> {
        let mix_buf = unsafe { host_get_mix_buf(*self.host_ptr.get_mut(), offset) };
        if mix_buf.is_null() || length == 0 {
            return None;
        }
        Some(unsafe { slice::from_raw_parts_mut(mix_buf as *mut [f32; 2], length) })
    }

    fn send_buf(&mut self, index: usize, length: usize) -> Option<&mut [[f32; 2]]> {
        let mix_buf = unsafe { host_get_send_buf(*self.host_ptr.get_mut(), index as intptr_t) };
        if mix_buf.is_null() || length == 0 {
            return None;
        }
        Some(unsafe { slice::from_raw_parts_mut(mix_buf as *mut [f32; 2], length) })
    }

    /// Get [`Voicer`](struct.Voicer.html)
    pub fn voice_handler(&self) -> Arc<Mutex<Voicer>> {
        Arc::clone(&self.voicer)
    }

    /// Get [`OutVoicer`](struct.OutVoicer.html).
    pub fn out_voice_handler(&self) -> Arc<Mutex<OutVoicer>> {
        Arc::clone(&self.out_voicer)
    }
}

#[no_mangle]
extern "C" {
    fn host_on_parameter(host: *mut c_void, tag: intptr_t, index: c_int, value: c_int);
    fn host_on_controller(host: *mut c_void, tag: intptr_t, index: intptr_t, value: intptr_t);
    fn host_on_hint(host: *mut c_void, tag: intptr_t, text: *mut c_char);
    fn host_midi_out(
        host: *mut c_void,
        tag: intptr_t,
        status: c_uchar,
        data1: c_uchar,
        data2: c_uchar,
        port: c_uchar,
    );
    fn host_midi_out_del(
        host: *mut c_void,
        tag: intptr_t,
        status: c_uchar,
        data1: c_uchar,
        data2: c_uchar,
        port: c_uchar,
    );
    fn host_loop_out(host: *mut c_void, tag: intptr_t, message: intptr_t);
    fn host_loop_kill(host: *mut c_void, tag: intptr_t, message: intptr_t);
    fn host_lock_mix(host: *mut c_void);
    fn host_unlock_mix(host: *mut c_void);
    fn host_lock_plugin(host: *mut c_void, tag: intptr_t);
    fn host_unlock_plugin(host: *mut c_void, tag: intptr_t);
    fn host_suspend_out(host: *mut c_void);
    fn host_resume_out(host: *mut c_void);
    fn host_get_input_buf(host: *mut c_void, tag: intptr_t, offset: intptr_t) -> TIOBuffer;
    fn host_get_output_buf(host: *mut c_void, tag: intptr_t, offset: intptr_t) -> TIOBuffer;
    fn host_get_insert_buf(host: *mut c_void, tag: intptr_t, offset: intptr_t) -> *mut c_void;
    fn host_get_mix_buf(host: *mut c_void, offset: intptr_t) -> *mut c_void;
    fn host_get_send_buf(host: *mut c_void, offset: intptr_t) -> *mut c_void;
}

/// Type of the write-only buffer you want to get, using
/// [`Host::buf_write`](../struct.Host.html#method.buf_write).
#[derive(Debug)]
pub enum Buffer {
    /// Multi input buffer for effects.
    ///
    /// The value is the buffer index.
    ///
    /// **Warning: Index starts at 1, to be compatible with
    /// [`Buffer::Insert`](enum.Buffer#variant.Insert) (index 0 would be render's function own
    /// buffer).**
    InputRead(usize),
    /// Multi output buffer for generators and effects.
    ///
    /// The value is the buffer index.
    ///
    /// **Warning: Index starts at 1, to be compatible with
    /// [`WriteBuffer::Insert`](../enum.WriteBuffer#variant.Insert) (index 0 would be render's
    /// function own buffer).**
    OutputWrite(usize),
    /// The insert buffer following the buffer a generator is currently processing in. This type is
    /// reserved for the Fruity wrapper (so it may change in the future).
    ///
    /// The value is offset to the current buffer. 0 means the same buffer as passed to
    /// [`Plugin::render`](../plugin/trait.Plugin.html#method.render), 1 means next insert track.
    InsertWrite(isize),
    /// Mixer track buffer relative to the current generator's track.
    ///
    /// The value is track index offset relative to the current generator's track index (i.e.
    /// value of 0 returns the current output buffer).
    ///
    /// Valid only for generators.
    MixWrite(isize),
    /// The send buffer (see
    /// [`host::Message::SetNumSends`](../enum.Message.html#variant.SetNumSends)).
    ///
    /// The value is the index of the send buffer.
    SendWrite(usize),
}

#[repr(C)]
struct TIOBuffer {
    buffer: *mut c_void,
    flags: u32,
}

/// Use this to manually release, kill and notify voices about events.
#[derive(Debug)]
pub struct Voicer {
    host_ptr: AtomicPtr<c_void>,
}

impl Voicer {
    fn new(host_ptr: AtomicPtr<c_void>) -> Self {
        Self { host_ptr }
    }
}

impl SendVoiceHandler for Voicer {
    /// Tell the host the specified voice should be silent (Note Off).
    fn release(&mut self, tag: voice::Tag) {
        trace!("manully release voice {}", tag);
        unsafe { host_release_voice(*self.host_ptr.get_mut(), tag.0) };
    }

    /// Tell the host that the specified voice can be killed (freed from memory).
    ///
    /// This method forces FL Studio to ask the plugin to destroy its voice.
    fn kill(&mut self, tag: voice::Tag) {
        trace!("manully kill voice {}", tag);
        unsafe { host_kill_voice(*self.host_ptr.get_mut(), tag.0) };
    }

    /// Tell the host that some event has happened concerning the specified voice.
    fn on_event(&mut self, tag: voice::Tag, event: voice::Event) -> Option<ValuePtr> {
        Option::<FlMessage>::from(event).map(|value| {
            ValuePtr(unsafe { host_on_voice_event(*self.host_ptr.get_mut(), tag.0, value) })
        })
    }
}

#[no_mangle]
extern "C" {
    fn host_release_voice(host: *mut c_void, tag: intptr_t);
    fn host_kill_voice(host: *mut c_void, tag: intptr_t);
    fn host_on_voice_event(host: *mut c_void, tag: intptr_t, message: FlMessage) -> intptr_t;
}

/// Use this for operations with output voices (i.e. for VFX inside [patcher](
/// https://www.image-line.com/support/flstudio_online_manual/html/plugins/Patcher.htm)).
#[derive(Debug)]
pub struct OutVoicer {
    voices: HashMap<voice::Tag, OutVoice>,
    host_ptr: AtomicPtr<c_void>,
}

impl OutVoicer {
    fn new(host_ptr: AtomicPtr<c_void>) -> Self {
        Self {
            voices: HashMap::new(),
            host_ptr,
        }
    }
}

impl SendVoiceHandler for OutVoicer {
    /// It returns `None` if the output has no destination.
    fn trigger(
        &mut self,
        params: voice::Params,
        index: usize,
        tag: voice::Tag,
    ) -> Option<&mut dyn Voice> {
        let params_ptr = Box::into_raw(Box::new(params));
        let inner_tag = unsafe {
            host_trig_out_voice(*self.host_ptr.get_mut(), params_ptr, index as i32, tag.0)
        };

        if inner_tag == -1 {
            // if FVH_Null
            unsafe { Box::from_raw(params_ptr) }; // free the memory
            trace!("send trigger voice is null");
            return None;
        }

        let voice = OutVoice::new(tag, AtomicPtr::new(params_ptr), voice::Tag(inner_tag));
        trace!("send trigger output voice {:?}", voice);
        self.voices.insert(tag, voice);
        Some(self.voices.get_mut(&tag).unwrap())
    }

    fn release(&mut self, tag: voice::Tag) {
        if let Some(voice) = self.voices.get_mut(&tag) {
            trace!("send release output voice {:?}", voice);
            unsafe { host_release_out_voice(*self.host_ptr.get_mut(), voice.inner_tag().0) }
        }
    }

    fn kill(&mut self, tag: voice::Tag) {
        if let Some(mut voice) = self.voices.remove(&tag) {
            trace!("send kill output voice {}", tag);
            unsafe {
                host_kill_out_voice(*self.host_ptr.get_mut(), voice.inner_tag().0);
                Box::from_raw(*voice.params_ptr.get_mut());
            };
        }
    }

    fn on_event(&mut self, tag: voice::Tag, event: voice::Event) -> Option<ValuePtr> {
        trace!("send event {:?} for out voice {:?}", event, tag);
        let host_ptr = *self.host_ptr.get_mut();
        self.voices.get_mut(&tag).and_then(|voice| {
            Option::<FlMessage>::from(event).map(|message| {
                ValuePtr(unsafe { host_on_out_voice_event(host_ptr, voice.inner_tag().0, message) })
            })
        })
    }
}

/// Output voice.
#[derive(Debug)]
pub struct OutVoice {
    tag: voice::Tag,
    params_ptr: AtomicPtr<voice::Params>,
    inner_tag: voice::Tag,
}

impl OutVoice {
    fn new(tag: voice::Tag, params_ptr: AtomicPtr<voice::Params>, inner_tag: voice::Tag) -> Self {
        Self {
            tag,
            params_ptr,
            inner_tag,
        }
    }

    /// Get voice parameters.
    pub fn params(&mut self) -> voice::Params {
        let boxed_params = unsafe { Box::from_raw(*self.params_ptr.get_mut()) };
        let params = boxed_params.clone();
        self.params_ptr = AtomicPtr::new(Box::into_raw(boxed_params));
        *params
    }

    /// Get inner tag.
    pub fn inner_tag(&self) -> voice::Tag {
        self.inner_tag
    }
}

impl Voice for OutVoice {
    fn tag(&self) -> voice::Tag {
        self.tag
    }
}

extern "C" {
    fn host_trig_out_voice(
        host: *mut c_void,
        params: *mut voice::Params,
        index: i32,
        tag: intptr_t,
    ) -> intptr_t;
    fn host_release_out_voice(host: *mut c_void, tag: intptr_t);
    fn host_kill_out_voice(host: *mut c_void, tag: intptr_t);
    fn host_on_out_voice_event(host: *mut c_void, tag: intptr_t, message: FlMessage) -> intptr_t;
}

/// Message from the host to the plugin.
#[derive(Debug)]
pub enum Message<'a> {
    /// Contains the handle of the parent window if the editor has to be shown.
    ShowEditor(Option<*mut c_void>),
    /// Change the processing mode flags. This can be ignored.
    ///
    /// The value is [ProcessModeFlags](../struct.ProcessModeFlags.html).
    ProcessMode(ProcessModeFlags),
    /// The continuity of processing is broken. This means that the user has jumped ahead or back
    /// in the playlist, for example. When this happens, the plugin needs to clear all buffers and
    /// start like new.
    ///
    /// **Warning: this can be called from the mixer thread!**
    Flush,
    /// This changes the maximum processing length, expressed in samples.
    ///
    /// The value is the new length.
    SetBlockSize(u32),
    /// This changes the sample rate.
    ///
    /// Value holds the new sample rate.
    SetSampleRate(u32),
    /// This allows the plugin to define how the editor window should be resized.
    ///
    /// The first value will hold a pointer to a rectangle (PRect) for the minimum (Left and Top)
    /// and maximum (Right and Bottom) width and height of the window.
    ///
    /// The second value holds a pointer (PPoint) to a point structure that defines by how much the
    /// window size should change horizontally and vertically when the user drags the border.
    WindowMinMax(*mut c_void, *mut c_void),
    /// (not used yet) The host has noticed that too much processing power is used and asks the
    /// plugin to kill its weakest voice.
    ///
    /// The plugin has to return `true` if it did anything, `false` otherwise.
    KillVoice,
    /// Only full generators have to respond to this message. It's meant to allow the cutoff and
    /// resonance parameters of a voice to be used for other purposes, if the generator doesn't use
    /// them as cutoff and resonance.
    ///
    /// - return `0u8` if the plugin doesn't support the default per-voice level value.
    /// - return `1u8` if the plugin supports the default per-voice level value (filter cutoff (0)
    ///   or filter resonance (1)).
    /// - return `2u8` if the plugin supports the per-voice level value, but for another function
    ///   (then check [`GetName::VoiceLevel`](../host/enum.GetName.html#variant.VoiceLevel) to
    ///   provide your own names).
    UseVoiceLevels(u8),
    /// Called when the user selects a preset.
    ///
    /// The value tells you which one to set.
    SetPreset(u64),
    /// A sample has been loaded into the parent channel. This is given to the plugin as a
    /// wavetable, in the same format as the WaveTables member of TFruityPlugin. Also see
    /// FPF_GetChanCustomShape.
    ///
    /// The value holds the new shape.
    ChanSampleChanged(&'a [f32]),
    /// The host has enabled/disabled the plugin.
    ///
    /// The value will contain the new state (`false` for disabled, `true` for enabled)
    ///
    /// **Warning: this can be called from the mixer thread!**
    SetEnabled(bool),
    /// The host is playing or stopped.
    ///
    /// The value is playing status.
    ///
    /// **Warning: can be called from the mixing thread.**
    SetPlaying(bool),
    /// The song position has jumped from one position to another non-consecutive position.
    ///
    /// **Warning: can be called from the mixing thread.**
    SongPosChanged,
    /// The time signature has changed.
    ///
    /// The value is [`TimeSignature`](../struct.TimeSignature.html).
    SetTimeSig(TimeSignature),
    /// This is called to let the plugin tell the host which files need to be collected or put in
    /// zip files.
    ///
    /// The value holds the file index, which starts at 0.
    ///
    /// The name of the file is passed to the host as a `String` in the result of the
    /// dispatcher function. The host keeps calling this until the plugin returns zero.
    CollectFile(usize),
    /// (private message to known plugins, ignore) tells the plugin to update a specific,
    /// non-automated param.
    SetInternalParam,
    /// This tells the plugin how many send tracks there are (fixed to 4, but could be set by the
    /// user at any time in a future update).
    ///
    /// The value holds the number of send tracks.
    SetNumSends(u64),
    /// Called when a file has been dropped onto the parent channel's button.
    ///
    /// The value holds filename.
    LoadFile(String),
    /// Set fit to time in beats.
    ///
    /// The value holds the time.
    SetFitTime(f32),
    /// Sets the number of samples in each tick. This value changes when the tempo, ppq or sample
    /// rate have changed.
    ///
    /// **Warning: can be called from the mixing thread.**
    SetSamplesPerTick(f32),
    /// Sets the frequency at which Idle is called.
    ///
    /// The value holds the new time (milliseconds).
    SetIdleTime(u64),
    /// (FL 7.0) The host has focused/unfocused the editor (focused in the value) (plugin can use
    /// this to steal keyboard focus).
    SetFocus(bool),
    /// (FL 8.0) This is sent by the host for special transport messages, from a controller.
    ///
    /// The value is the type of message (see [`Transport`](../enum.Transport.html)).
    ///
    /// Result should be `true` if handled, `false` otherwise.
    Transport(Transport),
    /// (FL 8.0) Live MIDI input preview. This allows the plugin to steal messages (mostly for
    /// transport purposes).
    ///
    /// The value has the packed MIDI message. Only note on/off for now.
    ///
    /// Result should be `true` if handled, `false` otherwise.
    MidiIn(MidiMessage),
    /// Mixer routing changed, must use
    /// [`PluginMessage::GetInOuts`](../plugin/enum.PluginMessage.html#variant.GetInOuts) if
    /// necessary.
    RoutingChanged,
    /// Retrieves info about a parameter.
    ///
    /// The value is the parameter number.
    ///
    /// see [`ParameterFlags`](../struct.ParameterFlags.html) for the result.
    GetParamInfo(usize),
    /// Called after a project has been loaded, to leave a chance to kill automation (that could be
    /// loaded after the plugin is created) if necessary.
    ProjLoaded,
    /// (private message to the plugin wrapper) Load a (VST, DX) plugin state,
    ///
    WrapperLoadState,
    /// Called when the settings button on the titlebar is switched.
    ///
    /// On/off in value.
    ShowSettings(bool),
    /// Input (the first value)/output (the second value) latency of the output, in samples (only
    /// for information)
    SetIoLatency(u32, u32),
    /// (message from Patcher) retrieves the preferred number of audio inputs (the value is `0`),
    /// audio outputs (the value is `1`) or voice outputs (the value is `2`)
    ///
    /// Result has to be:
    ///
    /// * `0i32` - default number.
    /// * `-1i32` - none.
    PreferredNumIo(u8),
    /// Unknown message.
    Unknown,
}

impl From<FlMessage> for Message<'_> {
    fn from(message: FlMessage) -> Self {
        trace!("host::Message::from {:?}", message);

        let result = match message.id {
            0 => Message::from_show_editor(message),
            1 => Message::from_process_mode(message),
            2 => Message::Flush,
            3 => Message::SetBlockSize(message.value as u32),
            4 => Message::SetSampleRate(message.value as u32),
            5 => Message::WindowMinMax(message.index as *mut c_void, message.value as *mut c_void),
            6 => Message::KillVoice,
            7 => Message::UseVoiceLevels(message.index as u8),
            9 => Message::SetPreset(message.index as u64),
            10 => Message::from_chan_sample_changed(message),
            11 => Message::SetEnabled(message.value != 0),
            12 => Message::SetPlaying(message.value != 0),
            13 => Message::SongPosChanged,
            14 => Message::SetTimeSig(TTimeSigInfo::from_raw_ptr(message.value).into()),
            15 => Message::CollectFile(message.index as usize),
            16 => Message::SetInternalParam,
            17 => Message::SetNumSends(message.value as u64),
            18 => Message::LoadFile(String::from_raw_ptr(message.value)),
            19 => Message::SetFitTime(f32::from_bits(message.value as i32 as u32)),
            20 => Message::SetSamplesPerTick(f32::from_bits(message.value as i32 as u32)),
            21 => Message::SetIdleTime(message.value as u64),
            22 => Message::SetFocus(message.value != 0),
            23 => Message::Transport(message.into()),
            24 => Message::MidiIn((message.value as c_int).into()),
            25 => Message::RoutingChanged,
            26 => Message::GetParamInfo(message.index as usize),
            27 => Message::ProjLoaded,
            28 => Message::WrapperLoadState,
            29 => Message::ShowSettings(message.value != 0),
            30 => Message::SetIoLatency(message.index as u32, message.value as u32),
            32 => Message::PreferredNumIo(message.index as u8),
            _ => Message::Unknown,
        };

        trace!("host::Message::{:?}", result);

        result
    }
}

impl Message<'_> {
    fn from_show_editor(message: FlMessage) -> Self {
        if message.value == 1 {
            Message::ShowEditor(None)
        } else {
            Message::ShowEditor(Some(message.value as *mut c_void))
        }
    }

    fn from_process_mode(message: FlMessage) -> Self {
        let flags = ProcessModeFlags::from_bits_truncate(message.value);
        Message::ProcessMode(flags)
    }

    fn from_chan_sample_changed(message: FlMessage) -> Self {
        let slice =
            unsafe { std::slice::from_raw_parts_mut(message.value as *mut f32, WAVETABLE_SIZE) };
        Message::ChanSampleChanged(slice)
    }
}

/// The host sends this message when it wants to know a text representation of some value.
///
/// See [`Plugin::name_of`](../plugin/trait.Plugin.html#tymethod.name_of)
#[derive(Debug)]
pub enum GetName {
    /// Retrieve the name of a parameter.
    ///
    /// Value specifies parameter index.
    Param(usize),
    /// Retrieve the text representation of the value of a parameter for use in the event editor.
    ///
    /// The first value specifies parameter index.
    ///
    /// The second value specifies value.
    ParamValue(usize, isize),
    /// Retrieve the name of a note in piano roll.
    ///
    /// The first value specifies note index.
    ///
    /// The second one specifies the color (or MIDI channel).
    Semitone(u8, u8),
    /// (not used yet) Retrieve the name of a patch.
    ///
    /// The value specifies patch index.
    Patch(usize),
    /// (optional) Retrieve the name of a per-voice parameter, specified by the value.
    ///
    /// Default is filter cutoff (value=0) and resonance (value=1).
    VoiceLevel(usize),
    /// Longer description for per-voice parameter (works like
    /// [`VoiceLevel`](enum.GetName.html#variant.VoiceLevel))
    VoiceLevelHint(usize),
    /// This is called when the host wants to know the name of a preset, for plugins that support
    /// presets (see
    /// [`PluginMessage::SetNumPresets`](../plugin/enum.PluginMessage.html#variant.SetNumPresets)).
    ///
    /// The value specifies preset index.
    Preset(usize),
    /// For plugins that output controllers, retrieve the name of output controller.
    ///
    /// The value specifies controller index.
    OutCtrl(usize),
    /// Retrieve name of per-voice color (MIDI channel).
    ///
    /// The value specifies the color.
    VoiceColor(u8),
    /// For plugins that output voices, retrieve the name of output voice.
    ///
    /// The value specifies voice index.
    OutVoice(usize),
    /// Message ID is unknown
    Unknown,
}

impl From<FlMessage> for GetName {
    fn from(message: FlMessage) -> Self {
        trace!("GetName::from {:?}", message);

        let result = match message.id {
            0 => GetName::Param(message.index as usize),
            1 => GetName::ParamValue(message.index as usize, message.value),
            2 => GetName::Semitone(message.index as u8, message.value as u8),
            3 => GetName::Patch(message.index as usize),
            4 => GetName::VoiceLevel(message.index as usize),
            5 => GetName::VoiceLevelHint(message.index as usize),
            6 => GetName::Preset(message.index as usize),
            7 => GetName::OutCtrl(message.index as usize),
            8 => GetName::VoiceColor(message.index as u8),
            9 => GetName::OutVoice(message.index as usize),
            _ => GetName::Unknown,
        };

        trace!("GetName::{:?}", result);

        result
    }
}

impl From<GetName> for Option<FlMessage> {
    fn from(value: GetName) -> Self {
        match value {
            GetName::Param(index) => Some(FlMessage {
                id: 0,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::ParamValue(index, value) => Some(FlMessage {
                id: 1,
                index: index.as_raw_ptr(),
                value,
            }),
            GetName::Semitone(index, value) => Some(FlMessage {
                id: 2,
                index: index.as_raw_ptr(),
                value: value.as_raw_ptr(),
            }),
            GetName::Patch(index) => Some(FlMessage {
                id: 3,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::VoiceLevel(index) => Some(FlMessage {
                id: 4,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::VoiceLevelHint(index) => Some(FlMessage {
                id: 5,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::Preset(index) => Some(FlMessage {
                id: 6,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::OutCtrl(index) => Some(FlMessage {
                id: 7,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::VoiceColor(index) => Some(FlMessage {
                id: 8,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::OutVoice(index) => Some(FlMessage {
                id: 9,
                index: index.as_raw_ptr(),
                value: 0,
            }),
            GetName::Unknown => None,
        }
    }
}

/// Event IDs.
#[derive(Debug)]
pub enum Event {
    /// The tempo has changed.
    ///
    /// First value holds the tempo.
    ///
    /// Second value holds the average samples per tick.
    Tempo(f32, u32),
    /// The maximum polyphony has changed. This is only of intrest to standalone generators.
    ///
    /// Value will hold the new maximum polyphony. A value <= 0 will mean infinite polyphony.
    MaxPoly(i32),
    /// The MIDI channel panning has changed.
    ///
    /// First value holds the new pan (0..127).
    ///
    /// Second value holds pan in -64..64 range.
    MidiPan(u8, i8),
    /// The MIDI channel volume has changed.
    ///
    /// First value holds the new volume (0..127).
    ///
    /// Second value also holds the new volume. It's in the range 0..1.
    MidiVol(u8, f32),
    /// The MIDI channel pitch has changed.
    ///
    /// Value will hold the new value in *cents*.
    ///
    /// This has to be translated according to the current pitch bend range.
    MidiPitch(i32),
    /// Unknown event.
    Unknown,
}

impl From<FlMessage> for Event {
    fn from(message: FlMessage) -> Self {
        trace!("Event::from {:?}", message);

        let result = match message.id {
            0 => Event::Tempo(f32::from_raw_ptr(message.index), message.value as u32),
            1 => Event::MaxPoly(message.index as i32),
            2 => Event::MidiPan(message.index as u8, message.value as i8),
            3 => Event::MidiVol(message.index as u8, f32::from_raw_ptr(message.value)),
            4 => Event::MidiPitch(message.index as i32),
            _ => Event::Unknown,
        };

        trace!("Event::{:?}", result);

        result
    }
}