Skip to content

Commit 7bd21de

Browse files
committed
refactor: turn local-only AudioData stuff into AudioEngineData
1 parent d72dc76 commit 7bd21de

3 files changed

Lines changed: 70 additions & 60 deletions

File tree

‎player/src/main.rs‎

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct Connection {
4343
message_tx: mpsc::UnboundedSender<Message>,
4444

4545
// transmit audio to playback subsystem
46-
audio_tx: std::sync::mpsc::Sender<AudioData>,
46+
audio_tx: std::sync::mpsc::Sender<AudioEngineData>,
4747

4848
// get status feedback from audio playing
4949
audio_status_rx: mpsc::UnboundedReceiver<AudioStatus>,
@@ -106,7 +106,7 @@ impl Connection {
106106

107107
let (message_tx, message_rx) = mpsc::unbounded_channel::<Message>();
108108

109-
let (audio_tx, audio_rx) = std::sync::mpsc::channel::<AudioData>();
109+
let (audio_tx, audio_rx) = std::sync::mpsc::channel::<AudioEngineData>();
110110
let audio_status_rx = Self::run_audio(audio_rx);
111111

112112
let (transmit_tx, transmit_rx) = tokio::sync::mpsc::unbounded_channel::<TransmitCommand>();
@@ -157,7 +157,7 @@ impl Connection {
157157
}
158158

159159
fn run_audio(
160-
audio_rx: std::sync::mpsc::Receiver<AudioData>,
160+
audio_rx: std::sync::mpsc::Receiver<AudioEngineData>,
161161
) -> mpsc::UnboundedReceiver<AudioStatus> {
162162
// audio player thread
163163

@@ -171,50 +171,49 @@ impl Connection {
171171

172172
while let Ok(data) = audio_rx.recv() {
173173
match data {
174-
AudioData::Frame(frame) => {
175-
if frame.frame % 10 == 0 {
176-
tx.send(AudioStatus::Elapsed(p.get_seconds_elapsed()))
177-
.unwrap();
178-
tx.send(AudioStatus::Buffer(p.buffer_status())).unwrap();
179-
}
174+
AudioEngineData::AudioData(d) => match d {
175+
AudioData::Frame(frame) => {
176+
if frame.frame % 10 == 0 {
177+
tx.send(AudioStatus::Elapsed(p.get_seconds_elapsed()))
178+
.unwrap();
179+
tx.send(AudioStatus::Buffer(p.buffer_status())).unwrap();
180+
}
180181

181-
p.receive(frame.data);
182-
if let Some(samples) = p.get_visualizer_buffer() {
183-
let bars = audio::calculate_visualizer(&samples);
184-
tx.send(AudioStatus::Visualizer(bars)).unwrap();
182+
p.receive(frame.data);
183+
if let Some(samples) = p.get_visualizer_buffer() {
184+
let bars = audio::calculate_visualizer(&samples);
185+
tx.send(AudioStatus::Visualizer(bars)).unwrap();
186+
}
185187
}
186-
}
187-
AudioData::Start => {
188-
wants_play = true;
189-
p.fake_frames_received(0);
190-
tx.send(AudioStatus::Buffering).unwrap();
191-
}
192-
AudioData::StartLate(frame_id) => {
188+
AudioData::Start => {
189+
wants_play = true;
190+
p.fake_frames_received(0);
191+
tx.send(AudioStatus::Buffering).unwrap();
192+
}
193+
AudioData::Finish => {
194+
while !p.finish() {
195+
std::thread::sleep(std::time::Duration::from_millis(20));
196+
}
197+
p.pause();
198+
tx.send(AudioStatus::Finished).unwrap();
199+
}
200+
AudioData::Resume => {
201+
p.resume();
202+
}
203+
},
204+
AudioEngineData::StartLate(frame_id) => {
193205
wants_play = true;
194206
p.fake_frames_received(frame_id);
195207
}
196-
AudioData::Stop => {
197-
p.pause();
198-
}
199-
AudioData::Resume => {
200-
p.resume();
201-
}
202-
AudioData::Finish => {
203-
println!("finishing.....");
204-
while !p.finish() {
205-
std::thread::sleep(std::time::Duration::from_millis(20));
206-
}
207-
p.pause();
208-
tx.send(AudioStatus::Finished).unwrap();
209-
}
210-
AudioData::Clear => {
208+
209+
AudioEngineData::Clear => {
211210
tx.send(AudioStatus::Elapsed(0)).unwrap();
212211
p.clear();
213212
}
214-
AudioData::Volume(val) => {
213+
AudioEngineData::Volume(val) => {
215214
p.volume(val);
216215
}
217-
AudioData::Shutdown => break,
216+
AudioEngineData::Shutdown => break,
218217
}
219218
if p.is_ready() && wants_play {
220219
wants_play = false;
@@ -245,7 +244,7 @@ impl Connection {
245244

246245
async fn main_loop(&mut self) {
247246
self.ui_status_default();
248-
247+
249248
let mut ui_interval = tokio::time::interval(tokio::time::Duration::from_millis(50));
250249

251250
loop {
@@ -347,18 +346,18 @@ impl Connection {
347346
if !self.is_synced {
348347
// we're late.... try and catch up will you
349348
self.is_synced = true;
350-
self.audio_tx.send(AudioData::StartLate(f.frame as usize)).unwrap();
349+
self.audio_tx.send(AudioEngineData::StartLate(f.frame as usize)).unwrap();
351350
}
352351

353-
self.audio_tx.send(AudioData::Frame(f)).unwrap();
352+
self.audio_tx.send(AudioEngineData::AudioData(AudioData::Frame(f))).unwrap();
354353
}
355354
protocol::AudioData::Start => {
356355
self.is_synced = true;
357-
self.audio_tx.send(data).unwrap();
356+
self.audio_tx.send(AudioEngineData::AudioData(data)).unwrap();
358357
}
359358
_ => {
360359
// forward to audio thread
361-
self.audio_tx.send(data).unwrap();
360+
self.audio_tx.send(AudioEngineData::AudioData(data)).unwrap();
362361
}
363362
}
364363

@@ -415,7 +414,7 @@ impl Connection {
415414
}
416415
}
417416
UIEvent::VolumeSlider(pos) => {
418-
self.audio_tx.send(AudioData::Volume(pos)).unwrap();
417+
self.audio_tx.send(AudioEngineData::Volume(pos)).unwrap();
419418
}
420419
UIEvent::Test(text) => {
421420
self.message_tx.send(Message::Text(text)).unwrap();
@@ -570,7 +569,9 @@ async fn main() -> std::io::Result<()> {
570569

571570
// blehhhh this is all such a mess
572571
gui.volume_slider.set_value(0.5);
573-
ui_tx.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(0.5))).unwrap();
572+
ui_tx
573+
.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(0.5)))
574+
.unwrap();
574575

575576
gui.lbl_title.set_text(&"hi, welcome >.<".to_string());
576577

@@ -607,16 +608,20 @@ async fn main() -> std::io::Result<()> {
607608
UIUpdateEvent::VolumeUp => {
608609
gui.volume_slider
609610
.set_value((gui.volume_slider.value() + 0.02).min(1.));
610-
ui_tx.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(
611-
gui.volume_slider.value(),
612-
))).unwrap();
611+
ui_tx
612+
.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(
613+
gui.volume_slider.value(),
614+
)))
615+
.unwrap();
613616
}
614617
UIUpdateEvent::VolumeDown => {
615618
gui.volume_slider
616619
.set_value((gui.volume_slider.value() - 0.02).max(0.));
617-
ui_tx.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(
618-
gui.volume_slider.value(),
619-
))).unwrap();
620+
ui_tx
621+
.send(UIEvent::VolumeSlider(gui::MainWindow::volume_scale(
622+
gui.volume_slider.value(),
623+
)))
624+
.unwrap();
620625
}
621626
UIUpdateEvent::UpdateUserList(val) => {
622627
gui.users.clear();
@@ -790,6 +795,15 @@ enum AudioStatus {
790795
Buffer(u8),
791796
}
792797

798+
#[derive(Debug, Clone)]
799+
pub enum AudioEngineData {
800+
AudioData(AudioData),
801+
StartLate(usize),
802+
Clear,
803+
Volume(f32),
804+
Shutdown,
805+
}
806+
793807
#[derive(Debug, PartialEq)]
794808
enum PlayState {
795809
Transmitting,

‎player/src/transmit.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use protocol::{AudioFrame, TrackMetadata};
1818

1919
use image::imageops::FilterType;
2020

21+
use crate::AudioEngineData;
22+
2123
pub struct AudioInfoReader {
2224
probe_result: ProbeResult,
2325
decoder: Box<dyn Decoder>,
@@ -336,7 +338,7 @@ pub enum TransmitCommand {
336338

337339
//temp
338340
type MessageTx = tokio::sync::mpsc::UnboundedSender<Message>;
339-
type AudioTx = std::sync::mpsc::Sender<AudioData>;
341+
type AudioTx = std::sync::mpsc::Sender<AudioEngineData>;
340342

341343
type TransmitTx = tokio::sync::mpsc::UnboundedSender<TransmitCommand>;
342344
type TransmitRx = tokio::sync::mpsc::UnboundedReceiver<TransmitCommand>;
@@ -407,7 +409,7 @@ impl TransmitThread {
407409
}
408410

409411
fn send_both(&self, data: AudioData) {
410-
self.audio_tx.send(data.clone()).unwrap();
412+
self.audio_tx.send(AudioEngineData::AudioData(data.clone())).unwrap();
411413
self.message_tx.send(Message::AudioData(data)).unwrap();
412414
}
413415

@@ -459,7 +461,7 @@ impl TransmitThread {
459461
if let Ok(frame) = f {
460462
// borrow checker doesn't like self.send_both() here since we already borrow self or something
461463
let data = AudioData::Frame(frame);
462-
self.audio_tx.send(data.clone()).unwrap();
464+
self.audio_tx.send(AudioEngineData::AudioData(data.clone())).unwrap();
463465
self.message_tx.send(Message::AudioData(data)).unwrap();
464466
} else {
465467
break; // we're done i guess

‎protocol/src/lib.rs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ pub struct AudioFrame {
1313
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
1414
pub enum AudioData {
1515
Start,
16-
StartLate(usize),
1716
Frame(AudioFrame),
18-
Stop,
19-
Resume,
2017
Finish,
21-
Clear,
22-
Volume(f32), // this is where having this be the thing we pass across the network kinda breaks down,,
23-
Shutdown,
18+
Resume,
2419
}
25-
2620
#[derive(Serialize, Deserialize, Clone, Debug)]
2721
pub struct Track {
2822
pub owner: String,

0 commit comments

Comments
 (0)