Skip to content

Commit 065efb0

Browse files
committed
bring MixerVoice back to building state; update documentation
1 parent 9939d0c commit 065efb0

10 files changed

Lines changed: 187 additions & 151 deletions

File tree

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ $(filter $(SRC_PATTERNS), \
306306
audioio/__init__.c \
307307
audiocore/__init__.c \
308308
audiocore/Mixer.c \
309+
audiocore/MixerVoice.c \
309310
audiocore/RawSample.c \
310311
audiocore/WaveFile.c \
311312
bitbangio/I2C.c \

shared-bindings/audiocore/Mixer.c

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626
#include "shared-bindings/audiocore/Mixer.h"
27+
#include "shared-bindings/audiocore/MixerVoice.h"
28+
#include "shared-module/audiocore/MixerVoice.h"
2729

2830
#include <stdint.h>
2931

@@ -43,33 +45,43 @@
4345
//|
4446
//| Mixer mixes multiple samples into one sample.
4547
//|
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)
4749
//|
4850
//| 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.
4952
//|
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
5154
//| :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
5259
//|
5360
//| Playing a wave file from flash::
5461
//|
5562
//| import board
5663
//| import audioio
64+
//| import audiocore
5765
//| import digitalio
5866
//|
5967
//| # Required for CircuitPlayground Express
6068
//| speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
6169
//| speaker_enable.switch_to_output(value=True)
6270
//|
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)
6675
//| a = audioio.AudioOut(board.A0)
6776
//|
6877
//| print("playing")
78+
//| # Have AudioOut play our Mixer source
6979
//| a.play(mixer)
70-
//| mixer.play(music, voice=0)
80+
//| # Play the first sample voice
81+
//| mixer.voice[0].play(music)
7182
//| while mixer.playing:
72-
//| mixer.play(drum, voice=1)
83+
//| # Play the second sample voice
84+
//| mixer.voice[1].play(drum)
7385
//| time.sleep(1)
7486
//| print("stopped")
7587
//|
@@ -151,54 +163,6 @@ STATIC mp_obj_t audioio_mixer_obj___exit__(size_t n_args, const mp_obj_t *args)
151163
}
152164
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixer___exit___obj, 4, 4, audioio_mixer_obj___exit__);
153165

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-
202166
//| .. attribute:: playing
203167
//|
204168
//| True when any voice is being output. (read-only)
@@ -237,11 +201,15 @@ const mp_obj_property_t audioio_mixer_sample_rate_obj = {
237201

238202
//| .. attribute:: voice
239203
//|
240-
//| tuple of voice objects
204+
//| A tuple of the mixer's `audioio.MixerVoice` object(s).
205+
//|
206+
//| .. code-block:: python
241207
//|
208+
//| >>> mixer.voice
209+
//| (<MixerVoice>,)
242210
STATIC mp_obj_t audioio_mixer_obj_get_voice(mp_obj_t self_in) {
243211
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);
245213
return self->voice_tuple;
246214
}
247215
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixer_get_voice_obj, audioio_mixer_obj_get_voice);
Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
/*
2-
* MixerVoice.c
2+
* This file is part of the Micro Python project, http://micropython.org/
33
*
4-
* Created on: Nov 15, 2018
5-
* Author: dean
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 DeanM for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
625
*/
7-
#include "shared-bindings/audioio/Mixer.h"
8-
#include "shared-bindings/audioio/MixerVoice.h"
26+
#include "shared-bindings/audiocore/Mixer.h"
27+
#include "shared-bindings/audiocore/MixerVoice.h"
928

1029
#include <stdint.h>
1130

@@ -14,23 +33,20 @@
1433
#include "py/objproperty.h"
1534
#include "py/runtime.h"
1635
#include "shared-bindings/microcontroller/Pin.h"
17-
#include "shared-bindings/audioio/RawSample.h"
36+
#include "shared-bindings/audiocore/RawSample.h"
1837
#include "shared-bindings/util.h"
1938
#include "supervisor/shared/translate.h"
2039

2140
//| .. currentmodule:: audioio
2241
//|
23-
//| :class:`Mixer` -- Mixes one or more audio samples together
24-
//| ===========================================================
25-
//|
26-
//| Mixer mixes multiple samples into one sample.
42+
//| :class:`MixerVoice` -- Voice objects used with Mixer
43+
//| =====================================================
2744
//|
28-
//| .. class:: Mixer(channel_count=2, buffer_size=1024)
45+
//| Used to access and control samples with `audioio.Mixer`.
2946
//|
30-
//| Create a Mixer object that can mix multiple channels with the same sample rate.
47+
//| .. class:: MixerVoice()
3148
//|
32-
//| :param int channel_count: The maximum number of samples to mix at once
33-
//| :param int buffer_size: The total size in bytes of the buffers to mix into
49+
//| MixerVoice instance object(s) created by `audioio.Mixer`.
3450
//|
3551
// TODO: support mono or stereo voices
3652
STATIC mp_obj_t audioio_mixervoice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -69,12 +85,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audioio_mixervoice___exit___obj, 4, 4
6985

