Send sound as data instead of name
This commit is contained in:
parent
d5a1897699
commit
1986341fdc
4 changed files with 21 additions and 12 deletions
|
|
@ -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<u8> {
|
||||
let path = format!("./audios/{}", sound_source);
|
||||
let file = BufReader::new(File::open(path).unwrap());
|
||||
let data: Vec<u8> = 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");
|
||||
|
|
|
|||
|
|
@ -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<u8>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<u8>) {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue