generated from GarandPLG/rust-flake-template
Preload audio using once_cell and correct typo
Add once_cell 1.21.4 and a Lazy‑initialized HashMap to store OGG bytes. Replace file reads with in‑memory Cursor decoding, fix the SoundrackParts enum typo, and derive Hash traits for it.
This commit is contained in:
Generated
+3
-2
@@ -1204,9 +1204,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.3"
|
||||
version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
@@ -2237,6 +2237,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"log",
|
||||
"once_cell",
|
||||
"ratatui",
|
||||
"rodio",
|
||||
"simplelog",
|
||||
|
||||
@@ -15,3 +15,4 @@ ratatui = "0.30.0"
|
||||
log = "0.4.29"
|
||||
simplelog = "0.12.2"
|
||||
rodio = "0.22.2"
|
||||
once_cell = "1.21.4"
|
||||
|
||||
+34
-26
@@ -1,7 +1,13 @@
|
||||
use once_cell::sync::Lazy;
|
||||
use rodio::{
|
||||
Decoder, DeviceSinkBuilder, MixerDeviceSink, Player, Source, cpal::BufferSize, source::Amplify,
|
||||
};
|
||||
use std::{fs::File, io::BufReader, sync::mpsc::Receiver};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::read,
|
||||
io::{BufReader, Cursor},
|
||||
sync::mpsc::Receiver,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AudioCmd {
|
||||
@@ -11,31 +17,40 @@ pub enum AudioCmd {
|
||||
ChangePart(SoundrackParts),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
pub enum SoundrackParts {
|
||||
Calm,
|
||||
Buildup,
|
||||
Assult,
|
||||
Assault,
|
||||
Outro,
|
||||
}
|
||||
|
||||
fn load_audio(part: &SoundrackParts) -> Amplify<Decoder<BufReader<File>>> {
|
||||
let file: File = match part {
|
||||
SoundrackParts::Calm => File::open("soundtrack/default/test.ogg").expect("open audio file"),
|
||||
SoundrackParts::Buildup => {
|
||||
File::open("soundtrack/default/test.ogg").expect("open audio file")
|
||||
}
|
||||
SoundrackParts::Assult => {
|
||||
File::open("soundtrack/default/test.ogg").expect("open audio file")
|
||||
}
|
||||
SoundrackParts::Outro => {
|
||||
File::open("soundtrack/default/test.ogg").expect("open audio file")
|
||||
}
|
||||
};
|
||||
static PRELOADED_DEFAULT_SOUNDTRACK: Lazy<HashMap<SoundrackParts, Vec<u8>>> = Lazy::new(|| {
|
||||
let mut map: HashMap<SoundrackParts, Vec<u8>> = HashMap::new();
|
||||
|
||||
Decoder::try_from(file)
|
||||
.expect("decoder issue")
|
||||
.amplify(0.20)
|
||||
macro_rules! load {
|
||||
($part:expr, $path:expr) => {
|
||||
map.insert($part, read($path).expect(concat!("cannot read ", $path)));
|
||||
};
|
||||
}
|
||||
|
||||
load!(SoundrackParts::Calm, "soundtrack/default/test.ogg"); // calm.ogg
|
||||
load!(SoundrackParts::Buildup, "soundtrack/default/test.ogg"); // buildup.ogg
|
||||
load!(SoundrackParts::Assault, "soundtrack/default/test.ogg"); // assault.ogg
|
||||
load!(SoundrackParts::Outro, "soundtrack/default/test.ogg"); // outro.ogg
|
||||
|
||||
map
|
||||
});
|
||||
|
||||
fn load_audio(part: &SoundrackParts) -> Amplify<Decoder<BufReader<Cursor<Vec<u8>>>>> {
|
||||
let data: &Vec<u8> = PRELOADED_DEFAULT_SOUNDTRACK
|
||||
.get(part)
|
||||
.expect("preloaded ogg not found");
|
||||
|
||||
let cursor: Cursor<Vec<u8>> = Cursor::new(data.clone());
|
||||
let reader: BufReader<Cursor<Vec<u8>>> = BufReader::new(cursor);
|
||||
|
||||
Decoder::new(reader).expect("decoder issue").amplify(0.20)
|
||||
}
|
||||
|
||||
pub fn handle_audio(rx: Receiver<AudioCmd>, mute: bool) {
|
||||
@@ -51,13 +66,6 @@ pub fn handle_audio(rx: Receiver<AudioCmd>, mute: bool) {
|
||||
let mut volume: f32 = player.volume();
|
||||
player.set_volume(if mute { 0.0 } else { volume });
|
||||
|
||||
// let preloaded_soundtrack_files: [File; 4] = [
|
||||
// File::open("soundtrack/default/test.ogg").expect("open audio file"),
|
||||
// File::open("soundtrack/default/test.ogg").expect("open audio file"),
|
||||
// File::open("soundtrack/default/test.ogg").expect("open audio file"),
|
||||
// File::open("soundtrack/default/test.ogg").expect("open audio file"),
|
||||
// ];
|
||||
|
||||
let mut current_part: SoundrackParts = SoundrackParts::Calm;
|
||||
player.append(load_audio(¤t_part));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user