NeuralAmpModeler-rs 3.0.2

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

use super::super::NamClapProcessor;
use crate::clap::extensions::params::{
    PARAM_ACTIVATION, PARAM_ADAPTIVE_COMPUTE, PARAM_BYPASS, PARAM_GATE_THRESH, PARAM_INPUT_GAIN,
    PARAM_OUTPUT_GAIN, PARAM_OVERSAMPLE, PARAM_SLIM_OVERRIDE,
};
use crate::clap::processor::dsp::{channels, peaks};
use crate::common::spsc::RT_STATUS_HOST_CONTRACT_VIOLATION;
use crate::dsp::gate::GateState;
use crate::dsp::gate_flags;
use crate::dsp::pipeline::{
    DspPipelineContext, apply_input_stage, apply_output_stage, run_inference,
};
use crate::math::dsp::stereo::compute_peak_abs_stereo;
use clack_plugin::events::event_types::{ParamModEvent, ParamValueEvent};
use clack_plugin::prelude::*;
use std::sync::atomic::Ordering;

const MAX_SCHEDULED_EVENTS: usize = 1024;

impl<'a> NamClapProcessor<'a> {
    #[inline(always)]
    pub(crate) fn process_dsp_audio(
        &mut self,
        audio: &mut Audio,
        input_events: &InputEvents,
        start_nanos: u64,
    ) -> Result<ProcessStatus, PluginError> {
        let mut event_count: usize = 0;
        let mut event_times: [usize; MAX_SCHEDULED_EVENTS] = [0; MAX_SCHEDULED_EVENTS];
        let mut event_param_ids: [u32; MAX_SCHEDULED_EVENTS] = [0; MAX_SCHEDULED_EVENTS];
        let mut event_values: [f32; MAX_SCHEDULED_EVENTS] = [0.0; MAX_SCHEDULED_EVENTS];
        let mut event_is_mod: [bool; MAX_SCHEDULED_EVENTS] = [false; MAX_SCHEDULED_EVENTS];

        for event in input_events {
            if event_count >= MAX_SCHEDULED_EVENTS {
                break;
            }
            let time = event.header().time() as usize;
            if let Some(param_event) = event.as_event::<ParamValueEvent>() {
                let Some(clap_id) = param_event.param_id() else {
                    continue;
                };
                event_times[event_count] = time;
                event_param_ids[event_count] = clap_id.get();
                event_values[event_count] = param_event.value() as f32;
                event_is_mod[event_count] = false;
                event_count += 1;
            } else if let Some(mod_event) = event.as_event::<ParamModEvent>() {
                let Some(clap_id) = mod_event.param_id() else {
                    continue;
                };
                event_times[event_count] = time;
                event_param_ids[event_count] = clap_id.get();
                event_values[event_count] = mod_event.amount() as f32;
                event_is_mod[event_count] = true;
                event_count += 1;
            }
        }

        let mut event_idx = 0;

        for mut port_pair in audio {
            let n_samples_raw = port_pair.frames_count() as usize;
            if n_samples_raw > self.max_frames_count {
                debug_assert!(
                    false,
                    "Host contract violation: n_samples={n_samples_raw} > max_frames_count={}",
                    self.max_frames_count
                );
                self.rt_status.set_flag(RT_STATUS_HOST_CONTRACT_VIOLATION);
            }
            let n_samples = n_samples_raw.min(self.max_frames_count);
            if n_samples == 0 {
                continue;
            }
            let n = n_samples as u32;
            if self.rt_status.last_n_samples.load(Ordering::Relaxed) != n {
                self.rt_status.last_n_samples.store(n, Ordering::Relaxed);
            }

            if self.process_bypass(&mut port_pair, n_samples)? {
                continue;
            }

            let Some((mut out_l, mut out_r)) = channels::extract_channels(
                &mut port_pair,
                &mut self.buf_host_l,
                &mut self.buf_host_r,
                &self.shared.rt_to_ui.active_channel_count,
                &mut self.process_mono,
                n_samples,
            )?
            else {
                continue;
            };

            if self
                .shared
                .ui_to_rt
                .host_r_deactivated
                .load(Ordering::Acquire)
            {
                self.process_mono = true;
            }

            if self.gate_dirty {
                let modulated_gate_db = self.params.gate_threshold_db + self.mod_gate_thresh;
                let close_db = modulated_gate_db - 6.0;
                self.cached_threshold_open_sq =
                    self.gain_lut.db_to_linear(modulated_gate_db).powi(2);
                self.cached_threshold_close_sq = self.gain_lut.db_to_linear(close_db).powi(2);
                self.cached_gate_params.threshold_open_db = modulated_gate_db;
                self.cached_gate_params.threshold_close_db = close_db;
                self.gate_dirty = false;
            }

            let model_load_fail = self.model_l.is_none() && !self.params.bypass;
            if model_load_fail {
                self.rt_status
                    .set_flag(crate::common::spsc::RT_STATUS_MODEL_LOAD_FAILED);
            } else {
                self.rt_status
                    .clear_flag(crate::common::spsc::RT_STATUS_MODEL_LOAD_FAILED);
            }

            let mut block_offset = 0usize;
            let mut output_offset = 0usize;
            let mut peak_l = 0.0f32;
            let mut peak_r = 0.0f32;
            let mut last_gate_state = GateState::Open;
            let mut any_active = false;
            let model_output_mult_adj = self.model_output_mult_adj;
            let shared_sample_rate = self.shared.cold.sample_rate.load(Ordering::Relaxed);

            let mut input_clipped = false;

            while block_offset < n_samples {
                while event_idx < event_count && event_times[event_idx] < block_offset {
                    event_idx += 1;
                }

                let sub_end = if event_idx < event_count {
                    let et = event_times[event_idx];
                    if et < n_samples { et } else { n_samples }
                } else {
                    n_samples
                };

                let sub_n = sub_end - block_offset;
                if sub_n > 0 {
                    let bypass = self.params.bypass;
                    let process_mono = self.process_mono;

                    let (n_out, gate_state) = {
                        let mut ctx = DspPipelineContext {
                            resampler: &mut self.resampler,
                            os_l: &mut self.os_l,
                            os_r: &mut self.os_r,
                            active_model_l: &mut self.model_l,
                            active_model_r: &mut None,
                            input_gain_mult: self.model_input_mult_adj,
                            output_gain_mult: model_output_mult_adj,
                            gate_params: &self.cached_gate_params,
                            silence_hysteresis: &mut self.silence_hyst,
                            mono_hysteresis: &mut self.mono_hyst,
                            threshold_open_sq: self.cached_threshold_open_sq,
                            threshold_close_sq: self.cached_threshold_close_sq,
                            process_mono: &mut self.process_mono,
                            rt_status: &self.rt_status,
                            adaptive: &mut self.adaptive_compute,
                            bridge_writer: None,
                            conv: self.conv_engine.as_mut().map(|e| e.as_mut()),
                        };

                        process_sub_block(
                            block_offset,
                            sub_n,
                            &mut out_l,
                            &mut out_r,
                            output_offset,
                            &mut ctx,
                            bypass,
                            process_mono,
                            &mut input_clipped,
                            &mut self.smoother_in,
                            &mut self.smoother_out,
                            &mut self.buf_host_l,
                            &mut self.buf_host_r,
                            &mut self.buf_mid_l,
                            &mut self.buf_mid_r,
                            &mut self.buf_out_l,
                            &mut self.buf_out_r,
                            &mut self.buf_model_l,
                            &mut self.buf_model_r,
                            &mut self.buf_os_in_l,
                            &mut self.buf_os_in_r,
                            &mut self.buf_os_model_l,
                            &mut self.buf_os_model_r,
                            model_output_mult_adj,
                            shared_sample_rate,
                            self.gain_lut,
                        )
                    };

                    if input_clipped {
                        self.shared
                            .rt_to_ui
                            .ui_clipped
                            .store(true, Ordering::Relaxed);
                    }

                    output_offset += n_out;

                    if let (Some(o_l), Some(o_r)) = (&out_l, &out_r) {
                        let o_start = output_offset - n_out;
                        let o_end = o_start + n_out;
                        let avail_l = o_l.len().min(o_end).saturating_sub(o_start);
                        let avail_r = o_r.len().min(o_end).saturating_sub(o_start);
                        let n = avail_l.min(avail_r);
                        if n > 0 {
                            let (pl, pr) = unsafe {
                                compute_peak_abs_stereo(
                                    &o_l[o_start..o_start + n],
                                    &o_r[o_start..o_start + n],
                                )
                            };
                            peak_l = peak_l.max(pl);
                            peak_r = peak_r.max(pr);
                        }
                    } else if let Some(o_l) = &out_l {
                        let o_start = output_offset - n_out;
                        let o_end = o_start + n_out;
                        let avail = o_l.len().min(o_end).saturating_sub(o_start);
                        if avail > 0 {
                            let (pl, _) = unsafe {
                                compute_peak_abs_stereo(
                                    &o_l[o_start..o_start + avail],
                                    &o_l[o_start..o_start + avail],
                                )
                            };
                            peak_l = peak_l.max(pl);
                            peak_r = peak_r.max(pl);
                        }
                    }

                    if gate_state != GateState::Closed {
                        last_gate_state = gate_state;
                        any_active = true;
                    }
                }

                while event_idx < event_count && event_times[event_idx] == sub_end {
                    apply_scheduled_event(
                        event_param_ids[event_idx],
                        event_values[event_idx],
                        event_is_mod[event_idx],
                        &mut self.params,
                        &mut self.smoother_in,
                        &mut self.smoother_out,
                        &mut self.gate_dirty,
                        &mut self.mod_input_gain,
                        &mut self.mod_output_gain,
                        &mut self.mod_gate_thresh,
                        &mut self.adaptive_compute,
                        &self.rt_status,
                        &self.shared.ui_to_rt,
                        self.gain_lut,
                    );
                    event_idx += 1;
                }

                if self.gate_dirty {
                    let modulated_gate_db = self.params.gate_threshold_db + self.mod_gate_thresh;
                    let close_db = modulated_gate_db - 6.0;
                    self.cached_threshold_open_sq =
                        self.gain_lut.db_to_linear(modulated_gate_db).powi(2);
                    self.cached_threshold_close_sq = self.gain_lut.db_to_linear(close_db).powi(2);
                    self.cached_gate_params.threshold_open_db = modulated_gate_db;
                    self.cached_gate_params.threshold_close_db = close_db;
                    self.gate_dirty = false;
                }

                block_offset = sub_end;
            }

            if any_active {
                gate_flags::report_gate_flags(&self.rt_status, last_gate_state);
            } else {
                gate_flags::report_gate_flags(&self.rt_status, GateState::Closed);
            }

            peaks::store_peaks(self.shared, peak_l, peak_r);
        }

        self.process_telemetry(start_nanos);

        #[cfg(feature = "heap-audit")]
        if crate::common::alloc_audit::AUDIT_ENABLED.load(Ordering::Relaxed) {
            let allocs = crate::common::alloc_audit::get_alloc_count();
            if allocs > 0 {
                self.rt_status
                    .set_flag(crate::common::spsc::RT_STATUS_HEAP_ALLOC);
                return Ok(ProcessStatus::Sleep);
            }
        }

        Ok(ProcessStatus::Continue)
    }
}

