MP3File: Add rms_level property
This lets a music player show it vu-meter style
This commit is contained in:
parent
97bb46c047
commit
ec22520992
|
@ -224,6 +224,24 @@ const mp_obj_property_t audiomp3_mp3file_channel_count_obj = {
|
|||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
//| .. attribute:: rms_level
|
||||
//|
|
||||
//| The RMS audio level of a recently played moment of audio. (read only)
|
||||
//|
|
||||
STATIC mp_obj_t audiomp3_mp3file_obj_get_rms_level(mp_obj_t self_in) {
|
||||
audiomp3_mp3file_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
check_for_deinit(self);
|
||||
return mp_obj_new_float(common_hal_audiomp3_mp3file_get_rms_level(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(audiomp3_mp3file_get_rms_level_obj, audiomp3_mp3file_obj_get_rms_level);
|
||||
|
||||
const mp_obj_property_t audiomp3_mp3file_rms_level_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&audiomp3_mp3file_get_rms_level_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
|
||||
STATIC const mp_rom_map_elem_t audiomp3_mp3file_locals_dict_table[] = {
|
||||
// Methods
|
||||
|
@ -236,6 +254,7 @@ STATIC const mp_rom_map_elem_t audiomp3_mp3file_locals_dict_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audiomp3_mp3file_sample_rate_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bits_per_sample), MP_ROM_PTR(&audiomp3_mp3file_bits_per_sample_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_channel_count), MP_ROM_PTR(&audiomp3_mp3file_channel_count_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_rms_level), MP_ROM_PTR(&audiomp3_mp3file_rms_level_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(audiomp3_mp3file_locals_dict, audiomp3_mp3file_locals_dict_table);
|
||||
|
||||
|
|
|
@ -45,5 +45,6 @@ uint32_t common_hal_audiomp3_mp3file_get_sample_rate(audiomp3_mp3file_obj_t* sel
|
|||
void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t* self, uint32_t sample_rate);
|
||||
uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t* self);
|
||||
uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t* self);
|
||||
float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_MP3FILE_H
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue