-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b687297
commit 98307de
Showing
7 changed files
with
83 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod audio_source; | ||
pub mod user_input_event; | ||
pub mod loudness; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pub fn calculate_loudness(samples: &[f32]) -> f64 { | ||
if samples.is_empty() { | ||
return 0.0; | ||
} | ||
|
||
// Calculate the root mean square (RMS) | ||
let rms: f64 = (samples | ||
.iter() | ||
.map(|&s| { | ||
let sample = s as f64; | ||
sample * sample | ||
}) | ||
.sum::<f64>() | ||
/ samples.len() as f64) | ||
.sqrt(); | ||
|
||
// Convert to decibels | ||
let db = 20.0 * rms.log10(); | ||
|
||
// Map decibels to 0-100 range | ||
// Assuming -60 dB as the minimum audible level and 0 dB as the maximum | ||
let normalized_loudness = (db + 60.0) * (1.0 / 60.0); | ||
|
||
normalized_loudness.clamp(0.0, 1.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters