Reorganize sound code
This commit is contained in:
parent
6ce6470236
commit
b48684201b
5 changed files with 43 additions and 43 deletions
|
@ -14,7 +14,7 @@ use components::{
|
||||||
Monster, Player, Position, Renderable,
|
Monster, Player, Position, Renderable,
|
||||||
};
|
};
|
||||||
use resources::map::TileType;
|
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 specs::prelude::*;
|
||||||
use state::State;
|
use state::State;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
@ -28,7 +28,7 @@ fn main() -> BError {
|
||||||
.with_tile_dimensions(8, 16)
|
.with_tile_dimensions(8, 16)
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
let mut ss = SoundSystem::new();
|
let mut ss = SoundOutput::new();
|
||||||
let sound_effects = SoundEffects::new(&ss);
|
let sound_effects = SoundEffects::new(&ss);
|
||||||
ss.play_sound(sound_effects.startup.clone());
|
ss.play_sound(sound_effects.startup.clone());
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod sound_effects;
|
pub mod sound_effects;
|
||||||
pub mod sound_system;
|
pub mod sound_output;
|
||||||
|
|
||||||
pub use map::Map;
|
pub use map::Map;
|
||||||
pub use sound_effects::SoundEffects;
|
pub use sound_effects::SoundEffects;
|
||||||
pub use sound_system::SoundSystem;
|
pub use sound_output::SoundOutput;
|
||||||
|
|
||||||
use crate::constants::CLOCK_PERIOD;
|
use crate::constants::CLOCK_PERIOD;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
|
@ -2,21 +2,38 @@ use std::{array, iter, time::Duration};
|
||||||
|
|
||||||
use bracket_lib::random::RandomNumberGenerator;
|
use bracket_lib::random::RandomNumberGenerator;
|
||||||
|
|
||||||
use crate::resources::sound_system::{
|
use crate::resources::sound_output::{SoundOutput, SoundSamples};
|
||||||
Sound, SoundEffect, SoundEffectSamples, SoundSystem, SoundType,
|
|
||||||
};
|
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 struct SoundEffects {
|
||||||
pub startup: SoundEffectSamples,
|
pub startup: SoundSamples,
|
||||||
pub step: SoundEffectSamples,
|
pub step: SoundSamples,
|
||||||
pub pickup: SoundEffectSamples,
|
pub pickup: SoundSamples,
|
||||||
pub bad_key: SoundEffectSamples,
|
pub bad_key: SoundSamples,
|
||||||
pub blocked: SoundEffectSamples,
|
pub blocked: SoundSamples,
|
||||||
rng: RandomNumberGenerator,
|
rng: RandomNumberGenerator,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SoundEffects {
|
impl SoundEffects {
|
||||||
pub fn new(ss: &SoundSystem) -> Self {
|
pub fn new(ss: &SoundOutput) -> Self {
|
||||||
Self {
|
Self {
|
||||||
startup: ss.render_sound_effect(&SoundEffect {
|
startup: ss.render_sound_effect(&SoundEffect {
|
||||||
sounds: (30..400)
|
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 {
|
ss.render_sound_effect(&SoundEffect {
|
||||||
sounds: (1..=33)
|
sounds: (1..=33)
|
||||||
.map(|_| {
|
.map(|_| {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{f32::consts::PI, sync::Arc, time::Duration};
|
use std::{f32::consts::PI, sync::Arc};
|
||||||
|
|
||||||
use cpal::{
|
use cpal::{
|
||||||
traits::{DeviceTrait, HostTrait, StreamTrait},
|
traits::{DeviceTrait, HostTrait, StreamTrait},
|
||||||
|
@ -7,40 +7,23 @@ use cpal::{
|
||||||
use oddio::{Frames, Handle, Mixer};
|
use oddio::{Frames, Handle, Mixer};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
type Frequency = u32;
|
use super::sound_effects::{SoundEffect, SoundType};
|
||||||
type MinFrequency = u32;
|
|
||||||
type MaxFrequency = u32;
|
|
||||||
|
|
||||||
pub type SoundEffectSamples = Arc<Frames<f32>>;
|
pub type SoundSamples = Arc<Frames<f32>>;
|
||||||
|
|
||||||
pub enum SoundType {
|
pub struct SoundOutput {
|
||||||
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 {
|
|
||||||
mixer_handle: Handle<Mixer<[f32; 2]>>,
|
mixer_handle: Handle<Mixer<[f32; 2]>>,
|
||||||
sample_rate: SampleRate,
|
sample_rate: SampleRate,
|
||||||
_stream: Stream,
|
_stream: Stream,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for SoundSystem {
|
impl Default for SoundOutput {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SoundSystem {
|
impl SoundOutput {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let host = cpal::default_host();
|
let host = cpal::default_host();
|
||||||
let device = 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
|
let effect_buffer: Vec<f32> = effect
|
||||||
.sounds
|
.sounds
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -131,7 +114,7 @@ impl SoundSystem {
|
||||||
oddio::Frames::from_iter(self.sample_rate.0, effect_buffer.iter().copied())
|
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
|
self.mixer_handle
|
||||||
.control::<oddio::Mixer<_>, _>()
|
.control::<oddio::Mixer<_>, _>()
|
||||||
.play(oddio::MonoToStereo::new(oddio::Gain::new(
|
.play(oddio::MonoToStereo::new(oddio::Gain::new(
|
|
@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
|
||||||
use crate::components::monster::damage_for_kind;
|
use crate::components::monster::damage_for_kind;
|
||||||
use crate::components::{Monster, Player, Position, Renderable};
|
use crate::components::{Monster, Player, Position, Renderable};
|
||||||
use crate::resources::map::TileType;
|
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::systems::MonsterMotion;
|
||||||
use crate::{constants::*, sidebar};
|
use crate::{constants::*, sidebar};
|
||||||
use bracket_lib::prelude::*;
|
use bracket_lib::prelude::*;
|
||||||
|
@ -67,7 +67,7 @@ impl State {
|
||||||
}
|
}
|
||||||
VirtualKeyCode::N | VirtualKeyCode::End => self.try_move_player(-1, 1),
|
VirtualKeyCode::N | VirtualKeyCode::End => self.try_move_player(-1, 1),
|
||||||
VirtualKeyCode::S => {
|
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>();
|
let sound_effects = self.ecs.fetch::<SoundEffects>();
|
||||||
sound_system.play_sound(sound_effects.pickup.clone());
|
sound_system.play_sound(sound_effects.pickup.clone());
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ impl State {
|
||||||
ctx.quit();
|
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>();
|
let sound_effects = self.ecs.fetch::<SoundEffects>();
|
||||||
sound_system.play_sound(sound_effects.bad_key.clone());
|
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 players = self.ecs.write_storage::<Player>();
|
||||||
let mut map = self.ecs.write_resource::<Map>();
|
let mut map = self.ecs.write_resource::<Map>();
|
||||||
let mut stats = self.ecs.write_resource::<Stats>();
|
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() {
|
for (player, pos) in (&mut players, &mut positions).join() {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
Loading…
Add table
Reference in a new issue