forked from crsaracco/audio-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
86 lines (71 loc) · 2.21 KB
/
main.rs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Extern crates
extern crate chan;
extern crate glutin_window;
extern crate graphics;
extern crate hound;
extern crate opengl_graphics;
extern crate piston;
extern crate portaudio;
extern crate rustfft;
// Crate uses
use chan::Receiver;
use std::env;
use std::{thread, time};
// Our own modules
mod audio_player;
mod audio_visualizer;
mod common_defs;
mod wav_reader;
// Constants
const PACKET_BUFFER_SIZE: usize = 100;
fn main() {
// Get command line arguments
let args: Vec<_> = env::args().collect();
let filename = args[1].clone();
// Create a channel so we can read the .wav in one thread, buffer up some samples,
// and play it in another thread
let (send_audio_samples, recv_audio_samples) = chan::sync(PACKET_BUFFER_SIZE);
// Create a channel so that we can pass samples from the audio player to the audio visualizer,
// again in another thread
let (send_graph_samples, recv_graph_samples) = chan::sync(PACKET_BUFFER_SIZE);
// Collect all our threads so we can .join() later
let mut threads = vec![];
// Create the thread that reads our .wav file
threads.push(thread::spawn(move || {
wav_reader::read_samples(&filename, send_audio_samples);
}));
// Preroll audio
thread::sleep(time::Duration::new(0, 500_000_000));
// Create the thread that plays our audio
threads.push(thread::spawn(move || {
audio_player::run(recv_audio_samples, send_graph_samples).expect("Error playing audio");
}));
audio_visualizer::audio_visualizer(
recv_graph_samples,
args[2].parse().unwrap(),
args[3].parse().unwrap(),
);
// Wait for all the threads to finish
for thread in threads {
let _ = thread.join();
}
}
/// Print samples from a channel (debug)
#[allow(dead_code)]
fn print_samples(recv_audio_samples: Receiver<(i16, i16)>) {
let mut counter = 0;
loop {
counter += 1;
match recv_audio_samples.recv() {
Some(pair) => {
if counter % 1024 == 0 {
println!("L: {} | R: {}", pair.0, pair.1);
}
}
None => {
println!("End of file.");
break;
}
};
}
}