Revert "Use FunDSP crate for beepboops"
This reverts commit 9219b6c475
.
This commit is contained in:
parent
9dcd94932f
commit
74d1c120fc
4 changed files with 63 additions and 133 deletions
|
@ -5,15 +5,12 @@ use bracket_lib::random::RandomNumberGenerator;
|
|||
use crate::resources::sound_output::{SoundOutput, SoundSamples};
|
||||
|
||||
type Frequency = u32;
|
||||
type StartFrequency = u32;
|
||||
type EndFrequency = u32;
|
||||
type MinFrequency = u32;
|
||||
type MaxFrequency = u32;
|
||||
|
||||
pub enum SoundType {
|
||||
Silence,
|
||||
Tone(Frequency),
|
||||
Sweep(StartFrequency, EndFrequency),
|
||||
Noise(MinFrequency, MaxFrequency),
|
||||
}
|
||||
|
||||
|
@ -49,10 +46,13 @@ impl SoundEffects {
|
|||
pub fn new(sound_output: &SoundOutput) -> Self {
|
||||
Self {
|
||||
startup: sound_output.render_sound_effect(&SoundEffect {
|
||||
sounds: vec![Sound {
|
||||
sound_type: SoundType::Sweep(1, 350),
|
||||
duration: Duration::from_secs(1),
|
||||
}],
|
||||
sounds: (30..400)
|
||||
.step_by(8)
|
||||
.map(|x| Sound {
|
||||
sound_type: SoundType::Tone(x),
|
||||
duration: Duration::from_millis(24),
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
step: sound_output.render_sound_effect(&SoundEffect {
|
||||
sounds: vec![
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use std::sync::Arc;
|
||||
use std::{f32::consts::PI, sync::Arc};
|
||||
|
||||
use cpal::{
|
||||
traits::{DeviceTrait, HostTrait, StreamTrait},
|
||||
SampleRate, Stream,
|
||||
};
|
||||
use fundsp::prelude::*;
|
||||
use oddio::{Frames, FramesSignal, Gain, Handle, Mixer, MonoToStereo, Stop};
|
||||
use rand::Rng;
|
||||
|
||||
use super::sound_effects::{SoundEffect, SoundType};
|
||||
|
||||
|
@ -70,30 +70,43 @@ impl SoundOutput {
|
|||
.map(|_| 0f32)
|
||||
.collect::<Vec<f32>>(),
|
||||
SoundType::Tone(freq) => {
|
||||
let mut c = square_hz(freq as f32);
|
||||
c.reset(Some(self.sample_rate.0 as f64));
|
||||
if freq == 0 {
|
||||
return (0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32())
|
||||
as usize)
|
||||
.map(|_| 0f32)
|
||||
.collect::<Vec<f32>>();
|
||||
}
|
||||
let num_harmonics = self.sample_rate.0 / (freq as u32 * 2);
|
||||
let coefficients = (0..=num_harmonics)
|
||||
.map(|i| {
|
||||
if i == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
(i as f32 * 0.5 * PI).sin() * 2.0 / (i as f32 * PI)
|
||||
})
|
||||
.collect::<Vec<f32>>();
|
||||
let scaler = freq as f32 * PI * 2.0 / self.sample_rate.0 as f32;
|
||||
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
|
||||
.map(|_| c.get_mono())
|
||||
.collect::<Vec<f32>>()
|
||||
}
|
||||
SoundType::Sweep(start_freq, end_freq) => {
|
||||
let mut c = lfo(|t| {
|
||||
lerp(
|
||||
start_freq as f32,
|
||||
end_freq as f32,
|
||||
t * sound.duration.as_secs_f32(),
|
||||
)
|
||||
}) >> square();
|
||||
c.reset(Some(self.sample_rate.0 as f64));
|
||||
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
|
||||
.map(|_| c.get_mono())
|
||||
.map(|i| {
|
||||
let temp = scaler * i as f32;
|
||||
coefficients
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(j, coef)| coef * (j as f32 * temp).cos())
|
||||
.sum::<f32>()
|
||||
})
|
||||
.collect::<Vec<f32>>()
|
||||
}
|
||||
SoundType::Noise(min, max) => {
|
||||
let mut c =
|
||||
(((white() + dc(1.0)) * dc(max as f32 / 2.0)) + dc(min as f32)) >> square();
|
||||
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
|
||||
.map(|_| c.get_mono())
|
||||
.map(|i| {
|
||||
let t = i as f32 / self.sample_rate.0 as f32;
|
||||
(t * (rand::thread_rng().gen_range(min as f32..max as f32))
|
||||
* 2.0
|
||||
* std::f32::consts::PI)
|
||||
.sin()
|
||||
.signum()
|
||||
})
|
||||
.collect::<Vec<f32>>()
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue