dora_ssr/dora/audio_bus.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission 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:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE 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. */
8
9extern "C" {
10 fn audiobus_type() -> i32;
11 fn audiobus_set_volume(slf: i64, val: f32);
12 fn audiobus_get_volume(slf: i64) -> f32;
13 fn audiobus_set_pan(slf: i64, val: f32);
14 fn audiobus_get_pan(slf: i64) -> f32;
15 fn audiobus_set_play_speed(slf: i64, val: f32);
16 fn audiobus_get_play_speed(slf: i64) -> f32;
17 fn audiobus_fade_volume(slf: i64, time: f64, to_volume: f32);
18 fn audiobus_fade_pan(slf: i64, time: f64, to_pan: f32);
19 fn audiobus_fade_play_speed(slf: i64, time: f64, to_play_speed: f32);
20 fn audiobus_set_filter(slf: i64, index: i32, name: i64);
21 fn audiobus_set_filter_parameter(slf: i64, index: i32, attr_id: i32, value: f32);
22 fn audiobus_get_filter_parameter(slf: i64, index: i32, attr_id: i32) -> f32;
23 fn audiobus_fade_filter_parameter(slf: i64, index: i32, attr_id: i32, to: f32, time: f64);
24 fn audiobus_new() -> i64;
25}
26use crate::dora::IObject;
27/// A class that represents an audio bus.
28pub struct AudioBus { raw: i64 }
29crate::dora_object!(AudioBus);
30impl AudioBus {
31 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
32 (unsafe { audiobus_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
33 match raw {
34 0 => None,
35 _ => Some(Box::new(AudioBus { raw: raw }))
36 }
37 })
38 }
39 /// Sets The volume of the audio bus. The value is between 0.0 and 1.0.
40 pub fn set_volume(&mut self, val: f32) {
41 unsafe { audiobus_set_volume(self.raw(), val) };
42 }
43 /// Gets The volume of the audio bus. The value is between 0.0 and 1.0.
44 pub fn get_volume(&self) -> f32 {
45 return unsafe { audiobus_get_volume(self.raw()) };
46 }
47 /// Sets The pan of the audio bus. The value is between -1.0 and 1.0.
48 pub fn set_pan(&mut self, val: f32) {
49 unsafe { audiobus_set_pan(self.raw(), val) };
50 }
51 /// Gets The pan of the audio bus. The value is between -1.0 and 1.0.
52 pub fn get_pan(&self) -> f32 {
53 return unsafe { audiobus_get_pan(self.raw()) };
54 }
55 /// Sets The play speed of the audio bus. The value 1.0 is the normal speed. 0.5 is half speed. 2.0 is double speed.
56 pub fn set_play_speed(&mut self, val: f32) {
57 unsafe { audiobus_set_play_speed(self.raw(), val) };
58 }
59 /// Gets The play speed of the audio bus. The value 1.0 is the normal speed. 0.5 is half speed. 2.0 is double speed.
60 pub fn get_play_speed(&self) -> f32 {
61 return unsafe { audiobus_get_play_speed(self.raw()) };
62 }
63 /// Fades the volume of the audio bus to the given value over the given time.
64 ///
65 /// # Arguments
66 ///
67 /// * `time` - The time to fade the volume.
68 /// * `toVolume` - The target volume.
69 pub fn fade_volume(&mut self, time: f64, to_volume: f32) {
70 unsafe { audiobus_fade_volume(self.raw(), time, to_volume); }
71 }
72 /// Fades the pan of the audio bus to the given value over the given time.
73 ///
74 /// # Arguments
75 ///
76 /// * `time` - The time to fade the pan.
77 /// * `toPan` - The target pan. The value is between -1.0 and 1.0.
78 pub fn fade_pan(&mut self, time: f64, to_pan: f32) {
79 unsafe { audiobus_fade_pan(self.raw(), time, to_pan); }
80 }
81 /// Fades the play speed of the audio bus to the given value over the given time.
82 ///
83 /// # Arguments
84 ///
85 /// * `time` - The time to fade the play speed.
86 /// * `toPlaySpeed` - The target play speed.
87 pub fn fade_play_speed(&mut self, time: f64, to_play_speed: f32) {
88 unsafe { audiobus_fade_play_speed(self.raw(), time, to_play_speed); }
89 }
90 /// Sets the filter of the audio bus.
91 ///
92 /// # Arguments
93 ///
94 /// * `index` - The index of the filter.
95 /// * `name` - The name of the filter.
96 /// - "": No filter.
97 /// - "BassBoost": The bass boost filter.
98 /// - "BiquadResonant": The biquad resonant filter.
99 /// - "DCRemoval": The DC removal filter.
100 /// - "Echo": The echo filter.
101 /// - "Eq": The equalizer filter.
102 /// - "FFT": The FFT filter.
103 /// - "Flanger": The flanger filter.
104 /// - "FreeVerb": The freeverb filter.
105 /// - "Lofi": The lofi filter.
106 /// - "Robotize": The robotize filter.
107 /// - "WaveShaper": The wave shaper filter.
108 pub fn set_filter(&mut self, index: i32, name: &str) {
109 unsafe { audiobus_set_filter(self.raw(), index, crate::dora::from_string(name)); }
110 }
111 /// Sets the filter parameter of the audio bus.
112 ///
113 /// # Arguments
114 ///
115 /// * `index` - The index of the filter.
116 /// * `attrId` - The attribute ID of the filter.
117 /// * `value` - The value of the filter parameter.
118 /// - "BassBoost": The bass boost filter.
119 /// - param0: WET, float, min: 0, max: 1
120 /// - param1: BOOST, float, min: 0, max: 10
121 /// - "BiquadResonant": The biquad resonant filter.
122 /// - param0: WET, float, min: 0, max: 1
123 /// - param1: TYPE, int, values: 0 - LOWPASS, 1 - HIGHPASS, 2 - BANDPASS
124 /// - param2: FREQUENCY, float, min: 10, max: 8000
125 /// - param3: RESONANCE, float, min: 0.1, max: 20
126 /// - "DCRemoval": The DC removal filter.
127 /// - param0: WET, float, min: 0, max: 1
128 /// - "Echo": The echo filter.
129 /// - param0: WET, float, min: 0, max: 1
130 /// - param1: DELAY, float, min: 0, max: 1
131 /// - param2: DECAY, float, min: 0, max: 1
132 /// - param3: FILTER, float, min: 0, max: 1
133 /// - "Eq": The equalizer filter.
134 /// - param0: WET, float, min: 0, max: 1
135 /// - param1: BAND0, float, min: 0, max: 4
136 /// - param2: BAND1, float, min: 0, max: 4
137 /// - param3: BAND2, float, min: 0, max: 4
138 /// - param4: BAND3, float, min: 0, max: 4
139 /// - param5: BAND4, float, min: 0, max: 4
140 /// - param6: BAND5, float, min: 0, max: 4
141 /// - param7: BAND6, float, min: 0, max: 4
142 /// - param8: BAND7, float, min: 0, max: 4
143 /// - "FFT": The FFT filter.
144 /// - param0: WET, float, min: 0, max: 1
145 /// - "Flanger": The flanger filter.
146 /// - param0: WET, float, min: 0, max: 1
147 /// - param1: DELAY, float, min: 0.001, max: 0.1
148 /// - param2: FREQ, float, min: 0.001, max: 100
149 /// - "FreeVerb": The freeverb filter.
150 /// - param0: WET, float, min: 0, max: 1
151 /// - param1: FREEZE, float, min: 0, max: 1
152 /// - param2: ROOMSIZE, float, min: 0, max: 1
153 /// - param3: DAMP, float, min: 0, max: 1
154 /// - param4: WIDTH, float, min: 0, max: 1
155 /// - "Lofi": The lofi filter.
156 /// - param0: WET, float, min: 0, max: 1
157 /// - param1: SAMPLE_RATE, float, min: 100, max: 22000
158 /// - param2: BITDEPTH, float, min: 0.5, max: 16
159 /// - "Robotize": The robotize filter.
160 /// - param0: WET, float, min: 0, max: 1
161 /// - param1: FREQ, float, min: 0.1, max: 100
162 /// - param2: WAVE, float, min: 0, max: 6
163 /// - "WaveShaper": The wave shaper filter.
164 /// - param0: WET, float, min: 0, max: 1
165 /// - param1: AMOUNT, float, min: -1, max: 1
166 pub fn set_filter_parameter(&mut self, index: i32, attr_id: i32, value: f32) {
167 unsafe { audiobus_set_filter_parameter(self.raw(), index, attr_id, value); }
168 }
169 /// Gets the filter parameter of the audio bus.
170 ///
171 /// # Arguments
172 ///
173 /// * `index` - The index of the filter.
174 /// * `attrId` - The attribute ID of the filter.
175 ///
176 /// # Returns
177 ///
178 /// * `float` - The value of the filter parameter.
179 pub fn get_filter_parameter(&mut self, index: i32, attr_id: i32) -> f32 {
180 unsafe { return audiobus_get_filter_parameter(self.raw(), index, attr_id); }
181 }
182 /// Fades the filter parameter of the audio bus to the given value over the given time.
183 ///
184 /// # Arguments
185 ///
186 /// * `index` - The index of the filter.
187 /// * `attrId` - The attribute ID of the filter.
188 /// * `to` - The target value of the filter parameter.
189 /// * `time` - The time to fade the filter parameter.
190 pub fn fade_filter_parameter(&mut self, index: i32, attr_id: i32, to: f32, time: f64) {
191 unsafe { audiobus_fade_filter_parameter(self.raw(), index, attr_id, to, time); }
192 }
193 /// Creates a new audio bus.
194 ///
195 /// # Returns
196 ///
197 /// * `AudioBus` - The created audio bus.
198 pub fn new() -> AudioBus {
199 unsafe { return AudioBus { raw: audiobus_new() }; }
200 }
201}