dora_ssr/dora/audio_source.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 audiosource_type() -> i32;
11 fn audiosource_set_volume(slf: i64, val: f32);
12 fn audiosource_get_volume(slf: i64) -> f32;
13 fn audiosource_set_pan(slf: i64, val: f32);
14 fn audiosource_get_pan(slf: i64) -> f32;
15 fn audiosource_set_looping(slf: i64, val: i32);
16 fn audiosource_is_looping(slf: i64) -> i32;
17 fn audiosource_is_playing(slf: i64) -> i32;
18 fn audiosource_seek(slf: i64, start_time: f64);
19 fn audiosource_schedule_stop(slf: i64, time_to_stop: f64);
20 fn audiosource_stop(slf: i64, fade_time: f64);
21 fn audiosource_play(slf: i64) -> i32;
22 fn audiosource_play_with_delay(slf: i64, delay_time: f64) -> i32;
23 fn audiosource_play_background(slf: i64) -> i32;
24 fn audiosource_play_3d(slf: i64) -> i32;
25 fn audiosource_play_3d_with_delay(slf: i64, delay_time: f64) -> i32;
26 fn audiosource_set_protected(slf: i64, value: i32);
27 fn audiosource_set_loop_point(slf: i64, loop_start_time: f64);
28 fn audiosource_set_velocity(slf: i64, vx: f32, vy: f32, vz: f32);
29 fn audiosource_set_min_max_distance(slf: i64, min: f32, max: f32);
30 fn audiosource_set_attenuation(slf: i64, model: i32, factor: f32);
31 fn audiosource_set_doppler_factor(slf: i64, factor: f32);
32 fn audiosource_new(filename: i64, auto_remove: i32) -> i64;
33 fn audiosource_with_bus(filename: i64, auto_remove: i32, bus: i64) -> i64;
34}
35use crate::dora::IObject;
36use crate::dora::INode;
37impl INode for AudioSource { }
38/// A class that represents an audio source node.
39pub struct AudioSource { raw: i64 }
40crate::dora_object!(AudioSource);
41impl AudioSource {
42 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
43 (unsafe { audiosource_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
44 match raw {
45 0 => None,
46 _ => Some(Box::new(AudioSource { raw: raw }))
47 }
48 })
49 }
50 /// Sets The volume of the audio source. The value is between 0.0 and 1.0.
51 pub fn set_volume(&mut self, val: f32) {
52 unsafe { audiosource_set_volume(self.raw(), val) };
53 }
54 /// Gets The volume of the audio source. The value is between 0.0 and 1.0.
55 pub fn get_volume(&self) -> f32 {
56 return unsafe { audiosource_get_volume(self.raw()) };
57 }
58 /// Sets The pan of the audio source. The value is between -1.0 and 1.0.
59 pub fn set_pan(&mut self, val: f32) {
60 unsafe { audiosource_set_pan(self.raw(), val) };
61 }
62 /// Gets The pan of the audio source. The value is between -1.0 and 1.0.
63 pub fn get_pan(&self) -> f32 {
64 return unsafe { audiosource_get_pan(self.raw()) };
65 }
66 /// Sets Whether the audio source is looping.
67 pub fn set_looping(&mut self, val: bool) {
68 unsafe { audiosource_set_looping(self.raw(), if val { 1 } else { 0 }) };
69 }
70 /// Gets Whether the audio source is looping.
71 pub fn is_looping(&self) -> bool {
72 return unsafe { audiosource_is_looping(self.raw()) != 0 };
73 }
74 /// Gets Whether the audio source is playing.
75 pub fn is_playing(&self) -> bool {
76 return unsafe { audiosource_is_playing(self.raw()) != 0 };
77 }
78 /// Seeks the audio source to the given time.
79 ///
80 /// # Arguments
81 ///
82 /// * `startTime` - The time to seek to.
83 pub fn seek(&mut self, start_time: f64) {
84 unsafe { audiosource_seek(self.raw(), start_time); }
85 }
86 /// Schedules the audio source to stop at the given time.
87 ///
88 /// # Arguments
89 ///
90 /// * `timeToStop` - The time to wait before stopping the audio source.
91 pub fn schedule_stop(&mut self, time_to_stop: f64) {
92 unsafe { audiosource_schedule_stop(self.raw(), time_to_stop); }
93 }
94 /// Stops the audio source.
95 ///
96 /// # Arguments
97 ///
98 /// * `fadeTime` - The time to fade out the audio source.
99 pub fn stop(&mut self, fade_time: f64) {
100 unsafe { audiosource_stop(self.raw(), fade_time); }
101 }
102 /// Plays the audio source.
103 ///
104 /// # Returns
105 ///
106 /// * `bool` - `true` if the audio source was played successfully, `false` otherwise.
107 pub fn play(&mut self) -> bool {
108 unsafe { return audiosource_play(self.raw()) != 0; }
109 }
110 /// Plays the audio source with a delay.
111 ///
112 /// # Arguments
113 ///
114 /// * `delayTime` - The time to wait before playing the audio source.
115 ///
116 /// # Returns
117 ///
118 /// * `bool` - `true` if the audio source was played successfully, `false` otherwise.
119 pub fn play_with_delay(&mut self, delay_time: f64) -> bool {
120 unsafe { return audiosource_play_with_delay(self.raw(), delay_time) != 0; }
121 }
122 /// Plays the audio source as a background audio.
123 ///
124 /// # Returns
125 ///
126 /// * `bool` - `true` if the audio source was played successfully, `false` otherwise.
127 pub fn play_background(&mut self) -> bool {
128 unsafe { return audiosource_play_background(self.raw()) != 0; }
129 }
130 /// Plays the audio source as a 3D audio.
131 ///
132 /// # Returns
133 ///
134 /// * `bool` - `true` if the audio source was played successfully, `false` otherwise.
135 pub fn play_3d(&mut self) -> bool {
136 unsafe { return audiosource_play_3d(self.raw()) != 0; }
137 }
138 /// Plays the audio source as a 3D audio with a delay.
139 ///
140 /// # Arguments
141 ///
142 /// * `delayTime` - The time to wait before playing the audio source.
143 ///
144 /// # Returns
145 ///
146 /// * `bool` - `true` if the audio source was played successfully, `false` otherwise.
147 pub fn play_3d_with_delay(&mut self, delay_time: f64) -> bool {
148 unsafe { return audiosource_play_3d_with_delay(self.raw(), delay_time) != 0; }
149 }
150 /// Sets the protected state of the audio source.
151 ///
152 /// # Arguments
153 ///
154 /// * `value` - The protected state.
155 pub fn set_protected(&mut self, value: bool) {
156 unsafe { audiosource_set_protected(self.raw(), if value { 1 } else { 0 }); }
157 }
158 /// Sets the loop point of the audio source.
159 ///
160 /// # Arguments
161 ///
162 /// * `loopStartTime` - The time to start the loop.
163 pub fn set_loop_point(&mut self, loop_start_time: f64) {
164 unsafe { audiosource_set_loop_point(self.raw(), loop_start_time); }
165 }
166 /// Sets the velocity of the audio source.
167 ///
168 /// # Arguments
169 ///
170 /// * `vx` - The X coordinate of the velocity.
171 /// * `vy` - The Y coordinate of the velocity.
172 /// * `vz` - The Z coordinate of the velocity.
173 pub fn set_velocity(&mut self, vx: f32, vy: f32, vz: f32) {
174 unsafe { audiosource_set_velocity(self.raw(), vx, vy, vz); }
175 }
176 /// Sets the minimum and maximum distance of the audio source.
177 ///
178 /// # Arguments
179 ///
180 /// * `min` - The minimum distance.
181 /// * `max` - The maximum distance.
182 pub fn set_min_max_distance(&mut self, min: f32, max: f32) {
183 unsafe { audiosource_set_min_max_distance(self.raw(), min, max); }
184 }
185 /// Sets the attenuation of the audio source.
186 ///
187 /// # Arguments
188 ///
189 /// * `model` - The attenuation model.
190 /// * `factor` - The factor of the attenuation.
191 pub fn set_attenuation(&mut self, model: crate::dora::AttenuationModel, factor: f32) {
192 unsafe { audiosource_set_attenuation(self.raw(), model as i32, factor); }
193 }
194 /// Sets the Doppler factor of the audio source.
195 ///
196 /// # Arguments
197 ///
198 /// * `factor` - The factor of the Doppler effect.
199 pub fn set_doppler_factor(&mut self, factor: f32) {
200 unsafe { audiosource_set_doppler_factor(self.raw(), factor); }
201 }
202 /// Creates a new audio source.
203 ///
204 /// # Arguments
205 ///
206 /// * `filename` - The path to the audio file.
207 /// * `autoRemove` - Whether to automatically remove the audio source when it is stopped.
208 ///
209 /// # Returns
210 ///
211 /// * `AudioSource` - The created audio source node.
212 pub fn new(filename: &str, auto_remove: bool) -> Option<AudioSource> {
213 unsafe { return AudioSource::from(audiosource_new(crate::dora::from_string(filename), if auto_remove { 1 } else { 0 })); }
214 }
215 /// Creates a new audio source.
216 ///
217 /// # Arguments
218 ///
219 /// * `filename` - The path to the audio file.
220 /// * `autoRemove` - Whether to automatically remove the audio source when it is stopped.
221 /// * `bus` - The audio bus to use for the audio source.
222 ///
223 /// # Returns
224 ///
225 /// * `AudioSource` - The created audio source node.
226 pub fn with_bus(filename: &str, auto_remove: bool, bus: &crate::dora::AudioBus) -> Option<AudioSource> {
227 unsafe { return AudioSource::from(audiosource_with_bus(crate::dora::from_string(filename), if auto_remove { 1 } else { 0 }, bus.raw())); }
228 }
229}