-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlistener.rs
300 lines (247 loc) · 7.1 KB
/
listener.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::ffi::{CString, CStr};
use std::ptr;
use std::mem;
use std::str::from_utf8_unchecked;
use std::marker::PhantomData;
use ffi::*;
use {Error, Device, Context, Source, Sample, Buffer};
use super::Attributes;
use ::util::{Vector, Position, Velocity, Orientation, Doppler};
/// Represents the listener.
///
/// Only one `Listener` may be open at a time.
///
/// _In OpenAL parlance a `Listener` contains both an OpenAL device and an
/// OpenAL context, and that context is made current on creation. This is
/// because there are some inconsistencies between implementations that make
/// having multiple devices or contexts impossible._
pub struct Listener<'a> {
device: *mut ALCdevice,
context: *mut ALCcontext,
_marker: PhantomData<&'a ()>,
}
unsafe impl<'a> Send for Listener<'a> { }
impl<'a> Listener<'a> {
#[doc(hidden)]
pub unsafe fn wrap(device: *mut ALCdevice, context: *mut ALCcontext) -> Self {
Listener { device: device, context: context, _marker: PhantomData }
}
}
impl<'a> Listener<'a> {
#[doc(hidden)]
pub fn default(attributes: &Attributes) -> Result<Self, Error> {
unsafe {
if !alcGetCurrentContext().is_null() {
return Err(Error::InvalidOperation);
}
let device = alcOpenDevice(ptr::null());
if device.is_null() {
return Err(Error::InvalidName);
}
let context = alcCreateContext(device, Vec::from(attributes).as_ptr());
if context.is_null() {
al_try!(&device, ());
}
if alcMakeContextCurrent(context) != ALC_TRUE {
al_try!(&device, ());
}
Ok(Listener::wrap(device, context))
}
}
#[doc(hidden)]
pub fn open(name: &str, attributes: &Attributes) -> Result<Self, Error> {
unsafe {
if !alcGetCurrentContext().is_null() {
return Err(Error::InvalidOperation);
}
let device = alcOpenDevice(CString::new(name.as_bytes()).unwrap().as_ptr());
if device.is_null() {
return Err(Error::InvalidName);
}
let context = alcCreateContext(device, Vec::from(attributes).as_ptr());
if context.is_null() {
al_try!(&device, ());
}
if alcMakeContextCurrent(context) != ALC_TRUE {
al_try!(&device, ());
}
Ok(Listener::wrap(device, context))
}
}
/// Process the `Listener`. See OpenAL documentation for `alcProcessContext`.
pub fn process(&mut self) {
unsafe {
alcProcessContext(self.context);
}
}
/// Suspend the `Listener`. See OpenAL documentation for `alcSuspendContext`.
pub fn suspend(&mut self) {
unsafe {
alcSuspendContext(self.context);
}
}
/// Create a new `Source`.
pub fn source<'b>(&self) -> Result<Source<'b>, Error> where 'a: 'b {
unsafe {
Source::new()
}
}
/// Create a new `Buffer` and fill it.
pub fn buffer<'b, T: Sample>(&self, channels: u16, data: &[T], rate: u32) -> Result<Buffer<'b>, Error> where 'a: 'b {
unsafe {
Buffer::new(channels, data, rate)
}
}
/// Get the vendor name.
pub fn vendor(&self) -> &'static str {
unsafe {
from_utf8_unchecked(CStr::from_ptr(alGetString(AL_VENDOR)).to_bytes())
}
}
/// Get the OpenAL specification version and the context specific version.
pub fn version(&self) -> (&'static str, &'static str) {
unsafe {
let string = from_utf8_unchecked(CStr::from_ptr(alGetString(AL_VERSION)).to_bytes());
let mut pieces = string.splitn(2, ' ');
(pieces.next().unwrap(), pieces.next().unwrap())
}
}
/// Get the name of the renderer.
pub fn renderer(&self) -> &'static str {
unsafe {
from_utf8_unchecked(CStr::from_ptr(alGetString(AL_RENDERER)).to_bytes())
}
}
/// Get a list of extensions supported.
pub fn extensions(&self) -> Vec<&'static str> {
unsafe {
from_utf8_unchecked(CStr::from_ptr(alGetString(AL_EXTENSIONS)).to_bytes())
.split(' ')
.collect()
}
}
/// Get the doppler factor and velocity.
pub fn doppler(&self) -> Doppler {
unsafe {
Doppler {
factor: alGetFloat(AL_DOPPLER_FACTOR),
velocity: alGetFloat(AL_DOPPLER_VELOCITY),
}
}
}
/// Set the doppler factor and velocity.
pub fn set_doppler(&mut self, value: Doppler) {
unsafe {
alDopplerFactor(value.factor);
alDopplerVelocity(value.velocity);
}
}
/// Get the speed of sound.
pub fn speed_of_sound(&self) -> f32 {
unsafe {
alGetFloat(AL_SPEED_OF_SOUND)
}
}
/// Set the speed of sound.
pub fn set_speed_of_sound(&mut self, value: f32) {
unsafe {
alSpeedOfSound(value as ALfloat);
}
}
/// Get the listener gain.
pub fn gain(&self) -> f32 {
unsafe {
let mut value = 0.0;
alGetListenerf(AL_GAIN, &mut value);
value as f32
}
}
/// Set the listener gain.
pub fn set_gain(&mut self, value: f32) {
unsafe {
alListenerf(AL_GAIN, value as ALfloat);
}
}
/// Get the listener position.
pub fn position(&self) -> Position {
unsafe {
let mut value = Position(Vector { x: 0.0, y: 0.0, z: 0.0 });
alGetListenerfv(AL_POSITION, mem::transmute(&mut value));
value
}
}
/// Set the listener position.
pub fn set_position(&mut self, value: &Position) {
unsafe {
alListenerfv(AL_POSITION, mem::transmute(value));
}
}
/// Get the listener velocity.
pub fn velocity(&self) -> Velocity {
unsafe {
let mut value = Velocity(Vector { x: 0.0, y: 0.0, z: 0.0 });
alGetListenerfv(AL_VELOCITY, mem::transmute(&mut value));
value
}
}
/// Set the listener velocity.
pub fn set_velocity(&mut self, value: &Velocity) {
unsafe {
alListenerfv(AL_VELOCITY, mem::transmute(value));
}
}
/// Get the listener orientation.
pub fn orientation(&self) -> Orientation {
unsafe {
let mut value = Orientation(Vector { x: 0.0, y: 0.0, z: 0.0 }, Vector { x: 0.0, y: 0.0, z: 0.0 });
alGetListenerfv(AL_ORIENTATION, mem::transmute(&mut value));
value
}
}
/// Set the listener orientation.
pub fn set_orientation(&mut self, value: &Orientation) {
unsafe {
alListenerfv(AL_ORIENTATION, mem::transmute(value));
}
}
}
unsafe impl<'a> Device for Listener<'a> {
fn as_ptr(&self) -> *const ALCdevice {
self.device as *const _
}
}
unsafe impl<'a> Context for Listener<'a> {
fn as_ptr(&self) -> *const ALCcontext {
self.context as *const _
}
}
impl<'a> ::std::fmt::Debug for Listener<'a> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
try!(f.write_str("openal::Listener("));
try!(f.write_str(&format!("vendor={:?} ", self.vendor())));
try!(f.write_str(&format!("version={:?} ", self.version())));
try!(f.write_str(&format!("renderer={:?} ", self.renderer())));
try!(f.write_str(&format!("extensions={:?}; ", self.extensions())));
try!(f.write_str(&format!("doppler={:?} ", self.doppler())));
try!(f.write_str(&format!("speed_of_sound={} ", self.speed_of_sound())));
try!(f.write_str(&format!("gain={} ", self.gain())));
try!(f.write_str(&format!("position={:?} ", self.position())));
try!(f.write_str(&format!("velocity={:?} ", self.velocity())));
try!(f.write_str(&format!("orientation={:?}", self.orientation())));
f.write_str(")")
}
}
impl<'a> Drop for Listener<'a> {
fn drop(&mut self) {
unsafe {
if alcMakeContextCurrent(ptr::null_mut()) != ALC_TRUE {
al_panic!(self);
}
alcDestroyContext(self.context);
al_panic!(self);
if alcCloseDevice(self.device) != ALC_TRUE {
al_panic!(self);
}
}
}
}