rfmod/
reverb.rs

1/*
2* Rust-FMOD - Copyright (c) 2016 Gomez Guillaume.
3*
4* The Original software, FMOD 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 ffi;
26use types::*;
27use vector;
28use reverb_properties;
29use fmod_sys;
30use fmod_sys::MemoryUsageDetails;
31use std::mem::transmute;
32use libc::{c_void};
33use std::default::Default;
34
35/// Reverb object
36pub struct Reverb {
37    reverb: *mut ffi::FMOD_REVERB,
38}
39
40impl Drop for Reverb {
41    fn drop(&mut self) {
42        self.release();
43    }
44}
45
46impl ffi::FFI<ffi::FMOD_REVERB> for Reverb {
47    fn wrap(r: *mut ffi::FMOD_REVERB) -> Reverb {
48        Reverb {reverb: r}
49    }
50
51    fn unwrap(r: &Reverb) -> *mut ffi::FMOD_REVERB {
52        r.reverb
53    }
54}
55
56impl Reverb {
57    pub fn release(&mut self) -> ::Status {
58        if self.reverb !=::std::ptr::null_mut() {
59            match unsafe { ffi::FMOD_Reverb_Release(self.reverb) } {
60                ::Status::Ok => {
61                    self.reverb = ::std::ptr::null_mut();
62                    ::Status::Ok
63                }
64                e => e,
65            }
66        } else {
67            ::Status::Ok
68        }
69    }
70
71    pub fn set_3D_attributes(&self, position: vector::Vector, min_distance: f32,
72                             max_distance: f32) -> ::Status {
73        let t_position = vector::get_ffi(&position);
74
75        unsafe { ffi::FMOD_Reverb_Set3DAttributes(self.reverb, &t_position, min_distance,
76                                                  max_distance) }
77    }
78
79    pub fn get_3D_attributes(&self) -> Result<(vector::Vector, f32, f32), ::Status> {
80        let mut position = vector::get_ffi(&vector::Vector::new());
81        let mut min_distance = 0f32;
82        let mut max_distance = 0f32;
83
84        match unsafe { ffi::FMOD_Reverb_Get3DAttributes(self.reverb, &mut position,
85                                                        &mut min_distance, &mut max_distance) } {
86            ::Status::Ok => Ok((vector::from_ptr(position), min_distance, max_distance)),
87            e => Err(e),
88        }
89    }
90
91    pub fn set_properties(&self,
92                          reverb_properties: reverb_properties::ReverbProperties) -> ::Status {
93        let t_reverb_properties = reverb_properties::get_ffi(reverb_properties);
94
95        unsafe { ffi::FMOD_Reverb_SetProperties(self.reverb, &t_reverb_properties) }
96    }
97
98    pub fn get_properties(&self, reverb_properties: reverb_properties::ReverbProperties)
99                          -> Result<reverb_properties::ReverbProperties, ::Status> {
100        let mut t_reverb_properties = reverb_properties::get_ffi(reverb_properties);
101
102        match unsafe { ffi::FMOD_Reverb_GetProperties(self.reverb, &mut t_reverb_properties) } {
103            ::Status::Ok => Ok(reverb_properties::from_ptr(t_reverb_properties)),
104            e => Err(e),
105        }
106    }
107
108    pub fn set_active(&self, active: bool) -> ::Status {
109        let t_active = if active == true {
110            1
111        } else {
112            0
113        };
114
115        unsafe { ffi::FMOD_Reverb_SetActive(self.reverb, t_active) }
116    }
117
118    pub fn get_active(&self) -> Result<bool, ::Status> {
119        let mut active = 0i32;
120
121        match unsafe { ffi::FMOD_Reverb_GetActive(self.reverb, &mut active) } {
122            ::Status::Ok => Ok(active == 1),
123            e => Err(e)
124        }
125    }
126
127    /// Returns:
128    ///
129    /// Ok(memory_used, details)
130    pub fn get_memory_info(&self, MemoryBits(memory_bits): MemoryBits,
131                           EventMemoryBits(event_memory_bits): EventMemoryBits)
132                           -> Result<(u32, MemoryUsageDetails), ::Status> {
133        let mut details = fmod_sys::get_memory_usage_details_ffi(Default::default());
134        let mut memory_used = 0u32;
135
136        match unsafe { ffi::FMOD_Reverb_GetMemoryInfo(self.reverb, memory_bits, event_memory_bits,
137                                                      &mut memory_used, &mut details) } {
138            ::Status::Ok => Ok((memory_used, fmod_sys::from_memory_usage_details_ptr(details))),
139            e => Err(e)
140        }
141    }
142
143    pub fn set_user_data<'r, T>(&'r self, user_data: &'r mut T) -> ::Status {
144        unsafe { ffi::FMOD_Reverb_SetUserData(self.reverb, transmute(user_data)) }
145    }
146
147    pub fn get_user_data<'r, T>(&'r self) -> Result<&'r mut T, ::Status> {
148        unsafe {
149            let mut user_data : *mut c_void = ::std::ptr::null_mut();
150
151            match ffi::FMOD_Reverb_GetUserData(self.reverb, &mut user_data) {
152                ::Status::Ok => {
153                    let tmp : &mut T = transmute::<*mut c_void, &mut T>(user_data);
154                    
155                    Ok(tmp)
156                },
157                e => Err(e)
158            }
159        }
160    }
161}