Skip to content

Commit b1f4a9a

Browse files
authored
Merge pull request #4447 from tyomitch/patch-3
[synthio] add a simple MidiTrack implementation
2 parents 9a3f04a + 92d7f31 commit b1f4a9a

File tree

19 files changed

+736
-39
lines changed

19 files changed

+736
-39
lines changed

locale/circuitpython.pot

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,11 @@ msgstr ""
879879
msgid "EXTINT channel already in use"
880880
msgstr ""
881881

882+
#: shared-module/synthio/MidiTrack.c
883+
#, c-format
884+
msgid "Error in MIDI stream at position %d"
885+
msgstr ""
886+
882887
#: extmod/modure.c
883888
msgid "Error in regex"
884889
msgstr ""
@@ -1084,10 +1089,6 @@ msgstr ""
10841089
msgid "I2SOut not available"
10851090
msgstr ""
10861091

1087-
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
1088-
msgid "IOs 0, 2 & 4 do not support internal pullup in sleep"
1089-
msgstr ""
1090-
10911092
#: shared-bindings/aesio/aes.c
10921093
#, c-format
10931094
msgid "IV must be %d bytes long"
@@ -1211,6 +1212,10 @@ msgstr ""
12111212
msgid "Invalid DAC pin supplied"
12121213
msgstr ""
12131214

1215+
#: shared-bindings/synthio/__init__.c
1216+
msgid "Invalid MIDI file"
1217+
msgstr ""
1218+
12141219
#: ports/atmel-samd/common-hal/pwmio/PWMOut.c
12151220
#: ports/cxd56/common-hal/pwmio/PWMOut.c
12161221
#: ports/mimxrt10xx/common-hal/pwmio/PWMOut.c
@@ -1601,6 +1606,7 @@ msgstr ""
16011606
msgid "Not a valid IP string"
16021607
msgstr ""
16031608

1609+
#: ports/nrf/common-hal/_bleio/PacketBuffer.c
16041610
#: ports/nrf/common-hal/_bleio/__init__.c
16051611
#: shared-bindings/_bleio/CharacteristicBuffer.c
16061612
msgid "Not connected"
@@ -2493,10 +2499,6 @@ msgstr ""
24932499
msgid "buffer is smaller than requested size"
24942500
msgstr ""
24952501

2496-
#: shared-bindings/audiocore/RawSample.c
2497-
msgid "buffer must be a bytes-like object"
2498-
msgstr ""
2499-
25002502
#: extmod/ulab/code/ulab_create.c extmod/ulab/code/utils/utils.c
25012503
msgid "buffer size must be a multiple of element size"
25022504
msgstr ""
@@ -2938,7 +2940,7 @@ msgid "f-string: single '}' is not allowed"
29382940
msgstr ""
29392941

29402942
#: shared-bindings/audiocore/WaveFile.c shared-bindings/audiomp3/MP3Decoder.c
2941-
#: shared-bindings/displayio/OnDiskBitmap.c
2943+
#: shared-bindings/displayio/OnDiskBitmap.c shared-bindings/synthio/__init__.c
29422944
msgid "file must be a file opened in byte mode"
29432945
msgstr ""
29442946

@@ -3253,10 +3255,6 @@ msgstr ""
32533255
msgid "invalid syntax for number"
32543256
msgstr ""
32553257

3256-
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
3257-
msgid "io must be rtc io"
3258-
msgstr ""
3259-
32603258
#: py/objtype.c
32613259
msgid "issubclass() arg 1 must be a class"
32623260
msgstr ""
@@ -3716,6 +3714,7 @@ msgstr ""
37163714
#: ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h
37173715
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
37183716
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
3717+
#: ports/esp32s2/boards/artisense_rd00/mpconfigboard.h
37193718
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
37203719
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
37213720
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
@@ -4014,10 +4013,6 @@ msgstr ""
40144013
msgid "trapz is defined for 1D arrays of equal length"
40154014
msgstr ""
40164015

