Skip to content

Commit

Permalink
more audio runtime disable feature dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidster committed Apr 27, 2024
1 parent 5d1e88f commit 1484647
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = ["ikari", "build_web", "example_game", "clikari"]
[workspace.package]
version = "0.0.1"
edition = "2021"
rust-version = "1.66"
rust-version = "1.75"
description = "3D Game Engine"
keywords = ["renderer", "3D", "rust", "wgpu", "game"]
license = "MIT"
Expand Down
3 changes: 1 addition & 2 deletions example_game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ name = "example_game"
path = "src/main.rs"

[features]
default = ["cpu-profiling", "gpu-profiling"]
# default = ["cpu-profiling", "gpu-profiling", "audio"]
default = ["cpu-profiling", "gpu-profiling", "audio"]
cpu-profiling = [
"ikari/tracy-profiling",
"profiling/profile-with-tracy",
Expand Down
3 changes: 1 addition & 2 deletions ikari/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ path = "src/lib.rs"

[features]
# TODO: exclude all audio-related deps when audio feature is not set!
default = []
# default = ["audio"]
default = ["audio"]
tracy-profiling = ["profiling/profile-with-tracy"]
gpu-profiling = []
audio = []
Expand Down
10 changes: 5 additions & 5 deletions ikari/src/asset_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::audio::AudioFileStreamer;
use crate::audio::AudioManager;
use crate::audio::IkariAudioManager;
use crate::audio::SoundParams;
use crate::audio::AUDIO_STREAM_BUFFER_LENGTH_SECONDS;
use crate::buffer::GpuBuffer;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct AssetLoader {
pending_audio: Arc<Mutex<Vec<(AssetId, AudioAssetLoadParams)>>>,
pub loaded_audio: Arc<Mutex<HashMap<AssetId, usize>>>,

audio_manager: Arc<Mutex<AudioManager>>,
audio_manager: Arc<Mutex<IkariAudioManager>>,
pending_scenes: Arc<Mutex<Vec<(AssetId, SceneAssetLoadParams)>>>,
bindable_scenes: Arc<Mutex<HashMap<AssetId, BindableScene>>>,

Expand Down Expand Up @@ -129,7 +129,7 @@ struct TimeSlicedSkyboxBinder {
}

impl AssetLoader {
pub fn new(audio_manager: Arc<Mutex<AudioManager>>) -> Self {
pub fn new(audio_manager: Arc<Mutex<IkariAudioManager>>) -> Self {
Self {
is_exiting: Arc::new(AtomicBool::new(false)),

Expand Down Expand Up @@ -239,7 +239,7 @@ impl AssetLoader {
Default::default()
};

let signal = AudioManager::get_signal(
let signal = IkariAudioManager::get_signal(
&sound_data,
next_audio_params.sound_params.clone(),
device_sample_rate,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl AssetLoader {

fn spawn_audio_streaming_thread(
is_exiting: Arc<AtomicBool>,
audio_manager: Arc<Mutex<AudioManager>>,
audio_manager: Arc<Mutex<IkariAudioManager>>,
sound_index: usize,
mut audio_file_streamer: AudioFileStreamer,
) {
Expand Down
175 changes: 107 additions & 68 deletions ikari/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ pub struct SoundParams {

pub const AUDIO_STREAM_BUFFER_LENGTH_SECONDS: f32 = 2.5;

pub trait AudioStreams {}
const CHANNEL_COUNT: usize = 2;

#[derive(Debug, Clone, Default)]
pub struct SoundData(Vec<[f32; CHANNEL_COUNT]>);

impl SoundData {
pub fn sample_count(&self) -> usize {
self.0.len()
}
}

mod ikari_audio {
use crate::file_manager::GameFilePath;
use crate::time::Instant;
use crate::{audio::CHANNEL_COUNT, file_manager::GameFilePath};

use anyhow::Result;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
Expand All @@ -38,9 +47,7 @@ mod ikari_audio {
_mixer_output_stream: cpal::Stream,
}

impl AudioStreams for IkariAudioStreams {}

pub struct AudioManager {
pub struct IkariAudioManager {
master_volume: f32,
device_sample_rate: u32,

Expand All @@ -49,17 +56,6 @@ mod ikari_audio {
sounds: Vec<Option<Sound>>,
}

const CHANNEL_COUNT: usize = 2;

#[derive(Debug, Clone, Default)]
pub struct SoundData(Vec<[f32; CHANNEL_COUNT]>);

impl SoundData {
pub fn sample_count(&self) -> usize {
self.0.len()
}
}

pub struct Sound {
volume: f32,
is_playing: bool,
Expand Down Expand Up @@ -145,7 +141,7 @@ mod ikari_audio {
#[cfg(target_arch = "wasm32")]
use web::get_media_source;

use super::{AudioStreams, SoundParams, SpacialParams, AUDIO_STREAM_BUFFER_LENGTH_SECONDS};
use super::{SoundData, SoundParams, SpacialParams, AUDIO_STREAM_BUFFER_LENGTH_SECONDS};

pub struct AudioFileStreamer {
device_sample_rate: u32,
Expand Down Expand Up @@ -321,9 +317,9 @@ mod ikari_audio {
}
}

impl AudioManager {
impl IkariAudioManager {
/// streams are returned separately since they aren't Send/Sync
pub fn new() -> Result<(AudioManager, Box<dyn AudioStreams>)> {
pub fn new() -> Result<(IkariAudioManager, IkariAudioStreams)> {
let host = cpal::default_host();
let device = host
.default_output_device()
Expand Down Expand Up @@ -365,18 +361,18 @@ mod ikari_audio {
mixer_output_stream.play()?;

Ok((
AudioManager {
IkariAudioManager {
master_volume: 1.0,
device_sample_rate,

spatial_scene_handle,
mixer_handle,
sounds: vec![],
},
Box::new(IkariAudioStreams {
IkariAudioStreams {
_spatial_scene_output_stream: spatial_scene_output_stream,
_mixer_output_stream: mixer_output_stream,
}),
},
))
}

Expand Down Expand Up @@ -490,7 +486,7 @@ mod ikari_audio {
.map(|sound| &sound.file_path)
}

pub fn _set_sound_volume(&mut self, sound_index: usize, volume: f32) {
pub fn set_sound_volume(&mut self, sound_index: usize, volume: f32) {
if let Some(sound) = self.sounds[sound_index].as_mut() {
sound.set_volume(self.master_volume, volume)
}
Expand Down Expand Up @@ -546,7 +542,7 @@ mod ikari_audio {

impl Sound {
fn new(
audio_manager: &mut AudioManager,
audio_manager: &mut IkariAudioManager,
file_path: GameFilePath,
sound_data: SoundData,
length_seconds: Option<f32>,
Expand Down Expand Up @@ -759,70 +755,105 @@ mod ikari_audio {
}
}

mod null_audio {
use crate::file_manager::GameFilePath;
use anyhow::Result;
pub trait AudioManager {
type SoundSignal;

use super::{AudioStreams, SoundParams};
fn get_signal(
_sound_data: &SoundData,
_params: SoundParams,
_device_sample_rate: u32,
) -> Option<Self::SoundSignal>;

pub struct NullAudioStreams {}
fn add_sound(
&mut self,
_file_path: GameFilePath,
_sound_data: SoundData,
_length_seconds: Option<f32>,
_params: SoundParams,
_signal: Option<Self::SoundSignal>,
) -> usize;

impl AudioStreams for NullAudioStreams {}
fn write_stream_data(&mut self, sound_index: usize, sound_data: SoundData);

pub struct AudioManager {}
fn play_sound(&mut self, sound_index: usize);

#[derive(Debug, Clone, Default)]
pub struct SoundData();
fn reload_sound(&mut self, _sound_index: usize, _params: SoundParams);

pub struct Sound {}
fn sound_is_playing(&self, sound_index: usize) -> bool;

pub struct AudioFileStreamer {
fn get_sound_length_seconds(&self, sound_index: usize) -> Option<Option<f32>>;
fn get_sound_pos_seconds(&self, sound_index: usize) -> Option<f32>;

fn get_sound_buffered_to_pos_seconds(&self, sound_index: usize) -> Option<f32>;

fn get_sound_file_path(&self, sound_index: usize) -> Option<&GameFilePath>;

fn set_sound_volume(&mut self, sound_index: usize, volume: f32);

fn device_sample_rate(&self) -> u32;

fn sound_indices(&self) -> impl Iterator<Item = usize> + '_;
}

pub trait AudioFileStreamer {
fn read_chunk(&mut self, _max_chunk_size: usize) -> anyhow::Result<(SoundData, bool)>;

fn track_length_seconds(&self) -> Option<f32>;

fn file_path(&self) -> &GameFilePath;
}

mod null_audio {
use crate::file_manager::GameFilePath;
use anyhow::Result;

use super::{AudioFileStreamer, AudioManager, SoundData, SoundParams};

pub struct AudioStreams {}

pub struct NullAudioManager {}

pub struct NullAudioFileStreamer {
file_path: GameFilePath,
}

pub struct SoundSignal {}

impl SoundData {
pub fn sample_count(&self) -> usize {
0
impl AudioFileStreamer for NullAudioFileStreamer {
fn read_chunk(&mut self, _max_chunk_size: usize) -> anyhow::Result<(SoundData, bool)> {
Ok((SoundData(vec![]), true))
}

fn track_length_seconds(&self) -> Option<f32> {
None
}

fn file_path(&self) -> &GameFilePath {
&self.file_path
}
}

impl AudioFileStreamer {
impl NullAudioFileStreamer {
pub async fn new(
_device_sample_rate: u32,
file_path: GameFilePath,
) -> anyhow::Result<Self> {
Ok(Self { file_path })
}

pub fn read_chunk(&mut self, _max_chunk_size: usize) -> anyhow::Result<(SoundData, bool)> {
Ok((SoundData(), true))
}

pub fn track_length_seconds(&self) -> Option<f32> {
None
}

pub fn file_path(&self) -> &GameFilePath {
&self.file_path
}
}

impl AudioManager {
pub fn new() -> Result<(AudioManager, NullAudioStreams)> {
Ok((AudioManager {}, NullAudioStreams {}))
}
impl AudioManager for NullAudioManager {
type SoundSignal = SoundSignal;

pub fn get_signal(
fn get_signal(
_sound_data: &SoundData,
_params: SoundParams,
_device_sample_rate: u32,
) -> Option<SoundSignal> {
None
}

pub fn add_sound(
fn add_sound(
&mut self,
_file_path: GameFilePath,
_sound_data: SoundData,
Expand All @@ -833,46 +864,54 @@ mod null_audio {
0
}

pub fn write_stream_data(&mut self, sound_index: usize, sound_data: SoundData) {}
fn write_stream_data(&mut self, sound_index: usize, sound_data: SoundData) {}

pub fn play_sound(&mut self, sound_index: usize) {}
fn play_sound(&mut self, sound_index: usize) {}

pub fn reload_sound(&mut self, _sound_index: usize, _params: SoundParams) {}
fn reload_sound(&mut self, _sound_index: usize, _params: SoundParams) {}

pub fn sound_is_playing(&self, sound_index: usize) -> bool {
fn sound_is_playing(&self, sound_index: usize) -> bool {
false
}

pub fn get_sound_length_seconds(&self, sound_index: usize) -> Option<Option<f32>> {
fn get_sound_length_seconds(&self, sound_index: usize) -> Option<Option<f32>> {
None
}

pub fn get_sound_pos_seconds(&self, sound_index: usize) -> Option<f32> {
fn get_sound_pos_seconds(&self, sound_index: usize) -> Option<f32> {
None
}

pub fn get_sound_buffered_to_pos_seconds(&self, sound_index: usize) -> Option<f32> {
fn get_sound_buffered_to_pos_seconds(&self, sound_index: usize) -> Option<f32> {
None
}

pub fn get_sound_file_path(&self, sound_index: usize) -> Option<&GameFilePath> {
fn get_sound_file_path(&self, sound_index: usize) -> Option<&GameFilePath> {
None
}

pub fn _set_sound_volume(&mut self, sound_index: usize, volume: f32) {}
fn set_sound_volume(&mut self, sound_index: usize, volume: f32) {}

pub fn device_sample_rate(&self) -> u32 {
fn device_sample_rate(&self) -> u32 {
1
}

pub fn sound_indices(&self) -> impl Iterator<Item = usize> + '_ {
fn sound_indices(&self) -> impl Iterator<Item = usize> + '_ {
std::iter::empty()
}
}

impl NullAudioManager {
pub fn new() -> Result<(NullAudioManager, AudioStreams)> {
Ok((NullAudioManager {}, AudioStreams {}))
}
}
}

#[cfg(feature = "audio")]
pub use ikari_audio::*;

#[cfg(not(feature = "audio"))]
pub use null_audio::*;

use crate::file_manager::GameFilePath;
Loading

0 comments on commit 1484647

Please sign in to comment.