Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ path = "src/examples/record/main.rs"
[[bin]]

name = "simple_player"
path = "src/examples/simple_player/main.rs"
path = "src/examples/simple_player/main.rs"

[dependencies]
libc = "*"
8 changes: 4 additions & 4 deletions src/audio_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub trait AudioController {
* * `position` - A three dimensional vector of f32 containing the
* position of the listener [x, y, z].
*/
fn set_position(&mut self, position: [f32, ..3]) -> ();
fn set_position(&mut self, position: [f32; 3]) -> ();

/**
* Get the position of the Audio Source in three dimensional space.
Expand All @@ -192,7 +192,7 @@ pub trait AudioController {
* A three dimensional vector of f32 containing the position of the
* listener [x, y, z].
*/
fn get_position(&self) -> [f32, ..3];
fn get_position(&self) -> [f32; 3];

/**
* Set the direction of the Audio Source.
Expand All @@ -204,15 +204,15 @@ pub trait AudioController {
* # Argument
* `direction` - The new direction of the Audio Source.
*/
fn set_direction(&mut self, direction: [f32, ..3]) -> ();
fn set_direction(&mut self, direction: [f32; 3]) -> ();

/**
* Get the direction of the Audio Source.
*
* # Return
* The current direction of the Audio Source.
*/
fn get_direction(&self) -> [f32, ..3];
fn get_direction(&self) -> [f32; 3];

/**
* Set the maximum distance of the Audio Source.
Expand Down
2 changes: 1 addition & 1 deletion src/audio_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sndfile::StringSoundType::{
*
* If the tags doesn't exist in the sound file, the string is "".
*/
#[deriving(Clone, Show, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub struct Tags {
/// The title of the sound as a String
pub title: String,
Expand Down
13 changes: 9 additions & 4 deletions src/ears.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ use ears::Music;
*/

#![crate_name = "ears"]
#![desc = "Easy Api in Rust for Sounds"]
#![license = "MIT"]
//#![desc = "Easy Api in Rust for Sounds"]
//#![license = "MIT"]
#![crate_type = "dylib"]
#![crate_type = "rlib"]
#![allow(dead_code, unused_attributes)]
#![feature(macro_rules)]
#![feature(unsafe_destructor)]
//#![feature(macro_rules)]
//#![feature(unsafe_destructor)]

#![allow(unused_imports)]
//#![allow(raw_pointer_derive)]
#![allow(unused_must_use)]
//#![allow(improper_ctypes)]

extern crate libc;

Expand Down
13 changes: 6 additions & 7 deletions src/examples/many_sounds/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@

extern crate ears;

use std::io::timer::sleep;
use std::time::Duration;
use std::task;
use std::thread::sleep_ms;
use std::thread;

use ears::{Sound, AudioController};

fn main() -> () {
// call ears_init() function to ensure that the ears context is not destroyed by a task.
ears::init();

let mut i = 0u;
let mut i = 0;

while i < 20 {
task::spawn(proc() {
thread::spawn(|| {
let mut snd2 = Sound::new("../res/shot.wav").expect("Error on Sound loading.");
snd2.play();
while snd2.is_playing() {}
});
i += 1;
sleep(Duration::milliseconds(150i64));
sleep_ms(150);
}

// Wait until the last sound is played, the main task own the ears context,
// so we should kepp it alive
sleep(Duration::milliseconds(900i64));
sleep_ms(900);
}
5 changes: 2 additions & 3 deletions src/examples/record/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

extern crate ears;

use std::time::Duration;
use std::io::timer::sleep;
use std::thread::sleep_ms;

fn main() -> () {
// call ears_init() function to ensure that the ears context is not destroyed by a task.
Expand All @@ -36,7 +35,7 @@ fn main() -> () {
// Create a new Recorder using the RecordContext
let mut recorder = ears::Recorder::new(ctxt);
recorder.start();
sleep(Duration::milliseconds(5000i64));
sleep_ms(5000);
recorder.stop();
match recorder.save_to_file("hello") {
true => println!("Save okay !"),
Expand Down
32 changes: 21 additions & 11 deletions src/examples/simple_player/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
extern crate ears;

use std::io::stdin;
use std::io::stdio::flush;
use std::io::stdout;
use std::io::Write;

use ears::{Music, AudioController};
use ears::State::{Playing, Stopped, Paused};
Expand All @@ -35,13 +36,20 @@ fn main() {
let mut stdin = stdin();

print!("Insert the path to an audio file : ");
flush();
stdout().flush().ok();

let mut line = stdin.read_line().unwrap();
unsafe { line.as_mut_vec().pop(); }
let mut line = String::new();
stdin.read_line(&mut line).ok();
loop {
match &line[line.len()-1..] {
"\n" => { line.pop(); () },
"\r" => { line.pop(); () },
_ => { break; },
}
}

// Try to create the music
let mut music = match Music::new(line.as_slice()) {
let mut music = match Music::new(&line[..]) {
Some(music) => music,
None => panic!("Cannot load the music.")
};
Expand All @@ -52,12 +60,14 @@ fn main() {
loop {
// Make your choice
println!("Commands :\n\tPlay : l\n\tPause : p\n\tStop : s\n\tExit : x\n");
match stdin.read_line().unwrap().as_slice() {
"l\n" => music.play(),
"p\n" => music.pause(),
"s\n" => music.stop(),
"x\n" => { music.stop(); break; },
_ => println!("Unknwon command.")
let mut cmd = String::new();
stdin.read_line(&mut cmd).ok();
match &cmd[..1] {
"l" => music.play(),
"p" => music.pause(),
"s" => music.stop(),
"x" => { music.stop(); break; },
_ => println!("Unknwon command.")
}
match music.get_state() {
Playing => println!("State : Playing"),
Expand Down
5 changes: 3 additions & 2 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ mod test {

use init;
use init_in;
use std::thread;

#[test]
fn test_init_ears_OK() -> () {
Expand All @@ -101,8 +102,8 @@ mod test {
#[test]
fn test_init_in_in_another_task_OK() -> () {
init();
spawn(proc() {
thread::spawn(move || {
assert_eq!(init_in(), None)
})
});
}
}
68 changes: 35 additions & 33 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! Internal class to handle OpenAl context and device.
//! internal class to handle OpenAl context and device.
//!
//! Work as a Singleton, check_al_context must be called before each OpenAl object
//! to be sure that the context is created.

#![macro_escape]
#![allow(raw_pointer_deriving)]
#![macro_use]
#![allow(raw_pointer_derive)]

use std::ffi::CString;
use std::cell::RefCell;
use std::ptr;
use openal::ffi;
use record_context;
use record_context::RecordContext;

thread_local!(static AL_CONTEXT: RefCell<Box<OpenAlData>> = RefCell::new(box OpenAlData::default()))
thread_local!(static AL_CONTEXT: RefCell<Box<OpenAlData>> = RefCell::new(Box::new(OpenAlData::default())));

#[deriving(Clone)]
#[derive(Clone)]
pub struct OpenAlData {
pub al_context: *mut ffi::ALCcontext,
pub al_device: *mut ffi::ALCdevice,
pub al_capt_device: *mut ffi::ALCdevice
pub al_context: ffi::ALCcontextPtr,
pub al_device: ffi::ALCdevicePtr,
pub al_capt_device: ffi::ALCdevicePtr
}

impl OpenAlData {
Expand All @@ -48,41 +49,41 @@ impl OpenAlData {
/// Private method.
fn new() -> Result<OpenAlData, String> {
let device = unsafe { ffi::alcOpenDevice(ptr::null_mut()) };
if device.is_null() {
return Err("Internal error: cannot open the default device.".to_string());
if device == 0 {
return Err("internal error: cannot open the default device.".to_string());
}
let context = unsafe { ffi::alcCreateContext(device, ptr::null_mut()) };
if context.is_null() {
return Err("Internal error: cannot create the OpenAL context.".to_string());
if context == 0 {
return Err("internal error: cannot create the OpenAL context.".to_string());
}
if unsafe { ffi::alcMakeContextCurrent(context) } == ffi::ALC_FALSE {
return Err("Internal error: cannot make the OpenAL context current.".to_string());
return Err("internal error: cannot make the OpenAL context current.".to_string());
}

Ok(
OpenAlData {
al_context: context,
al_device: device,
al_capt_device: ptr::null_mut()
al_capt_device: 0
}
)
}

fn default() -> OpenAlData {
OpenAlData {
al_context: ptr::null_mut(),
al_device: ptr::null_mut(),
al_capt_device: ptr::null_mut()
al_context: 0,
al_device: 0,
al_capt_device: 0
}
}

fn is_default(&self) -> bool {
if self.al_context.is_null() &&
self.al_device.is_null() &&
self.al_capt_device.is_null() {
false
} else {
if self.al_context == 0 &&
self.al_device == 0 &&
self.al_capt_device == 0 {
true
} else {
false
}
}

Expand All @@ -96,15 +97,15 @@ impl OpenAlData {
/// A result containing nothing if the OpenAlData struct exist,
/// otherwise an error message.
pub fn check_al_context() -> Result<(), String> {
if unsafe { ffi::alcGetCurrentContext().is_not_null() } {
if unsafe { ffi::alcGetCurrentContext() != 0 } {
return Ok(())
}
AL_CONTEXT.with(|f| {
let is_def = f.borrow_mut().is_default();
if is_def {
match OpenAlData::new() {
Ok(al_data) => {
*f.borrow_mut() = box al_data; Ok(())
*f.borrow_mut() = Box::new(al_data); Ok(())
},
Err(err) => Err(err)
}
Expand All @@ -120,20 +121,21 @@ impl OpenAlData {
let is_def = f.borrow_mut().is_default();
if !is_def {
let mut new_context = f.borrow_mut();
if new_context.al_capt_device.is_not_null() {
if !new_context.al_capt_device == 0 {
Ok(record_context::new(new_context.al_capt_device))
} else {
if "ALC_EXT_CAPTURE".with_c_str(|c_str| unsafe {
ffi::alcIsExtensionPresent(new_context.al_device, c_str) }) == ffi::ALC_FALSE {
} else {
let c_str = CString::new("ALC_EXT_CAPTURE").unwrap();
if unsafe {
ffi::alcIsExtensionPresent(new_context.al_device, c_str.as_ptr()) } == ffi::ALC_FALSE {
return Err("Error: no input device available on your system.".to_string())
} else {
new_context.al_capt_device = unsafe {
ffi::alcCaptureOpenDevice(ptr::null_mut(),
44100,
ffi::AL_FORMAT_MONO16,
44100) };
if new_context.al_capt_device.is_null() {
return Err("Internal error: cannot open the default capture device.".to_string())
if new_context.al_capt_device == 0 {
return Err("internal error: cannot open the default capture device.".to_string())
} else {
let cap_device = new_context.al_capt_device;
return Ok(record_context::new(cap_device))
Expand All @@ -159,7 +161,7 @@ impl OpenAlData {
/// A result containing nothing if the OpenAlData struct exist,
/// otherwise an error message.
pub fn check_al_input_context() -> Result<RecordContext, String> {
if unsafe { ffi::alcGetCurrentContext().is_not_null() } {
if unsafe { !ffi::alcGetCurrentContext() == 0 } {
OpenAlData::is_input_context_init()
} else {
match OpenAlData::check_al_context() {
Expand All @@ -174,7 +176,7 @@ impl Drop for OpenAlData {
fn drop(&mut self) {
unsafe {
ffi::alcDestroyContext(self.al_context);
if self.al_capt_device.is_not_null() {
if !self.al_capt_device == 0 {
ffi::alcCaptureCloseDevice(self.al_capt_device);
}
ffi::alcCloseDevice(self.al_device);
Expand All @@ -189,4 +191,4 @@ macro_rules! check_openal_context(
Err(err) => { println!("{}", err); return $def_ret; }
}
);
)
);
Loading