@@ -315,18 +315,32 @@ MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
315315/******************************************************************************/
316316// AudioFrame class
317317
318- 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 ) {
318+ 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 ) {
319319 (void )type_in ;
320- (void )args ;
321- mp_arg_check_num (n_args , n_kw , 0 , 1 , false);
322- size_t size = AUDIO_CHUNK_SIZE ;
323- if (n_args == 1 ) {
324- size = mp_obj_get_int (args [0 ]);
325- if (size <= 0 ) {
326- mp_raise_ValueError (MP_ERROR_TEXT ("size out of bounds" ));
327- }
320+
321+ enum { ARG_duration , ARG_rate };
322+ static const mp_arg_t allowed_args [] = {
323+ { MP_QSTR_duration , MP_ARG_INT , {.u_int = -1 } },
324+ { MP_QSTR_rate , MP_ARG_INT , {.u_int = 7812 } },
325+ };
326+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
327+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
328+
329+ mp_uint_t rate = args [ARG_rate ].u_int ;
330+ if (rate <= 0 ) {
331+ mp_raise_ValueError (MP_ERROR_TEXT ("rate out of bounds" ));
332+ }
333+
334+ size_t size ;
335+ if (args [ARG_duration ].u_int < 0 ) {
336+ size = AUDIO_CHUNK_SIZE ;
337+ } else if (args [ARG_duration ].u_int == 0 ) {
338+ mp_raise_ValueError (MP_ERROR_TEXT ("size out of bounds" ));
339+ } else {
340+ size = args [ARG_duration ].u_int * rate / 1000 ;
328341 }
329- return microbit_audio_frame_make_new (size );
342+
343+ return microbit_audio_frame_make_new (size , rate );
330344}
331345
332346STATIC mp_obj_t audio_frame_subscr (mp_obj_t self_in , mp_obj_t index_in , mp_obj_t value_in ) {
@@ -384,7 +398,7 @@ static void add_into(microbit_audio_frame_obj_t *self, microbit_audio_frame_obj_
384398}
385399
386400static microbit_audio_frame_obj_t * copy (microbit_audio_frame_obj_t * self ) {
387- microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size );
401+ microbit_audio_frame_obj_t * result = microbit_audio_frame_make_new (self -> alloc_size , self -> rate );
388402 result -> used_size = self -> used_size ;
389403 for (int i = 0 ; i < self -> alloc_size ; i ++ ) {
390404 result -> data [i ] = self -> data [i ];
@@ -468,7 +482,26 @@ STATIC mp_obj_t audio_frame_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj
468482 }
469483}
470484
485+ STATIC mp_obj_t audio_frame_get_rate (mp_obj_t self_in ) {
486+ microbit_audio_frame_obj_t * self = MP_OBJ_TO_PTR (self_in );
487+ return MP_OBJ_NEW_SMALL_INT (self -> rate );
488+ }
489+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (audio_frame_get_rate_obj , audio_frame_get_rate );
490+
491+ STATIC mp_obj_t audio_frame_set_rate (mp_obj_t self_in , mp_obj_t rate_in ) {
492+ microbit_audio_frame_obj_t * self = MP_OBJ_TO_PTR (self_in );
493+ mp_uint_t rate = mp_obj_get_int (rate_in );
494+ if (rate <= 0 ) {
495+ mp_raise_ValueError (MP_ERROR_TEXT ("rate out of bounds" ));
496+ }
497+ self -> rate = rate ;
498+ return mp_const_none ;
499+ }
500+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (audio_frame_set_rate_obj , audio_frame_set_rate );
501+
471502STATIC const mp_map_elem_t microbit_audio_frame_locals_dict_table [] = {
503+ { MP_OBJ_NEW_QSTR (MP_QSTR_get_rate ), (mp_obj_t )& audio_frame_get_rate_obj },
504+ { MP_OBJ_NEW_QSTR (MP_QSTR_set_rate ), (mp_obj_t )& audio_frame_set_rate_obj },
472505 { MP_OBJ_NEW_QSTR (MP_QSTR_copyfrom ), (mp_obj_t )& copyfrom_obj },
473506};
474507STATIC MP_DEFINE_CONST_DICT (microbit_audio_frame_locals_dict , microbit_audio_frame_locals_dict_table );
@@ -485,11 +518,12 @@ MP_DEFINE_CONST_OBJ_TYPE(
485518 locals_dict , & microbit_audio_frame_locals_dict
486519 );
487520
488- microbit_audio_frame_obj_t * microbit_audio_frame_make_new (size_t size ) {
521+ microbit_audio_frame_obj_t * microbit_audio_frame_make_new (size_t size , uint16_t rate ) {
489522 microbit_audio_frame_obj_t * res = m_new_obj_var (microbit_audio_frame_obj_t , uint8_t , size );
490523 res -> base .type = & microbit_audio_frame_type ;
491524 res -> alloc_size = size ;
492525 res -> used_size = 0 ;
526+ res -> rate = rate ;
493527 memset (res -> data , 128 , size );
494528 return res ;
495529}
0 commit comments