@@ -106,7 +106,7 @@ STATIC mp_obj_t audioio_mixer_make_new(const mp_obj_type_t *type, size_t n_args,
106106 if (bits_per_sample != 8 && bits_per_sample != 16 ) {
107107 mp_raise_ValueError (translate ("bits_per_sample must be 8 or 16" ));
108108 }
109- audioio_mixer_obj_t * self = m_new_obj_var (audioio_mixer_obj_t , audioio_mixer_voice_obj_t , voice_count );
109+ audioio_mixer_obj_t * self = m_new_obj_var (audioio_mixer_obj_t , mp_obj_t , voice_count );
110110 self -> base .type = & audioio_mixer_type ;
111111 common_hal_audioio_mixer_construct (self , voice_count , args [ARG_buffer_size ].u_int , bits_per_sample , args [ARG_samples_signed ].u_bool , channel_count , sample_rate );
112112
@@ -226,208 +226,25 @@ const mp_obj_property_t audioio_mixer_sample_rate_obj = {
226226 (mp_obj_t )& mp_const_none_obj },
227227};
228228
229- //| .. currentmodule:: audioio
230- //|
231- //| :class:`Mixer` -- Mixes one or more audio samples together
232- //| ===========================================================
233- //|
234- //| Mixer mixes multiple samples into one sample.
235- //|
236- //| .. class:: Mixer(channel_count=2, buffer_size=1024)
237- //|
238- //| Create a Mixer object that can mix multiple channels with the same sample rate.
239- //|
240- //| :param int channel_count: The maximum number of samples to mix at once
241- //| :param int buffer_size: The total size in bytes of the buffers to mix into
242- //|
243- STATIC mp_obj_t audioio_mixer_voice_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * pos_args ) {
244- #if 0
245- mp_arg_check_num (n_args , n_kw , 0 , 2 , true);
246- mp_map_t kw_args ;
247- mp_map_init_fixed_table (& kw_args , n_kw , pos_args + n_args );
248- enum { ARG_voice_count , ARG_buffer_size , ARG_channel_count , ARG_bits_per_sample , ARG_samples_signed , ARG_sample_rate };
249- static const mp_arg_t allowed_args [] = {
250- { MP_QSTR_voice_count , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 2 } },
251- { MP_QSTR_buffer_size , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 1024 } },
252- { MP_QSTR_channel_count , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 2 } },
253- { MP_QSTR_bits_per_sample , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 16 } },
254- { MP_QSTR_samples_signed , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = true} },
255- { MP_QSTR_sample_rate , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 8000 } },
256- };
257- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
258- mp_arg_parse_all (n_args , pos_args , & kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
259-
260- mp_int_t voice_count = args [ARG_voice_count ].u_int ;
261- if (voice_count < 1 || voice_count > 255 ) {
262- mp_raise_ValueError (translate ("Invalid voice count" ));
263- }
264-
265- mp_int_t channel_count = args [ARG_channel_count ].u_int ;
266- if (channel_count < 1 || channel_count > 2 ) {
267- mp_raise_ValueError (translate ("Invalid channel count" ));
268- }
269- mp_int_t sample_rate = args [ARG_sample_rate ].u_int ;
270- if (sample_rate < 1 ) {
271- mp_raise_ValueError (translate ("Sample rate must be positive" ));
272- }
273- mp_int_t bits_per_sample = args [ARG_bits_per_sample ].u_int ;
274- if (bits_per_sample != 8 && bits_per_sample != 16 ) {
275- mp_raise_ValueError (translate ("bits_per_sample must be 8 or 16" ));
276- }
277- audioio_mixer_obj_t * self = m_new_obj_var (audioio_mixer_obj_t , audioio_mixer_voice_obj_t , voice_count );
278- self -> base .type = & audioio_mixer_type ;
279- common_hal_audioio_mixer_construct (self , voice_count , args [ARG_buffer_size ].u_int , bits_per_sample , args [ARG_samples_signed ].u_bool , channel_count , sample_rate );
280-
281- return MP_OBJ_FROM_PTR (self );
282- #endif
283- return mp_const_none ;
284- }
285-
286- //| .. method:: deinit()
287- //|
288- //| Deinitialises the Voice and releases any hardware resources for reuse.
289- //|
290- STATIC mp_obj_t audioio_mixer_voice_deinit (mp_obj_t self_in ) {
291- #if 0
292- audioio_mixer_voice_obj_t * self = MP_OBJ_TO_PTR (self_in );
293- common_hal_audioio_mixer_voice_deinit (self );
294- #endif
295- return mp_const_none ;
296- }
297- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (audioio_mixer_voice_deinit_obj , audioio_mixer_voice_deinit );
298-
299- //| .. method:: __enter__()
300- //|
301- //| No-op used by Context Managers.
302- //|
303- // Provided by context manager helper.
304-
305- //| .. method:: __exit__()
306- //|
307- //| Automatically deinitializes the hardware when exiting a context. See
308- //| :ref:`lifetime-and-contextmanagers` for more info.
309- //|
310- STATIC mp_obj_t audioio_mixer_voice_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
311- #if 0
312- (void )n_args ;
313- common_hal_audioio_mixer_voice_deinit (args [0 ]);
314- #endif
315- return mp_const_none ;
316- }
317- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (audioio_mixer_voice___exit___obj , 4 , 4 , audioio_mixer_voice_obj___exit__ );
318-
319-
320- //| .. method:: play(sample, *, loop=False)
321- //|
322- //| Plays the sample once when loop=False and continuously when loop=True.
323- //| Does not block. Use `playing` to block.
324- //|
325- //| Sample must be an `audioio.WaveFile`, `audioio.Mixer` or `audioio.RawSample`.
326- //|
327- //| The sample must match the Mixer's encoding settings given in the constructor.
328- //|
329- STATIC mp_obj_t audioio_mixer_voice_obj_play (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
330- #if 0
331- enum { ARG_sample , ARG_loop };
332- static const mp_arg_t allowed_args [] = {
333- { MP_QSTR_sample , MP_ARG_OBJ | MP_ARG_REQUIRED },
334- { MP_QSTR_loop , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = false} },
335- };
336- audioio_mixer_voice_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
337- raise_error_if_deinited (common_hal_audioio_mixer_voice_deinited (self ));
338- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
339- mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
340-
341- mp_obj_t sample = args [ARG_sample ].u_obj ;
342- common_hal_audioio_mixer_play (self , sample , self -> u_int , args [ARG_loop ].u_ool );
343- #endif
344- return mp_const_none ;
345- }
346- MP_DEFINE_CONST_FUN_OBJ_KW (audioio_mixer_voice_play_obj , 1 , audioio_mixer_voice_obj_play );
347-
348- //| .. method:: stop_voice()
229+ //| .. attribute:: voice
349230//|
350- //| Stops playback of the sample on this voice.
351- //|
352- STATIC mp_obj_t audioio_mixer_voice_obj_stop (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
353- #if 0
354- enum { ARG_voice };
355- static const mp_arg_t allowed_args [] = {
356- { MP_QSTR_voice , MP_ARG_INT , {.u_int = 0 } },
357- };
358- audioio_mixer_voice_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
359- raise_error_if_deinited (common_hal_audioio_mixer_deinited (self ));
360- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
361- mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
362-
363- common_hal_audioio_mixer_stop_voice (self , args [ARG_voice ].u_int );
364- #endif
365- return mp_const_none ;
366- }
367- MP_DEFINE_CONST_FUN_OBJ_KW (audioio_mixer_voice_stop_obj , 1 , audioio_mixer_voice_obj_stop );
368-
369- //| .. method:: set_gain(voice, gain)
231+ //| tuple of voice objects
370232//|
371- //| Set the gain of a voice.
372- //|
373- //| gain must be a floating point number between 0 and 1
374- //|
375- STATIC mp_obj_t audioio_mixer_voice_obj_set_gain (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
376- #if 0
377- enum { ARG_voice , ARG_gain };
378- static const mp_arg_t allowed_args [] = {
379- { MP_QSTR_voice , MP_ARG_INT | MP_ARG_REQUIRED },
380- { MP_QSTR_gain , MP_ARG_OBJ | MP_ARG_REQUIRED },
381- };
382- audioio_mixer_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
383- raise_error_if_deinited (common_hal_audioio_mixer_deinited (self ));
384- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
385- mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
386-
387- #if MICROPY_PY_BUILTINS_FLOAT
388- float gain = mp_obj_get_float (args [ARG_gain ].u_obj );
389- #else
390- #error "floating point not supported"
391- #endif
392-
393- if (gain > 1 || gain < 0 ) {
394- mp_raise_ValueError (translate ("gain must be between 0 and 1" ));
395- }
396-
397- common_hal_audioio_mixer_set_gain (self ,args [ARG_voice ].u_int , gain );
398- #endif
399- return mp_const_none ;
400- }
401- MP_DEFINE_CONST_FUN_OBJ_KW (audioio_mixer_voice_set_gain_obj , 1 , audioio_mixer_voice_obj_set_gain );
402-
403- const mp_obj_property_t audioio_mixer_voice_gain_obj = {
404- .base .type = & mp_type_property ,
405- .proxy = {(mp_obj_t )& mp_const_none_obj ,
406- (mp_obj_t )& audioio_mixer_voice_set_gain_obj ,
407- (mp_obj_t )& mp_const_none_obj },
408- };
409-
410- //| .. attribute:: playing
411- //|
412- //| True when any voice is being output. (read-only)
413- //|
414- STATIC mp_obj_t audioio_mixer_voice_obj_get_playing (mp_obj_t self_in ) {
415- #if 0
233+ STATIC mp_obj_t audioio_mixer_obj_get_voice (mp_obj_t self_in ) {
416234 audioio_mixer_obj_t * self = MP_OBJ_TO_PTR (self_in );
417235 raise_error_if_deinited (common_hal_audioio_mixer_deinited (self ));
418- return mp_obj_new_bool (common_hal_audioio_mixer_get_playing (self ));
419- #endif
420- return mp_const_none ;
236+ return mp_obj_new_tuple (self -> voice_count , self -> voice );
421237}
422- MP_DEFINE_CONST_FUN_OBJ_1 (audioio_mixer_voice_get_playing_obj , audioio_mixer_voice_obj_get_playing );
238+ MP_DEFINE_CONST_FUN_OBJ_1 (audioio_mixer_get_voice_obj , audioio_mixer_obj_get_voice );
423239
424- const mp_obj_property_t audioio_mixer_voice_playing_obj = {
240+ const mp_obj_property_t audioio_mixer_voice_obj = {
425241 .base .type = & mp_type_property ,
426- .proxy = {(mp_obj_t )& audioio_mixer_voice_get_playing_obj ,
242+ .proxy = {(mp_obj_t )& audioio_mixer_obj_get_voice ,
427243 (mp_obj_t )& mp_const_none_obj ,
428244 (mp_obj_t )& mp_const_none_obj },
429245};
430246
247+
431248STATIC const mp_rom_map_elem_t audioio_mixer_locals_dict_table [] = {
432249 // Methods
433250 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& audioio_mixer_deinit_obj ) },
@@ -439,6 +256,7 @@ STATIC const mp_rom_map_elem_t audioio_mixer_locals_dict_table[] = {
439256 // Properties
440257 { MP_ROM_QSTR (MP_QSTR_playing ), MP_ROM_PTR (& audioio_mixer_playing_obj ) },
441258 { MP_ROM_QSTR (MP_QSTR_sample_rate ), MP_ROM_PTR (& audioio_mixer_sample_rate_obj ) },
259+ { MP_ROM_QSTR (MP_QSTR_voice ), MP_ROM_PTR (& audioio_mixer_voice_obj ) }
442260};
443261STATIC MP_DEFINE_CONST_DICT (audioio_mixer_locals_dict , audioio_mixer_locals_dict_table );
444262
@@ -448,24 +266,3 @@ const mp_obj_type_t audioio_mixer_type = {
448266 .make_new = audioio_mixer_make_new ,
449267 .locals_dict = (mp_obj_dict_t * )& audioio_mixer_locals_dict ,
450268};
451-
452- STATIC const mp_rom_map_elem_t audioio_mixer_voice_locals_dict_table [] = {
453- // Methods
454- { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& audioio_mixer_voice_deinit_obj ) },
455- { MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& default___enter___obj ) },
456- { MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& audioio_mixer_voice___exit___obj ) },
457- { MP_ROM_QSTR (MP_QSTR_play ), MP_ROM_PTR (& audioio_mixer_voice_play_obj ) },
458- { MP_ROM_QSTR (MP_QSTR_stop ), MP_ROM_PTR (& audioio_mixer_voice_stop_obj ) },
459-
460- // Properties
461- { MP_ROM_QSTR (MP_QSTR_playing ), MP_ROM_PTR (& audioio_mixer_voice_playing_obj ) },
462- { MP_ROM_QSTR (MP_QSTR_gain ), MP_ROM_PTR (& audioio_mixer_voice_gain_obj ) },
463- };
464- STATIC MP_DEFINE_CONST_DICT (audioio_mixer_voice_locals_dict , audioio_mixer_voice_locals_dict_table );
465-
466- const mp_obj_type_t audioio_mixer_voice_type = {
467- { & mp_type_type },
468- .name = MP_QSTR_Mixer_Voice ,
469- .make_new = audioio_mixer_voice_make_new ,
470- .locals_dict = (mp_obj_dict_t * )& audioio_mixer_voice_locals_dict ,
471- };
0 commit comments