Skip to content

Commit ec22520

Browse files
committed
MP3File: Add rms_level property
This lets a music player show it vu-meter style
1 parent 97bb46c commit ec22520

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

shared-bindings/audiomp3/MP3File.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ const mp_obj_property_t audiomp3_mp3file_channel_count_obj = {
224224
(mp_obj_t)&mp_const_none_obj},
225225
};
226226

227+
//| .. attribute:: rms_level
228+
//|
229+
//| The RMS audio level of a recently played moment of audio. (read only)
230+
//|
231+
STATIC mp_obj_t audiomp3_mp3file_obj_get_rms_level(mp_obj_t self_in) {
232+
audiomp3_mp3file_obj_t *self = MP_OBJ_TO_PTR(self_in);
233+
check_for_deinit(self);
234+
return mp_obj_new_float(common_hal_audiomp3_mp3file_get_rms_level(self));
235+
}
236+
MP_DEFINE_CONST_FUN_OBJ_1(audiomp3_mp3file_get_rms_level_obj, audiomp3_mp3file_obj_get_rms_level);
237+
238+
const mp_obj_property_t audiomp3_mp3file_rms_level_obj = {
239+
.base.type = &mp_type_property,
240+
.proxy = {(mp_obj_t)&audiomp3_mp3file_get_rms_level_obj,
241+
(mp_obj_t)&mp_const_none_obj,
242+
(mp_obj_t)&mp_const_none_obj},
243+
};
244+
227245

228246
STATIC const mp_rom_map_elem_t audiomp3_mp3file_locals_dict_table[] = {
229247
// Methods
@@ -236,6 +254,7 @@ STATIC const mp_rom_map_elem_t audiomp3_mp3file_locals_dict_table[] = {
236254
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audiomp3_mp3file_sample_rate_obj) },
237255
{ MP_ROM_QSTR(MP_QSTR_bits_per_sample), MP_ROM_PTR(&audiomp3_mp3file_bits_per_sample_obj) },
238256
{ MP_ROM_QSTR(MP_QSTR_channel_count), MP_ROM_PTR(&audiomp3_mp3file_channel_count_obj) },
257+
{ MP_ROM_QSTR(MP_QSTR_rms_level), MP_ROM_PTR(&audiomp3_mp3file_rms_level_obj) },
239258
};
240259
STATIC MP_DEFINE_CONST_DICT(audiomp3_mp3file_locals_dict, audiomp3_mp3file_locals_dict_table);
241260

shared-bindings/audiomp3/MP3File.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ uint32_t common_hal_audiomp3_mp3file_get_sample_rate(audiomp3_mp3file_obj_t* sel
4545
void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t* self, uint32_t sample_rate);
4646
uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t* self);
4747
uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t* self);
48+
float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self);
4849

4950
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_MP3FILE_H

shared-module/audiomp3/MP3File.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <stdint.h>
3131
#include <string.h>
32+
#include <math.h>
3233

3334
#include "py/mperrno.h"
3435
#include "py/runtime.h"
@@ -339,3 +340,13 @@ void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool si
339340
*spacing = 1;
340341
}
341342
}
343+
344+
float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self) {
345+
float sumsq = 0.f;
346+
// Assumes no DC component to the audio. Is that a safe assumption?
347+
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
348+
for(size_t i=0; i<self->frame_buffer_size / sizeof(int16_t); i++) {
349+
sumsq += (float)buffer[i] * buffer[i];
350+
}
351+
return sqrtf(sumsq) / (self->frame_buffer_size / sizeof(int16_t));
352+
}

shared-module/audiomp3/MP3File.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool si
6767
bool* single_buffer, bool* samples_signed,
6868
uint32_t* max_buffer_length, uint8_t* spacing);
6969

70+
float audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self);
71+
7072
#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MP3FILE_H

0 commit comments

Comments
 (0)