Skip to content

Commit

Permalink
doom: Remember music volume across levels
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Oct 16, 2024
1 parent 74e9e88 commit 23a8bd3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 46 deletions.
1 change: 1 addition & 0 deletions disk-scripts/install_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ if ! [ -f sysroot/assets/FluidR3_GM.sf2 ]; then
fi

cp sysroot/assets/FluidR3_GM.sf2 mnt/
cp sysroot/assets/Retro_Synth_PC.sf2 mnt/

rsync -a sysroot/cykusz/usr mnt/
rsync -a sysroot/cykusz/etc mnt/
Expand Down
9 changes: 6 additions & 3 deletions userspace/doom/src/cykusz/audio/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl Read for MidiData {
}
}


impl MidiData {
fn to_sound(mut self, sound_font: &Arc<rustysynth::SoundFont>) -> kittyaudio::Sound {
use rustysynth::*;
Expand Down Expand Up @@ -120,6 +121,7 @@ pub struct Music {
current: Option<SoundHandle>,
sound_font: Arc<rustysynth::SoundFont>,
id: usize,
volume: f32,
}

impl Music {
Expand All @@ -131,16 +133,16 @@ impl Music {
current: None,
sound_font: Arc::new(rustysynth::SoundFont::new(&mut sf).unwrap()),
id: 1,
volume: 0.5,
}
}

pub fn shutdown(&mut self) {}

pub fn set_volume(&mut self, volume: c_int) {
self.volume = (volume as f32) / 128f32;
if let Some(track) = &self.current {
let volume = (volume as f32) / 128f32;

track.set_volume(volume);
track.set_volume(self.volume);
}
}

Expand Down Expand Up @@ -182,6 +184,7 @@ impl Music {
if let Some(handle) = handle {
self.current = Some(handle.clone());

handle.set_volume(self.volume);
handle.loop_all(looping);

self.mixer.play(handle.clone());
Expand Down
91 changes: 49 additions & 42 deletions userspace/doom/src/cykusz/audio/sounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,49 @@ impl sfxinfo_t {
std::str::from_utf8(name_bytes).unwrap().to_string()
}
}

fn get_sound(&self) -> Option<Box<kittyaudio::Sound>> {
let lumpnum = self.lumpnum;
let data: *mut u8 =
unsafe { W_CacheLumpNum(lumpnum, crate::doomgeneric::PU_STATIC as c_int) as *mut u8 };
let lumplen = unsafe { W_LumpLength(lumpnum as c_uint) };

if lumplen <= 8 {
return None;
}

let data = unsafe { std::slice::from_raw_parts_mut(data, lumplen as usize) };

if data[0] != 0x03 || data[1] != 0x00 {
return None;
}

let sample_rate = ((data[3] as u32) << 8) | (data[2] as u32);
let length: usize = ((data[7] as usize) << 24)
| ((data[6] as usize) << 16)
| ((data[5] as usize) << 8)
| (data[4] as usize);

if (length > lumplen as usize - 8) || (length <= 48) {
return None;
}

let data = &data[16..length - 16];

// do conversion
let audio = fon::Audio::<ChU8, 1>::with_u8_buffer(sample_rate, data);
let audio = fon::Audio::<Ch32, 2>::with_audio(44100, &audio);

let frames: &[Frame] = unsafe { std::mem::transmute(audio.as_slice()) };

let snd = kittyaudio::Sound::from_frames(44100, frames);

unsafe {
W_ReleaseLumpNum(lumpnum);
}

Some(Box::new(snd))
}
}

impl Sounds {
Expand Down Expand Up @@ -104,51 +147,15 @@ impl Sounds {
}

fn cache_sound(&mut self, sound: &mut sfxinfo_t) -> bool {
let lumpnum = sound.lumpnum;
let data: *mut u8 =
unsafe { W_CacheLumpNum(lumpnum, crate::doomgeneric::PU_STATIC as c_int) as *mut u8 };
let lumplen = unsafe { W_LumpLength(lumpnum as c_uint) };

if lumplen <= 8 {
return false;
}

let data = unsafe { std::slice::from_raw_parts_mut(data, lumplen as usize) };

if data[0] != 0x03 || data[1] != 0x00 {
return false;
}

let sample_rate = ((data[3] as u32) << 8) | (data[2] as u32);
let length: usize = ((data[7] as usize) << 24)
| ((data[6] as usize) << 16)
| ((data[5] as usize) << 8)
| (data[4] as usize);

if (length > lumplen as usize - 8) || (length <= 48) {
return false;
}

let data = &data[16..length - 16];
if let Some(snd) = sound.get_sound() {
let r = Box::into_raw(snd);

// do conversion
let audio = fon::Audio::<ChU8, 1>::with_u8_buffer(sample_rate, data);
let audio = fon::Audio::<Ch32, 2>::with_audio(44100, &audio);

let frames: &[Frame] = unsafe { std::mem::transmute(audio.as_slice()) };
sound.driver_data = r as *mut c_void;

let snd = kittyaudio::Sound::from_frames(44100, frames);

let b = Box::new(snd);
let r = Box::into_raw(b);

sound.driver_data = r as *mut c_void;

unsafe {
W_ReleaseLumpNum(lumpnum);
true
} else {
false
}

true
}

pub fn cache_sounds(&mut self, sounds: &mut [sfxinfo_t]) {
Expand Down
2 changes: 1 addition & 1 deletion userspace/playaudio/src/playmidi/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() -> Result<(), ExitCode> {

println!("soundfont: {soundfont_path}, midi: {midi_path}");

let mut sf2 = MMapFileReader::open(soundfont_path).map_err(|_| ExitCode::FAILURE)?;
let mut sf2 = File::open(soundfont_path).map_err(|_| ExitCode::FAILURE)?;
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());

println!("Creating sequencer...");
Expand Down

0 comments on commit 23a8bd3

Please sign in to comment.