blob: ae044ae6a30bc3d28527deedf010125942645fff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
use std::time::Duration;
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();
let bpm = 131.2;
let seconds_per_beat = (1.0 / bpm) * 60.0;
loop {
let mut rng = rand::thread_rng();
let freq = (440.0
* (2f32.powf(1.0 / 12.0)).powf(rng.gen_range((12 * -2)..=(12 * 0)) as f32))
as u32;
let wave = SineWave::new(freq);
let zero: Zero<f32> = Zero::new(1, 48_000);
let length = (rng.gen_range(1..4) as f32) / 4.0 * seconds_per_beat;
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();
}
}
|