#[inline(always)]
#[expect(clippy::too_many_arguments)]
fn process_sub_block(
    offset: usize,
    n_samples: usize,
    out_l: &mut Option<&mut [f32]>,
    out_r: &mut Option<&mut [f32]>,
    output_offset: usize,
    ctx: &mut DspPipelineContext<'_>,
    bypass: bool,
    process_mono: bool,
    input_clipped: &mut bool,
    smoother_in: &mut crate::dsp::smoother::ParamSmoother,
    smoother_out: &mut crate::dsp::smoother::ParamSmoother,
    buf_host_l: &mut [f32],
    buf_host_r: &mut [f32],
    buf_mid_l: &mut [f32],
    buf_mid_r: &mut [f32],
    buf_out_l: &mut [f32],
    buf_out_r: &mut [f32],
    buf_model_l: &mut [f32],
    buf_model_r: &mut [f32],
    buf_os_in_l: &mut [f32],
    buf_os_in_r: &mut [f32],
    buf_os_model_l: &mut [f32],
    buf_os_model_r: &mut [f32],
    model_output_mult_adj: f32,
    shared_sample_rate: u32,
    gain_lut: &crate::math::dsp::gain_lut::GainLUT,
) -> (usize, GateState) {
    if bypass {
        copy_bypass_to_output(
            out_l,
            out_r,
            &buf_host_l[offset..offset + n_samples],
            &buf_host_r[offset..offset + n_samples],
            output_offset,
            process_mono,
        );
        return (n_samples, GateState::Open);
    }

    apply_input_gain_sub_block_inner(
        smoother_in,
        gain_lut,
        buf_host_l,
        buf_host_r,
        offset,
        n_samples,
        input_clipped,
    );

    let gate_state = apply_input_stage(
        &mut buf_host_l[offset..offset + n_samples],
        &mut buf_host_r[offset..offset + n_samples],
        n_samples,
        ctx,
    );

    if gate_state == GateState::Closed {
        copy_silence_to_output(out_l, out_r, output_offset, n_samples, process_mono);
        return (n_samples, GateState::Closed);
    }

    let n_out = run_inference(
        &mut buf_host_l[offset..offset + n_samples],
        &mut buf_host_r[offset..offset + n_samples],
        n_samples,
        ctx,
        buf_mid_l,
        buf_mid_r,
        buf_out_l,
        buf_out_r,
        buf_model_l,
        buf_model_r,
        buf_os_in_l,
        buf_os_in_r,
        buf_os_model_l,
        buf_os_model_r,
    );

    apply_output_stage(
        &mut buf_out_l[..n_out],
        &mut buf_out_r[..n_out],
        n_out,
        model_output_mult_adj,
        ctx.silence_hysteresis,
        ctx.rt_status,
        *ctx.process_mono,
        ctx.adaptive,
        shared_sample_rate,
    );

    apply_output_gain_sub_block_inner(smoother_out, buf_out_l, buf_out_r, n_out);

    copy_output_from_sub_block(
        out_l,
        out_r,
        buf_out_l,
        buf_out_r,
        n_out,
        output_offset,
        process_mono,
    );

    (n_out, gate_state)
}

