Reorganize sound code

This commit is contained in:
Alex Page 2022-01-27 17:22:14 -05:00
parent 6ce6470236
commit b48684201b
5 changed files with 43 additions and 43 deletions

View file

@ -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());

View file

@ -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};

View file

@ -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<Sound>,
}
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(|_| {

View file

@ -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<Frames<f32>>;
pub type SoundSamples = Arc<Frames<f32>>;
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<Sound>,
}
pub struct SoundSystem {
pub struct SoundOutput {
mixer_handle: Handle<Mixer<[f32; 2]>>,
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<f32> = 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::<oddio::Mixer<_>, _>()
.play(oddio::MonoToStereo::new(oddio::Gain::new(

View file

@ -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::<SoundSystem>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>();
let sound_effects = self.ecs.fetch::<SoundEffects>();
sound_system.play_sound(sound_effects.pickup.clone());
}
@ -79,7 +79,7 @@ impl State {
ctx.quit();
}
_ => {
let mut sound_system = self.ecs.write_resource::<SoundSystem>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>();
let sound_effects = self.ecs.fetch::<SoundEffects>();
sound_system.play_sound(sound_effects.bad_key.clone());
}
@ -93,7 +93,7 @@ impl State {
let mut players = self.ecs.write_storage::<Player>();
let mut map = self.ecs.write_resource::<Map>();
let mut stats = self.ecs.write_resource::<Stats>();
let mut sound_system = self.ecs.write_resource::<SoundSystem>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>();
for (player, pos) in (&mut players, &mut positions).join() {
let now = Instant::now();