4017-
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
4018-
msgid "trigger level must be 0 or 1"
4019-
msgstr ""
4020-
40214016
#: py/obj.c
40224017
msgid "tuple/list has wrong length"
40234018
msgstr ""
@@ -4152,10 +4147,6 @@ msgstr ""
41524147
msgid "value_count must be > 0"
41534148
msgstr ""
41544149

4155-
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
4156-
msgid "wakeup conflict"
4157-
msgstr ""
4158-
41594150
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
41604151
msgid "watchdog not initialized"
41614152
msgstr ""

ports/atmel-samd/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CIRCUITPY_BUILTINS_POW3 ?= 0
4141
CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1
4242
CIRCUITPY_FREQUENCYIO ?= 0
4343
CIRCUITPY_JSON ?= 0
44+
CIRCUITPY_SYNTHIO ?= 0
4445
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
4546

4647
# No room for HCI _bleio on SAMD21.

ports/stm/boards/stm32f411ce_blackpill_with_flash/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ MCU_PACKAGE = UFQFPN48
1616

1717
LD_COMMON = boards/common_default.ld
1818
LD_FILE = boards/STM32F411_nvm_nofs.ld
19+
20+
CIRCUITPY_SYNTHIO = 0

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ endif
280280
ifeq ($(CIRCUITPY_SUPERVISOR),1)
281281
SRC_PATTERNS += supervisor/%
282282
endif
283+
ifeq ($(CIRCUITPY_SYNTHIO),1)
284+
SRC_PATTERNS += synthio/%
285+
endif
283286
ifeq ($(CIRCUITPY_TERMINALIO),1)
284287
SRC_PATTERNS += terminalio/% fontio/%
285288
endif
@@ -524,6 +527,8 @@ SRC_SHARED_MODULE_ALL = \
524527
socket/__init__.c \
525528
storage/__init__.c \
526529
struct/__init__.c \
530+
synthio/MidiTrack.c \
531+
synthio/__init__.c \
527532
terminalio/Terminal.c \
528533
terminalio/__init__.c \
529534
time/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,13 @@ extern const struct _mp_obj_module_t supervisor_module;
728728
#define SUPERVISOR_MODULE
729729
#endif
730730

731+
#if CIRCUITPY_SYNTHIO
732+
#define SYNTHIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_synthio), (mp_obj_t)&synthio_module },
733+
extern const struct _mp_obj_module_t synthio_module;
734+
#else
735+
#define SYNTHIO_MODULE
736+
#endif
737+
731738
#if CIRCUITPY_TIME
732739
extern const struct _mp_obj_module_t time_module;
733740
#define TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
@@ -897,6 +904,7 @@ extern const struct _mp_obj_module_t msgpack_module;
897904
STORAGE_MODULE \
898905
STRUCT_MODULE \
899906
SUPERVISOR_MODULE \
907+
SYNTHIO_MODULE \
900908
TOUCHIO_MODULE \
901909
UHEAP_MODULE \
902910
USB_CDC_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ CFLAGS += -DCIRCUITPY_STRUCT=$(CIRCUITPY_STRUCT)
311311
CIRCUITPY_SUPERVISOR ?= 1
312312
CFLAGS += -DCIRCUITPY_SUPERVISOR=$(CIRCUITPY_SUPERVISOR)
313313

314+
CIRCUITPY_SYNTHIO ?= $(CIRCUITPY_AUDIOCORE)
315+
CFLAGS += -DCIRCUITPY_SYNTHIO=$(CIRCUITPY_SYNTHIO)
316+
314317
CIRCUITPY_TERMINALIO ?= $(CIRCUITPY_DISPLAYIO)
315318
CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
316319