#[inline(always)]
fn apply_input_gain_sub_block_inner(
    smoother_in: &mut crate::dsp::smoother::ParamSmoother,
    _gain_lut: &crate::math::dsp::gain_lut::GainLUT,
    buf_host_l: &mut [f32],
    buf_host_r: &mut [f32],
    offset: usize,
    n_samples: usize,
    input_clipped: &mut bool,
) {
    #[cfg(feature = "stereo")]
    {
        let start = smoother_in.peek();
        let target = smoother_in.target_value();
        if (start - target).abs() < 1e-9 {
            // Fast-path: gain is stable — use SIMD constant-gain with clipping detection.
            let clipped = unsafe {
                crate::math::dsp::gain::apply_gain_and_detect_clipping_stereo(
                    &mut buf_host_l[offset..offset + n_samples],
                    &mut buf_host_r[offset..offset + n_samples],
                    start,
                )
            };
            if clipped {
                *input_clipped = true;
            }
        } else if n_samples < 8 {
            // Transition path for very small sub-blocks: run the IIR smoother tick-per-sample
            // to prevent zipper noise when block-splitting fatias the block aggressively (e.g. size 1).
            let slice_l = &mut buf_host_l[offset..offset + n_samples];
            let slice_r = &mut buf_host_r[offset..offset + n_samples];
            for (l, r) in slice_l.iter_mut().zip(slice_r.iter_mut()) {
                let g = smoother_in.tick();
                *l *= g;
                *r *= g;
                if l.abs() > 1.0 || r.abs() > 1.0 {
                    *input_clipped = true;
                }
            }
        } else {
            // For larger sub-blocks, use the efficient linear ramp and snap to target at the end.
            let step = (target - start) / n_samples as f32;
            unsafe {
                crate::math::dsp::gain::apply_ramp_stereo(
                    &mut buf_host_l[offset..offset + n_samples],
                    &mut buf_host_r[offset..offset + n_samples],
                    start,
                    step,
                );
            }
            smoother_in.set(target);
            let (peak_l, peak_r) = unsafe {
                crate::math::dsp::stereo::compute_peak_abs_stereo(
                    &buf_host_l[offset..offset + n_samples],
                    &buf_host_r[offset..offset + n_samples],
                )
            };
            if peak_l > 1.0 || peak_r > 1.0 {
                *input_clipped = true;
            }
        }
    }
    #[cfg(not(feature = "stereo"))]
    {
        let _ = buf_host_r;
        let start = smoother_in.peek();
        let target = smoother_in.target_value();
        if (start - target).abs() < 1e-9 {
            // Fast-path: gain is stable — use SIMD constant-gain with clipping detection.
            let clipped = unsafe {
                crate::math::dsp::gain::apply_gain_and_detect_clipping_mono(
                    &mut buf_host_l[offset..offset + n_samples],
                    start,
                )
            };
            if clipped {
                *input_clipped = true;
            }
        } else if n_samples < 8 {
            // Transition path: run the IIR smoother tick-per-sample.
            for sample in &mut buf_host_l[offset..offset + n_samples] {
                let g = smoother_in.tick();
                *sample *= g;
                if sample.abs() > 1.0 {
                    *input_clipped = true;
                }
            }
        } else {
            let step = (target - start) / n_samples as f32;
            crate::math::dsp::gain::apply_ramp_simd(
                &mut buf_host_l[offset..offset + n_samples],
                start,
                step,
            );
            smoother_in.set(target);
            for &sample in &buf_host_l[offset..offset + n_samples] {
                if sample.abs() > 1.0 {
                    *input_clipped = true;
                    break;
                }
            }
        }
    }
}

