Make volume curve cubed instead of squared
This commit is contained in:
+5
-4
@@ -39,16 +39,17 @@ impl AudioScheme {
|
||||
pub fn buffer(&mut self) -> [(i16, i16); HW_BUFFER_SIZE] {
|
||||
let mut mix_buffer = [(0i16, 0i16); HW_BUFFER_SIZE];
|
||||
|
||||
// Multiply each sample by the cube of volume divided by 100
|
||||
// This mimics natural perception of loudness
|
||||
let volume_factor = ((self.volume as f32) / 100.0).powi(3);
|
||||
for (_id, handle) in self.handles.iter_mut() {
|
||||
match handle {
|
||||
Handle::Audio { flags: _, ref mut buffer } => {
|
||||
let mut i = 0;
|
||||
while i < mix_buffer.len() {
|
||||
if let Some(sample) = buffer.pop_front() {
|
||||
// Multiply each sample by volume squared divided by 100 squared
|
||||
// This feels better than just multiplying by volume divided by 100
|
||||
let left = ((sample.0 as i32 * self.volume * self.volume) / 10000) as i16;
|
||||
let right = ((sample.1 as i32 * self.volume * self.volume) / 10000) as i16;
|
||||
let left = (sample.0 as f32 * volume_factor) as i16;
|
||||
let right = (sample.1 as f32 * volume_factor) as i16;
|
||||
mix_buffer[i].0 = mix_buffer[i].0.saturating_add(left);
|
||||
mix_buffer[i].1 = mix_buffer[i].1.saturating_add(right);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user