Allow for volume control

This commit is contained in:
Gwilym Kuiper 2021-06-25 21:57:24 +01:00
parent c81d73f8d5
commit 3c0d881a45
2 changed files with 13 additions and 2 deletions

View file

@ -30,6 +30,7 @@ pub struct SoundChannel {
should_loop: bool,
playback_speed: Num<usize, 8>,
volume: Num<i16, 4>, // between 0 and 1
panning: Num<i16, 4>, // between -1 and 1
is_done: bool,
@ -47,6 +48,7 @@ impl SoundChannel {
panning: 0.into(),
is_done: false,
priority: SoundPriority::Low,
volume: 1.into(),
}
}
@ -59,6 +61,7 @@ impl SoundChannel {
panning: 0.into(),
is_done: false,
priority: SoundPriority::High,
volume: 1.into(),
}
}
@ -79,4 +82,12 @@ impl SoundChannel {
self.panning = panning;
self
}
pub fn volume<'a>(&'a mut self, volume: Num<i16, 4>) -> &'a mut Self {
assert!(volume <= Num::new(1), "volume must be <= 1");
assert!(volume >= Num::new(0), "volume must be >= 0");
self.volume = volume;
self
}
}

View file

@ -117,8 +117,8 @@ impl MixerBuffer {
continue;
}
let right_amount = (channel.panning + 1) / 2;
let left_amount = -right_amount + 1;
let right_amount = ((channel.panning + 1) / 2) * channel.volume;
let left_amount = (-right_amount + 1) * channel.volume;
if channel.pos + channel.playback_speed * SOUND_BUFFER_SIZE >= channel.data.len().into()
{