#[inline(always)]
fn apply_output_gain_sub_block_inner(
    smoother_out: &mut crate::dsp::smoother::ParamSmoother,
    buf_out_l: &mut [f32],
    buf_out_r: &mut [f32],
    n_out: usize,
) {
    #[cfg(feature = "stereo")]
    {
        let start = smoother_out.peek();
        let target = smoother_out.target_value();
        if (start - target).abs() < 1e-9 {
            // Fast-path: gain is stable — SIMD constant-gain.
            unsafe {
                crate::math::dsp::gain::apply_gain_stereo(
                    &mut buf_out_l[..n_out],
                    &mut buf_out_r[..n_out],
                    start,
                );
            }
        } else if n_out < 8 {
            // Transition path: IIR tick-per-sample to prevent zipper noise.
            for (l, r) in buf_out_l[..n_out]
                .iter_mut()
                .zip(buf_out_r[..n_out].iter_mut())
            {
                let g = smoother_out.tick();
                *l *= g;
                *r *= g;
            }
        } else {
            let step = (target - start) / n_out as f32;
            unsafe {
                crate::math::dsp::gain::apply_ramp_stereo(
                    &mut buf_out_l[..n_out],
                    &mut buf_out_r[..n_out],
                    start,
                    step,
                );
            }
            smoother_out.set(target);
        }
    }
    #[cfg(not(feature = "stereo"))]
    {
        let _ = buf_out_r;
        let start = smoother_out.peek();
        let target = smoother_out.target_value();
        if (start - target).abs() < 1e-9 {
            // Fast-path: gain is stable — SIMD constant-gain.
            crate::math::dsp::gain::apply_gain_simd(&mut buf_out_l[..n_out], start);
        } else if n_out < 8 {
            // Transition path: IIR tick-per-sample to prevent zipper noise.
            for sample in &mut buf_out_l[..n_out] {
                let g = smoother_out.tick();
                *sample *= g;
            }
        } else {
            let step = (target - start) / n_out as f32;
            crate::math::dsp::gain::apply_ramp_simd(&mut buf_out_l[..n_out], start, step);
            smoother_out.set(target);
        }
    }
}

