aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-04-29 00:48:05 -0600
committerMelody Horn <melody@boringcactus.com>2021-04-29 00:48:05 -0600
commit33efd41a3da31c3a1d6f2ab4855147ccf576f42c (patch)
tree9e522b27b3ec8a8fa5da0bc91bf9b164232817e4 /src
parentb805af0fdcbda54df657ca9e53a94a04e8b9e2dd (diff)
downloadsynthfinity-33efd41a3da31c3a1d6f2ab4855147ccf576f42c.tar.gz
synthfinity-33efd41a3da31c3a1d6f2ab4855147ccf576f42c.zip
loop infinitely, randomizing frequency and duration
Diffstat (limited to 'src')
-rw-r--r--src/main.rs28
1 files changed, 19 insertions, 9 deletions
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<f32> = 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();
+ }
}