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/Cargo.toml | 2 ++ mumd/src/audio.rs | 40 +++++++++++----------------------------- 2 files changed, 13 insertions(+), 29 deletions(-) (limited to 'mumd') diff --git a/mumd/Cargo.toml b/mumd/Cargo.toml index 8c6958c..9af1f73 100644 --- a/mumd/Cargo.toml +++ b/mumd/Cargo.toml @@ -33,6 +33,8 @@ native-tls = "0.2" openssl = { version = "0.10" } opus = "0.2" serde = { version = "1.0", features = ["derive"] } +strum = "0.20" +strum_macros = "0.20" tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "sync", "net", "time"] } tokio-native-tls = "0.3" tokio-util = { version = "0.6", features = ["codec", "net"] } 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