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
// 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.

//! Provides generic [`iced`] GUI for implementations of [`audio_processor_traits::parameters::AudioProcessorHandle`]
//!
//! GUI can be ran as a standalone app or as a `vst::editor::Editor`
//!
//! * [`open`] runs the app standalone
//! * [`editor`] runs a boxed `Editor` instance

use baseview::{Size, WindowOpenOptions, WindowScalePolicy};
use iced::Theme;
use iced_audio::{Normal, NormalParam};
use iced_baseview::{
    widget::Column, widget::Container, widget::Row, widget::Text, Alignment, Application, Command,
    Element, Length, Settings,
};

use audio_processor_iced_design_system::knob as audio_knob;
use audio_processor_iced_design_system::knob::Knob;
use audio_processor_iced_design_system::spacing::Spacing;
use audio_processor_iced_design_system::style::Container1;
use audio_processor_traits::parameters::{
    AudioProcessorHandleRef, FloatType, ParameterSpec, ParameterType, ParameterValue,
};

#[derive(Clone)]
struct Flags {
    handle: AudioProcessorHandleRef,
}

struct ParameterModel {
    spec: ParameterSpec,
    knob_state: iced_audio::NormalParam,
}

struct GenericAudioProcessorApplication {
    handle: AudioProcessorHandleRef,
    parameter_models: Vec<ParameterModel>,
}

#[derive(Debug, Clone)]
enum Message {
    KnobChange(usize, f32),
}

impl Application for GenericAudioProcessorApplication {
    type Executor = iced::executor::Default;
    type Message = Message;
    type Theme = Theme;
    type Flags = Flags;

    fn new(flags: Self::Flags) -> (Self, Command<Self::Message>) {
        let mut parameter_models = vec![];
        for i in 0..flags.handle.parameter_count() {
            let spec = flags.handle.get_parameter_spec(i);
            let value = flags.handle.get_parameter(i).unwrap();
            let value = match (value, spec.ty()) {
                (
                    ParameterValue::Float { value },
                    ParameterType::Float(FloatType { range, .. }),
                ) => (value - range.0) / (range.1 - range.0),
            };
            parameter_models.push(ParameterModel {
                spec,
                knob_state: NormalParam {
                    value: Normal::from_clipped(value),
                    default: Normal::from_clipped(value),
                },
            });
        }

        (
            Self {
                handle: flags.handle,
                parameter_models,
            },
            Command::none(),
        )
    }

    fn title(&self) -> String {
        "Audio processor standalone".to_string()
    }

    fn update(
        &mut self,
        _window_queue: &mut iced_baseview::window::WindowQueue,
        message: Self::Message,
    ) -> Command<Self::Message> {
        match message {
            Message::KnobChange(index, value) => self
                .handle
                .set_parameter(index, ParameterValue::Float { value }),
        }
        Command::none()
    }

    fn view(&self) -> Element<Self::Message, Self::Theme> {
        let handle = &self.handle;

        Container::new(Row::with_children(
            self.parameter_models
                .iter()
                .enumerate()
                .map(|(parameter_index, model)| {
                    let value = handle.get_parameter(parameter_index).unwrap();
                    parameter_view(parameter_index, model, value)
                })
                .collect(),
        ))
        .width(Length::Fill)
        .padding(Spacing::base_spacing())
        .height(Length::Fill)
        .center_x()
        .center_y()
        .style(Container1::default())
        .into()
    }
}

fn parameter_view(
    parameter_index: usize,
    model: &ParameterModel,
    value: ParameterValue,
) -> Element<Message, Theme> {
    match (value, model.spec.ty()) {
        (ParameterValue::Float { value }, ParameterType::Float(FloatType { range, .. })) => {
            let range = *range;

            Column::with_children(vec![
                Text::new(model.spec.name())
                    .size(Spacing::small_font_size())
                    .into(),
                Knob::new(model.knob_state, move |value| {
                    let n_value = range.0 + value.as_f32() * (range.1 - range.0);
                    Message::KnobChange(parameter_index, n_value)
                })
                .size(Length::Fixed(Spacing::base_control_size() as f32))
                .style(audio_knob::style::Knob)
                .into(),
                Text::new(format!("{:.2}", value))
                    .size(Spacing::small_font_size())
                    .into(),
            ])
            .align_items(Alignment::Center)
            .spacing(Spacing::small_spacing())
            .into()
        }
    }
}

/// Create a VST editor for this handle
pub fn editor(handle: AudioProcessorHandleRef) -> Box<dyn vst::editor::Editor> {
    let editor =
        augmented_iced_editor::IcedEditor::<GenericAudioProcessorApplication>::new(Flags {
            handle,
        });
    Box::new(editor)
}

/// Open this generic editor as a standalone app. Will block, needs to be run on the main-thread.
pub fn open(handle: AudioProcessorHandleRef) {
    iced_baseview::open_blocking::<GenericAudioProcessorApplication>(Settings {
        window: WindowOpenOptions {
            title: "audio-processor-standalone".to_string(),
            size: Size {
                width: 300.0,
                height: 300.0,
            },
            scale: WindowScalePolicy::SystemScaleFactor,
            #[cfg(feature = "glow")]
            gl_config: None,
        },
        iced_baseview: iced_baseview::settings::IcedBaseviewSettings {
            ignore_non_modifier_keys: false,
            always_redraw: true,
        },
        flags: Flags { handle },
    });
}