File tree Expand file tree Collapse file tree 1 file changed +5
-2
lines changed Expand file tree Collapse file tree 1 file changed +5
-2
lines changed Original file line number Diff line number Diff line change 25
25
* THE SOFTWARE.
26
26
*/
27
27
28
+ #include <math.h>
28
29
#include "py/mphal.h"
29
30
#include "drv_system.h"
30
31
#include "modaudio.h"
@@ -133,7 +134,7 @@ STATIC void audio_data_fetcher(void) {
133
134
// Compute the sound level.
134
135
sound_level += (src [i ] - 128 ) * (src [i ] - 128 );
135
136
}
136
- audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE / AUDIO_CHUNK_SIZE ;
137
+ audio_current_sound_level = sound_level / AUDIO_CHUNK_SIZE ;
137
138
138
139
audio_buffer_ready ();
139
140
}
@@ -290,8 +291,10 @@ mp_obj_t is_playing(void) {
290
291
}
291
292
MP_DEFINE_CONST_FUN_OBJ_0 (microbit_audio_is_playing_obj , is_playing );
292
293
294
+ // Returns a number between 0 and 254, being the average intensity of the sound played
295
+ // from the most recent chunk of data.
293
296
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 ) );
295
298
}
296
299
STATIC MP_DEFINE_CONST_FUN_OBJ_0 (microbit_audio_sound_level_obj , microbit_audio_sound_level );
297
300
You can’t perform that action at this time.
0 commit comments