diff --git a/src/dispatcher.rs b/src/dispatcher.rs index d0b5317..e6e5e6c 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -1,12 +1,20 @@ -use std::io::Write; +use std::io::{BufReader, Read}; use std::net::TcpStream; use std::time::SystemTime; +use std::{fs::File, io::Write}; use crate::protocol::{Message, PlaySound}; pub struct Dispatcher; impl Dispatcher { + fn load_sound(sound_source: String) -> Vec { + let path = format!("./audios/{}", sound_source); + let file = BufReader::new(File::open(path).unwrap()); + let data: Vec = file.bytes().map(|byte| byte.unwrap()).collect(); + data + } + pub fn handle_dispatch_sample() { let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -14,9 +22,11 @@ impl Dispatcher { .as_micros(); let timestamp = now + 10000000; let addr = "localhost:3000".to_owned(); + let sound_source = "augh.mp3".to_owned(); + let sound_data = Dispatcher::load_sound(sound_source); let msg = Message::PlaySound(PlaySound { timestamp: timestamp, - sound_source: "augh.mp3".to_owned(), + sound_data: sound_data, }); let mut sock = TcpStream::connect(addr).expect("Failed to connect"); let buf = bincode::serialize(&msg).expect("Failed to serialize"); diff --git a/src/protocol.rs b/src/protocol.rs index 8d65dc6..3404033 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -8,5 +8,5 @@ pub enum Message { #[derive(Serialize, Deserialize, Debug)] pub struct PlaySound { pub timestamp: u128, - pub sound_source: String, + pub sound_data: Vec, } diff --git a/src/sound_player.rs b/src/sound_player.rs index 3f25214..d88e239 100644 --- a/src/sound_player.rs +++ b/src/sound_player.rs @@ -1,13 +1,12 @@ use rodio::{Decoder, OutputStream, Sink}; -use std::{fs::File, io::BufReader}; +use std::io::Cursor; pub struct SoundPlayer; impl SoundPlayer { - pub fn play_sound(sound_source: String) { - let path = format!("./audios/{}", sound_source); - let file = BufReader::new(File::open(path).unwrap()); - let source = Decoder::new(file).unwrap(); + pub fn play_sound(sound_data: Vec) { + let data = Cursor::new(sound_data); + let source = Decoder::new(data).unwrap(); let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap(); diff --git a/src/sound_scheduler.rs b/src/sound_scheduler.rs index f09fe26..4ffec7d 100644 --- a/src/sound_scheduler.rs +++ b/src/sound_scheduler.rs @@ -8,10 +8,10 @@ pub struct SoundScheduler; impl SoundScheduler { pub fn handle_scheduled_sound(play_msg: PlaySound) { let PlaySound { - sound_source, + sound_data, timestamp, } = play_msg; - println!("Scheduled sound: {:?} at {:?}", sound_source, timestamp); + println!("Scheduled sound: len={:?} at {:?}", sound_data.len(), timestamp); let now_micros = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Could not get time") @@ -22,7 +22,7 @@ impl SoundScheduler { return; } sleep(Duration::from_micros(wait as u64)); - println!("Playing sound: {:?}", sound_source); - SoundPlayer::play_sound(sound_source); + println!("Playing sound..."); + SoundPlayer::play_sound(sound_data); } }