From 5c1c1f18b0e2cebaae277c15c66c8dcc7b209ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 10:42:31 +0100 Subject: load sound effects by file name --- mumd/src/audio.rs | 84 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 0820147..f1c3ec9 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -9,39 +9,25 @@ use dasp_interpolate::linear::Linear; use dasp_signal::{self as signal, Signal}; use log::*; use mumble_protocol::voice::VoicePacketPayload; +use mumlib::config::SoundEffect; use opus::Channels; -use std::collections::hash_map::Entry; +use std::{collections::hash_map::Entry, fs::File}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; //TODO? move to mumlib -pub const EVENT_SOUNDS: &[(&[u8], NotificationEvents)] = &[ - (include_bytes!("resources/connect.wav"), NotificationEvents::ServerConnect), - ( - include_bytes!("resources/disconnect.wav"), - NotificationEvents::ServerDisconnect, - ), - ( - include_bytes!("resources/channel_join.wav"), - NotificationEvents::UserConnected, - ), - ( - include_bytes!("resources/channel_leave.wav"), - NotificationEvents::UserDisconnected, - ), - ( - include_bytes!("resources/channel_join.wav"), - NotificationEvents::UserJoinedChannel, - ), - ( - include_bytes!("resources/channel_leave.wav"), - NotificationEvents::UserLeftChannel, - ), - (include_bytes!("resources/mute.wav"), NotificationEvents::Mute), - (include_bytes!("resources/unmute.wav"), NotificationEvents::Unmute), - (include_bytes!("resources/deafen.wav"), NotificationEvents::Deafen), - (include_bytes!("resources/undeafen.wav"), NotificationEvents::Undeafen), +pub const DEFAULT_SOUND_FILES: &[(NotificationEvents, &str)] = &[ + (NotificationEvents::ServerConnect, "resources/connect.wav"), + (NotificationEvents::ServerDisconnect, "resources/disconnect.wav"), + (NotificationEvents::UserConnected, "resources/channel_join.wav"), + (NotificationEvents::UserDisconnected, "resources/channel_leave.wav"), + (NotificationEvents::UserJoinedChannel, "resources/channel_join.wav"), + (NotificationEvents::UserLeftChannel, "resources/channel_leave.wav"), + (NotificationEvents::Mute, "resources/mute.wav"), + (NotificationEvents::Unmute, "resources/unmute.wav"), + (NotificationEvents::Deafen, "resources/deafen.wav"), + (NotificationEvents::Undeafen, "resources/undeafen.wav"), ]; const SAMPLE_RATE: u32 = 48000; @@ -216,10 +202,27 @@ impl Audio { output_stream.play().unwrap(); - let sounds = EVENT_SOUNDS + let mut res = Self { + output_config, + _output_stream: output_stream, + _input_stream: input_stream, + input_volume_sender, + input_channel_receiver: Some(input_receiver), + client_streams, + sounds: HashMap::new(), + output_volume_sender, + user_volumes, + play_sounds, + }; + res.load_sound_effects(&vec![]); + res + } + + pub fn load_sound_effects(&mut self, _sound_effects: &Vec) -> Result<(), ()> { + self.sounds = DEFAULT_SOUND_FILES .iter() - .map(|(bytes, event)| { - let reader = hound::WavReader::new(*bytes).unwrap(); + .map(|(event, file)| { + let reader = hound::WavReader::new(File::open(file).unwrap()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { hound::SampleFormat::Float => reader @@ -242,24 +245,19 @@ impl Audio { .from_hz_to_hz(interp, spec.sample_rate as f64, SAMPLE_RATE as f64) .until_exhausted() // if the source audio is stereo and is being played as mono, discard the right audio - .flat_map(|e| if output_config.channels == 1 { vec![e[0]] } else { e.to_vec() }) + .flat_map( + |e| if self.output_config.channels == 1 { + vec![e[0]] + } else { + e.to_vec() + } + ) .collect::>(); (*event, samples) }) .collect(); - Self { - output_config, - _output_stream: output_stream, - _input_stream: input_stream, - input_volume_sender, - input_channel_receiver: Some(input_receiver), - client_streams, - sounds, - output_volume_sender, - user_volumes, - play_sounds, - } + Ok(()) } pub fn decode_packet(&self, session_id: u32, payload: VoicePacketPayload) { -- cgit v1.2.1 From 3fdaf1a59687d9f4e47810826e4ea9d527a0c689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 12:58:23 +0100 Subject: override sound effects --- mumd/src/audio.rs | 61 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 14 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index f1c3ec9..67c3e59 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -11,23 +11,23 @@ use log::*; use mumble_protocol::voice::VoicePacketPayload; use mumlib::config::SoundEffect; use opus::Channels; -use std::{collections::hash_map::Entry, fs::File}; +use std::{collections::hash_map::Entry, fs::File, io}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; //TODO? move to mumlib pub const DEFAULT_SOUND_FILES: &[(NotificationEvents, &str)] = &[ - (NotificationEvents::ServerConnect, "resources/connect.wav"), - (NotificationEvents::ServerDisconnect, "resources/disconnect.wav"), - (NotificationEvents::UserConnected, "resources/channel_join.wav"), - (NotificationEvents::UserDisconnected, "resources/channel_leave.wav"), - (NotificationEvents::UserJoinedChannel, "resources/channel_join.wav"), - (NotificationEvents::UserLeftChannel, "resources/channel_leave.wav"), - (NotificationEvents::Mute, "resources/mute.wav"), - (NotificationEvents::Unmute, "resources/unmute.wav"), - (NotificationEvents::Deafen, "resources/deafen.wav"), - (NotificationEvents::Undeafen, "resources/undeafen.wav"), + (NotificationEvents::ServerConnect, "connect.wav"), + (NotificationEvents::ServerDisconnect, "disconnect.wav"), + (NotificationEvents::UserConnected, "channel_join.wav"), + (NotificationEvents::UserDisconnected, "channel_leave.wav"), + (NotificationEvents::UserJoinedChannel, "channel_join.wav"), + (NotificationEvents::UserLeftChannel, "channel_leave.wav"), + (NotificationEvents::Mute, "mute.wav"), + (NotificationEvents::Unmute, "unmute.wav"), + (NotificationEvents::Deafen, "deafen.wav"), + (NotificationEvents::Undeafen, "undeafen.wav"), ]; const SAMPLE_RATE: u32 = 48000; @@ -214,15 +214,44 @@ impl Audio { user_volumes, play_sounds, }; - res.load_sound_effects(&vec![]); + res.load_sound_effects(&vec![]).unwrap(); res } - pub fn load_sound_effects(&mut self, _sound_effects: &Vec) -> Result<(), ()> { + pub fn load_sound_effects(&mut self, sound_effects: &Vec) -> Result<(), ()> { + let overrides: HashMap<_, _> = sound_effects + .iter() + .map(|sound_effect| { (&sound_effect.event, &sound_effect.file) }) + .filter_map(|(event, file)| { + let event = match event.as_str() { + "server_connect" => NotificationEvents::ServerConnect, + "server_disconnect" => NotificationEvents::ServerDisconnect, + "user_connected" => NotificationEvents::UserConnected, + "user_disconnected" => NotificationEvents::UserDisconnected, + "user_joined_channel" => NotificationEvents::UserJoinedChannel, + "user_left_channel" => NotificationEvents::UserLeftChannel, + "mute" => NotificationEvents::Mute, + "unmute" => NotificationEvents::Unmute, + "deafen" => NotificationEvents::Deafen, + "undeafen" => NotificationEvents::Undeafen, + _ => { + warn!("Unknown notification event '{}' in config", event); + return None; + } + }; + Some((event, file)) + }) + .collect(); + self.sounds = DEFAULT_SOUND_FILES .iter() .map(|(event, file)| { - let reader = hound::WavReader::new(File::open(file).unwrap()).unwrap(); + let file = if let Some(file) = overrides.get(event) { + *file + } else { + *file + }; + let reader = hound::WavReader::new(get_resource(file).unwrap()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { hound::SampleFormat::Float => reader @@ -352,3 +381,7 @@ impl Audio { play_sounds.extend(samples.iter().skip(l)); } } + +fn get_resource(file: &str) -> io::Result { + File::open(file) +} -- cgit v1.2.1 From 07f84c7738f7dd1aeaba3f7cae6a26d6a7e5835b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 18:12:53 +0100 Subject: load fallback sfx if no file can be found --- mumd/src/audio.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 67c3e59..61e2179 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -11,23 +11,23 @@ use log::*; use mumble_protocol::voice::VoicePacketPayload; use mumlib::config::SoundEffect; use opus::Channels; -use std::{collections::hash_map::Entry, fs::File, io}; +use std::{collections::hash_map::Entry, fs::File, io::Read}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; //TODO? move to mumlib pub const DEFAULT_SOUND_FILES: &[(NotificationEvents, &str)] = &[ - (NotificationEvents::ServerConnect, "connect.wav"), - (NotificationEvents::ServerDisconnect, "disconnect.wav"), - (NotificationEvents::UserConnected, "channel_join.wav"), - (NotificationEvents::UserDisconnected, "channel_leave.wav"), - (NotificationEvents::UserJoinedChannel, "channel_join.wav"), - (NotificationEvents::UserLeftChannel, "channel_leave.wav"), - (NotificationEvents::Mute, "mute.wav"), - (NotificationEvents::Unmute, "unmute.wav"), - (NotificationEvents::Deafen, "deafen.wav"), - (NotificationEvents::Undeafen, "undeafen.wav"), + (NotificationEvents::ServerConnect, "fallback_sfx.wav"), + (NotificationEvents::ServerDisconnect, "fallback_sfx.wav"), + (NotificationEvents::UserConnected, "fallback_sfx.wav"), + (NotificationEvents::UserDisconnected, "fallback_sfx.wav"), + (NotificationEvents::UserJoinedChannel, "fallback_sfx.wav"), + (NotificationEvents::UserLeftChannel, "fallback_sfx.wav"), + (NotificationEvents::Mute, "fallback_sfx.wav"), + (NotificationEvents::Unmute, "fallback_sfx.wav"), + (NotificationEvents::Deafen, "fallback_sfx.wav"), + (NotificationEvents::Undeafen, "fallback_sfx.wav"), ]; const SAMPLE_RATE: u32 = 48000; @@ -251,7 +251,12 @@ impl Audio { } else { *file }; - let reader = hound::WavReader::new(get_resource(file).unwrap()).unwrap(); + let bytes = if let Some(bytes_vec) = get_resource(file) { + &*bytes_vec.leak() // needs immutability + } else { + include_bytes!("fallback_sfx.wav") + }; + let reader = hound::WavReader::new(bytes).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { hound::SampleFormat::Float => reader @@ -382,6 +387,14 @@ impl Audio { } } -fn get_resource(file: &str) -> io::Result { - File::open(file) +fn get_resource(file: &str) -> Option> { + let mut buf: Vec = Vec::new(); + if let Ok(mut file) = File::open(file) + .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) + { + file.read_to_end(&mut buf).unwrap(); + Some(buf) + } else { + None + } } -- cgit v1.2.1 From e23509097b904dcd134a2b54babe85eb16aedd52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 18:58:42 +0100 Subject: enumiter --- mumd/src/audio.rs | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 61e2179..08a4b48 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -14,25 +14,13 @@ use opus::Channels; use std::{collections::hash_map::Entry, fs::File, io::Read}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; +use strum::IntoEnumIterator; +use strum_macros::EnumIter; use tokio::sync::{mpsc, watch}; -//TODO? move to mumlib -pub const DEFAULT_SOUND_FILES: &[(NotificationEvents, &str)] = &[ - (NotificationEvents::ServerConnect, "fallback_sfx.wav"), - (NotificationEvents::ServerDisconnect, "fallback_sfx.wav"), - (NotificationEvents::UserConnected, "fallback_sfx.wav"), - (NotificationEvents::UserDisconnected, "fallback_sfx.wav"), - (NotificationEvents::UserJoinedChannel, "fallback_sfx.wav"), - (NotificationEvents::UserLeftChannel, "fallback_sfx.wav"), - (NotificationEvents::Mute, "fallback_sfx.wav"), - (NotificationEvents::Unmute, "fallback_sfx.wav"), - (NotificationEvents::Deafen, "fallback_sfx.wav"), - (NotificationEvents::Undeafen, "fallback_sfx.wav"), -]; - const SAMPLE_RATE: u32 = 48000; -#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] +#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash, EnumIter)] pub enum NotificationEvents { ServerConnect, ServerDisconnect, @@ -243,19 +231,13 @@ impl Audio { }) .collect(); - self.sounds = DEFAULT_SOUND_FILES - .iter() - .map(|(event, file)| { - let file = if let Some(file) = overrides.get(event) { - *file - } else { - *file - }; - let bytes = if let Some(bytes_vec) = get_resource(file) { - &*bytes_vec.leak() // needs immutability - } else { - include_bytes!("fallback_sfx.wav") - }; + self.sounds = NotificationEvents::iter() + .map(|event| { + let bytes = overrides.get(&event) + .map(|file| get_resource(file)) + .flatten() + .map(|byte_vec| &*byte_vec.leak()) + .unwrap_or(include_bytes!("fallback_sfx.wav")); let reader = hound::WavReader::new(bytes).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { @@ -287,7 +269,7 @@ impl Audio { } ) .collect::>(); - (*event, samples) + (event, samples) }) .collect(); -- cgit v1.2.1 From 3e9b55d70301cdb322a1d2aa2fb599b55521621d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 19:10:15 +0100 Subject: cow moo --- mumd/src/audio.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 08a4b48..0e53a63 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -11,7 +11,7 @@ use log::*; use mumble_protocol::voice::VoicePacketPayload; use mumlib::config::SoundEffect; use opus::Channels; -use std::{collections::hash_map::Entry, fs::File, io::Read}; +use std::{borrow::Cow, collections::hash_map::Entry, fs::File, io::Read}; use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; use strum::IntoEnumIterator; @@ -233,11 +233,11 @@ impl Audio { self.sounds = NotificationEvents::iter() .map(|event| { - let bytes = overrides.get(&event) + let bytes_cow = overrides.get(&event) .map(|file| get_resource(file)) .flatten() - .map(|byte_vec| &*byte_vec.leak()) - .unwrap_or(include_bytes!("fallback_sfx.wav")); + .unwrap_or(Cow::from(include_bytes!("fallback_sfx.wav").as_ref())); + let bytes = bytes_cow.as_ref(); let reader = hound::WavReader::new(bytes).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { @@ -369,13 +369,14 @@ impl Audio { } } -fn get_resource(file: &str) -> Option> { +// moo +fn get_resource(file: &str) -> Option> { let mut buf: Vec = Vec::new(); if let Ok(mut file) = File::open(file) .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) { file.read_to_end(&mut buf).unwrap(); - Some(buf) + Some(Cow::from(buf)) } else { None } -- cgit v1.2.1 From fc208994a1e1228b79091303920fc90e066a087f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 19:14:13 +0100 Subject: remove unused return type --- mumd/src/audio.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 0e53a63..fe0ce40 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -202,11 +202,11 @@ impl Audio { user_volumes, play_sounds, }; - res.load_sound_effects(&vec![]).unwrap(); + res.load_sound_effects(&vec![]); res } - pub fn load_sound_effects(&mut self, sound_effects: &Vec) -> Result<(), ()> { + pub fn load_sound_effects(&mut self, sound_effects: &Vec) { let overrides: HashMap<_, _> = sound_effects .iter() .map(|sound_effect| { (&sound_effect.event, &sound_effect.file) }) @@ -272,8 +272,6 @@ impl Audio { (event, samples) }) .collect(); - - Ok(()) } pub fn decode_packet(&self, session_id: u32, payload: VoicePacketPayload) { -- cgit v1.2.1 From 3692561f46cad490917683b17d5753433e26a38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 19:17:23 +0100 Subject: merge maps --- mumd/src/audio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index fe0ce40..548618f 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -209,8 +209,8 @@ impl Audio { pub fn load_sound_effects(&mut self, sound_effects: &Vec) { let overrides: HashMap<_, _> = sound_effects .iter() - .map(|sound_effect| { (&sound_effect.event, &sound_effect.file) }) - .filter_map(|(event, file)| { + .filter_map(|sound_effect| { + let (event, file) = (&sound_effect.event, &sound_effect.file); let event = match event.as_str() { "server_connect" => NotificationEvents::ServerConnect, "server_disconnect" => NotificationEvents::ServerDisconnect, -- cgit v1.2.1 From 781c2e21d111971fe1df98d00271342fbf5f7675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 23:44:57 +0100 Subject: cow lifetime moo --- mumd/src/audio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 548618f..ee20b01 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -368,7 +368,7 @@ impl Audio { } // moo -fn get_resource(file: &str) -> Option> { +fn get_resource(file: &str) -> Option> { let mut buf: Vec = Vec::new(); if let Ok(mut file) = File::open(file) .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) -- cgit v1.2.1 From 40338b7f5e795124e1ae1e1a5741d5e5189ee405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 23:45:56 +0100 Subject: dont bind ref to variable --- mumd/src/audio.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index ee20b01..ad9c62d 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -233,12 +233,11 @@ impl Audio { self.sounds = NotificationEvents::iter() .map(|event| { - let bytes_cow = overrides.get(&event) + let bytes = overrides.get(&event) .map(|file| get_resource(file)) .flatten() .unwrap_or(Cow::from(include_bytes!("fallback_sfx.wav").as_ref())); - let bytes = bytes_cow.as_ref(); - let reader = hound::WavReader::new(bytes).unwrap(); + let reader = hound::WavReader::new(bytes.as_ref()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { hound::SampleFormat::Float => reader -- cgit v1.2.1 From ea65db55764bf369d8e02ca8bae9088c738b020b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 3 Jan 2021 23:54:51 +0100 Subject: notification events try from --- mumd/src/audio.rs | 59 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index ad9c62d..b5b5c7f 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -11,9 +11,14 @@ use log::*; use mumble_protocol::voice::VoicePacketPayload; use mumlib::config::SoundEffect; use opus::Channels; -use std::{borrow::Cow, collections::hash_map::Entry, fs::File, io::Read}; -use std::collections::{HashMap, VecDeque}; -use std::sync::{Arc, Mutex}; +use std::{ + borrow::Cow, + collections::{hash_map::Entry, HashMap, VecDeque}, + convert::TryFrom, + fs::File, + io::Read, + sync::{Arc, Mutex} +}; use strum::IntoEnumIterator; use strum_macros::EnumIter; use tokio::sync::{mpsc, watch}; @@ -34,6 +39,28 @@ pub enum NotificationEvents { Undeafen, } +impl TryFrom<&str> for NotificationEvents { + type Error = (); + fn try_from(s: &str) -> Result { + match s { + "server_connect" => Ok(NotificationEvents::ServerConnect), + "server_disconnect" => Ok(NotificationEvents::ServerDisconnect), + "user_connected" => Ok(NotificationEvents::UserConnected), + "user_disconnected" => Ok(NotificationEvents::UserDisconnected), + "user_joined_channel" => Ok(NotificationEvents::UserJoinedChannel), + "user_left_channel" => Ok(NotificationEvents::UserLeftChannel), + "mute" => Ok(NotificationEvents::Mute), + "unmute" => Ok(NotificationEvents::Unmute), + "deafen" => Ok(NotificationEvents::Deafen), + "undeafen" => Ok(NotificationEvents::Undeafen), + _ => { + warn!("Unknown notification event '{}' in config", s); + Err(()) + } + } + } +} + pub struct Audio { output_config: StreamConfig, _output_stream: Stream, @@ -202,32 +229,20 @@ impl Audio { user_volumes, play_sounds, }; - res.load_sound_effects(&vec![]); + res.load_sound_effects(&[]); res } - pub fn load_sound_effects(&mut self, sound_effects: &Vec) { + pub fn load_sound_effects(&mut self, sound_effects: &[SoundEffect]) { let overrides: HashMap<_, _> = sound_effects .iter() .filter_map(|sound_effect| { let (event, file) = (&sound_effect.event, &sound_effect.file); - let event = match event.as_str() { - "server_connect" => NotificationEvents::ServerConnect, - "server_disconnect" => NotificationEvents::ServerDisconnect, - "user_connected" => NotificationEvents::UserConnected, - "user_disconnected" => NotificationEvents::UserDisconnected, - "user_joined_channel" => NotificationEvents::UserJoinedChannel, - "user_left_channel" => NotificationEvents::UserLeftChannel, - "mute" => NotificationEvents::Mute, - "unmute" => NotificationEvents::Unmute, - "deafen" => NotificationEvents::Deafen, - "undeafen" => NotificationEvents::Undeafen, - _ => { - warn!("Unknown notification event '{}' in config", event); - return None; - } - }; - Some((event, file)) + if let Ok(event) = NotificationEvents::try_from(event.as_str()) { + Some((event, file)) + } else { + None + } }) .collect(); -- cgit v1.2.1 From 38a9a4d11821fe5b9948d4a94935e5ccb103fdf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 4 Jan 2021 21:45:32 +0100 Subject: cows aren't optional --- mumd/src/audio.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index b5b5c7f..569cdc5 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -248,10 +248,11 @@ impl Audio { self.sounds = NotificationEvents::iter() .map(|event| { - let bytes = overrides.get(&event) - .map(|file| get_resource(file)) - .flatten() - .unwrap_or(Cow::from(include_bytes!("fallback_sfx.wav").as_ref())); + + let bytes = match overrides.get(&event) { + Some(file) => get_sfx(file), + None => get_default_sfx(), + }; let reader = hound::WavReader::new(bytes.as_ref()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { @@ -382,14 +383,19 @@ impl Audio { } // moo -fn get_resource(file: &str) -> Option> { +fn get_sfx(file: &str) -> Cow<'static, [u8]> { let mut buf: Vec = Vec::new(); if let Ok(mut file) = File::open(file) .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) { file.read_to_end(&mut buf).unwrap(); - Some(Cow::from(buf)) + Cow::from(buf) } else { - None + warn!("File not found: '{}'", file); + get_default_sfx() } } + +fn get_default_sfx() -> Cow<'static, [u8]> { + Cow::from(include_bytes!("fallback_sfx.wav").as_ref()) +} -- cgit v1.2.1 From bf37c8ddcc4d372ee70af703c0c5e97ede741630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 4 Jan 2021 22:48:25 +0100 Subject: no hardcoded path --- mumd/src/audio.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 569cdc5..57bd9f0 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -385,9 +385,7 @@ impl Audio { // moo fn get_sfx(file: &str) -> Cow<'static, [u8]> { let mut buf: Vec = Vec::new(); - if let Ok(mut file) = File::open(file) - .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) - { + if let Ok(mut file) = File::open(file) { file.read_to_end(&mut buf).unwrap(); Cow::from(buf) } else { -- cgit v1.2.1 From 9cc7b67da79b6f5994d4bff63db9552c6d87b3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 4 Jan 2021 22:50:11 +0100 Subject: map unwrap or else --- mumd/src/audio.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 57bd9f0..e11c328 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -248,11 +248,9 @@ impl Audio { self.sounds = NotificationEvents::iter() .map(|event| { - - let bytes = match overrides.get(&event) { - Some(file) => get_sfx(file), - None => get_default_sfx(), - }; + let bytes = overrides.get(&event) + .map(|file| get_sfx(file)) + .unwrap_or_else(|| get_default_sfx()); let reader = hound::WavReader::new(bytes.as_ref()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { -- cgit v1.2.1 From 2cef2e31a139e453474298b3278c98f18eba7203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 4 Jan 2021 23:17:50 +0100 Subject: function pointer instead --- mumd/src/audio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index e11c328..0df2852 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -250,7 +250,7 @@ impl Audio { .map(|event| { let bytes = overrides.get(&event) .map(|file| get_sfx(file)) - .unwrap_or_else(|| get_default_sfx()); + .unwrap_or_else(get_default_sfx); let reader = hound::WavReader::new(bytes.as_ref()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { -- cgit v1.2.1