@@ -304,18 +304,32 @@ MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
304304/******************************************************************************/
305305// AudioFrame class
306306
307- STATIC mp_obj_t microbit_audio_frame_new (const mp_obj_type_t * type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
307+ STATIC mp_obj_t microbit_audio_frame_new (const mp_obj_type_t * type_in , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
308308 (void )type_in ;
309- (void )args ;
310- mp_arg_check_num (n_args , n_kw , 0 , 1 , false);
311- size_t size = AUDIO_CHUNK_SIZE ;
312- if (n_args == 1 ) {
313- size = mp_obj_get_int (args [0 ]);
314- if (size <= 0 ) {
315- mp_raise_ValueError (MP_ERROR_TEXT ("size out of bounds" ));
316- }
309+
310+ enum { ARG_duration , ARG_rate };
311+ static const mp_arg_t allowed_args [] = {
312+ { MP_QSTR_duration , MP_ARG_INT , {.u_int = -1 } },
313+ { MP_QSTR_rate , MP_ARG_INT , {.u_int = 7812 } },
314+ };
315+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
316+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
317+
318+ mp_int_t rate = args [ARG_rate ].u_int ;
319+ if (rate <= 0 ) {
320+ mp_raise_ValueError (MP_ERROR_TEXT ("rate out of bounds" ));
321+ }
322+
323+ size_t size ;
324+ if (args [ARG_duration ].u_int < 0 ) {
325+ size = AUDIO_CHUNK_SIZE ;
326+ } else if (args [ARG_duration ].u_int == 0 ) {
327+ mp_raise_ValueError (MP_ERROR_TEXT ("size out of bounds" ));
328+ } else {
329+ size = args [ARG_duration ].u_int * rate / 1000 ;
317330 }
318- return microbit_audio_frame_make_new (size );
331+
332+ return microbit_audio_frame_make_new (size , rate );
319333}
320334
321335STATIC mp_obj_t audio_frame_subscr (mp_obj_t self_in , mp_obj_t index_in , mp_obj_t value_in ) {
@@ -373,7 +387,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
373387}
374388
375389static microbit_audio_frame_obj_t * copy (microbit_audio_frame_obj_t * self ) {
376- microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size );
390+ microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size , self -> rate );
377391 result -> used_size = self -> used_size ;
378392 for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
379393 result -> data [i ] = self -> data [i ];
@@ -457,7 +471,26 @@ STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj
457471 }
458472}
459473
474+ STATIC mp_obj_t audio_frame_get_rate (mp_obj_t self_in ) {
475+ microbit_audio_frame_obj_t * self = MP_OBJ_TO_PTR (self_in );
476+ return MP_OBJ_NEW_SMALL_INT (self -> rate );
477+ }
478+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (audio_frame_get_rate_obj , audio_frame_get_rate );
479+
480+ STATIC mp_obj_t audio_frame_set_rate (mp_obj_t self_in , mp_obj_t rate_in ) {
481+ microbit_audio_frame_obj_t * self = MP_OBJ_TO_PTR (self_in );
482+ mp_int_t rate = mp_obj_get_int (rate_in );
483+ if (rate <= 0 ) {
484+ mp_raise_ValueError (MP_ERROR_TEXT ("rate out of bounds" ));
485+ }
486+ self -> rate = rate ;
487+ return mp_const_none ;
488+ }
489+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (audio_frame_set_rate_obj , audio_frame_set_rate );
490+
460491STATIC const mp_map_elem_t microbit_audio_frame_locals_dict_table [] = {
492+ { MP_OBJ_NEW_QSTR (MP_QSTR_get_rate ), (mp_obj_t )& audio_frame_get_rate_obj },
493+ { MP_OBJ_NEW_QSTR (MP_QSTR_set_rate ), (mp_obj_t )& audio_frame_set_rate_obj },
461494 { MP_OBJ_NEW_QSTR (MP_QSTR_copyfrom ), (mp_obj_t )& copyfrom_obj },
462495};
463496STATIC MP_DEFINE_CONST_DICT (microbit_audio_frame_locals_dict , microbit_audio_frame_locals_dict_table );
@@ -474,11 +507,12 @@ MP_DEFINE_CONST_OBJ_TYPE(
474507 locals_dict , & microbit_audio_frame_locals_dict
475508 );
476509
477- microbit_audio_frame_obj_t * microbit_audio_frame_make_new (size_t size ) {
510+ microbit_audio_frame_obj_t * microbit_audio_frame_make_new (size_t size , uint32_t rate ) {
478511 microbit_audio_frame_obj_t * res = m_new_obj_var (microbit_audio_frame_obj_t , uint8_t , size );
479512 res -> base .type = & microbit_audio_frame_type ;
480513 res -> alloc_size = size ;
481514 res -> used_size = 0 ;
515+ res -> rate = rate ;
482516 memset (res -> data , 128 , size );
483517 return res ;
484518}
0 commit comments