#[expect(clippy::too_many_arguments)]
fn apply_scheduled_event(
    param_id: u32,
    value: f32,
    is_mod: bool,
    params: &mut crate::common::params::RtPluginParams,
    smoother_in: &mut crate::dsp::smoother::ParamSmoother,
    smoother_out: &mut crate::dsp::smoother::ParamSmoother,
    gate_dirty: &mut bool,
    mod_input_gain: &mut f32,
    mod_output_gain: &mut f32,
    mod_gate_thresh: &mut f32,
    adaptive_compute: &mut crate::dsp::adaptive::AdaptiveCompute,
    rt_status: &crate::common::spsc::RtStatusFlags,
    ui_to_rt: &crate::clap::plugin::UiToRt,
    gain_lut: &crate::math::dsp::gain_lut::GainLUT,
) {
    use crate::clap::extensions::params::{bypass_bool_to_u32, bypass_f32_to_bool};
    use std::sync::atomic::Ordering;

    if is_mod {
        let amount = value;
        match param_id {
            PARAM_INPUT_GAIN => {
                *mod_input_gain = amount;
                smoother_in.set_target(gain_lut.db_to_linear(params.input_gain_db + amount));
            }
            PARAM_OUTPUT_GAIN => {
                *mod_output_gain = amount;
                smoother_out.set_target(gain_lut.db_to_linear(params.output_gain_db + amount));
            }
            PARAM_GATE_THRESH => {
                *mod_gate_thresh = amount;
                *gate_dirty = true;
            }
            _ => {}
        }
    } else {
        let val = value;
        match param_id {
            PARAM_INPUT_GAIN => {
                params.input_gain_db = val;
                ui_to_rt
                    .param_input_gain
                    .store(val.to_bits(), Ordering::Relaxed);
                smoother_in.set_target(gain_lut.db_to_linear(val + *mod_input_gain));
            }
            PARAM_OUTPUT_GAIN => {
                params.output_gain_db = val;
                ui_to_rt
                    .param_output_gain
                    .store(val.to_bits(), Ordering::Relaxed);
                smoother_out.set_target(gain_lut.db_to_linear(val + *mod_output_gain));
            }
            PARAM_GATE_THRESH => {
                params.gate_threshold_db = val;
                ui_to_rt
                    .param_gate_thresh
                    .store(val.to_bits(), Ordering::Relaxed);
                *gate_dirty = true;
            }
            PARAM_BYPASS => {
                let bypass = bypass_f32_to_bool(val);
                params.bypass = bypass;
                ui_to_rt
                    .param_bypass
                    .store(bypass_bool_to_u32(bypass), Ordering::Relaxed);
            }
            PARAM_ADAPTIVE_COMPUTE => {
                let mode = crate::common::params::AdaptiveComputeMode::from_f32(val);
                params.adaptive_compute = mode;
                ui_to_rt
                    .param_adaptive_compute
                    .store(mode as u32, Ordering::Relaxed);
                adaptive_compute.set_mode(mode, rt_status);
            }
            PARAM_SLIM_OVERRIDE => {
                let ov = crate::dsp::adaptive::SlimOverride::from_f32(val);
                params.slim_override = ov;
                ui_to_rt
                    .param_slim_override
                    .store(ov as u32, Ordering::Relaxed);
                adaptive_compute.set_slim_override(ov);
            }
            PARAM_OVERSAMPLE => {
                let factor = crate::dsp::oversample::OversampleFactor::from_f32(val);
                if factor != params.oversample {
                    params.oversample = factor;
                    ui_to_rt
                        .param_oversample
                        .store(factor.to_f32() as u32, Ordering::Relaxed);
                    rt_status
                        .requested_os_factor
                        .store(factor.to_f32() as u32, Ordering::Relaxed);
                    rt_status.set_flag_release(crate::common::spsc::RT_STATUS_NEEDS_OS_REBUILD);
                }
            }
            PARAM_ACTIVATION => {
                let mode = crate::common::params::ActivationPrecision::from_f32(val);
                if mode != params.activation_precision {
                    params.activation_precision = mode;
                    ui_to_rt
                        .param_activation
                        .store(mode as u32, Ordering::Relaxed);
                    crate::math::activations::set_activation_precision(mode);
                }
            }
            _ => {}
        }
    }
}

