diff --git a/src/main.rs b/src/main.rs index ffc6cd0..fd941c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use components::{ Monster, Player, Position, Renderable, }; use resources::map::TileType; -use resources::{Clock, LevelNumber, Map, ShowDebugInfo, SoundEffects, SoundSystem, Stats}; +use resources::{Clock, LevelNumber, Map, ShowDebugInfo, SoundEffects, SoundOutput, Stats}; use specs::prelude::*; use state::State; use std::time::Instant; @@ -28,7 +28,7 @@ fn main() -> BError { .with_tile_dimensions(8, 16) .build()?; - let mut ss = SoundSystem::new(); + let mut ss = SoundOutput::new(); let sound_effects = SoundEffects::new(&ss); ss.play_sound(sound_effects.startup.clone()); diff --git a/src/resources/mod.rs b/src/resources/mod.rs index bc444d3..f497725 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -1,10 +1,10 @@ pub mod map; pub mod sound_effects; -pub mod sound_system; +pub mod sound_output; pub use map::Map; pub use sound_effects::SoundEffects; -pub use sound_system::SoundSystem; +pub use sound_output::SoundOutput; use crate::constants::CLOCK_PERIOD; use std::time::{Duration, Instant}; diff --git a/src/resources/sound_effects.rs b/src/resources/sound_effects.rs index d704ee2..9fcb9df 100644 --- a/src/resources/sound_effects.rs +++ b/src/resources/sound_effects.rs @@ -2,21 +2,38 @@ use std::{array, iter, time::Duration}; use bracket_lib::random::RandomNumberGenerator; -use crate::resources::sound_system::{ - Sound, SoundEffect, SoundEffectSamples, SoundSystem, SoundType, -}; +use crate::resources::sound_output::{SoundOutput, SoundSamples}; + +type Frequency = u32; +type MinFrequency = u32; +type MaxFrequency = u32; + +pub enum SoundType { + Silence, + Tone(Frequency), + Noise(MinFrequency, MaxFrequency), +} + +pub struct Sound { + pub sound_type: SoundType, + pub duration: Duration, +} + +pub struct SoundEffect { + pub sounds: Vec, +} pub struct SoundEffects { - pub startup: SoundEffectSamples, - pub step: SoundEffectSamples, - pub pickup: SoundEffectSamples, - pub bad_key: SoundEffectSamples, - pub blocked: SoundEffectSamples, + pub startup: SoundSamples, + pub step: SoundSamples, + pub pickup: SoundSamples, + pub bad_key: SoundSamples, + pub blocked: SoundSamples, rng: RandomNumberGenerator, } impl SoundEffects { - pub fn new(ss: &SoundSystem) -> Self { + pub fn new(ss: &SoundOutput) -> Self { Self { startup: ss.render_sound_effect(&SoundEffect { sounds: (30..400) @@ -92,7 +109,7 @@ impl SoundEffects { } } - pub fn get_new_static_effect(&mut self, ss: &SoundSystem) -> SoundEffectSamples { + pub fn get_new_static_effect(&mut self, ss: &SoundOutput) -> SoundSamples { ss.render_sound_effect(&SoundEffect { sounds: (1..=33) .map(|_| { diff --git a/src/resources/sound_system.rs b/src/resources/sound_output.rs similarity index 87% rename from src/resources/sound_system.rs rename to src/resources/sound_output.rs index 73cf1b8..d951719 100644 --- a/src/resources/sound_system.rs +++ b/src/resources/sound_output.rs @@ -1,4 +1,4 @@ -use std::{f32::consts::PI, sync::Arc, time::Duration}; +use std::{f32::consts::PI, sync::Arc}; use cpal::{ traits::{DeviceTrait, HostTrait, StreamTrait}, @@ -7,40 +7,23 @@ use cpal::{ use oddio::{Frames, Handle, Mixer}; use rand::Rng; -type Frequency = u32; -type MinFrequency = u32; -type MaxFrequency = u32; +use super::sound_effects::{SoundEffect, SoundType}; -pub type SoundEffectSamples = Arc>; +pub type SoundSamples = Arc>; -pub enum SoundType { - Silence, - Tone(Frequency), - Noise(MinFrequency, MaxFrequency), -} - -pub struct Sound { - pub sound_type: SoundType, - pub duration: Duration, -} - -pub struct SoundEffect { - pub sounds: Vec, -} - -pub struct SoundSystem { +pub struct SoundOutput { mixer_handle: Handle>, sample_rate: SampleRate, _stream: Stream, } -impl Default for SoundSystem { +impl Default for SoundOutput { fn default() -> Self { Self::new() } } -impl SoundSystem { +impl SoundOutput { pub fn new() -> Self { let host = cpal::default_host(); let device = host @@ -76,7 +59,7 @@ impl SoundSystem { } } - pub fn render_sound_effect(&self, effect: &SoundEffect) -> SoundEffectSamples { + pub fn render_sound_effect(&self, effect: &SoundEffect) -> SoundSamples { let effect_buffer: Vec = effect .sounds .iter() @@ -131,7 +114,7 @@ impl SoundSystem { oddio::Frames::from_iter(self.sample_rate.0, effect_buffer.iter().copied()) } - pub fn play_sound(&mut self, samples: SoundEffectSamples) { + pub fn play_sound(&mut self, samples: SoundSamples) { self.mixer_handle .control::, _>() .play(oddio::MonoToStereo::new(oddio::Gain::new( diff --git a/src/state.rs b/src/state.rs index dc26c9c..eeea126 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; use crate::components::monster::damage_for_kind; use crate::components::{Monster, Player, Position, Renderable}; use crate::resources::map::TileType; -use crate::resources::{Clock, Map, ShowDebugInfo, SoundEffects, SoundSystem, Stats}; +use crate::resources::{Clock, Map, ShowDebugInfo, SoundEffects, SoundOutput, Stats}; use crate::systems::MonsterMotion; use crate::{constants::*, sidebar}; use bracket_lib::prelude::*; @@ -67,7 +67,7 @@ impl State { } VirtualKeyCode::N | VirtualKeyCode::End => self.try_move_player(-1, 1), VirtualKeyCode::S => { - let mut sound_system = self.ecs.write_resource::(); + let mut sound_system = self.ecs.write_resource::(); let sound_effects = self.ecs.fetch::(); sound_system.play_sound(sound_effects.pickup.clone()); } @@ -79,7 +79,7 @@ impl State { ctx.quit(); } _ => { - let mut sound_system = self.ecs.write_resource::(); + let mut sound_system = self.ecs.write_resource::(); let sound_effects = self.ecs.fetch::(); sound_system.play_sound(sound_effects.bad_key.clone()); } @@ -93,7 +93,7 @@ impl State { let mut players = self.ecs.write_storage::(); let mut map = self.ecs.write_resource::(); let mut stats = self.ecs.write_resource::(); - let mut sound_system = self.ecs.write_resource::(); + let mut sound_system = self.ecs.write_resource::(); for (player, pos) in (&mut players, &mut positions).join() { let now = Instant::now();