diff --git a/Cargo.lock b/Cargo.lock index 3c8ba5b..170b0d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -814,37 +814,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" -[[package]] -name = "fundsp" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "914385c9ea3226815d12f436c2c60b2e6f88a89af684ed9e24d62e55f1b0dba1" -dependencies = [ - "generic-array", - "lazy_static", - "num-complex", - "numeric-array", - "rsor", - "rustfft", - "tinyvec", -] - [[package]] name = "futures" version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" -[[package]] -name = "generic-array" -version = "0.14.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.4" @@ -1092,10 +1067,10 @@ dependencies = [ "bracket-lib", "console_error_panic_hook", "cpal", - "fundsp", "hecs", "instant", "oddio", + "rand", "specs-derive", "typenum", "wasm-bindgen", @@ -1549,15 +1524,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "num-complex" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085" -dependencies = [ - "num-traits", -] - [[package]] name = "num-derive" version = "0.3.3" @@ -1664,16 +1630,6 @@ dependencies = [ "syn", ] -[[package]] -name = "numeric-array" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c59a9b227913a685ed84aa0de5ded58274e8bcfa55760faae07bc82aee64ccf" -dependencies = [ - "generic-array", - "num-traits", -] - [[package]] name = "objc" version = "0.2.7" @@ -1833,13 +1789,10 @@ dependencies = [ ] [[package]] -name = "primal-check" -version = "0.3.1" +name = "ppv-lite86" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01419cee72c1a1ca944554e23d83e483e1bccf378753344e881de28b5487511d" -dependencies = [ - "num-integer", -] +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro-crate" @@ -1880,10 +1833,22 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", "rand_core", ] @@ -1892,6 +1857,9 @@ name = "rand_core" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] [[package]] name = "rand_xorshift" @@ -1966,12 +1934,6 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" -[[package]] -name = "rsor" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b57e3964dc31a38416366d2e8f7675755402a10832d5cf4e4112d66ac77cdda" - [[package]] name = "rustc-hash" version = "1.1.0" @@ -1987,20 +1949,6 @@ dependencies = [ "semver", ] -[[package]] -name = "rustfft" -version = "6.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1d089e5c57521629a59f5f39bca7434849ff89bd6873b521afe389c1c602543" -dependencies = [ - "num-complex", - "num-integer", - "num-traits", - "primal-check", - "strength_reduce", - "transpose", -] - [[package]] name = "rusttype" version = "0.9.2" @@ -2243,12 +2191,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" -[[package]] -name = "strength_reduce" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254" - [[package]] name = "strsim" version = "0.9.3" @@ -2292,21 +2234,6 @@ dependencies = [ "syn", ] -[[package]] -name = "tinyvec" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - [[package]] name = "tokio-executor" version = "0.1.10" @@ -2338,16 +2265,6 @@ dependencies = [ "serde", ] -[[package]] -name = "transpose" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95f9c900aa98b6ea43aee227fd680550cdec726526aab8ac801549eadb25e39f" -dependencies = [ - "num-integer", - "strength_reduce", -] - [[package]] name = "ttf-parser" version = "0.6.2" diff --git a/Cargo.toml b/Cargo.toml index 061fa0b..4d39a67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,11 @@ hecs = "0.9.0" specs-derive = "0.4.1" cpal = { version = "0.14.1", features = ["wasm-bindgen"] } oddio = "0.6.2" -fundsp = "0.1.0" typenum = "1.15.0" console_error_panic_hook = "0.1.7" wasm-bindgen = "0.2.79" instant = "0.1.12" +rand = "0.8.5" [target.'cfg(windows)'.build-dependencies] winres = "0.1.12" diff --git a/src/resources/sound_effects.rs b/src/resources/sound_effects.rs index 3b3be5e..7e512d5 100644 --- a/src/resources/sound_effects.rs +++ b/src/resources/sound_effects.rs @@ -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![ diff --git a/src/resources/sound_output.rs b/src/resources/sound_output.rs index 3d3eb6d..cdb96ff 100644 --- a/src/resources/sound_output.rs +++ b/src/resources/sound_output.rs @@ -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::>(), 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::>(); + } + 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::>(); + 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::>() - } - 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::() + }) .collect::>() } 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::>() } })