@@ -76,7 +76,7 @@ STATIC void audio_data_fetcher(void) {
7676
7777 if (audio_source_frame != NULL ) {
7878 // An existing AudioFrame is being played, see if there's any data left.
79- if (audio_raw_offset >= audio_source_frame -> alloc_size ) {
79+ if (audio_raw_offset >= audio_source_frame -> used_size ) {
8080 // AudioFrame is exhausted.
8181 audio_source_frame = NULL ;
8282 }
@@ -321,7 +321,7 @@ STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t
321321STATIC mp_obj_t audio_frame_subscr (mp_obj_t self_in , mp_obj_t index_in , mp_obj_t value_in ) {
322322 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
323323 mp_int_t index = mp_obj_get_int (index_in );
324- if (index < 0 || index >= self -> alloc_size ) {
324+ if (index < 0 || index >= self -> used_size ) {
325325 mp_raise_ValueError (MP_ERROR_TEXT ("index out of bounds" ));
326326 }
327327 if (value_in == MP_OBJ_NULL ) {
@@ -344,7 +344,7 @@ static mp_obj_t audio_frame_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
344344 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
345345 switch (op ) {
346346 case MP_UNARY_OP_LEN :
347- return MP_OBJ_NEW_SMALL_INT (self -> alloc_size );
347+ return MP_OBJ_NEW_SMALL_INT (self -> used_size );
348348 default :
349349 return MP_OBJ_NULL ; // op not supported
350350 }
@@ -354,14 +354,14 @@ static mp_int_t audio_frame_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufin
354354 (void )flags ;
355355 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
356356 bufinfo -> buf = self -> data ;
357- bufinfo -> len = self -> alloc_size ;
357+ bufinfo -> len = self -> used_size ;
358358 bufinfo -> typecode = 'b' ;
359359 return 0 ;
360360}
361361
362362static void add_into (microbit_audio_frame_obj_t * self , microbit_audio_frame_obj_t * other , bool add ) {
363363 int mult = add ? 1 : -1 ;
364- size_t size = MIN (self -> alloc_size , other -> alloc_size );
364+ size_t size = MIN (self -> used_size , other -> used_size );
365365 for (int i = 0 ; i < size ; i ++ ) {
366366 unsigned val = (int )self -> data [i ] + mult * (other -> data [i ]- 128 );
367367 // Clamp to 0-255
@@ -374,6 +374,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
374374
375375static microbit_audio_frame_obj_t * copy (microbit_audio_frame_obj_t * self ) {
376376 microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size );
377+ result -> used_size = self -> used_size ;
377378 for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
378379 result -> data [i ] = self -> data [i ];
379380 }
@@ -384,7 +385,7 @@ mp_obj_t copyfrom(mp_obj_t self_in, mp_obj_t other) {
384385 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
385386 mp_buffer_info_t bufinfo ;
386387 mp_get_buffer_raise (other , & bufinfo , MP_BUFFER_READ );
387- uint32_t len = MIN (bufinfo .len , self -> alloc_size );
388+ uint32_t len = MIN (bufinfo .len , self -> used_size );
388389 for (uint32_t i = 0 ; i < len ; i ++ ) {
389390 self -> data [i ] = ((uint8_t * )bufinfo .buf )[i ];
390391 }
@@ -421,7 +422,7 @@ int32_t float_to_fixed(float f, uint32_t scale) {
421422
422423static void mult (microbit_audio_frame_obj_t * self , float f ) {
423424 int scaled = float_to_fixed (f , 15 );
424- for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
425+ for (int i = 0 ; i < self -> used_size ; i ++ ) {
425426 unsigned val = ((((int )self -> data [i ]- 128 ) * scaled ) >> 15 )+ 128 ;
426427 if (val > 255 ) {
427428 val = (1 - (val >>31 ))* 255 ;
@@ -477,6 +478,7 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
477478 microbit_audio_frame_obj_t * res = m_new_obj_var (microbit_audio_frame_obj_t , uint8_t , size );
478479 res -> base .type = & microbit_audio_frame_type ;
479480 res -> alloc_size = size ;
481+ res -> used_size = 0 ;
480482 memset (res -> data , 128 , size );
481483 return res ;
482484}
0 commit comments