1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-15 18:32:48 +00:00
doukutsu-rs/src/sound/wave_bank.rs

50 lines
1.2 KiB
Rust
Raw Normal View History

2020-09-02 22:58:11 +00:00
use std::fmt;
2022-01-06 18:51:21 +00:00
use std::io;
use crate::sound::wav;
2020-09-02 22:58:11 +00:00
pub struct SoundBank {
2021-08-16 06:48:17 +00:00
pub wave100: Box<[u8; 25600]>,
2022-01-06 18:51:21 +00:00
pub samples: Vec<wav::WavSample>,
2020-09-02 22:58:11 +00:00
}
impl fmt::Display for SoundBank {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "WAVE100: {:2X?}...", &self.wave100[..8])?;
2022-01-06 18:51:21 +00:00
2022-01-17 22:29:30 +00:00
for sample in &self.samples {
2020-09-02 22:58:11 +00:00
writeln!(f, "{}", sample)?;
}
2022-01-06 18:51:21 +00:00
2020-09-02 22:58:11 +00:00
Ok(())
}
}
impl SoundBank {
pub fn load_from<R: io::Read>(mut f: R) -> io::Result<SoundBank> {
2021-08-16 06:48:17 +00:00
let mut wave100 = Box::new([0u8; 25600]);
2022-01-06 18:51:21 +00:00
2021-08-16 06:48:17 +00:00
f.read_exact(wave100.as_mut())?;
2022-01-06 18:51:21 +00:00
2020-09-02 22:58:11 +00:00
let mut samples = Vec::with_capacity(16);
2022-01-06 18:51:21 +00:00
2020-09-02 22:58:11 +00:00
loop {
match wav::WavSample::read_from(&mut f) {
2022-01-06 18:51:21 +00:00
Ok(sample) => {
log::info!("Loaded sample: {:?}", sample.format);
samples.push(sample)
}
Err(err) => {
log::error!("Failed to read next sample: {}", err);
return Ok(SoundBank { wave100, samples });
}
2020-09-02 22:58:11 +00:00
}
}
}
2022-01-06 18:51:21 +00:00
2020-09-02 22:58:11 +00:00
pub fn get_wave(&self, index: usize) -> &[u8] {
2022-01-06 18:51:21 +00:00
&self.wave100[index * 256..(index + 1) * 256]
2020-09-02 22:58:11 +00:00
}
}