Skip to content

Commit 160cb21

Browse files
committed
feat: more user input
1 parent a905fbd commit 160cb21

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

player/src/audio.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::error::Error;
12
use std::mem::MaybeUninit;
23
use std::sync::{Arc, Mutex};
34

@@ -25,8 +26,8 @@ pub struct Track {
2526
}
2627

2728
impl Track {
28-
pub fn load(path: &str) -> Result<Self, ()> {
29-
let src = std::fs::File::open(path).expect("failed to open media");
29+
pub fn load(path: &str) -> Result<Self, Box<dyn Error>> {
30+
let src = std::fs::File::open(path)?;
3031

3132
let mss = MediaSourceStream::new(Box::new(src), Default::default());
3233

@@ -60,6 +61,8 @@ impl Track {
6061
)
6162
.unwrap();
6263

64+
println!("loading into memory...");
65+
6366
loop {
6467
match probe_result.format.next_packet() {
6568
Ok(packet) => {
@@ -91,10 +94,12 @@ impl Track {
9194
}
9295
}
9396
Err(SymphoniaError::IoError(_)) => break,
94-
Err(e) => return Err(()),
97+
Err(e) => return Err("failed to parse track".into()),
9598
}
9699
}
97100

101+
println!("resampling....");
102+
98103
// resample to standard 48000 if needed
99104
let samples_correct_rate = if sample_rate != 48000 {
100105
let l = samples_not_interleaved.as_ref().unwrap()[0].len().clone();
@@ -108,6 +113,8 @@ impl Track {
108113
samples_not_interleaved.unwrap()
109114
};
110115

116+
println!("interlacing....");
117+
111118
// now we have to interleave it since we had to use the resampling thing
112119
let samples: Vec<f32> = samples_correct_rate[0]
113120
.chunks(1)

player/src/main.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,22 @@ impl Connection {
8989
Some(UIEvent::GetInfo(GetInfo::QueueList))
9090
} else if line == "aq" {
9191
Some(UIEvent::TestAddQueue)
92-
} else if line == "p" {
93-
Some(UIEvent::TestPlay)
92+
} else if line.starts_with("p ") {
93+
let p = line.split_ascii_whitespace().nth(1);
94+
if let Some(path) = p {
95+
Some(UIEvent::TestPlay(path.to_string()))
96+
} else {
97+
None
98+
}
99+
} else if line.starts_with("c ") {
100+
let mut c = line.split_ascii_whitespace().into_iter().collect::<Vec<&str>>();
101+
if c.len() < 2{
102+
None
103+
} else {
104+
c.remove(0);
105+
let t = c.join(" ");
106+
Some(UIEvent::Test(t.to_string()))
107+
}
94108
} else {
95109
println!("?");
96110
None
@@ -149,7 +163,7 @@ impl Connection {
149163
}
150164
});
151165
}
152-
async fn xmit_test(&mut self) {
166+
async fn xmit_test(&mut self, path: String) {
153167
let track = protocol::Track {
154168
path: "blah".to_string(),
155169
owner: self.my_id.clone(),
@@ -168,7 +182,11 @@ impl Connection {
168182

169183
self.play_state = PlayState::Transmitting;
170184
tokio::spawn(async move {
171-
let mut t: Track = Track::load("../temp/demo.mp3").unwrap();
185+
let mut t: Track = match Track::load(&path) {
186+
Ok(t) => t,
187+
Err(_) => return
188+
};
189+
172190

173191
dbg!(&t.samples.len());
174192
audio_tx_2.send(AudioData::Clear).unwrap();
@@ -269,7 +287,9 @@ impl Connection {
269287
// some ui event
270288
Some(event) = self.ui_rx.recv() => {
271289
match event {
272-
UIEvent::Test(_) => todo!(),
290+
UIEvent::Test(text) => {
291+
self.message_tx.send(Message::Text(text)).unwrap();
292+
},
273293
UIEvent::GetInfo(i) => {
274294
self.message_tx.send(Message::GetInfo(i)).unwrap();
275295
},
@@ -283,10 +303,10 @@ impl Connection {
283303
self.message_tx.send(Message::GetInfo(GetInfo::QueueList)).unwrap();
284304
// and uhh put it in idk
285305
},
286-
UIEvent::TestPlay => {
306+
UIEvent::TestPlay(path) => {
287307
if self.play_state == PlayState::Stopped {
288308
println!("{:?}", self.play_state);
289-
self.xmit_test().await;
309+
self.xmit_test(path).await;
290310
}
291311
},
292312
UIEvent::Quit => break,
@@ -317,7 +337,7 @@ enum UIEvent {
317337
Test(String),
318338
GetInfo(GetInfo),
319339
TestAddQueue,
320-
TestPlay,
340+
TestPlay(String),
321341
Quit,
322342
}
323343

protocol/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub enum Message {
4242
Authenticate(AuthenticateRequest),
4343
PlaybackState(PlaybackState),
4444
AudioFrame(AudioFrame),
45+
Text(String),
4546

4647
GetInfo(GetInfo),
4748
Info(Info),

server/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ async fn handle_message(
254254
Ok(())
255255
}
256256
Message::Info(_) => todo!(),
257+
Message::Text(text) => {
258+
println!("* {} - '{}'", peer.addr, text);
259+
// resend this message to all other peers
260+
state.lock().await.broadcast_others(&peer, m).await;
261+
Ok(())
262+
},
257263

258264
// should not be reached
259265
Message::Handshake(_) => Ok(()),

0 commit comments

Comments
 (0)