audio_processor_bitcrusher/
generic_handle.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23
24use audio_garbage_collector::Shared;
25use audio_processor_traits::atomic_float::AtomicFloatRepresentable;
26use audio_processor_traits::parameters::{
27    AudioProcessorHandle, FloatType, ParameterSpec, ParameterType, ParameterValue,
28};
29use audio_processor_traits::Float;
30
31use crate::BitCrusherHandleImpl;
32
33pub struct BitCrusherHandleRef<ST: AtomicFloatRepresentable + Float = f32>(
34    Shared<BitCrusherHandleImpl<ST>>,
35);
36
37impl<ST> BitCrusherHandleRef<ST>
38where
39    ST: AtomicFloatRepresentable + Float,
40{
41    pub fn new(inner: Shared<BitCrusherHandleImpl<ST>>) -> Self {
42        BitCrusherHandleRef(inner)
43    }
44}
45
46impl<ST> AudioProcessorHandle for BitCrusherHandleRef<ST>
47where
48    ST: AtomicFloatRepresentable + Float,
49    ST: From<f32>,
50    f32: From<ST>,
51    ST::AtomicType: Send + Sync,
52{
53    fn name(&self) -> String {
54        "Bit-crusher".to_string()
55    }
56
57    fn parameter_count(&self) -> usize {
58        1
59    }
60
61    fn get_parameter_spec(&self, _index: usize) -> ParameterSpec {
62        ParameterSpec::new(
63            "Bit rate".into(),
64            ParameterType::Float(FloatType {
65                range: (100.0, self.0.sample_rate()),
66                step: None,
67            }),
68        )
69    }
70
71    fn get_parameter(&self, _index: usize) -> Option<ParameterValue> {
72        Some(ParameterValue::Float {
73            value: self.0.bit_rate(),
74        })
75    }
76
77    fn set_parameter(&self, _index: usize, request: ParameterValue) {
78        let ParameterValue::Float { value } = request;
79        self.0.set_bit_rate(value);
80    }
81}