shared-bindings/_typing/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ WriteableBuffer = Union[
3838
"""
3939

4040
AudioSample = Union[
41-
audiocore.WaveFile, audiocore.RawSample, audiomixer.Mixer, audiomp3.MP3Decoder
41+
audiocore.WaveFile, audiocore.RawSample, audiomixer.Mixer, audiomp3.MP3Decoder, synthio.MidiTrack
4242
]
4343
"""Classes that implement the audiosample protocol
4444
4545
- `audiocore.WaveFile`
4646
- `audiocore.RawSample`
4747
- `audiomixer.Mixer`
4848
- `audiomp3.MP3Decoder`
49+
- `synthio.MidiTrack`
4950
5051
You can play these back with `audioio.AudioOut`, `audiobusio.I2SOut` or `audiopwmio.PWMAudioOut`.
5152
"""

shared-bindings/audiocore/RawSample.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "py/binary.h"
3131
#include "py/objproperty.h"
3232
#include "py/runtime.h"
33-
#include "shared-bindings/microcontroller/Pin.h"
3433
#include "shared-bindings/util.h"
3534
#include "shared-bindings/audiocore/RawSample.h"
3635
#include "supervisor/shared/translate.h"
@@ -83,26 +82,23 @@ STATIC mp_obj_t audioio_rawsample_make_new(const mp_obj_type_t *type, size_t n_a
8382
audioio_rawsample_obj_t *self = m_new_obj(audioio_rawsample_obj_t);
8483
self->base.type = &audioio_rawsample_type;
8584
mp_buffer_info_t bufinfo;
86-
if (mp_get_buffer(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ)) {
87-
uint8_t bytes_per_sample = 1;
88-
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
89-
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
90-
bytes_per_sample = 2;
91-
} else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
92-
mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'"));
93-
}
94-
common_hal_audioio_rawsample_construct(self, ((uint8_t *)bufinfo.buf), bufinfo.len,
95-
bytes_per_sample, signed_samples, args[ARG_channel_count].u_int,
96-
args[ARG_sample_rate].u_int);
97-
} else {
98-
mp_raise_TypeError(translate("buffer must be a bytes-like object"));
85+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
86+
uint8_t bytes_per_sample = 1;
87+
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
88+
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
89+
bytes_per_sample = 2;
90+
} else if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
91+
mp_raise_ValueError(translate("sample_source buffer must be a bytearray or array of type 'h', 'H', 'b' or 'B'"));
9992
}
93+
common_hal_audioio_rawsample_construct(self, ((uint8_t *)bufinfo.buf), bufinfo.len,
94+
bytes_per_sample, signed_samples, args[ARG_channel_count].u_int,
95+
args[ARG_sample_rate].u_int);
10096

10197
return MP_OBJ_FROM_PTR(self);
10298
}
10399

104100
//| def deinit(self) -> None:
105-
//| """Deinitialises the AudioOut and releases any hardware resources for reuse."""
101+
//| """Deinitialises the RawSample and releases any hardware resources for reuse."""
106102
//| ...
107103
//|
108104
STATIC mp_obj_t audioio_rawsample_deinit(mp_obj_t self_in) {

shared-bindings/audiocore/RawSample.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
2929

30-
#include "common-hal/microcontroller/Pin.h"
3130
#include "shared-module/audiocore/RawSample.h"
3231

3332
extern const mp_obj_type_t audioio_rawsample_type;

shared-bindings/audiocore/WaveFile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
4545
//|
4646
//| :param typing.BinaryIO file: Already opened wave file
47-
//| :param ~_typing.WriteableBuffer buffer: Optional pre-allocated buffer, that will be split in half and used for double-buffering of the data. If not provided, two 512 byte buffers are allocated internally.
47+
//| :param ~_typing.WriteableBuffer buffer: Optional pre-allocated buffer, that will be split in half and used for double-buffering of the data. If not provided, two 256 byte buffers are allocated internally.
4848
//|
4949
//|
5050
//| Playing a wave file from flash::

0 commit comments

Comments
 (0)