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
6 changes: 5 additions & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ impl OpenAlData {
}
}

extern "C" fn cleanup_openal_context() {
/// Does early cleanup of the library. This is automatically called when the program exits.
pub fn cleanup() {
if let Ok(mut guard) = AL_CONTEXT.lock() {
if let Ok(ref mut context) = *guard {
unsafe {
Expand All @@ -224,6 +225,9 @@ extern "C" fn cleanup_openal_context() {
}
}
}
extern "C" fn cleanup_openal_context() {
cleanup()
}

macro_rules! check_openal_context(
($def_ret:expr) => (
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub use audio_controller::AudioController;
pub use audio_tags::{AudioTags, Tags};
pub use einit::{init, init_in};
pub use error::SoundError;
pub use internal::OpenAlContextError;
pub use internal::{cleanup, OpenAlContextError};
pub use music::Music;
pub use presets::ReverbPreset;
pub use record_context::RecordContext;
Expand Down
60 changes: 34 additions & 26 deletions src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

//! Play Sounds easily.

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use audio_controller::AudioController;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct Sound {
/// The internal OpenAl source identifier
al_source: u32,
/// The SoundData associated to the Sound.
sound_data: Rc<RefCell<SoundData>>,
sound_data: Arc<Mutex<SoundData>>,
}

impl Sound {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Sound {
check_openal_context!(Err(SoundError::InvalidOpenALContext));

let sound_data = SoundData::new(path)?;
let sound_data = Rc::new(RefCell::new(sound_data));
let sound_data = Arc::new(Mutex::new(sound_data));
Sound::new_with_data(sound_data)
}

Expand All @@ -111,37 +111,38 @@ impl Sound {
* # Example
* ```ignore
* use ears::{Sound, SoundData, SoundError, AudioController};
* use std::rc::Rc;
* use std::cell::RefCell;
* use std::sync::{Mutex, Arc};
*
* fn main() -> Result<(), SoundError> {
* let data = Rc::new(RefCell::new(SoundData::new("path/to/the/sound.ogg")?));
* let data = Arc::new(Mutex::new(SoundData::new("path/to/the/sound.ogg")?));
* let sound = Sound::new_with_data(data)?;
* Ok(())
* }
* ```
*/
pub fn new_with_data(sound_data: Rc<RefCell<SoundData>>) -> Result<Sound, SoundError> {
pub fn new_with_data(sound_data: Arc<Mutex<SoundData>>) -> Result<Sound, SoundError> {
check_openal_context!(Err(SoundError::InvalidOpenALContext));

let mut source_id = 0;
// create the source
al::alGenSources(1, &mut source_id);
// set the buffer
al::alSourcei(
source_id,
ffi::AL_BUFFER,
sound_data::get_buffer(&*sound_data.borrow_mut()) as i32,
);

{
// we are not expecting threads to ever fail while holding the lock, so we `unwrap()`
let sd = sound_data.lock().unwrap();
al::alSourcei(
source_id,
ffi::AL_BUFFER,
sound_data::get_buffer(&sd) as i32,
);
}
// Check if there is OpenAL internal error
if let Some(err) = al::openal_has_error() {
return Err(SoundError::InternalOpenALError(err));
};

Ok(Sound {
al_source: source_id,
sound_data: sound_data,
sound_data,
})
}

Expand All @@ -160,7 +161,7 @@ impl Sound {
* }
* ```
*/
pub fn get_datas(&self) -> Rc<RefCell<SoundData>> {
pub fn get_datas(&self) -> Arc<Mutex<SoundData>> {
self.sound_data.clone()
}

Expand All @@ -183,20 +184,23 @@ impl Sound {
* }
* ```
*/
pub fn set_datas(&mut self, sound_data: Rc<RefCell<SoundData>>) {
pub fn set_datas(&mut self, sound_data: Arc<Mutex<SoundData>>) {
check_openal_context!(());

if self.is_playing() {
return;
}

// set the buffer
al::alSourcei(
self.al_source,
ffi::AL_BUFFER,
sound_data::get_buffer(&*sound_data.borrow()) as i32,
);

{
// we are not expecting threads to ever fail while holding the lock, so we `unwrap()`
let sd = sound_data.lock().unwrap();
al::alSourcei(
self.al_source,
ffi::AL_BUFFER,
sound_data::get_buffer(&sd) as i32,
);
}
self.sound_data = sound_data
}

Expand Down Expand Up @@ -272,7 +276,9 @@ impl AudioTags for Sound {
* A borrowed pointer to the internal struct SoundTags
*/
fn get_tags(&self) -> Tags {
(*self.sound_data).borrow().get_tags().clone()
// we are not expecting threads to ever fail while holding the lock, so we `unwrap()`
let sound_data = self.sound_data.lock().unwrap();
sound_data.get_tags().clone()
}
}

Expand Down Expand Up @@ -909,7 +915,9 @@ impl AudioController for Sound {
* Returns the duration of the Sound.
*/
fn get_duration(&self) -> Duration {
let data = self.sound_data.borrow();
// we are not expecting threads to ever fail while holding the lock, so we `unwrap()`
let sound_data = self.sound_data.lock().unwrap();
let data = sound_data;
let snd_info = sound_data::get_sndinfo(&data);

let frames = snd_info.frames as u64;
Expand Down