summaryrefslogtreecommitdiff
path: root/shared-module/audiomp3/MP3File.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module/audiomp3/MP3File.c')
-rw-r--r--shared-module/audiomp3/MP3File.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/shared-module/audiomp3/MP3File.c b/shared-module/audiomp3/MP3File.c
index 390ba2544..515aa0086 100644
--- a/shared-module/audiomp3/MP3File.c
+++ b/shared-module/audiomp3/MP3File.c
@@ -29,6 +29,7 @@
#include <stdint.h>
#include <string.h>
+#include <math.h>
#include "py/mperrno.h"
#include "py/runtime.h"
@@ -339,3 +340,13 @@ void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool si
*spacing = 1;
}
}
+
+float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self) {
+ float sumsq = 0.f;
+ // Assumes no DC component to the audio. Is that a safe assumption?
+ int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
+ for(size_t i=0; i<self->frame_buffer_size / sizeof(int16_t); i++) {
+ sumsq += (float)buffer[i] * buffer[i];
+ }
+ return sqrtf(sumsq) / (self->frame_buffer_size / sizeof(int16_t));
+}