-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathdistortion_wav_alternate.rs
More file actions
49 lines (40 loc) · 1.67 KB
/
distortion_wav_alternate.rs
File metadata and controls
49 lines (40 loc) · 1.67 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::error::Error;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::thread;
use std::time::Duration;
use rodio::Source;
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OsSinkBuilder::open_default_sink()?;
let player = rodio::Player::connect_new(stream_handle.mixer());
let file = std::fs::File::open("assets/music.wav")?;
let source = rodio::Decoder::try_from(file)?;
// Shared flag to enable/disable distortion
let distortion_enabled = Arc::new(AtomicBool::new(true));
let distortion_enabled_clone = distortion_enabled.clone();
// Apply distortion and alternate the effect during playback
let distorted =
source
.distortion(4.0, 0.3)
.periodic_access(Duration::from_millis(250), move |src| {
// src is &mut PeriodicAccess<Distortion<Decoder<...>>>
let enable = distortion_enabled_clone.load(Ordering::Relaxed);
// Call the setters on the distortion filter inside the source
src.set_gain(if enable { 4.0 } else { 1.0 });
src.set_threshold(if enable { 0.3 } else { 1.0 });
});
player.append(distorted);
println!("Playing music.wav with alternating distortion effect...");
// Alternate the distortion effect every second for 10 seconds
for _ in 0..10 {
thread::sleep(Duration::from_secs(1));
let prev = distortion_enabled.load(Ordering::Relaxed);
distortion_enabled.store(!prev, Ordering::Relaxed);
println!("Distortion {}", if !prev { "ON" } else { "OFF" });
}
// Wait for playback to finish
player.sleep_until_end();
Ok(())
}