3838#include "shared-bindings/util.h"
3939#include "supervisor/shared/translate.h"
4040
41- //| .. currentmodule:: audiomixer
41+ //|class Mixer:
42+ //| """.. currentmodule:: audiomixer
4243//|
43- //| :class:`Mixer` -- Mixes one or more audio samples together
44- //| ===========================================================
44+ //| :class:`Mixer` -- Mixes one or more audio samples together
45+ //| ===========================================================
4546//|
46- //| Mixer mixes multiple samples into one sample.
47+ //| Mixer mixes multiple samples into one sample."""
4748//|
48- //| .. class:: Mixer(voice_count=2, buffer_size=1024, channel_count=2, bits_per_sample=16, samples_signed=True, sample_rate=8000)
49+ //| def __init__(self, voice_count: int = 2, buffer_size: int = 1024, channel_count: int = 2, bits_per_sample: int = 16, samples_signed: bool = True, sample_rate: int = 8000):
50+ //| """Create a Mixer object that can mix multiple channels with the same sample rate.
51+ //| Samples are accessed and controlled with the mixer's `audiomixer.MixerVoice` objects.
4952//|
50- //| Create a Mixer object that can mix multiple channels with the same sample rate.
51- //| Samples are accessed and controlled with the mixer's `audiomixer.MixerVoice` objects.
53+ //| :param int voice_count: The maximum number of voices to mix
54+ //| :param int buffer_size: The total size in bytes of the buffers to mix into
55+ //| :param int channel_count: The number of channels the source samples contain. 1 = mono; 2 = stereo.
56+ //| :param int bits_per_sample: The bits per sample of the samples being played
57+ //| :param bool samples_signed: Samples are signed (True) or unsigned (False)
58+ //| :param int sample_rate: The sample rate to be used for all samples
5259//|
53- //| :param int voice_count: The maximum number of voices to mix
54- //| :param int buffer_size: The total size in bytes of the buffers to mix into
55- //| :param int channel_count: The number of channels the source samples contain. 1 = mono; 2 = stereo.
56- //| :param int bits_per_sample: The bits per sample of the samples being played
57- //| :param bool samples_signed: Samples are signed (True) or unsigned (False)
58- //| :param int sample_rate: The sample rate to be used for all samples
60+ //| Playing a wave file from flash::
5961//|
60- //| Playing a wave file from flash::
62+ //| import board
63+ //| import audioio
64+ //| import audiocore
65+ //| import audiomixer
66+ //| import digitalio
6167//|
62- //| import board
63- //| import audioio
64- //| import audiocore
65- //| import audiomixer
66- //| import digitalio
67- //|
68- //| a = audioio.AudioOut(board.A0)
69- //| music = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb"))
70- //| drum = audiocore.WaveFile(open("drum.wav", "rb"))
71- //| mixer = audiomixer.Mixer(voice_count=2, sample_rate=16000, channel_count=1,
72- //| bits_per_sample=16, samples_signed=True)
73- //|
74- //| print("playing")
75- //| # Have AudioOut play our Mixer source
76- //| a.play(mixer)
77- //| # Play the first sample voice
78- //| mixer.voice[0].play(music)
79- //| while mixer.playing:
80- //| # Play the second sample voice
81- //| mixer.voice[1].play(drum)
82- //| time.sleep(1)
83- //| print("stopped")
68+ //| a = audioio.AudioOut(board.A0)
69+ //| music = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb"))
70+ //| drum = audiocore.WaveFile(open("drum.wav", "rb"))
71+ //| mixer = audiomixer.Mixer(voice_count=2, sample_rate=16000, channel_count=1,
72+ //| bits_per_sample=16, samples_signed=True)
8473//|
74+ //| print("playing")
75+ //| # Have AudioOut play our Mixer source
76+ //| a.play(mixer)
77+ //| # Play the first sample voice
78+ //| mixer.voice[0].play(music)
79+ //| while mixer.playing:
80+ //| # Play the second sample voice
81+ //| mixer.voice[1].play(drum)
82+ //| time.sleep(1)
83+ //| print("stopped")"""
84+ //| ...
8585STATIC mp_obj_t audiomixer_mixer_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
8686 enum { ARG_voice_count , ARG_buffer_size , ARG_channel_count , ARG_bits_per_sample , ARG_samples_signed , ARG_sample_rate };
8787 static const mp_arg_t allowed_args [] = {
@@ -125,10 +125,9 @@ STATIC mp_obj_t audiomixer_mixer_make_new(const mp_obj_type_t *type, size_t n_ar
125125 return MP_OBJ_FROM_PTR (self );
126126}
127127
128- //| .. method:: deinit()
129- //|
130- //| Deinitialises the Mixer and releases any hardware resources for reuse.
131- //|
128+ //| def deinit(self, ) -> Any:
129+ //| """Deinitialises the Mixer and releases any hardware resources for reuse."""
130+ //| ...
132131STATIC mp_obj_t audiomixer_mixer_deinit (mp_obj_t self_in ) {
133132 audiomixer_mixer_obj_t * self = MP_OBJ_TO_PTR (self_in );
134133 common_hal_audiomixer_mixer_deinit (self );
@@ -142,28 +141,25 @@ STATIC void check_for_deinit(audiomixer_mixer_obj_t *self) {
142141 }
143142}
144143
145- //| .. method:: __enter__()
146- //|
147- //| No-op used by Context Managers.
148- //|
144+ //| def __enter__(self, ) -> Any:
145+ //| """No-op used by Context Managers."""
146+ //| ...
149147// Provided by context manager helper.
150148
151- //| .. method:: __exit__()
152- //|
153- //| Automatically deinitializes the hardware when exiting a context. See
154- //| :ref:`lifetime-and-contextmanagers` for more info.
155- //|
149+ //| def __exit__(self, ) -> Any:
150+ //| """Automatically deinitializes the hardware when exiting a context. See
151+ //| :ref:`lifetime-and-contextmanagers` for more info."""
152+ //| ...
156153STATIC mp_obj_t audiomixer_mixer_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
157154 (void )n_args ;
158155 common_hal_audiomixer_mixer_deinit (args [0 ]);
159156 return mp_const_none ;
160157}
161158STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (audiomixer_mixer___exit___obj , 4 , 4 , audiomixer_mixer_obj___exit__ );
162159
163- //| .. attribute:: playing
164- //|
165- //| True when any voice is being output. (read-only)
166- //|
160+ //| playing: Any =
161+ //| """True when any voice is being output. (read-only)"""
162+ //| ...
167163STATIC mp_obj_t audiomixer_mixer_obj_get_playing (mp_obj_t self_in ) {
168164 audiomixer_mixer_obj_t * self = MP_OBJ_TO_PTR (self_in );
169165 check_for_deinit (self );
@@ -178,10 +174,9 @@ const mp_obj_property_t audiomixer_mixer_playing_obj = {
178174 (mp_obj_t )& mp_const_none_obj },
179175};
180176
181- //| .. attribute:: sample_rate
182- //|
183- //| 32 bit value that dictates how quickly samples are played in Hertz (cycles per second).
184- //|
177+ //| sample_rate: Any =
178+ //| """32 bit value that dictates how quickly samples are played in Hertz (cycles per second)."""
179+ //| ...
185180STATIC mp_obj_t audiomixer_mixer_obj_get_sample_rate (mp_obj_t self_in ) {
186181 audiomixer_mixer_obj_t * self = MP_OBJ_TO_PTR (self_in );
187182 check_for_deinit (self );
@@ -196,14 +191,14 @@ const mp_obj_property_t audiomixer_mixer_sample_rate_obj = {
196191 (mp_obj_t )& mp_const_none_obj },
197192};
198193
199- //| .. attribute:: voice
200- //|
201- //| A tuple of the mixer's `audiomixer.MixerVoice` object(s).
194+ //| voice: Any =
195+ //| """A tuple of the mixer's `audiomixer.MixerVoice` object(s).
202196//|
203- //| .. code-block:: python
197+ //| .. code-block:: python
204198//|
205- //| >>> mixer.voice
206- //| (<MixerVoice>,)
199+ //| >>> mixer.voice
200+ //| (<MixerVoice>,)"""
201+ //| ...
207202STATIC mp_obj_t audiomixer_mixer_obj_get_voice (mp_obj_t self_in ) {
208203 audiomixer_mixer_obj_t * self = MP_OBJ_TO_PTR (self_in );
209204 check_for_deinit (self );
@@ -218,15 +213,14 @@ const mp_obj_property_t audiomixer_mixer_voice_obj = {
218213 (mp_obj_t )& mp_const_none_obj },
219214};
220215
221- //| .. method:: play(sample, *, voice=0, loop=False)
216+ //| def play(self, sample: Any, *, voice: Any = 0, loop: Any = False) -> Any:
217+ //| """Plays the sample once when loop=False and continuously when loop=True.
218+ //| Does not block. Use `playing` to block.
222219//|
223- //| Plays the sample once when loop=False and continuously when loop=True.
224- //| Does not block. Use `playing` to block.
225- //|
226- //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
227- //|
228- //| The sample must match the Mixer's encoding settings given in the constructor.
220+ //| Sample must be an `audiocore.WaveFile`, `audiocore.RawSample`, or `audiomixer.Mixer`.
229221//|
222+ //| The sample must match the Mixer's encoding settings given in the constructor."""
223+ //| ...
230224STATIC mp_obj_t audiomixer_mixer_obj_play (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
231225 enum { ARG_sample , ARG_voice , ARG_loop };
232226 static const mp_arg_t allowed_args [] = {
@@ -251,10 +245,9 @@ STATIC mp_obj_t audiomixer_mixer_obj_play(size_t n_args, const mp_obj_t *pos_arg
251245}
252246MP_DEFINE_CONST_FUN_OBJ_KW (audiomixer_mixer_play_obj , 1 , audiomixer_mixer_obj_play );
253247
254- //| .. method:: stop_voice(voice=0)
255- //|
256- //| Stops playback of the sample on the given voice.
257- //|
248+ //| def stop_voice(self, voice: Any = 0) -> Any:
249+ //| """Stops playback of the sample on the given voice."""
250+ //| ...
258251STATIC mp_obj_t audiomixer_mixer_obj_stop_voice (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
259252 enum { ARG_voice };
260253 static const mp_arg_t allowed_args [] = {
0 commit comments