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
// Augmented Audio: Audio libraries and applications
// Copyright (c) 2022 Pedro Tacla Yamada
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
use audio_processor_traits::parameters::AudioProcessorHandleRef;
use audio_processor_traits::{AudioProcessor, MidiEventHandler, MidiMessageLike};

/// Abstract standalone processor with runtime optional MIDI handling.
pub trait StandaloneProcessor: Send + 'static {
    type Processor: AudioProcessor<SampleType = f32>;
    type Midi: MidiEventHandler;

    fn processor(&mut self) -> &mut Self::Processor;

    fn midi(&mut self) -> Option<&mut Self::Midi> {
        None
    }

    fn supports_midi(&mut self) -> bool {
        self.midi().is_some()
    }

    fn options(&self) -> &StandaloneOptions;

    fn handle(&self) -> Option<AudioProcessorHandleRef> {
        None
    }
}

pub struct StandaloneOptions {
    pub accepts_input: bool,
    pub handle: Option<AudioProcessorHandleRef>,
}

impl Default for StandaloneOptions {
    fn default() -> Self {
        StandaloneOptions {
            accepts_input: true,
            handle: None,
        }
    }
}

/// Noop MIDI event handler.
pub struct NoMidiEventHandler {}
impl MidiEventHandler for NoMidiEventHandler {
    fn process_midi_events<Message: MidiMessageLike>(&mut self, _midi_messages: &[Message]) {}
}

/// A standalone processor impl that will only process audio
pub struct StandaloneAudioOnlyProcessor<P> {
    processor: P,
    options: StandaloneOptions,
}

impl<P> StandaloneAudioOnlyProcessor<P> {
    pub fn new(processor: P, options: StandaloneOptions) -> Self {
        StandaloneAudioOnlyProcessor { processor, options }
    }
}

impl<P> StandaloneProcessor for StandaloneAudioOnlyProcessor<P>
where
    P: AudioProcessor<SampleType = f32> + Send + 'static,
{
    type Processor = P;
    type Midi = NoMidiEventHandler;

    fn processor(&mut self) -> &mut Self::Processor {
        &mut self.processor
    }

    fn midi(&mut self) -> Option<&mut Self::Midi> {
        None
    }

    fn options(&self) -> &StandaloneOptions {
        &self.options
    }

    fn handle(&self) -> Option<AudioProcessorHandleRef> {
        self.options.handle.clone()
    }
}

/// A standalone processor impl that will process audio and MIDI
pub struct StandaloneProcessorImpl<P> {
    processor: P,
    options: StandaloneOptions,
}

impl<P> StandaloneProcessorImpl<P> {
    pub fn new(processor: P) -> Self {
        StandaloneProcessorImpl {
            processor,
            options: StandaloneOptions::default(),
        }
    }

    pub fn new_with(processor: P, options: StandaloneOptions) -> Self {
        StandaloneProcessorImpl { processor, options }
    }
}

impl<P> StandaloneProcessor for StandaloneProcessorImpl<P>
where
    P: AudioProcessor<SampleType = f32> + Send + 'static,
    P: MidiEventHandler,
{
    type Processor = P;
    type Midi = P;

    fn processor(&mut self) -> &mut Self::Processor {
        &mut self.processor
    }

    fn midi(&mut self) -> Option<&mut Self::Midi> {
        Some(&mut self.processor)
    }

    fn options(&self) -> &StandaloneOptions {
        &self.options
    }

    fn handle(&self) -> Option<AudioProcessorHandleRef> {
        self.options.handle.clone()
    }
}

#[cfg(test)]
mod test {
    use audio_processor_standalone_midi::host::MidiMessageEntry;
    use audio_processor_traits::simple_processor::SimpleAudioProcessor;
    use audio_processor_traits::{BufferProcessor, NoopAudioProcessor};

    use super::*;

    #[test]
    fn test_midi_event_handler() {
        let mut handler = NoMidiEventHandler {};
        let midi_messages: Vec<MidiMessageEntry> = vec![];
        handler.process_midi_events(&midi_messages);
    }

    #[test]
    fn test_create_standalone_audio_processor() {
        let processor = BufferProcessor(NoopAudioProcessor::new());
        let mut standalone_audio_processor =
            StandaloneAudioOnlyProcessor::new(processor, Default::default());
        assert!(!standalone_audio_processor.supports_midi());
        assert!(standalone_audio_processor.midi().is_none());
        let _processor = standalone_audio_processor.processor();
    }

    #[test]
    fn test_create_standalone_audio_midi_processor() {
        struct MockProcessor {}
        impl SimpleAudioProcessor for MockProcessor {
            type SampleType = f32;
        }
        impl MidiEventHandler for MockProcessor {
            fn process_midi_events<Message: MidiMessageLike>(
                &mut self,
                _midi_messages: &[Message],
            ) {
            }
        }

        let processor = MockProcessor {};
        let mut standalone_audio_processor =
            StandaloneProcessorImpl::new(BufferProcessor(processor));
        assert!(standalone_audio_processor.supports_midi());
        assert!(standalone_audio_processor.midi().is_some());
        let _processor = standalone_audio_processor.processor();
    }
}