|
24 | 24 | * THE SOFTWARE. |
25 | 25 | */ |
26 | 26 | #include "shared-bindings/audiocore/Mixer.h" |
| 27 | +#include "shared-bindings/audiocore/MixerVoice.h" |
| 28 | +#include "shared-module/audiocore/MixerVoice.h" |
27 | 29 |
|
28 | 30 | #include <stdint.h> |
29 | 31 |
|
|
43 | 45 | //| |
44 | 46 | //| Mixer mixes multiple samples into one sample. |
45 | 47 | //| |
46 | | -//| .. class:: Mixer(channel_count=2, buffer_size=1024) |
| 48 | +//| .. class:: Mixer(voice_count=2, buffer_size=1024, channel_count=2, bits_per_sample=16, samples_signed=True, sample_rate=8000) |
47 | 49 | //| |
48 | 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 `audioio.MixerVoice` objects. |
49 | 52 | //| |
50 | | -//| :param int channel_count: The maximum number of samples to mix at once |
| 53 | +//| :param int voice_count: The maximum number of voices to mix |
51 | 54 | //| :param int buffer_size: The total size in bytes of the buffers to mix into |
| 55 | +//| :param int channel_count: The maximum number of samples to mix at once |
| 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 |
52 | 59 | //| |
53 | 60 | //| Playing a wave file from flash:: |
54 | 61 | //| |
55 | 62 | //| import board |
56 | 63 | //| import audioio |
| 64 | +//| import audiocore |
57 | 65 | //| import digitalio |
58 | 66 | //| |
59 | 67 | //| # Required for CircuitPlayground Express |
60 | 68 | //| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) |
61 | 69 | //| speaker_enable.switch_to_output(value=True) |
62 | 70 | //| |
63 | | -//| music = audioio.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb")) |
64 | | -//| drum = audioio.WaveFile(open("drum.wav", "rb")) |
65 | | -//| mixer = audioio.Mixer(voice_count=2, sample_rate=16000, channel_count=1, bits_per_sample=16, samples_signed=True) |
| 71 | +//| music = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb")) |
| 72 | +//| drum = audiocore.WaveFile(open("drum.wav", "rb")) |
| 73 | +//| mixer = audiocore.Mixer(voice_count=2, sample_rate=16000, channel_count=1, |
| 74 | +//| bits_per_sample=16, samples_signed=True) |
66 | 75 | //| a = audioio.AudioOut(board.A0) |
67 | 76 | //| |
68 | 77 | //| print("playing") |
| 78 | +//| # Have AudioOut play our Mixer source |
69 | 79 | //| a.play(mixer) |
70 | | -//| mixer.play(music, voice=0) |
| 80 | +//| # Play the first sample voice |
| 81 | +//| mixer.voice[0].play(music) |
71 | 82 | //| while mixer.playing: |
72 | | -//| mixer.play(drum, voice=1) |
| 83 | +//| # Play the second sample voice |
| 84 | +//| mixer.voice[1].play(drum) |
73 | 85 | //| time.sleep(1) |
74 | 86 | //| print("stopped") |
75 | 87 | //| |
@@ -151,54 +163,6 @@ STATIC mp_obj_t audioio_mixer_obj___exit__(size_t n_args, const mp_obj_t *args) |
151 | 163 | } |
152 | 164 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixer___exit___obj, 4, 4, audioio_mixer_obj___exit__); |
153 | 165 |
|
154 | | - |
155 | | -//| .. method:: play(sample, *, voice=0, loop=False) |
156 | | -//| |
157 | | -//| Plays the sample once when loop=False and continuously when loop=True. |
158 | | -//| Does not block. Use `playing` to block. |
159 | | -//| |
160 | | -//| Sample must be an `audioio.WaveFile`, `audioio.Mixer` or `audioio.RawSample`. |
161 | | -//| |
162 | | -//| The sample must match the Mixer's encoding settings given in the constructor. |
163 | | -//| |
164 | | -STATIC mp_obj_t audioio_mixer_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
165 | | - enum { ARG_sample, ARG_voice, ARG_loop }; |
166 | | - static const mp_arg_t allowed_args[] = { |
167 | | - { MP_QSTR_sample, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
168 | | - { MP_QSTR_voice, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, |
169 | | - { MP_QSTR_loop, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, |
170 | | - }; |
171 | | - audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
172 | | - check_for_deinit(self); |
173 | | - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
174 | | - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
175 | | - |
176 | | - mp_obj_t sample = args[ARG_sample].u_obj; |
177 | | - common_hal_audioio_mixer_play(self, sample, args[ARG_voice].u_int, args[ARG_loop].u_bool); |
178 | | - |
179 | | - return mp_const_none; |
180 | | -} |
181 | | -MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixer_play_obj, 1, audioio_mixer_obj_play); |
182 | | - |
183 | | -//| .. method:: stop_voice(voice=0) |
184 | | -//| |
185 | | -//| Stops playback of the sample on the given voice. |
186 | | -//| |
187 | | -STATIC mp_obj_t audioio_mixer_obj_stop_voice(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
188 | | - enum { ARG_voice }; |
189 | | - static const mp_arg_t allowed_args[] = { |
190 | | - { MP_QSTR_voice, MP_ARG_INT, {.u_int = 0} }, |
191 | | - }; |
192 | | - audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); |
193 | | - check_for_deinit(self); |
194 | | - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
195 | | - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
196 | | - |
197 | | - common_hal_audioio_mixer_stop_voice(self, args[ARG_voice].u_int); |
198 | | - return mp_const_none; |
199 | | -} |
200 | | -MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixer_stop_voice_obj, 1, audioio_mixer_obj_stop_voice); |
201 | | - |
202 | 166 | //| .. attribute:: playing |
203 | 167 | //| |
204 | 168 | //| True when any voice is being output. (read-only) |
@@ -237,11 +201,15 @@ const mp_obj_property_t audioio_mixer_sample_rate_obj = { |
237 | 201 |
|
238 | 202 | //| .. attribute:: voice |
239 | 203 | //| |
240 | | -//| tuple of voice objects |
| 204 | +//| A tuple of the mixer's `audioio.MixerVoice` object(s). |
| 205 | +//| |
| 206 | +//| .. code-block:: python |
241 | 207 | //| |
| 208 | +//| >>> mixer.voice |
| 209 | +//| (<MixerVoice>,) |
242 | 210 | STATIC mp_obj_t audioio_mixer_obj_get_voice(mp_obj_t self_in) { |
243 | 211 | audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
244 | | - raise_error_if_deinited(common_hal_audioio_mixer_deinited(self)); |
| 212 | + check_for_deinit(self); |
245 | 213 | return self->voice_tuple; |
246 | 214 | } |
247 | 215 | MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixer_get_voice_obj, audioio_mixer_obj_get_voice); |
|
0 commit comments