7086
//| .. method:: play(sample, *, loop=False)
7187
//|
72-
//| Plays the sample once when loop=False and continuously when loop=True.
88+
//| Plays the sample once when ``loop=False``, and continuously when ``loop=True``.
7389
//| Does not block. Use `playing` to block.
7490
//|
7591
//| Sample must be an `audioio.WaveFile`, `audioio.Mixer` or `audioio.RawSample`.
7692
//|
77-
//| The sample must match the Mixer's encoding settings given in the constructor.
93+
//| The sample must match the `audioio.Mixer`'s encoding settings given in the constructor.
7894
//|
7995
STATIC mp_obj_t audioio_mixervoice_obj_play(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
8096
enum { ARG_sample, ARG_loop };
@@ -113,11 +129,9 @@ STATIC mp_obj_t audioio_mixervoice_obj_stop(size_t n_args, const mp_obj_t *pos_a
113129
}
114130
MP_DEFINE_CONST_FUN_OBJ_KW(audioio_mixervoice_stop_obj, 1, audioio_mixervoice_obj_stop);
115131

116-
//| .. method:: get_gain(voice, gain)
117-
//|
118-
//| Get the gain of a voice.
132+
//| .. method:: level()
119133
//|
120-
//| Returns as a floating point number between 0 and 1
134+
//| The volume level of a voice, as a floating point number between 0 and 1.
121135
//|
122136
STATIC mp_obj_t audioio_mixervoice_obj_get_level(mp_obj_t self_in) {
123137

@@ -129,12 +143,6 @@ STATIC mp_obj_t audioio_mixervoice_obj_get_level(mp_obj_t self_in) {
129143
}
130144
MP_DEFINE_CONST_FUN_OBJ_1(audioio_mixervoice_get_level_obj, audioio_mixervoice_obj_get_level);
131145

132-
//| .. method:: set_gain(voice, gain)
133-
//|
134-
//| Set the gain of a voice.
135-
//|
136-
//| gain must be a floating point number between 0 and 1
137-
//|
138146
STATIC mp_obj_t audioio_mixervoice_obj_set_level(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
139147
enum { ARG_level };
140148
static const mp_arg_t allowed_args[] = {
@@ -167,10 +175,13 @@ const mp_obj_property_t audioio_mixervoice_level_obj = {
167175
(mp_obj_t)&mp_const_none_obj},
168176
};
169177

170-
//| .. attribute:: playing
171-
//|
172-
//| True when any voice is being output. (read-only)
173-
//|
178+
//TODO: @sommersoft - enable voice.playing tracking? will need an additional field
179+
// in the struct, with supporting logic. disabled documentation for now.
180+
181+
// .. attribute:: playing
182+
//
183+
// True when any voice is being output. (read-only)
184+
//
174185
STATIC mp_obj_t audioio_mixervoice_obj_get_playing(mp_obj_t self_in) {
175186
#if 0
176187
audioio_mixer_obj_t *self = MP_OBJ_TO_PTR(self_in);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 DeanM for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_
27+
#define SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_
28+
29+
#include "common-hal/microcontroller/Pin.h"
30+
#include "shared-module/audiocore/Mixer.h"
31+
#include "shared-bindings/audiocore/RawSample.h"
32+
#include "shared-module/audiocore/MixerVoice.h"
33+
#include "shared-module/audiocore/Mixer.h"
34+
35+
extern const mp_obj_type_t audioio_mixer_type;
36+
extern const mp_obj_type_t audioio_mixervoice_type;
37+
38+
void common_hal_audioio_mixervoice_deinit(audioio_mixervoice_obj_t* self);
39+
void common_hal_audioio_mixervoice_set_parent(audioio_mixervoice_obj_t* self, audioio_mixer_obj_t *parent);
40+
void common_hal_audioio_mixervoice_play(audioio_mixervoice_obj_t* self, mp_obj_t sample, bool loop);
41+
void common_hal_audioio_mixervoice_stop(audioio_mixervoice_obj_t* self);
42+
float common_hal_audioio_mixervoice_get_level(audioio_mixervoice_obj_t* self);
43+
void common_hal_audioio_mixervoice_set_level(audioio_mixervoice_obj_t* self, float gain);
44+
45+
bool common_hal_audioio_mixervoice_get_playing(audioio_mixervoice_obj_t* self);
46+
47+
#endif /* SHARED_BINDINGS_AUDIOIO_MIXERVOICE_H_ */

shared-bindings/audiocore/__init__.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
//| :maxdepth: 3
5151
//|
5252
//| Mixer
53+
//| MixerVoice
5354
//| RawSample
5455
//| WaveFile
5556
//|

shared-bindings/audioio/MixerVoice.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

shared-module/audiocore/Mixer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "shared-bindings/audiocore/Mixer.h"
28+
#include "shared-bindings/audiocore/MixerVoice.h"
2829

2930
#include <stdint.h>
3031

0 commit comments

Comments
 (0)