NeuralAmpModeler-rs 3.0.0

An opinionated, high-performance Neural Amp Modeler (NAM) client and core implementation in Rust for Linux/PipeWire and CLAP plugins.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Fábio Henrique de Lima Silva (fhl.bsb@gmail.com) All rights reserved.
//! Zone 1 (left): Identity — logo, version, SIMD badge, model load button.

use crate::clap::plugin::NamClapShared;
use clack_plugin::host::HostSharedHandle;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};

use crate::clap::gui::ui::{
    colors::{COL_BG, COL_BORDER, COL_MUTED, COL_PANEL, COL_TEXT, COL_VU_RED},
    simd::get_simd_badge,
    state::UiState,
};

use super::file_dialogs::{spawn_file_dialog, spawn_ir_file_dialog};

pub(crate) fn draw_zone1_identity(
    ui: &mut egui::Ui,
    shared: &NamClapShared,
    host: &HostSharedHandle,
    state: &mut UiState,
    accent_color: egui::Color32,
) -> (Option<egui::Id>, Option<egui::Id>) {
    let mut load_btn_id = None;
    let mut load_ir_btn_id = None;
    ui.allocate_ui(egui::vec2(135.0, 280.0), |ui| {
        ui.vertical(|ui| {
            ui.add_space(8.0);

            ui.label(
                egui::RichText::new("NAM-rs⚡")
                    .font(egui::FontId::proportional(24.0))
                    .strong()
                    .color(accent_color),
            );

            ui.label(
                egui::RichText::new("Neural Amp Modeler")
                    .font(egui::FontId::proportional(9.5))
                    .color(COL_MUTED),
            );

            ui.horizontal(|ui| {
                ui.label(
                    egui::RichText::new(format!("v{}", env!("CARGO_PKG_VERSION")))
                        .font(egui::FontId::proportional(9.0))
                        .color(COL_MUTED),
                );
                ui.add_space(4.0);
                let simd = get_simd_badge();
                let badge_color = if simd.contains("AVX") {
                    accent_color
                } else {
                    COL_MUTED
                };
                let response = ui.label(
                    egui::RichText::new(simd)
                        .font(egui::FontId::monospace(8.0))
                        .strong()
                        .color(badge_color),
                );
                response.on_hover_text(format!(
                    "Active runtime SIMD engine backend: {}.",
                    crate::math::common::SIMD_MATH.name
                ));
            });

            ui.add_space(14.0);

            ui.label(
                egui::RichText::new("MODEL")
                    .font(egui::FontId::proportional(9.0))
                    .strong()
                    .color(COL_MUTED),
            );
            ui.add_space(2.0);

            // "Load Model" button
            let load_btn = ui.add(
                egui::Button::new(
                    egui::RichText::new("📂 Load Model")
                        .font(egui::FontId::proportional(11.5))
                        .strong()
                        .color(COL_TEXT),
                )
                .fill(COL_PANEL)
                .stroke(egui::Stroke::new(1.0_f32, COL_BORDER)),
            );
            let btn_id = load_btn.id;
            load_btn_id = Some(btn_id);
            ui.memory_mut(|mem| mem.interested_in_focus(btn_id, ui.layer_id()));

            if load_btn.has_focus() {
                ui.painter().rect_stroke(
                    load_btn.rect,
                    2.0,
                    egui::Stroke::new(2.0_f32, accent_color),
                    egui::StrokeKind::Outside,
                );
            }

            let mut load_clicked = load_btn.clicked();
            if load_btn.has_focus() {
                ui.input(|i| {
                    if i.key_pressed(egui::Key::Space) || i.key_pressed(egui::Key::Enter) {
                        load_clicked = true;
                    }
                });
            }

            if load_clicked && !shared.cold.ui_loading.load(Ordering::Relaxed) {
                shared.cold.ui_loading.store(true, Ordering::Relaxed);
                let host_static: clack_plugin::host::HostSharedHandle<'static> =
                    // SAFETY: FFI call, host pointer transmute, or raw graphics context access with verified lifetimes.
                    unsafe { crate::clap::gui::extend_host_lifetime(*host) };
                if let Some(dialog_state_arc) = shared.cold.dialog_state.as_ref() {
                    let handle = spawn_file_dialog(Arc::clone(dialog_state_arc), host_static);
                    if let Ok(mut guard) = shared.cold.dialog_handle_sink.lock() {
                        *guard = Some(handle);
                    }
                }
            }

            ui.add_space(6.0);

            let is_error_active = if let Some(expiration) = state.error_expiration {
                if Instant::now() < expiration {
                    ui.ctx().request_repaint();
                    true
                } else {
                    state.error_expiration = None;
                    false
                }
            } else {
                false
            };

            let model_name: &str = if is_error_active {
                if state.model_display_name != "⚠ Load failed" {
                    state.model_display_name.clear();
                    state.model_display_name.push_str("⚠ Load failed");
                }
                &state.model_display_name
            } else if shared.cold.ui_loading.load(Ordering::Relaxed) {
                ui.ctx().request_repaint_after(Duration::from_millis(100));
                let elapsed = ui.input(|i| i.time);
                let frames = ["Loading", "Loading.", "Loading..", "Loading..."];
                let idx = (elapsed * 4.0) as usize % frames.len();
                state.model_display_name.clear();
                state.model_display_name.push_str(frames[idx]);
                &state.model_display_name
            } else {
                let name_guard = shared
                    .cold
                    .ui_model_name
                    .lock()
                    .unwrap_or_else(|e| e.into_inner());
                if name_guard.is_empty() {
                    if state.model_display_name != "No model loaded" {
                        state.model_display_name.clear();
                        state.model_display_name.push_str("No model loaded");
                    }
                    &state.model_display_name
                } else {
                    if *name_guard != state.model_display_name {
                        state.model_display_name.clear();
                        state.model_display_name.push_str(&name_guard);
                    }
                    &state.model_display_name
                }
            };

            let text_color = if is_error_active {
                COL_VU_RED
            } else {
                COL_MUTED
            };

            let frame_res = egui::Frame::new()
                .fill(COL_BG)
                .stroke(egui::Stroke::new(
                    1.0_f32,
                    if is_error_active {
                        COL_VU_RED
                    } else {
                        COL_BORDER
                    },
                ))
                .corner_radius(egui::CornerRadius::same(3))
                .inner_margin(egui::Margin::symmetric(6, 4))
                .show(ui, |ui| {
                    ui.set_min_width(120.0);
                    ui.set_max_width(120.0);
                    ui.add(
                        egui::Label::new(
                            egui::RichText::new(model_name)
                                .font(egui::FontId::proportional(9.5))
                                .color(text_color)
                                .italics(),
                        )
                        .wrap(),
                    );
                });

            if is_error_active {
                frame_res.response.on_hover_text(&state.error_msg);
            } else {
                frame_res.response.on_hover_text(model_name);
            }

            ui.add_space(12.0);

            ui.label(
                egui::RichText::new("CAB SIM IR")
                    .font(egui::FontId::proportional(9.0))
                    .strong()
                    .color(COL_MUTED),
            );
            ui.add_space(2.0);

            let has_ir = shared
                .cold
                .ir_path
                .lock()
                .map(|g| g.is_some())
                .unwrap_or(false);

            let ir_loading = shared.cold.ui_ir_loading.load(Ordering::Relaxed);

            // "Load IR" button
            let load_ir_btn = ui.add(
                egui::Button::new(
                    egui::RichText::new("📂 Load IR")
                        .font(egui::FontId::proportional(11.5))
                        .strong()
                        .color(COL_TEXT),
                )
                .fill(COL_PANEL)
                .stroke(egui::Stroke::new(1.0_f32, COL_BORDER)),
            );
            let ir_btn_id = load_ir_btn.id;
            load_ir_btn_id = Some(ir_btn_id);
            ui.memory_mut(|mem| mem.interested_in_focus(ir_btn_id, ui.layer_id()));

            if load_ir_btn.has_focus() {
                ui.painter().rect_stroke(
                    load_ir_btn.rect,
                    2.0,
                    egui::Stroke::new(2.0_f32, accent_color),
                    egui::StrokeKind::Outside,
                );
            }

            let mut load_ir_clicked = load_ir_btn.clicked();
            if load_ir_btn.has_focus() {
                ui.input(|i| {
                    if i.key_pressed(egui::Key::Space) || i.key_pressed(egui::Key::Enter) {
                        load_ir_clicked = true;
                    }
                });
            }

            if load_ir_clicked && !ir_loading {
                shared.cold.ui_ir_loading.store(true, Ordering::Relaxed);
                let host_static: clack_plugin::host::HostSharedHandle<'static> =
                    // SAFETY: FFI call, host pointer transmute, or raw graphics context access with verified lifetimes.
                    unsafe { crate::clap::gui::extend_host_lifetime(*host) };
                if let Some(ir_dialog_state_arc) = shared.cold.ir_dialog_state.as_ref() {
                    let handle = spawn_ir_file_dialog(Arc::clone(ir_dialog_state_arc), host_static);
                    if let Ok(mut guard) = shared.cold.ir_dialog_handle_sink.lock() {
                        *guard = Some(handle);
                    }
                }
            }

            ui.add_space(4.0);

            // "Clear IR" button (only show if IR is loaded)
            if has_ir && !ir_loading {
                let clear_ir_btn = ui.add(
                    egui::Button::new(
                        egui::RichText::new("🗑 Clear IR")
                            .font(egui::FontId::proportional(10.5))
                            .color(COL_MUTED),
                    )
                    .fill(COL_PANEL)
                    .stroke(egui::Stroke::new(1.0_f32, COL_BORDER)),
                );
                let clear_ir_id = clear_ir_btn.id;
                ui.memory_mut(|mem| mem.interested_in_focus(clear_ir_id, ui.layer_id()));

                if clear_ir_btn.has_focus() {
                    ui.painter().rect_stroke(
                        clear_ir_btn.rect,
                        2.0,
                        egui::Stroke::new(2.0_f32, accent_color),
                        egui::StrokeKind::Outside,
                    );
                }

                let mut clear_ir_clicked = clear_ir_btn.clicked();
                if clear_ir_btn.has_focus() {
                    ui.input(|i| {
                        if i.key_pressed(egui::Key::Space) || i.key_pressed(egui::Key::Enter) {
                            clear_ir_clicked = true;
                        }
                    });
                }

                if clear_ir_clicked {
                    shared.cold.ui_clear_ir.store(true, Ordering::Relaxed);
                    let host_static: clack_plugin::host::HostSharedHandle<'static> =
                        // SAFETY: FFI call, host pointer transmute, or raw graphics context access with verified lifetimes.
                        unsafe { crate::clap::gui::extend_host_lifetime(*host) };
                    host_static.request_callback();
                }
            }

            ui.add_space(4.0);

            // IR file name display
            let ir_is_error = if let Some(expiration) = state.error_expiration {
                if Instant::now() < expiration {
                    ui.ctx().request_repaint();
                    state.ir_display_name == "⚠ IR load failed"
                } else {
                    false
                }
            } else {
                false
            };

            let ir_name: &str = if ir_is_error {
                if state.ir_display_name != "⚠ IR load failed" {
                    state.ir_display_name.clear();
                    state.ir_display_name.push_str("⚠ IR load failed");
                }
                &state.ir_display_name
            } else if ir_loading {
                ui.ctx().request_repaint_after(Duration::from_millis(100));
                let elapsed = ui.input(|i| i.time);
                let frames = ["Loading", "Loading.", "Loading..", "Loading..."];
                let idx = (elapsed * 4.0) as usize % frames.len();
                state.ir_display_name.clear();
                state.ir_display_name.push_str(frames[idx]);
                &state.ir_display_name
            } else if let Ok(ir_guard) = shared.cold.ir_path.lock() {
                if let Some(ref path) = *ir_guard {
                    let basename = std::path::Path::new(path)
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or(path);
                    if state.ir_display_name != basename {
                        state.ir_display_name.clear();
                        state.ir_display_name.push_str(basename);
                    }
                    &state.ir_display_name
                } else {
                    if state.ir_display_name != "No IR loaded" {
                        state.ir_display_name.clear();
                        state.ir_display_name.push_str("No IR loaded");
                    }
                    &state.ir_display_name
                }
            } else {
                if state.ir_display_name != "No IR loaded" {
                    state.ir_display_name.clear();
                    state.ir_display_name.push_str("No IR loaded");
                }
                &state.ir_display_name
            };

            let ir_text_color = if ir_is_error { COL_VU_RED } else { COL_MUTED };

            let ir_frame_res = egui::Frame::new()
                .fill(COL_BG)
                .stroke(egui::Stroke::new(
                    1.0_f32,
                    if ir_is_error { COL_VU_RED } else { COL_BORDER },
                ))
                .corner_radius(egui::CornerRadius::same(3))
                .inner_margin(egui::Margin::symmetric(6, 4))
                .show(ui, |ui| {
                    ui.set_min_width(120.0);
                    ui.set_max_width(120.0);
                    ui.add(
                        egui::Label::new(
                            egui::RichText::new(ir_name)
                                .font(egui::FontId::proportional(9.5))
                                .color(ir_text_color)
                                .italics(),
                        )
                        .wrap(),
                    );
                });

            if ir_is_error {
                ir_frame_res.response.on_hover_text(&state.error_msg);
            } else {
                ir_frame_res.response.on_hover_text(ir_name);
            }
        });
    });
    (load_btn_id, load_ir_btn_id)
}