dora_ssr/dora/
audio.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 audio_set_sound_speed(val: f32);
11	fn audio_get_sound_speed() -> f32;
12	fn audio_set_global_volume(val: f32);
13	fn audio_get_global_volume() -> f32;
14	fn audio_set_listener(val: i64);
15	fn audio_get_listener() -> i64;
16	fn audio_play(filename: i64, looping: i32) -> i32;
17	fn audio_stop(handle: i32);
18	fn audio_play_stream(filename: i64, looping: i32, cross_fade_time: f32);
19	fn audio_stop_stream(fade_time: f32);
20	fn audio_set_pause_all_current(pause: i32);
21	fn audio_set_listener_at(at_x: f32, at_y: f32, at_z: f32);
22	fn audio_set_listener_up(up_x: f32, up_y: f32, up_z: f32);
23	fn audio_set_listener_velocity(velocity_x: f32, velocity_y: f32, velocity_z: f32);
24}
25/// A interface of an audio player.
26pub struct Audio { }
27impl Audio {
28	/// Sets The speed of the 3D sound.
29	pub fn set_sound_speed(val: f32) {
30		unsafe { audio_set_sound_speed(val) };
31	}
32	/// Gets The speed of the 3D sound.
33	pub fn get_sound_speed() -> f32 {
34		return unsafe { audio_get_sound_speed() };
35	}
36	/// Sets The global volume of the audio. The value is between 0.0 and 1.0.
37	pub fn set_global_volume(val: f32) {
38		unsafe { audio_set_global_volume(val) };
39	}
40	/// Gets The global volume of the audio. The value is between 0.0 and 1.0.
41	pub fn get_global_volume() -> f32 {
42		return unsafe { audio_get_global_volume() };
43	}
44	/// Sets The 3D listener as a node of the audio.
45	pub fn set_listener(val: &dyn crate::dora::INode) {
46		unsafe { audio_set_listener(val.raw()) };
47	}
48	/// Gets The 3D listener as a node of the audio.
49	pub fn get_listener() -> Option<crate::dora::Node> {
50		return unsafe { crate::dora::Node::from(audio_get_listener()) };
51	}
52	/// Plays a sound effect and returns a handler for the audio.
53	///
54	/// # Arguments
55	///
56	/// * `filename` - The path to the sound effect file (must be a WAV file).
57	/// * `loop` - Optional. Whether to loop the sound effect. Default is `false`.
58	///
59	/// # Returns
60	///
61	/// * `i32` - A handler for the audio that can be used to stop the sound effect.
62	pub fn play(filename: &str, looping: bool) -> i32 {
63		unsafe { return audio_play(crate::dora::from_string(filename), if looping { 1 } else { 0 }); }
64	}
65	/// Stops a sound effect that is currently playing.
66	///
67	/// # Arguments
68	///
69	/// * `handler` - The handler for the audio that is returned by the `play` function.
70	pub fn stop(handle: i32) {
71		unsafe { audio_stop(handle); }
72	}
73	/// Plays a streaming audio file.
74	///
75	/// # Arguments
76	///
77	/// * `filename` - The path to the streaming audio file (can be OGG, WAV, MP3, or FLAC).
78	/// * `loop` - Whether to loop the streaming audio.
79	/// * `crossFadeTime` - The time (in seconds) to crossfade between the previous and new streaming audio.
80	pub fn play_stream(filename: &str, looping: bool, cross_fade_time: f32) {
81		unsafe { audio_play_stream(crate::dora::from_string(filename), if looping { 1 } else { 0 }, cross_fade_time); }
82	}
83	/// Stops a streaming audio file that is currently playing.
84	///
85	/// # Arguments
86	///
87	/// * `fade_time` - The time (in seconds) to fade out the streaming audio.
88	pub fn stop_stream(fade_time: f32) {
89		unsafe { audio_stop_stream(fade_time); }
90	}
91	/// Pauses all the current audio.
92	///
93	/// # Arguments
94	///
95	/// * `pause` - Whether to pause the audio.
96	pub fn set_pause_all_current(pause: bool) {
97		unsafe { audio_set_pause_all_current(if pause { 1 } else { 0 }); }
98	}
99	/// Sets the position of the 3D listener.
100	///
101	/// # Arguments
102	///
103	/// * `atX` - The X coordinate of the listener position.
104	/// * `atY` - The Y coordinate of the listener position.
105	/// * `atZ` - The Z coordinate of the listener position.
106	pub fn set_listener_at(at_x: f32, at_y: f32, at_z: f32) {
107		unsafe { audio_set_listener_at(at_x, at_y, at_z); }
108	}
109	/// Sets the up vector of the 3D listener.
110	///
111	/// # Arguments
112	///
113	/// * `upX` - The X coordinate of the listener up vector.
114	/// * `upY` - The Y coordinate of the listener up vector.
115	/// * `upZ` - The Z coordinate of the listener up vector.
116	pub fn set_listener_up(up_x: f32, up_y: f32, up_z: f32) {
117		unsafe { audio_set_listener_up(up_x, up_y, up_z); }
118	}
119	/// Sets the velocity of the 3D listener.
120	///
121	/// # Arguments
122	///
123	/// * `velocityX` - The X coordinate of the listener velocity.
124	/// * `velocityY` - The Y coordinate of the listener velocity.
125	/// * `velocityZ` - The Z coordinate of the listener velocity.
126	pub fn set_listener_velocity(velocity_x: f32, velocity_y: f32, velocity_z: f32) {
127		unsafe { audio_set_listener_velocity(velocity_x, velocity_y, velocity_z); }
128	}
129}