Skip to content

Commit

Permalink
Tidy up AudioBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Apr 4, 2024
1 parent c5e424f commit 5363de5
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 50 deletions.
2 changes: 1 addition & 1 deletion common/src/concurrency/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<F: Future> BlockableFuture for F {
fn block(self) -> Self::Output {
let mut boxed = Box::pin(self);
let waker = std::task::Waker::noop();
let mut context = std::task::Context::from_waker(&waker);
let mut context = std::task::Context::from_waker(waker);

loop {
if let std::task::Poll::Ready(value) = boxed.as_mut().poll(&mut context) {
Expand Down
34 changes: 10 additions & 24 deletions common/src/diagnostics/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,11 @@ pub mod frames {
//! Frame rate telemetry.
use super::*;

pub struct FramesPerSecond {
pub frames: u32,
}

pub struct FrameTime {
pub time: std::time::Duration,
}

pub struct FrameTimeAverage {
pub time: std::time::Duration,
}

pub struct FrameTimeMinimum {
pub time: std::time::Duration,
}

pub struct FrameTimeMaximum {
pub time: std::time::Duration,
}
pub struct FramesPerSecond(pub u32);
pub struct FrameTime(pub std::time::Duration);
pub struct FrameTimeAverage(pub std::time::Duration);
pub struct FrameTimeMinimum(pub std::time::Duration);
pub struct FrameTimeMaximum(pub std::time::Duration);

impl_telemetry!(FramesPerSecond, "frames_per_second");
impl_telemetry!(FrameTime, "frame_time");
Expand All @@ -99,10 +85,10 @@ mod tests {
println!("telemetry: {}", telemetry.name());
});

recorder.record(&frames::FramesPerSecond { frames: 60 });
recorder.record(&frames::FramesPerSecond { frames: 60 });
recorder.record(&frames::FramesPerSecond { frames: 60 });
recorder.record(&frames::FramesPerSecond { frames: 59 });
recorder.record(&frames::FramesPerSecond { frames: 60 });
recorder.record(&frames::FramesPerSecond(60));
recorder.record(&frames::FramesPerSecond(60));
recorder.record(&frames::FramesPerSecond(60));
recorder.record(&frames::FramesPerSecond(59));
recorder.record(&frames::FramesPerSecond(60));
}
}
6 changes: 2 additions & 4 deletions common/src/maths/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
pub use csg::*;

mod csg;

// TODO: implement geometry helpers like ear cutting and triangulation

use super::{DVec2, DVec3, Vec2, Vec3, Vector};

mod csg;

/// A triangle in a vector space V.
#[repr(C)]
#[derive(Clone, Debug)]
Expand Down
51 changes: 46 additions & 5 deletions modules/audio/src/headless.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
use std::sync::atomic::{AtomicU64, Ordering};

use super::*;

/// A headless [`AudioBackend`] implementation.
///
/// This backend does nothing (no-ops) and can be used for testing/etc.
#[derive(Default)]
pub struct HeadlessAudioBackend {}
pub struct HeadlessAudioBackend {
next_clip_id: AtomicU64,
next_source_id: AtomicU64,
}

#[allow(unused_variables)]
impl AudioBackend for HeadlessAudioBackend {
fn new_audio_device(&self) -> Box<dyn AudioDevice> {
todo!()
fn clip_create(&self) -> Result<ClipId, ClipError> {
Ok(ClipId(self.next_clip_id.fetch_add(1, Ordering::Relaxed)))
}

fn clip_write_data(&self, clip: ClipId, data: *const u8, length: usize) -> Result<(), ClipError> {
Ok(())
}

fn clip_delete(&self, clip: ClipId) -> Result<(), ClipError> {
Ok(())
}

fn source_create(&self) -> Result<SourceId, SourceError> {
Ok(SourceId(self.next_source_id.fetch_add(1, Ordering::Relaxed)))
}

fn source_is_playing(&self, source: SourceId) -> bool {
false
}

fn source_get_volume(&self, source: SourceId) -> f32 {
1.
}

fn source_set_volume(&self, source: SourceId, volume: f32) {
// no-op
}

fn source_get_clip(&self, source: SourceId) -> Option<ClipId> {
None
}

fn source_set_clip(&self, source: SourceId, clip: ClipId) {
// no-op
}

fn source_play(&self, source: SourceId) {
// no-op
}

fn new_audio_recorder(&self) -> Box<dyn AudioRecorder> {
todo!()
fn source_delete(&self, source: SourceId) -> Result<(), SourceError> {
Ok(())
}
}
15 changes: 1 addition & 14 deletions modules/audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ pub enum SourceError {
/// This is a high-level abstraction that makes use of 'opaque' handles to hide
/// away implementation details. The server is intended to be a low-level
/// implementation abstraction.
pub trait AudioBackend {
fn new_audio_device(&self) -> Box<dyn AudioDevice>;
fn new_audio_recorder(&self) -> Box<dyn AudioRecorder>;
}

/// Represents an audio device.
///
/// A device is capable of playing audio clips via audio sources.
#[rustfmt::skip]
pub trait AudioDevice {
pub trait AudioBackend {
// clips
fn clip_create(&self) -> Result<ClipId, ClipError>;
fn clip_write_data(&self, clip: ClipId, data: *const u8, length: usize) -> Result<(), ClipError>;
Expand All @@ -66,8 +58,3 @@ pub trait AudioDevice {
fn source_play(&self, source: SourceId);
fn source_delete(&self, source: SourceId) -> Result<(), SourceError>;
}

/// Represents an audio recorder.
///
/// A recorder is capable of recording audio from the host.
pub trait AudioRecorder {}
53 changes: 51 additions & 2 deletions modules/audio/src/openal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The OpenAL backend implementation for the audio subsystem.
use common::profiling;

use super::*;

/// A OpenAL-based [`AudioBackend`] implementation.
Expand All @@ -20,11 +22,58 @@ impl Drop for OpenALAudioBackend {

#[allow(unused_variables)]
impl AudioBackend for OpenALAudioBackend {
fn new_audio_device(&self) -> Box<dyn AudioDevice> {
#[profiling]
fn clip_create(&self) -> Result<ClipId, ClipError> {
todo!()
}

#[profiling]
fn clip_write_data(&self, clip: ClipId, data: *const u8, length: usize) -> Result<(), ClipError> {
todo!()
}

#[profiling]
fn clip_delete(&self, clip: ClipId) -> Result<(), ClipError> {
todo!()
}

#[profiling]
fn source_create(&self) -> Result<SourceId, SourceError> {
todo!()
}

#[profiling]
fn source_is_playing(&self, source: SourceId) -> bool {
todo!()
}

#[profiling]
fn source_get_volume(&self, source: SourceId) -> f32 {
todo!()
}

#[profiling]
fn source_set_volume(&self, source: SourceId, volume: f32) {
todo!()
}

#[profiling]
fn source_get_clip(&self, source: SourceId) -> Option<ClipId> {
todo!()
}

#[profiling]
fn source_set_clip(&self, source: SourceId, clip: ClipId) {
todo!()
}

#[profiling]
fn source_play(&self, source: SourceId) {
todo!()
}

fn new_audio_recorder(&self) -> Box<dyn AudioRecorder> {
#[profiling]
fn source_delete(&self, source: SourceId) -> Result<(), SourceError> {
todo!()
}
}
Expand Down

0 comments on commit 5363de5

Please sign in to comment.