@@ -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 }
@@ -332,7 +332,7 @@ STATIC mp_obj_t microbit_audio_frame_new(const mp_obj_type_t *type_in, mp_uint_t
332332STATIC mp_obj_t audio_frame_subscr (mp_obj_t self_in , mp_obj_t index_in , mp_obj_t value_in ) {
333333 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
334334 mp_int_t index = mp_obj_get_int (index_in );
335- if (index < 0 || index >= self -> alloc_size ) {
335+ if (index < 0 || index >= self -> used_size ) {
336336 mp_raise_ValueError (MP_ERROR_TEXT ("index out of bounds" ));
337337 }
338338 if (value_in == MP_OBJ_NULL ) {
@@ -355,7 +355,7 @@ static mp_obj_t audio_frame_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
355355 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
356356 switch (op ) {
357357 case MP_UNARY_OP_LEN :
358- return MP_OBJ_NEW_SMALL_INT (self -> alloc_size );
358+ return MP_OBJ_NEW_SMALL_INT (self -> used_size );
359359 default :
360360 return MP_OBJ_NULL ; // op not supported
361361 }
@@ -365,14 +365,14 @@ static mp_int_t audio_frame_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufin
365365 (void )flags ;
366366 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
367367 bufinfo -> buf = self -> data ;
368- bufinfo -> len = self -> alloc_size ;
368+ bufinfo -> len = self -> used_size ;
369369 bufinfo -> typecode = 'b' ;
370370 return 0 ;
371371}
372372
373373static void add_into (microbit_audio_frame_obj_t * self , microbit_audio_frame_obj_t * other , bool add ) {
374374 int mult = add ? 1 : -1 ;
375- size_t size = MIN (self -> alloc_size , other -> alloc_size );
375+ size_t size = MIN (self -> used_size , other -> used_size );
376376 for (int i = 0 ; i < size ; i ++ ) {
377377 unsigned val = (int )self -> data [i ] + mult * (other -> data [i ]- 128 );
378378 // Clamp to 0-255
@@ -385,6 +385,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
385385
386386static microbit_audio_frame_obj_t * copy (microbit_audio_frame_obj_t * self ) {
387387 microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size );
388+ result -> used_size = self -> used_size ;
388389 for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
389390 result -> data [i ] = self -> data [i ];
390391 }
@@ -395,7 +396,7 @@ mp_obj_t copyfrom(mp_obj_t self_in, mp_obj_t other) {
395396 microbit_audio_frame_obj_t * self = (microbit_audio_frame_obj_t * )self_in ;
396397 mp_buffer_info_t bufinfo ;
397398 mp_get_buffer_raise (other , & bufinfo , MP_BUFFER_READ );
398- uint32_t len = MIN (bufinfo .len , self -> alloc_size );
399+ uint32_t len = MIN (bufinfo .len , self -> used_size );
399400 for (uint32_t i = 0 ; i < len ; i ++ ) {
400401 self -> data [i ] = ((uint8_t * )bufinfo .buf )[i ];
401402 }
@@ -432,7 +433,7 @@ int32_t float_to_fixed(float f, uint32_t scale) {
432433
433434static void mult (microbit_audio_frame_obj_t * self , float f ) {
434435 int scaled = float_to_fixed (f , 15 );
435- for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
436+ for (int i = 0 ; i < self -> used_size ; i ++ ) {
436437 unsigned val = ((((int )self -> data [i ]- 128 ) * scaled ) >> 15 )+ 128 ;
437438 if (val > 255 ) {
438439 val = (1 - (val >>31 ))* 255 ;
@@ -488,6 +489,7 @@ microbit_audio_frame_obj_t *microbit_audio_frame_make_new(size_t size) {
488489 microbit_audio_frame_obj_t * res = m_new_obj_var (microbit_audio_frame_obj_t , uint8_t , size );
489490 res -> base .type = & microbit_audio_frame_type ;
490491 res -> alloc_size = size ;
492+ res -> used_size = 0 ;
491493 memset (res -> data , 128 , size );
492494 return res ;
493495}
0 commit comments