#[inline(always)]
fn copy_silence_to_output(
    out_l: &mut Option<&mut [f32]>,
    out_r: &mut Option<&mut [f32]>,
    output_offset: usize,
    n_samples: usize,
    _process_mono: bool,
) {
    if let Some(o_l) = out_l {
        let end = (output_offset + n_samples).min(o_l.len());
        o_l[output_offset..end].fill(0.0);
    }
    if let Some(o_r) = out_r {
        let end = (output_offset + n_samples).min(o_r.len());
        o_r[output_offset..end].fill(0.0);
    }
}

#[inline(always)]
fn copy_output_from_sub_block(
    out_l: &mut Option<&mut [f32]>,
    out_r: &mut Option<&mut [f32]>,
    buf_out_l: &[f32],
    buf_out_r: &[f32],
    n_out: usize,
    output_offset: usize,
    process_mono: bool,
) {
    if let Some(o_l) = out_l {
        let n = n_out.min(o_l.len().saturating_sub(output_offset));
        o_l[output_offset..output_offset + n].copy_from_slice(&buf_out_l[..n]);
    }
    if let Some(o_r) = out_r {
        let n = n_out.min(o_r.len().saturating_sub(output_offset));
        if process_mono {
            o_r[output_offset..output_offset + n].copy_from_slice(&buf_out_l[..n]);
        } else {
            o_r[output_offset..output_offset + n].copy_from_slice(&buf_out_r[..n]);
        }
    }
}

#[inline(always)]
fn copy_bypass_to_output(
    out_l: &mut Option<&mut [f32]>,
    out_r: &mut Option<&mut [f32]>,
    buf_host_l: &[f32],
    buf_host_r: &[f32],
    output_offset: usize,
    process_mono: bool,
) {
    if let Some(o_l) = out_l {
        let n = buf_host_l
            .len()
            .min(o_l.len().saturating_sub(output_offset));
        o_l[output_offset..output_offset + n].copy_from_slice(&buf_host_l[..n]);
    }
    if let Some(o_r) = out_r {
        let n = buf_host_l
            .len()
            .min(o_r.len().saturating_sub(output_offset));
        if process_mono {
            o_r[output_offset..output_offset + n].copy_from_slice(&buf_host_l[..n]);
        } else {
            o_r[output_offset..output_offset + n].copy_from_slice(&buf_host_r[..n]);
        }
    }
}