From 33efd41a3da31c3a1d6f2ab4855147ccf576f42c Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 29 Apr 2021 00:48:05 -0600 Subject: loop infinitely, randomizing frequency and duration --- Cargo.lock | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 28 ++++++++++++++++++--------- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b45d78..a98976b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -230,6 +230,17 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.0" @@ -565,6 +576,12 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro-crate" version = "0.1.5" @@ -592,6 +609,46 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + [[package]] name = "redox_syscall" version = "0.2.7" @@ -706,6 +763,7 @@ dependencies = [ name = "synthfinity" version = "0.1.0" dependencies = [ + "rand", "rodio", ] @@ -776,6 +834,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "wasm-bindgen" version = "0.2.73" diff --git a/Cargo.toml b/Cargo.toml index 717971b..95cdc17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +rand = "0.8.3" rodio = "0.13.1" diff --git a/src/main.rs b/src/main.rs index 8a3395a..6b79749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,28 @@ use std::time::Duration; -use rodio::{source::SineWave, OutputStream, Sink, Source}; +use rand::Rng; +use rodio::{ + source::{SineWave, Zero}, + OutputStream, Sink, Source, +}; fn main() { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap(); - // Add a dummy source of the sake of the example. - let source = SineWave::new(440) - .take_duration(Duration::from_secs_f32(0.25)) - .amplify(0.20); - sink.append(source); + loop { + let mut rng = rand::thread_rng(); + let freq = rng.gen_range((440 / 4)..(440 * 4)); + let wave = SineWave::new(freq); + let zero: Zero = Zero::new(1, 48_000); + let length = rng.gen_range(0.1..0.3); + let source = wave + .take_crossfade_with(zero, Duration::from_secs_f32(length)) + .amplify(0.20); + sink.append(source); - // The sound plays in a separate thread. This call will block the current thread until the sink - // has finished playing all its queued sounds. - sink.sleep_until_end(); + // The sound plays in a separate thread. This call will block the current thread until the sink + // has finished playing all its queued sounds. + sink.sleep_until_end(); + } } -- cgit v1.2.3