Skip to content

Commit f1b03ee

Browse files
committed
codal_port/modaudio: Take the sqrt of sound_level.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 19dbe4b commit f1b03ee

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/codal_port/modaudio.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include <math.h>
2829
#include "py/mphal.h"
2930
#include "drv_system.h"
3031
#include "modaudio.h"
@@ -134,7 +135,7 @@ static void audio_data_fetcher(void) {
134135
// Compute the sound level.
135136
sound_level += (src[i] - 128) * (src[i] - 128);
136137
}
137-
audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE / AUDIO_CHUNK_SIZE;
138+
audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE;
138139

139140
audio_buffer_ready();
140141
}
@@ -277,8 +278,10 @@ mp_obj_t is_playing(void) {
277278
}
278279
MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_is_playing_obj, is_playing);
279280

281+
// Returns a number between 0 and 254, being the average intensity of the sound played
282+
// from the most recent chunk of data.
280283
static mp_obj_t microbit_audio_sound_level(void) {
281-
return MP_OBJ_NEW_SMALL_INT(audio_current_sound_level);
284+
return MP_OBJ_NEW_SMALL_INT(2 * sqrt(audio_current_sound_level));
282285
}
283286
static MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_sound_level_obj, microbit_audio_sound_level);
284287

0 commit comments

Comments
 (0)