rfmod/
sound_group.rs

1/*
2* Rust-FMOD - Copyright (c) 2016 Gomez Guillaume.
3*
4* The Original software, FmodEx library, is provided by FIRELIGHT TECHNOLOGIES.
5*
6* This software is provided 'as-is', without any express or implied warranty.
7* In no event will the authors be held liable for any damages arising from
8* the use of this software.
9*
10* Permission is granted to anyone to use this software for any purpose,
11* including commercial applications, and to alter it and redistribute it
12* freely, subject to the following restrictions:
13*
14* 1. The origin of this software must not be misrepresented; you must not claim
15*    that you wrote the original software. If you use this software in a product,
16*    an acknowledgment in the product documentation would be appreciated but is
17*    not required.
18*
19* 2. Altered source versions must be plainly marked as such, and must not be
20*    misrepresented as being the original software.
21*
22* 3. This notice may not be removed or altered from any source distribution.
23*/
24
25use types::*;
26use ffi;
27use sound;
28use libc::c_void;
29use fmod_sys;
30use fmod_sys::MemoryUsageDetails;
31use std::mem::transmute;
32use libc::{c_char};
33use std::default::Default;
34
35/// SoundGroup object
36pub struct SoundGroup {
37    sound_group: *mut ffi::FMOD_SOUNDGROUP,
38}
39
40impl ffi::FFI<ffi::FMOD_SOUNDGROUP> for SoundGroup {
41    fn wrap(s: *mut ffi::FMOD_SOUNDGROUP) -> SoundGroup {
42        SoundGroup {sound_group: s}
43    }
44
45    fn unwrap(s: &SoundGroup) -> *mut ffi::FMOD_SOUNDGROUP {
46        s.sound_group
47    }
48}
49
50impl Drop for SoundGroup {
51    fn drop(&mut self) {
52        self.release();
53    }
54}
55
56impl SoundGroup {
57    pub fn release(&mut self) -> ::Status {
58        if !self.sound_group.is_null() {
59            match unsafe { ffi::FMOD_SoundGroup_Release(self.sound_group) } {
60               ::Status::Ok => {
61                    self.sound_group =::std::ptr::null_mut();
62                   ::Status::Ok
63                }
64                e => e
65            }
66        } else {
67           ::Status::Ok
68        }
69    }
70
71    pub fn set_max_audible(&self, max_audible: i32) -> ::Status {
72        unsafe { ffi::FMOD_SoundGroup_SetMaxAudible(self.sound_group, max_audible) }
73    }
74
75    pub fn get_max_audible(&self) -> Result<i32, ::Status> {
76        let mut max_audible = 0i32;
77
78        match unsafe { ffi::FMOD_SoundGroup_GetMaxAudible(self.sound_group, &mut max_audible) } {
79            ::Status::Ok => Ok(max_audible),
80            e => Err(e)
81        }
82    }
83
84    pub fn set_max_audible_behavior(&self, max_audible_behavior: ::SoundGroupBehavior) -> ::Status {
85        unsafe { ffi::FMOD_SoundGroup_SetMaxAudibleBehavior(self.sound_group,
86                                                            max_audible_behavior) }
87    }
88
89    pub fn get_max_audible_behavior(&self) -> Result<::SoundGroupBehavior, ::Status> {
90        let mut max_audible_behavior = ::SoundGroupBehavior::Fail;
91
92        match unsafe { ffi::FMOD_SoundGroup_GetMaxAudibleBehavior(self.sound_group,
93                                                                  &mut max_audible_behavior) } {
94            ::Status::Ok => Ok(max_audible_behavior),
95            e => Err(e)
96        }
97    }
98
99    pub fn set_mute_fade_speed(&self, speed: f32) -> ::Status {
100        unsafe { ffi::FMOD_SoundGroup_SetMuteFadeSpeed(self.sound_group, speed) }
101    }
102
103    pub fn get_mute_fade_speed(&self) -> Result<f32, ::Status> {
104        let mut speed = 0f32;
105
106        match unsafe { ffi::FMOD_SoundGroup_GetMuteFadeSpeed(self.sound_group, &mut speed) } {
107            ::Status::Ok => Ok(speed),
108            e => Err(e)
109        }
110    }
111
112    pub fn set_volume(&self, volume: f32) -> ::Status {
113        unsafe { ffi::FMOD_SoundGroup_SetVolume(self.sound_group, volume) }
114    }
115
116    pub fn get_volume(&self) -> Result<f32, ::Status> {
117        let mut volume = 0f32;
118
119        match unsafe { ffi::FMOD_SoundGroup_GetVolume(self.sound_group, &mut volume) } {
120            ::Status::Ok => Ok(volume),
121            e => Err(e)
122        }
123    }
124
125    pub fn stop(&self) -> ::Status {
126        unsafe { ffi::FMOD_SoundGroup_Stop(self.sound_group) }
127    }
128
129    pub fn get_name(&self, name_len: usize) -> Result<String, ::RStatus> {
130        let mut c = Vec::with_capacity(name_len + 1);
131
132        for _ in 0..(name_len + 1) {
133            c.push(0);
134        }
135
136        match unsafe { ffi::FMOD_SoundGroup_GetName(self.sound_group, c.as_mut_ptr() as *mut c_char,
137                                                    name_len as i32) } {
138            ::Status::Ok => Ok(from_utf8!(c)),
139            e => Err(::RStatus::FMOD(e)),
140        }
141    }
142
143    pub fn get_num_sounds(&self) -> Result<i32, ::Status> {
144        let mut num_sounds = 0i32;
145
146        match unsafe { ffi::FMOD_SoundGroup_GetNumSounds(self.sound_group, &mut num_sounds) } {
147            ::Status::Ok => Ok(num_sounds),
148            e => Err(e)
149        }
150    }
151
152    pub fn get_sound(&self, index: i32) -> Result<sound::Sound, ::Status> {
153        let mut sound = ::std::ptr::null_mut();
154
155        match unsafe { ffi::FMOD_SoundGroup_GetSound(self.sound_group, index, &mut sound) } {
156            ::Status::Ok => Ok(ffi::FFI::wrap(sound)),
157            e => Err(e)
158        }
159    }
160
161    pub fn get_num_playing(&self) -> Result<i32, ::Status> {
162        let mut num_playing = 0i32;
163
164        match unsafe { ffi::FMOD_SoundGroup_GetNumPlaying(self.sound_group, &mut num_playing) } {
165            ::Status::Ok => Ok(num_playing),
166            e => Err(e)
167        }
168    }
169
170    /// Returns:
171    ///
172    /// Ok(memory_used, details)
173    pub fn get_memory_info(&self, MemoryBits(memory_bits): MemoryBits,
174                           EventMemoryBits(event_memory_bits): EventMemoryBits)
175                           -> Result<(u32, MemoryUsageDetails), ::Status> {
176        let mut details = fmod_sys::get_memory_usage_details_ffi(Default::default());
177        let mut memory_used = 0u32;
178
179        match unsafe { ffi::FMOD_SoundGroup_GetMemoryInfo(self.sound_group, memory_bits, event_memory_bits, &mut memory_used, &mut details) } {
180            ::Status::Ok => Ok((memory_used, fmod_sys::from_memory_usage_details_ptr(details))),
181            e => Err(e)
182        }
183    }
184
185    pub fn set_user_data<'r, T>(&'r self, user_data: &'r mut T) -> ::Status {
186        unsafe { ffi::FMOD_SoundGroup_SetUserData(self.sound_group, transmute(user_data)) }
187    }
188
189    pub fn get_user_data<'r, T>(&'r self) -> Result<&'r mut T, ::Status> {
190        unsafe {
191            let mut user_data : *mut c_void = ::std::ptr::null_mut();
192
193            match ffi::FMOD_SoundGroup_GetUserData(self.sound_group, &mut user_data) {
194               ::Status::Ok => {
195                    let tmp : &mut T = transmute::<*mut c_void, &mut T>(user_data);
196                    
197                    Ok(tmp)
198                },
199                e => Err(e)
200            }
201        }
202    }
203}