summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-01-02 15:23:42 -0600
committerJeff Epler <jepler@gmail.com>2020-01-02 15:23:42 -0600
commitec22520992723a89dbc57a5e59bf03815df9f509 (patch)
tree216cadcdb372249eebc04614b1a2ebaa9f91cda9 /shared-module
parent97bb46c0471b5cb9a0323f06183945d4c711207b (diff)
MP3File: Add rms_level property
This lets a music player show it vu-meter style
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/audiomp3/MP3File.c11
-rw-r--r--shared-module/audiomp3/MP3File.h2
2 files changed, 13 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));
+}
diff --git a/shared-module/audiomp3/MP3File.h b/shared-module/audiomp3/MP3File.h
index 9d99e8d1f..9ee1d0949 100644
--- a/shared-module/audiomp3/MP3File.h
+++ b/shared-module/audiomp3/MP3File.h
@@ -67,4 +67,6 @@ void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool si
bool* single_buffer, bool* samples_signed,
uint32_t* max_buffer_length, uint8_t* spacing);
+float audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MP3FILE_H