openal/capture/
capture.rs1use std::marker::PhantomData;
2use std::ptr;
3use std::ffi::CString;
4
5use ffi::*;
6use ::{Error, Sample};
7use traits::Device;
8
9pub struct Capture<T: Sample> {
11 ptr: *mut ALCdevice,
12
13 _marker: PhantomData<T>,
14}
15
16unsafe impl<T: Sample> Send for Capture<T> { }
17
18impl<T: Sample> Capture<T> {
19 #[doc(hidden)]
20 pub unsafe fn wrap(ptr: *mut ALCdevice) -> Self {
21 Capture { ptr: ptr, _marker: PhantomData }
22 }
23}
24
25impl<T: Sample> Capture<T> {
26 #[doc(hidden)]
27 pub fn default<U: Sample>(channels: u16, rate: u32, size: usize) -> Result<Capture<U>, Error> {
28 unsafe {
29 let ptr = alcCaptureOpenDevice(ptr::null(),
30 rate as ALCuint,
31 try!(<U as Sample>::format(channels)),
32 size as ALCsizei);
33
34 if ptr.is_null() {
35 Err(Error::InvalidName)
36 }
37 else {
38 Ok(Capture::wrap(ptr))
39 }
40 }
41 }
42
43 #[doc(hidden)]
44 pub fn open<U: Sample>(name: &str, channels: u16, rate: u32, size: usize) -> Result<Capture<U>, Error> {
45 unsafe {
46 let ptr = alcCaptureOpenDevice(CString::new(name.as_bytes()).unwrap().as_ptr(),
47 rate as ALCuint,
48 try!(<U as Sample>::format(channels)),
49 size as ALCsizei);
50
51 if ptr.is_null() {
52 Err(Error::InvalidName)
53 }
54 else {
55 Ok(Capture::wrap(ptr))
56 }
57 }
58 }
59
60 pub fn start(&mut self) {
62 unsafe {
63 alcCaptureStart(self.as_mut_ptr());
64 }
65 }
66
67 pub fn stop(&mut self) {
69 unsafe {
70 alcCaptureStop(self.as_mut_ptr());
71 }
72 }
73
74 pub fn len(&self) -> usize {
76 unsafe {
77 let mut value = 0;
78 alcGetIntegerv(self.as_ptr(), ALC_CAPTURE_SAMPLES, 1, &mut value);
79
80 value as usize
81 }
82 }
83
84 pub fn take(&mut self) -> Result<Vec<T>, Error> {
86 unsafe {
87 let mut result = Vec::with_capacity(self.len());
88
89 al_try!(self,
90 alcCaptureSamples(self.as_mut_ptr(), result.as_mut_ptr() as *mut _, self.len() as ALCsizei));
91
92 Ok(result)
93 }
94 }
95}
96
97unsafe impl<T: Sample> Device for Capture<T> {
98 fn as_ptr(&self) -> *const ALCdevice {
99 self.ptr as *const _
100 }
101}
102
103impl<T: Sample> ::std::fmt::Debug for Capture<T> {
104 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
105 try!(f.write_str("openal::Capture("));
106 try!(f.write_str(&format!("len={}; ", self.len())));
107 f.write_str(")")
108 }
109}
110
111impl<T: Sample> Drop for Capture<T> {
112 fn drop(&mut self) {
113 unsafe {
114 alcCaptureCloseDevice(self.as_mut_ptr());
115 al_panic!(self);
116 }
117 }
118}