diff --git a/audios/rickroll.mp3 b/audios/rickroll.mp3 new file mode 100644 index 0000000..04b17d3 Binary files /dev/null and b/audios/rickroll.mp3 differ diff --git a/src/dispatcher.rs b/src/dispatcher.rs index 6094b06..198997f 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -1,5 +1,6 @@ use std::io::BufReader; use std::net::TcpStream; +use std::thread; use std::time::{Duration, SystemTime}; use std::{fs::File, io::Write}; @@ -34,27 +35,31 @@ impl Dispatcher { continue; } - let mut sock = TcpStream::connect(addr).expect("Failed to connect"); + let source = source.clone(); - while offset < duration { - let sound_data = source - .clone() - .skip_duration(offset) - .take_duration(chunk_len) - .collect::>(); + thread::spawn(move || { + let mut sock = TcpStream::connect(addr).expect("Failed to connect"); - offset += chunk_len; + while offset < duration { + let sound_data = source + .clone() + .skip_duration(offset) + .take_duration(chunk_len) + .collect::>(); - let msg = Message::PlaySound(PlaySound { - timestamp: (now + offset).as_micros(), - channels: channels, - sample_rate: sample_rate, - sound_data: sound_data, - }); + offset += chunk_len; - let buf = bincode::serialize(&msg).expect("Failed to serialize"); - sock.write_all(&buf).unwrap(); - } + let msg = Message::PlaySound(PlaySound { + timestamp: (now + offset).as_micros(), + channels: channels, + sample_rate: sample_rate, + sound_data: sound_data, + }); + + let buf = bincode::serialize(&msg).expect("Failed to serialize"); + sock.write_all(&buf).unwrap(); + } + }); } } } diff --git a/src/main.rs b/src/main.rs index 79e1456..7f6625e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ mod ui_app; fn main() -> eframe::Result { let options = eframe::NativeOptions { - viewport: egui::ViewportBuilder::default().with_inner_size([1000.0, 1000.0]), + viewport: egui::ViewportBuilder::default().with_inner_size([700.0, 400.0]), ..Default::default() }; diff --git a/src/ui_app.rs b/src/ui_app.rs index 585e686..6620b9a 100644 --- a/src/ui_app.rs +++ b/src/ui_app.rs @@ -46,15 +46,22 @@ impl eframe::App for UIApp { if ui.button("Add address").clicked() { self.addresses.push("localhost:3000".to_owned()); } + ui.add_space(25.0); ui.separator(); + ui.add_space(25.0); if ui.button("Pick file").clicked() { - if let Some(file) = FileDialog::new().set_directory("/").pick_file() { - self.picked_file = Some(file); + let file = FileDialog::new().pick_file(); + if !file.is_none() { + self.picked_file = file; } } + ui.label(format!("Picked file: {:?}", self.picked_file)); + + ui.add_space(25.0); ui.separator(); + ui.add_space(25.0); if ui.button("Play sound").clicked() { if let Some(path) = &self.picked_file {