Skip to content

Commit faa52ae

Browse files
committed
codal_port/modaudio: Take the sqrt of sound_level.
Signed-off-by: Damien George <[email protected]>
1 parent 537e547 commit faa52ae

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"
@@ -133,7 +134,7 @@ STATIC void audio_data_fetcher(void) {
133134
// Compute the sound level.
134135
sound_level += (src[i] - 128) * (src[i] - 128);
135136
}
136-
audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE / AUDIO_CHUNK_SIZE;
137+
audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE;
137138

138139
audio_buffer_ready();
139140
}
@@ -290,8 +291,10 @@ mp_obj_t is_playing(void) {
290291
}
291292
MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_is_playing_obj, is_playing);
292293

294+
// Returns a number between 0 and 254, being the average intensity of the sound played
295+
// from the most recent chunk of data.
293296
STATIC mp_obj_t microbit_audio_sound_level(void) {
294-
return MP_OBJ_NEW_SMALL_INT(audio_current_sound_level);
297+
return MP_OBJ_NEW_SMALL_INT(2 * sqrt(audio_current_sound_level));
295298
}
296299
STATIC MP_DEFINE_CONST_FUN_OBJ_0(microbit_audio_sound_level_obj, microbit_audio_sound_level);
297300

0 commit comments

Comments
 (0)