Skip to content

Commit

Permalink
Sketch out some wrapper structs in audio
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Apr 5, 2024
1 parent dbb15cb commit ace3651
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
29 changes: 29 additions & 0 deletions modules/audio/src/clips.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use super::*;

pub struct AudioClip {
id: ClipId,
engine: AudioEngine,
}

impl AudioClip {
pub fn new(engine: &AudioEngine) -> Self {
Self {
id: engine.clip_create().unwrap(),
engine: engine.clone(),
}
}

pub fn id(&self) -> ClipId {
self.id
}

pub fn write_data(&self, data: &[u8]) {
self.engine.clip_write_data(self.id, data.as_ptr(), data.len()).unwrap();
}
}

impl Drop for AudioClip {
fn drop(&mut self) {
self.engine.clip_delete(self.id).unwrap();
}
}
4 changes: 4 additions & 0 deletions modules/audio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//! Audio engine for Surreal.
pub use clips::*;
pub use sampling::*;
pub use sources::*;

mod clips;
mod headless;
mod openal;
mod sampling;
mod sources;

common::impl_arena_index!(ClipId, "Identifies an Audio Clip.");
common::impl_arena_index!(SourceId, "Identifies an Audio Source.");
Expand Down
42 changes: 42 additions & 0 deletions modules/audio/src/sources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::*;

pub struct AudioSource {
id: SourceId,
engine: AudioEngine,
}

impl AudioSource {
pub fn new(engine: &AudioEngine) -> Self {
Self {
id: engine.source_create().unwrap(),
engine: engine.clone(),
}
}

pub fn id(&self) -> SourceId {
self.id
}

pub fn is_playing(&self) -> bool {
self.engine.source_is_playing(self.id).unwrap_or_default()
}

pub fn volume(&self) -> f32 {
self.engine.source_get_volume(self.id).unwrap_or_default()
}

pub fn set_volume(&mut self, volume: f32) {
self.engine.source_set_volume(self.id, volume).unwrap();
}

pub fn play(&self, clip: &AudioClip) {
self.engine.source_set_clip(self.id, clip.id()).unwrap();
self.engine.source_play(self.id).unwrap()
}
}

impl Drop for AudioSource {
fn drop(&mut self) {
self.engine.source_delete(self.id).unwrap();
}
}
12 changes: 6 additions & 6 deletions modules/physics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#![allow(dead_code)]

use common::Vec2;
pub use bodies::*;
pub use colliders::*;
pub use effectors::*;
pub use joints::*;
pub use materials::*;

mod bodies;
mod colliders;
Expand All @@ -11,11 +15,7 @@ mod internal;
mod joints;
mod materials;

pub use bodies::*;
pub use colliders::*;
pub use effectors::*;
pub use joints::*;
pub use materials::*;
use common::Vec2;

common::impl_arena_index!(ColliderId, "Identifies a collider.");
common::impl_arena_index!(BodyId, "Identifies a physics body.");
Expand Down

0 comments on commit ace3651

Please sign in to comment.