rfmod/
dsp_connection.rs

1/*
2* Rust-FMOD - Copyright (c) 2014 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 dsp;
28use libc::{c_int, c_void};
29use fmod_sys;
30use fmod_sys::MemoryUsageDetails;
31use std::mem::transmute;
32use std::default::Default;
33
34/// DspConnection object
35pub struct DspConnection {
36    dsp_connection: *mut ffi::FMOD_DSPCONNECTION
37}
38
39impl ffi::FFI<ffi::FMOD_DSPCONNECTION> for DspConnection {
40    fn wrap(d: *mut ffi::FMOD_DSPCONNECTION) -> DspConnection {
41        DspConnection {dsp_connection: d}
42    }
43
44    fn unwrap(d: &DspConnection) -> *mut ffi::FMOD_DSPCONNECTION {
45        d.dsp_connection
46    }
47}
48
49impl Drop for DspConnection {
50    fn drop(&mut self) {
51        self.release();
52    }
53}
54
55impl DspConnection {
56    pub fn release(&mut self) {
57        self.dsp_connection = ::std::ptr::null_mut();
58    }
59
60    pub fn get_input(&self) -> Result<dsp::Dsp, ::Status> {
61        let mut input = ::std::ptr::null_mut();
62
63        match unsafe { ffi::FMOD_DSPConnection_GetInput(self.dsp_connection, &mut input) } {
64            ::Status::Ok => Ok(ffi::FFI::wrap(input)),
65            e => Err(e)
66        }
67    }
68
69    pub fn get_output(&self) -> Result<dsp::Dsp, ::Status> {
70        let mut output = ::std::ptr::null_mut();
71
72        match unsafe { ffi::FMOD_DSPConnection_GetOutput(self.dsp_connection, &mut output) } {
73            ::Status::Ok => Ok(ffi::FFI::wrap(output)),
74            e => Err(e)
75        }
76    }
77
78    pub fn set_mix(&self, volume: f32) -> ::Status {
79        unsafe { ffi::FMOD_DSPConnection_SetMix(self.dsp_connection, volume) }
80    }
81
82    pub fn get_mix(&self) -> Result<f32, ::Status> {
83        let mut volume = 0f32;
84
85        match unsafe { ffi::FMOD_DSPConnection_GetMix(self.dsp_connection, &mut volume) } {
86            ::Status::Ok => Ok(volume),
87            e => Err(e)
88        }
89    }
90
91    pub fn set_levels(&self, speaker: ::Speaker, levels: &mut Vec<f32>) -> ::Status {
92        unsafe { ffi::FMOD_DSPConnection_SetLevels(self.dsp_connection, speaker,
93                                                   levels.as_mut_ptr(), levels.len() as c_int) }
94    }
95
96    pub fn get_levels(&self, speaker: ::Speaker, num_levels: usize) -> Result<Vec<f32>, ::Status> {
97        let mut levels : Vec<f32> = ::std::iter::repeat(0f32).take(num_levels).collect();
98
99        match unsafe { ffi::FMOD_DSPConnection_GetLevels(self.dsp_connection, speaker,
100                                                         levels.as_mut_ptr(),
101                                                         levels.len() as c_int) } {
102            ::Status::Ok => Ok(levels),
103            e => Err(e),
104        }
105    }
106
107    pub fn get_memory_info(&self, MemoryBits(memory_bits): MemoryBits,
108                           EventMemoryBits(event_memory_bits): EventMemoryBits)
109                           -> Result<(u32, MemoryUsageDetails), ::Status> {
110        let mut details = fmod_sys::get_memory_usage_details_ffi(Default::default());
111        let mut memory_used = 0u32;
112
113        match unsafe { ffi::FMOD_DSPConnection_GetMemoryInfo(self.dsp_connection, memory_bits,
114                                                             event_memory_bits, &mut memory_used,
115                                                             &mut details) } {
116            ::Status::Ok => Ok((memory_used, fmod_sys::from_memory_usage_details_ptr(details))),
117            e => Err(e),
118        }
119    }
120
121    pub fn set_user_data<'r, T>(&'r self, user_data: &'r mut T) -> ::Status {
122        unsafe { ffi::FMOD_DSPConnection_SetUserData(self.dsp_connection, transmute(user_data)) }
123    }
124
125    pub fn get_user_data<'r, T>(&'r self) -> Result<&'r mut T, ::Status> {
126        unsafe {
127            let mut user_data : *mut c_void = ::std::ptr::null_mut();
128
129            match ffi::FMOD_DSPConnection_GetUserData(self.dsp_connection, &mut user_data) {
130               ::Status::Ok => {
131                    let tmp : &mut T = transmute::<*mut c_void, &mut T>(user_data);
132                    
133                    Ok(tmp)
134                }
135                e => Err(e),
136            }
137        }
138    }
139}