Merge pull request #4447 from tyomitch/patch-3
[synthio] add a simple MidiTrack implementation
This commit is contained in:
commit
b1f4a9a9fd
@ -879,6 +879,11 @@ msgstr ""
|
|||||||
msgid "EXTINT channel already in use"
|
msgid "EXTINT channel already in use"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: shared-module/synthio/MidiTrack.c
|
||||||
|
#, c-format
|
||||||
|
msgid "Error in MIDI stream at position %d"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: extmod/modure.c
|
#: extmod/modure.c
|
||||||
msgid "Error in regex"
|
msgid "Error in regex"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -1084,10 +1089,6 @@ msgstr ""
|
|||||||
msgid "I2SOut not available"
|
msgid "I2SOut not available"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
|
|
||||||
msgid "IOs 0, 2 & 4 do not support internal pullup in sleep"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: shared-bindings/aesio/aes.c
|
#: shared-bindings/aesio/aes.c
|
||||||
#, c-format
|
#, c-format
|
||||||
msgid "IV must be %d bytes long"
|
msgid "IV must be %d bytes long"
|
||||||
@ -1211,6 +1212,10 @@ msgstr ""
|
|||||||
msgid "Invalid DAC pin supplied"
|
msgid "Invalid DAC pin supplied"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: shared-bindings/synthio/__init__.c
|
||||||
|
msgid "Invalid MIDI file"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: ports/atmel-samd/common-hal/pwmio/PWMOut.c
|
#: ports/atmel-samd/common-hal/pwmio/PWMOut.c
|
||||||
#: ports/cxd56/common-hal/pwmio/PWMOut.c
|
#: ports/cxd56/common-hal/pwmio/PWMOut.c
|
||||||
#: ports/mimxrt10xx/common-hal/pwmio/PWMOut.c
|
#: ports/mimxrt10xx/common-hal/pwmio/PWMOut.c
|
||||||
@ -1601,6 +1606,7 @@ msgstr ""
|
|||||||
msgid "Not a valid IP string"
|
msgid "Not a valid IP string"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: ports/nrf/common-hal/_bleio/PacketBuffer.c
|
||||||
#: ports/nrf/common-hal/_bleio/__init__.c
|
#: ports/nrf/common-hal/_bleio/__init__.c
|
||||||
#: shared-bindings/_bleio/CharacteristicBuffer.c
|
#: shared-bindings/_bleio/CharacteristicBuffer.c
|
||||||
msgid "Not connected"
|
msgid "Not connected"
|
||||||
@ -2493,10 +2499,6 @@ msgstr ""
|
|||||||
msgid "buffer is smaller than requested size"
|
msgid "buffer is smaller than requested size"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: shared-bindings/audiocore/RawSample.c
|
|
||||||
msgid "buffer must be a bytes-like object"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: extmod/ulab/code/ulab_create.c extmod/ulab/code/utils/utils.c
|
#: extmod/ulab/code/ulab_create.c extmod/ulab/code/utils/utils.c
|
||||||
msgid "buffer size must be a multiple of element size"
|
msgid "buffer size must be a multiple of element size"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -2938,7 +2940,7 @@ msgid "f-string: single '}' is not allowed"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: shared-bindings/audiocore/WaveFile.c shared-bindings/audiomp3/MP3Decoder.c
|
#: shared-bindings/audiocore/WaveFile.c shared-bindings/audiomp3/MP3Decoder.c
|
||||||
#: shared-bindings/displayio/OnDiskBitmap.c
|
#: shared-bindings/displayio/OnDiskBitmap.c shared-bindings/synthio/__init__.c
|
||||||
msgid "file must be a file opened in byte mode"
|
msgid "file must be a file opened in byte mode"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@ -3253,10 +3255,6 @@ msgstr ""
|
|||||||
msgid "invalid syntax for number"
|
msgid "invalid syntax for number"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
|
|
||||||
msgid "io must be rtc io"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: py/objtype.c
|
#: py/objtype.c
|
||||||
msgid "issubclass() arg 1 must be a class"
|
msgid "issubclass() arg 1 must be a class"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -3716,6 +3714,7 @@ msgstr ""
|
|||||||
#: ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h
|
#: ports/esp32s2/boards/adafruit_funhouse/mpconfigboard.h
|
||||||
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
|
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
|
||||||
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
|
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
|
||||||
|
#: ports/esp32s2/boards/artisense_rd00/mpconfigboard.h
|
||||||
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
|
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
|
||||||
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
|
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
|
||||||
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
|
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
|
||||||
@ -4014,10 +4013,6 @@ msgstr ""
|
|||||||
msgid "trapz is defined for 1D arrays of equal length"
|
msgid "trapz is defined for 1D arrays of equal length"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
|
|
||||||
msgid "trigger level must be 0 or 1"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: py/obj.c
|
#: py/obj.c
|
||||||
msgid "tuple/list has wrong length"
|
msgid "tuple/list has wrong length"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -4152,10 +4147,6 @@ msgstr ""
|
|||||||
msgid "value_count must be > 0"
|
msgid "value_count must be > 0"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: ports/esp32s2/common-hal/alarm/pin/__init__.c
|
|
||||||
msgid "wakeup conflict"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
|
#: ports/esp32s2/common-hal/watchdog/WatchDogTimer.c
|
||||||
msgid "watchdog not initialized"
|
msgid "watchdog not initialized"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -41,6 +41,7 @@ CIRCUITPY_BUILTINS_POW3 ?= 0
|
|||||||
CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1
|
CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1
|
||||||
CIRCUITPY_FREQUENCYIO ?= 0
|
CIRCUITPY_FREQUENCYIO ?= 0
|
||||||
CIRCUITPY_JSON ?= 0
|
CIRCUITPY_JSON ?= 0
|
||||||
|
CIRCUITPY_SYNTHIO ?= 0
|
||||||
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
|
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
|
||||||
|
|
||||||
# No room for HCI _bleio on SAMD21.
|
# No room for HCI _bleio on SAMD21.
|
||||||
|
@ -16,3 +16,5 @@ MCU_PACKAGE = UFQFPN48
|
|||||||
|
|
||||||
LD_COMMON = boards/common_default.ld
|
LD_COMMON = boards/common_default.ld
|
||||||
LD_FILE = boards/STM32F411_nvm_nofs.ld
|
LD_FILE = boards/STM32F411_nvm_nofs.ld
|
||||||
|
|
||||||
|
CIRCUITPY_SYNTHIO = 0
|
||||||
|
@ -280,6 +280,9 @@ endif
|
|||||||
ifeq ($(CIRCUITPY_SUPERVISOR),1)
|
ifeq ($(CIRCUITPY_SUPERVISOR),1)
|
||||||
SRC_PATTERNS += supervisor/%
|
SRC_PATTERNS += supervisor/%
|
||||||
endif
|
endif
|
||||||
|
ifeq ($(CIRCUITPY_SYNTHIO),1)
|
||||||
|
SRC_PATTERNS += synthio/%
|
||||||
|
endif
|
||||||
ifeq ($(CIRCUITPY_TERMINALIO),1)
|
ifeq ($(CIRCUITPY_TERMINALIO),1)
|
||||||
SRC_PATTERNS += terminalio/% fontio/%
|
SRC_PATTERNS += terminalio/% fontio/%
|
||||||
endif
|
endif
|
||||||
@ -524,6 +527,8 @@ SRC_SHARED_MODULE_ALL = \
|
|||||||
socket/__init__.c \
|
socket/__init__.c \
|
||||||
storage/__init__.c \
|
storage/__init__.c \
|
||||||
struct/__init__.c \
|
struct/__init__.c \
|
||||||
|
synthio/MidiTrack.c \
|
||||||
|
synthio/__init__.c \
|
||||||
terminalio/Terminal.c \
|
terminalio/Terminal.c \
|
||||||
terminalio/__init__.c \
|
terminalio/__init__.c \
|
||||||
time/__init__.c \
|
time/__init__.c \
|
||||||
|
@ -728,6 +728,13 @@ extern const struct _mp_obj_module_t supervisor_module;
|
|||||||
#define SUPERVISOR_MODULE
|
#define SUPERVISOR_MODULE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if CIRCUITPY_SYNTHIO
|
||||||
|
#define SYNTHIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_synthio), (mp_obj_t)&synthio_module },
|
||||||
|
extern const struct _mp_obj_module_t synthio_module;
|
||||||
|
#else
|
||||||
|
#define SYNTHIO_MODULE
|
||||||
|
#endif
|
||||||
|
|
||||||
#if CIRCUITPY_TIME
|
#if CIRCUITPY_TIME
|
||||||
extern const struct _mp_obj_module_t time_module;
|
extern const struct _mp_obj_module_t time_module;
|
||||||
#define TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module },
|
#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;
|
|||||||
STORAGE_MODULE \
|
STORAGE_MODULE \
|
||||||
STRUCT_MODULE \
|
STRUCT_MODULE \
|
||||||
SUPERVISOR_MODULE \
|
SUPERVISOR_MODULE \
|
||||||
|
SYNTHIO_MODULE \
|
||||||
TOUCHIO_MODULE \
|
TOUCHIO_MODULE \
|
||||||
UHEAP_MODULE \
|
UHEAP_MODULE \
|
||||||
USB_CDC_MODULE \
|
USB_CDC_MODULE \
|
||||||
|
@ -311,6 +311,9 @@ CFLAGS += -DCIRCUITPY_STRUCT=$(CIRCUITPY_STRUCT)
|
|||||||
CIRCUITPY_SUPERVISOR ?= 1
|
CIRCUITPY_SUPERVISOR ?= 1
|
||||||
CFLAGS += -DCIRCUITPY_SUPERVISOR=$(CIRCUITPY_SUPERVISOR)
|
CFLAGS += -DCIRCUITPY_SUPERVISOR=$(CIRCUITPY_SUPERVISOR)
|
||||||
|
|
||||||
|
CIRCUITPY_SYNTHIO ?= $(CIRCUITPY_AUDIOCORE)
|
||||||
|
CFLAGS += -DCIRCUITPY_SYNTHIO=$(CIRCUITPY_SYNTHIO)
|
||||||
|
|
||||||
CIRCUITPY_TERMINALIO ?= $(CIRCUITPY_DISPLAYIO)
|
CIRCUITPY_TERMINALIO ?= $(CIRCUITPY_DISPLAYIO)
|
||||||
CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
|
CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ WriteableBuffer = Union[
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
AudioSample = Union[
|
AudioSample = Union[
|
||||||
audiocore.WaveFile, audiocore.RawSample, audiomixer.Mixer, audiomp3.MP3Decoder
|
audiocore.WaveFile, audiocore.RawSample, audiomixer.Mixer, audiomp3.MP3Decoder, synthio.MidiTrack
|
||||||
]
|
]
|
||||||
"""Classes that implement the audiosample protocol
|
"""Classes that implement the audiosample protocol
|
||||||
|
|
||||||
@ -46,6 +46,7 @@ AudioSample = Union[
|
|||||||
- `audiocore.RawSample`
|
- `audiocore.RawSample`
|
||||||
- `audiomixer.Mixer`
|
- `audiomixer.Mixer`
|
||||||
- `audiomp3.MP3Decoder`
|
- `audiomp3.MP3Decoder`
|
||||||
|
- `synthio.MidiTrack`
|
||||||
|
|
||||||
You can play these back with `audioio.AudioOut`, `audiobusio.I2SOut` or `audiopwmio.PWMAudioOut`.
|
You can play these back with `audioio.AudioOut`, `audiobusio.I2SOut` or `audiopwmio.PWMAudioOut`.
|
||||||
"""
|
"""
|
||||||
|
@ -30,7 +30,6 @@
|
|||||||
#include "py/binary.h"
|
#include "py/binary.h"
|
||||||
#include "py/objproperty.h"
|
#include "py/objproperty.h"
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
#include "shared-bindings/microcontroller/Pin.h"
|
|
||||||
#include "shared-bindings/util.h"
|
#include "shared-bindings/util.h"
|
||||||
#include "shared-bindings/audiocore/RawSample.h"
|
#include "shared-bindings/audiocore/RawSample.h"
|
||||||
#include "supervisor/shared/translate.h"
|
#include "supervisor/shared/translate.h"
|
||||||
@ -83,7 +82,7 @@ STATIC mp_obj_t audioio_rawsample_make_new(const mp_obj_type_t *type, size_t n_a
|
|||||||
audioio_rawsample_obj_t *self = m_new_obj(audioio_rawsample_obj_t);
|
audioio_rawsample_obj_t *self = m_new_obj(audioio_rawsample_obj_t);
|
||||||
self->base.type = &audioio_rawsample_type;
|
self->base.type = &audioio_rawsample_type;
|
||||||
mp_buffer_info_t bufinfo;
|
mp_buffer_info_t bufinfo;
|
||||||
if (mp_get_buffer(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ)) {
|
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
|
||||||
uint8_t bytes_per_sample = 1;
|
uint8_t bytes_per_sample = 1;
|
||||||
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
|
bool signed_samples = bufinfo.typecode == 'b' || bufinfo.typecode == 'h';
|
||||||
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
|
if (bufinfo.typecode == 'h' || bufinfo.typecode == 'H') {
|
||||||
@ -94,15 +93,12 @@ STATIC mp_obj_t audioio_rawsample_make_new(const mp_obj_type_t *type, size_t n_a
|
|||||||
common_hal_audioio_rawsample_construct(self, ((uint8_t *)bufinfo.buf), bufinfo.len,
|
common_hal_audioio_rawsample_construct(self, ((uint8_t *)bufinfo.buf), bufinfo.len,
|
||||||
bytes_per_sample, signed_samples, args[ARG_channel_count].u_int,
|
bytes_per_sample, signed_samples, args[ARG_channel_count].u_int,
|
||||||
args[ARG_sample_rate].u_int);
|
args[ARG_sample_rate].u_int);
|
||||||
} else {
|
|
||||||
mp_raise_TypeError(translate("buffer must be a bytes-like object"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return MP_OBJ_FROM_PTR(self);
|
return MP_OBJ_FROM_PTR(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
//| def deinit(self) -> None:
|
//| def deinit(self) -> None:
|
||||||
//| """Deinitialises the AudioOut and releases any hardware resources for reuse."""
|
//| """Deinitialises the RawSample and releases any hardware resources for reuse."""
|
||||||
//| ...
|
//| ...
|
||||||
//|
|
//|
|
||||||
STATIC mp_obj_t audioio_rawsample_deinit(mp_obj_t self_in) {
|
STATIC mp_obj_t audioio_rawsample_deinit(mp_obj_t self_in) {
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
|
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
|
||||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
|
#define MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_RAWSAMPLE_H
|
||||||
|
|
||||||
#include "common-hal/microcontroller/Pin.h"
|
|
||||||
#include "shared-module/audiocore/RawSample.h"
|
#include "shared-module/audiocore/RawSample.h"
|
||||||
|
|
||||||
extern const mp_obj_type_t audioio_rawsample_type;
|
extern const mp_obj_type_t audioio_rawsample_type;
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
|
//| """Load a .wav file for playback with `audioio.AudioOut` or `audiobusio.I2SOut`.
|
||||||
//|
|
//|
|
||||||
//| :param typing.BinaryIO file: Already opened wave file
|
//| :param typing.BinaryIO file: Already opened wave file
|
||||||
//| :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.
|
//| :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.
|
||||||
//|
|
//|
|
||||||
//|
|
//|
|
||||||
//| Playing a wave file from flash::
|
//| Playing a wave file from flash::
|
||||||
|
169
shared-bindings/synthio/MidiTrack.c
Normal file
169
shared-bindings/synthio/MidiTrack.c
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Micro Python project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "lib/utils/context_manager_helpers.h"
|
||||||
|
#include "py/binary.h"
|
||||||
|
#include "py/objproperty.h"
|
||||||
|
#include "py/runtime.h"
|
||||||
|
#include "shared-bindings/util.h"
|
||||||
|
#include "shared-bindings/synthio/MidiTrack.h"
|
||||||
|
#include "supervisor/shared/translate.h"
|
||||||
|
|
||||||
|
//| class MidiTrack:
|
||||||
|
//| """Simple square-wave MIDI synth"""
|
||||||
|
//|
|
||||||
|
//| def __init__(self, buffer: ReadableBuffer, tempo: int, *, sample_rate: int = 11025) -> None:
|
||||||
|
//| """Create a MidiTrack from the given stream of MIDI events. Only "Note On" and "Note Off" events
|
||||||
|
//| are supported; channel numbers and key velocities are ignored. Up to two notes may be on at the
|
||||||
|
//| same time.
|
||||||
|
//|
|
||||||
|
//| :param ~_typing.ReadableBuffer buffer: Stream of MIDI events, as stored in a MIDI file track chunk
|
||||||
|
//| :param int tempo: Tempo of the streamed events, in MIDI ticks per second
|
||||||
|
//| :param int sample_rate: The desired playback sample rate; higher sample rate requires more memory
|
||||||
|
//|
|
||||||
|
//| Simple melody::
|
||||||
|
//|
|
||||||
|
//| import audioio
|
||||||
|
//| import board
|
||||||
|
//| import synthio
|
||||||
|
//|
|
||||||
|
//| dac = audioio.AudioOut(board.SPEAKER)
|
||||||
|
//| melody = synthio.MidiTrack(b"\\0\\x90H\\0*\\x80H\\0\\6\\x90J\\0*\\x80J\\0\\6\\x90L\\0*\\x80L\\0\\6\\x90J\\0" +
|
||||||
|
//| b"*\\x80J\\0\\6\\x90H\\0*\\x80H\\0\\6\\x90J\\0*\\x80J\\0\\6\\x90L\\0T\\x80L\\0" +
|
||||||
|
//| b"\\x0c\\x90H\\0T\\x80H\\0\\x0c\\x90H\\0T\\x80H\\0", tempo=640)
|
||||||
|
//| dac.play(melody)
|
||||||
|
//| print("playing")
|
||||||
|
//| while dac.playing:
|
||||||
|
//| pass
|
||||||
|
//| print("stopped")"""
|
||||||
|
//| ...
|
||||||
|
//|
|
||||||
|
STATIC mp_obj_t synthio_miditrack_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
|
enum { ARG_buffer, ARG_tempo, ARG_sample_rate };
|
||||||
|
static const mp_arg_t allowed_args[] = {
|
||||||
|
{ MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||||
|
{ MP_QSTR_tempo, MP_ARG_INT | MP_ARG_REQUIRED },
|
||||||
|
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 11025} },
|
||||||
|
};
|
||||||
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||||
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||||
|
|
||||||
|
mp_buffer_info_t bufinfo;
|
||||||
|
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
|
||||||
|
|
||||||
|
synthio_miditrack_obj_t *self = m_new_obj(synthio_miditrack_obj_t);
|
||||||
|
self->base.type = &synthio_miditrack_type;
|
||||||
|
|
||||||
|
common_hal_synthio_miditrack_construct(self,
|
||||||
|
(uint8_t *)bufinfo.buf, bufinfo.len,
|
||||||
|
args[ARG_tempo].u_int,
|
||||||
|
args[ARG_sample_rate].u_int);
|
||||||
|
|
||||||
|
return MP_OBJ_FROM_PTR(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
//| def deinit(self) -> None:
|
||||||
|
//| """Deinitialises the MidiTrack and releases any hardware resources for reuse."""
|
||||||
|
//| ...
|
||||||
|
//|
|
||||||
|
STATIC mp_obj_t synthio_miditrack_deinit(mp_obj_t self_in) {
|
||||||
|
synthio_miditrack_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
|
common_hal_synthio_miditrack_deinit(self);
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_deinit_obj, synthio_miditrack_deinit);
|
||||||
|
|
||||||
|
STATIC void check_for_deinit(synthio_miditrack_obj_t *self) {
|
||||||
|
if (common_hal_synthio_miditrack_deinited(self)) {
|
||||||
|
raise_deinited_error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//| def __enter__(self) -> MidiTrack:
|
||||||
|
//| """No-op used by Context Managers."""
|
||||||
|
//| ...
|
||||||
|
//|
|
||||||
|
// Provided by context manager helper.
|
||||||
|
|
||||||
|
//| def __exit__(self) -> None:
|
||||||
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
||||||
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
||||||
|
//| ...
|
||||||
|
//|
|
||||||
|
STATIC mp_obj_t synthio_miditrack_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
||||||
|
(void)n_args;
|
||||||
|
common_hal_synthio_miditrack_deinit(args[0]);
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(synthio_miditrack___exit___obj, 4, 4, synthio_miditrack_obj___exit__);
|
||||||
|
|
||||||
|
//| sample_rate: Optional[int]
|
||||||
|
//| """32 bit value that tells how quickly samples are played in Hertz (cycles per second)."""
|
||||||
|
//|
|
||||||
|
STATIC mp_obj_t synthio_miditrack_obj_get_sample_rate(mp_obj_t self_in) {
|
||||||
|
synthio_miditrack_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
|
check_for_deinit(self);
|
||||||
|
return MP_OBJ_NEW_SMALL_INT(common_hal_synthio_miditrack_get_sample_rate(self));
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_get_sample_rate_obj, synthio_miditrack_obj_get_sample_rate);
|
||||||
|
|
||||||
|
const mp_obj_property_t synthio_miditrack_sample_rate_obj = {
|
||||||
|
.base.type = &mp_type_property,
|
||||||
|
.proxy = {(mp_obj_t)&synthio_miditrack_get_sample_rate_obj,
|
||||||
|
(mp_obj_t)&mp_const_none_obj,
|
||||||
|
(mp_obj_t)&mp_const_none_obj},
|
||||||
|
};
|
||||||
|
|
||||||
|
STATIC const mp_rom_map_elem_t synthio_miditrack_locals_dict_table[] = {
|
||||||
|
// Methods
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&synthio_miditrack_deinit_obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&synthio_miditrack___exit___obj) },
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&synthio_miditrack_sample_rate_obj) },
|
||||||
|
};
|
||||||
|
STATIC MP_DEFINE_CONST_DICT(synthio_miditrack_locals_dict, synthio_miditrack_locals_dict_table);
|
||||||
|
|
||||||
|
STATIC const audiosample_p_t synthio_miditrack_proto = {
|
||||||
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample)
|
||||||
|
.sample_rate = (audiosample_sample_rate_fun)common_hal_synthio_miditrack_get_sample_rate,
|
||||||
|
.bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_synthio_miditrack_get_bits_per_sample,
|
||||||
|
.channel_count = (audiosample_channel_count_fun)common_hal_synthio_miditrack_get_channel_count,
|
||||||
|
.reset_buffer = (audiosample_reset_buffer_fun)synthio_miditrack_reset_buffer,
|
||||||
|
.get_buffer = (audiosample_get_buffer_fun)synthio_miditrack_get_buffer,
|
||||||
|
.get_buffer_structure = (audiosample_get_buffer_structure_fun)synthio_miditrack_get_buffer_structure,
|
||||||
|
};
|
||||||
|
|
||||||
|
const mp_obj_type_t synthio_miditrack_type = {
|
||||||
|
{ &mp_type_type },
|
||||||
|
.name = MP_QSTR_MidiTrack,
|
||||||
|
.make_new = synthio_miditrack_make_new,
|
||||||
|
.locals_dict = (mp_obj_dict_t *)&synthio_miditrack_locals_dict,
|
||||||
|
.protocol = &synthio_miditrack_proto,
|
||||||
|
};
|
43
shared-bindings/synthio/MidiTrack.h
Normal file
43
shared-bindings/synthio/MidiTrack.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Micro Python project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO_MIDITRACK_H
|
||||||
|
#define MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO_MIDITRACK_H
|
||||||
|
|
||||||
|
#include "shared-module/synthio/MidiTrack.h"
|
||||||
|
|
||||||
|
extern const mp_obj_type_t synthio_miditrack_type;
|
||||||
|
|
||||||
|
void common_hal_synthio_miditrack_construct(synthio_miditrack_obj_t *self,
|
||||||
|
const uint8_t *buffer, uint32_t len, uint32_t tempo, uint32_t sample_rate);
|
||||||
|
|
||||||
|
void common_hal_synthio_miditrack_deinit(synthio_miditrack_obj_t *self);
|
||||||
|
bool common_hal_synthio_miditrack_deinited(synthio_miditrack_obj_t *self);
|
||||||
|
uint32_t common_hal_synthio_miditrack_get_sample_rate(synthio_miditrack_obj_t *self);
|
||||||
|
uint8_t common_hal_synthio_miditrack_get_bits_per_sample(synthio_miditrack_obj_t *self);
|
||||||
|
uint8_t common_hal_synthio_miditrack_get_channel_count(synthio_miditrack_obj_t *self);
|
||||||
|
|
||||||
|
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO_MIDITRACK_H
|
136
shared-bindings/synthio/__init__.c
Normal file
136
shared-bindings/synthio/__init__.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "py/mperrno.h"
|
||||||
|
#include "py/obj.h"
|
||||||
|
#include "py/runtime.h"
|
||||||
|
#include "extmod/vfs_fat.h"
|
||||||
|
|
||||||
|
#include "shared-bindings/synthio/__init__.h"
|
||||||
|
#include "shared-bindings/synthio/MidiTrack.h"
|
||||||
|
|
||||||
|
//| """Support for MIDI synthesis"""
|
||||||
|
//|
|
||||||
|
//| def from_file(file: typing.BinaryIO, *, sample_rate: int = 11025) -> MidiTrack:
|
||||||
|
//| """Create an AudioSample from an already opened MIDI file.
|
||||||
|
//| Currently, only single-track MIDI (type 0) is supported.
|
||||||
|
//|
|
||||||
|
//| :param typing.BinaryIO file: Already opened MIDI file
|
||||||
|
//| :param int sample_rate: The desired playback sample rate; higher sample rate requires more memory
|
||||||
|
//|
|
||||||
|
//|
|
||||||
|
//| Playing a MIDI file from flash::
|
||||||
|
//|
|
||||||
|
//| import audioio
|
||||||
|
//| import board
|
||||||
|
//| import synthio
|
||||||
|
//|
|
||||||
|
//| data = open("single-track.midi", "rb")
|
||||||
|
//| midi = synthio.from_file(data)
|
||||||
|
//| a = audioio.AudioOut(board.A0)
|
||||||
|
//|
|
||||||
|
//| print("playing")
|
||||||
|
//| a.play(midi)
|
||||||
|
//| while a.playing:
|
||||||
|
//| pass
|
||||||
|
//| print("stopped")"""
|
||||||
|
//| ...
|
||||||
|
//|
|
||||||
|
STATIC mp_obj_t synthio_from_file(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
|
enum { ARG_file, ARG_sample_rate };
|
||||||
|
static const mp_arg_t allowed_args[] = {
|
||||||
|
{ MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||||
|
{ MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 11025} },
|
||||||
|
};
|
||||||
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||||
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||||
|
if (!MP_OBJ_IS_TYPE(args[ARG_file].u_obj, &mp_type_fileio)) {
|
||||||
|
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
|
||||||
|
}
|
||||||
|
pyb_file_obj_t *file = MP_OBJ_TO_PTR(args[ARG_file].u_obj);
|
||||||
|
|
||||||
|
uint8_t chunk_header[14];
|
||||||
|
f_rewind(&file->fp);
|
||||||
|
UINT bytes_read;
|
||||||
|
if (f_read(&file->fp, chunk_header, sizeof(chunk_header), &bytes_read) != FR_OK) {
|
||||||
|
mp_raise_OSError(MP_EIO);
|
||||||
|
}
|
||||||
|
if (bytes_read != sizeof(chunk_header) ||
|
||||||
|
memcmp(chunk_header, "MThd\0\0\0\6\0\0\0\1", 12)) {
|
||||||
|
mp_raise_ValueError(translate("Invalid MIDI file"));
|
||||||
|
// TODO: for a multi-track MIDI (type 1), return an AudioMixer
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t tempo;
|
||||||
|
if (chunk_header[12] & 0x80) {
|
||||||
|
tempo = -(int8_t)chunk_header[12] * chunk_header[13];
|
||||||
|
} else {
|
||||||
|
tempo = 2 * ((chunk_header[12] << 8) | chunk_header[13]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (f_read(&file->fp, chunk_header, 8, &bytes_read) != FR_OK) {
|
||||||
|
mp_raise_OSError(MP_EIO);
|
||||||
|
}
|
||||||
|
if (bytes_read != 8 || memcmp(chunk_header, "MTrk", 4)) {
|
||||||
|
mp_raise_ValueError(translate("Invalid MIDI file"));
|
||||||
|
}
|
||||||
|
uint32_t track_size = (chunk_header[4] << 24) |
|
||||||
|
(chunk_header[5] << 16) | (chunk_header[6] << 8) | chunk_header[7];
|
||||||
|
uint8_t *buffer = m_malloc(track_size, false);
|
||||||
|
if (f_read(&file->fp, buffer, track_size, &bytes_read) != FR_OK) {
|
||||||
|
mp_raise_OSError(MP_EIO);
|
||||||
|
}
|
||||||
|
if (bytes_read != track_size) {
|
||||||
|
mp_raise_ValueError(translate("Invalid MIDI file"));
|
||||||
|
}
|
||||||
|
|
||||||
|
synthio_miditrack_obj_t *result = m_new_obj(synthio_miditrack_obj_t);
|
||||||
|
result->base.type = &synthio_miditrack_type;
|
||||||
|
|
||||||
|
common_hal_synthio_miditrack_construct(result, buffer, track_size,
|
||||||
|
tempo, args[ARG_sample_rate].u_int);
|
||||||
|
|
||||||
|
m_free(buffer);
|
||||||
|
|
||||||
|
return MP_OBJ_FROM_PTR(result);
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_KW(synthio_from_file_obj, 1, synthio_from_file);
|
||||||
|
|
||||||
|
|
||||||
|
STATIC const mp_rom_map_elem_t synthio_module_globals_table[] = {
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_synthio) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_MidiTrack), MP_ROM_PTR(&synthio_miditrack_type) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_from_file), MP_ROM_PTR(&synthio_from_file_obj) },
|
||||||
|
};
|
||||||
|
|
||||||
|
STATIC MP_DEFINE_CONST_DICT(synthio_module_globals, synthio_module_globals_table);
|
||||||
|
|
||||||
|
const mp_obj_module_t synthio_module = {
|
||||||
|
.base = { &mp_type_module },
|
||||||
|
.globals = (mp_obj_dict_t *)&synthio_module_globals,
|
||||||
|
};
|
34
shared-bindings/synthio/__init__.h
Normal file
34
shared-bindings/synthio/__init__.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO___INIT___H
|
||||||
|
#define MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO___INIT___H
|
||||||
|
|
||||||
|
#include "py/obj.h"
|
||||||
|
|
||||||
|
// Nothing now.
|
||||||
|
|
||||||
|
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SYNTHIO___INIT___H
|
212
shared-module/synthio/MidiTrack.c
Normal file
212
shared-module/synthio/MidiTrack.c
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Micro Python project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "py/runtime.h"
|
||||||
|
#include "shared-bindings/synthio/MidiTrack.h"
|
||||||
|
|
||||||
|
#define LOUDNESS 0x4000 // 0.5
|
||||||
|
#define BITS_PER_SAMPLE 16
|
||||||
|
#define BYTES_PER_SAMPLE (BITS_PER_SAMPLE / 8)
|
||||||
|
#define SILENCE 0x80
|
||||||
|
|
||||||
|
STATIC uint8_t parse_note(const uint8_t *buffer, uint32_t len, uint32_t *pos) {
|
||||||
|
if (*pos + 1 >= len) {
|
||||||
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), *pos);
|
||||||
|
}
|
||||||
|
uint8_t note = buffer[(*pos)++];
|
||||||
|
if (note > 127 || buffer[(*pos)++] > 127) {
|
||||||
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), *pos);
|
||||||
|
}
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC void terminate_span(synthio_miditrack_obj_t *self, uint16_t *dur, uint16_t *max_dur) {
|
||||||
|
if (*dur) {
|
||||||
|
self->track[self->total_spans - 1].dur = *dur;
|
||||||
|
if (*dur > *max_dur) {
|
||||||
|
*max_dur = *dur;
|
||||||
|
}
|
||||||
|
*dur = 0;
|
||||||
|
} else {
|
||||||
|
self->total_spans--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC void add_span(synthio_miditrack_obj_t *self, uint8_t note1, uint8_t note2) {
|
||||||
|
synthio_midi_span_t span = { 0, {note1, note2} };
|
||||||
|
self->track = m_realloc(self->track,
|
||||||
|
(self->total_spans + 1) * sizeof(synthio_midi_span_t));
|
||||||
|
self->track[self->total_spans++] = span;
|
||||||
|
}
|
||||||
|
|
||||||
|
void common_hal_synthio_miditrack_construct(synthio_miditrack_obj_t *self,
|
||||||
|
const uint8_t *buffer, uint32_t len, uint32_t tempo, uint32_t sample_rate) {
|
||||||
|
|
||||||
|
synthio_midi_span_t initial = { 0, {SILENCE, SILENCE} };
|
||||||
|
self->sample_rate = sample_rate;
|
||||||
|
self->track = m_malloc(sizeof(synthio_midi_span_t), false);
|
||||||
|
self->next_span = 0;
|
||||||
|
self->total_spans = 1;
|
||||||
|
*self->track = initial;
|
||||||
|
|
||||||
|
uint16_t dur = 0, max_dur = 0;
|
||||||
|
uint32_t pos = 0;
|
||||||
|
while (pos < len) {
|
||||||
|
uint8_t c;
|
||||||
|
uint32_t delta = 0;
|
||||||
|
do {
|
||||||
|
c = buffer[pos++];
|
||||||
|
delta <<= 7;
|
||||||
|
delta |= c & 0x7f;
|
||||||
|
} while ((c & 0x80) && (pos < len));
|
||||||
|
|
||||||
|
if (c & 0x80) {
|
||||||
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
dur += delta * sample_rate / tempo;
|
||||||
|
|
||||||
|
switch (buffer[pos++] >> 4) {
|
||||||
|
case 8: { // Note Off
|
||||||
|
uint8_t note = parse_note(buffer, len, &pos);
|
||||||
|
|
||||||
|
// Ignore if not a note which is playing
|
||||||
|
synthio_midi_span_t last_span = self->track[self->total_spans - 1];
|
||||||
|
if (last_span.note[0] == note || last_span.note[1] == note) {
|
||||||
|
terminate_span(self, &dur, &max_dur);
|
||||||
|
if (last_span.note[0] == note) {
|
||||||
|
add_span(self, last_span.note[1], SILENCE);
|
||||||
|
} else {
|
||||||
|
add_span(self, last_span.note[0], SILENCE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 9: { // Note On
|
||||||
|
uint8_t note = parse_note(buffer, len, &pos);
|
||||||
|
|
||||||
|
// Ignore if two notes are already playing
|
||||||
|
synthio_midi_span_t last_span = self->track[self->total_spans - 1];
|
||||||
|
if (last_span.note[1] == SILENCE) {
|
||||||
|
terminate_span(self, &dur, &max_dur);
|
||||||
|
if (last_span.note[0] == SILENCE) {
|
||||||
|
add_span(self, note, SILENCE);
|
||||||
|
} else {
|
||||||
|
add_span(self, last_span.note[0], note);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 10:
|
||||||
|
case 11:
|
||||||
|
case 14: // two data bytes to ignore
|
||||||
|
parse_note(buffer, len, &pos);
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
case 13: // one data byte to ignore
|
||||||
|
if (pos >= len || buffer[pos++] > 127) {
|
||||||
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), pos);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 15: // the full syntax is too complicated, just assume it's "End of Track" event
|
||||||
|
pos = len;
|
||||||
|
break;
|
||||||
|
default: // invalid event
|
||||||
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
terminate_span(self, &dur, &max_dur);
|
||||||
|
|
||||||
|
self->buffer_length = max_dur * BYTES_PER_SAMPLE;
|
||||||
|
self->buffer = m_malloc(self->buffer_length, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void common_hal_synthio_miditrack_deinit(synthio_miditrack_obj_t *self) {
|
||||||
|
m_free(self->buffer);
|
||||||
|
self->buffer = NULL;
|
||||||
|
m_free(self->track);
|
||||||
|
self->track = NULL;
|
||||||
|
}
|
||||||
|
bool common_hal_synthio_miditrack_deinited(synthio_miditrack_obj_t *self) {
|
||||||
|
return self->buffer == NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t common_hal_synthio_miditrack_get_sample_rate(synthio_miditrack_obj_t *self) {
|
||||||
|
return self->sample_rate;
|
||||||
|
}
|
||||||
|
uint8_t common_hal_synthio_miditrack_get_bits_per_sample(synthio_miditrack_obj_t *self) {
|
||||||
|
return BITS_PER_SAMPLE;
|
||||||
|
}
|
||||||
|
uint8_t common_hal_synthio_miditrack_get_channel_count(synthio_miditrack_obj_t *self) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void synthio_miditrack_reset_buffer(synthio_miditrack_obj_t *self,
|
||||||
|
bool single_channel, uint8_t channel) {
|
||||||
|
|
||||||
|
self->next_span = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC const uint16_t notes[] = {8372, 8870, 9397, 9956, 10548, 11175, 11840,
|
||||||
|
12544, 13290, 14080, 14917, 15804}; // 9th octave
|
||||||
|
|
||||||
|
audioio_get_buffer_result_t synthio_miditrack_get_buffer(synthio_miditrack_obj_t *self,
|
||||||
|
bool single_channel, uint8_t channel, uint8_t **buffer, uint32_t *buffer_length) {
|
||||||
|
|
||||||
|
if (self->next_span >= self->total_spans) {
|
||||||
|
*buffer_length = 0;
|
||||||
|
return GET_BUFFER_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
synthio_midi_span_t span = self->track[self->next_span++];
|
||||||
|
*buffer_length = span.dur * BYTES_PER_SAMPLE;
|
||||||
|
uint8_t octave1 = span.note[0] / 12; // 0..10
|
||||||
|
uint8_t octave2 = span.note[1] / 12; // 0..10
|
||||||
|
int32_t base_freq1 = notes[span.note[0] % 12];
|
||||||
|
int32_t base_freq2 = notes[span.note[1] % 12];
|
||||||
|
int32_t sample_rate = self->sample_rate;
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < span.dur; i++) {
|
||||||
|
int16_t semiperiod1 = span.note[0] == SILENCE ? 0 :
|
||||||
|
((base_freq1 * i * 2) / sample_rate) >> (10 - octave1);
|
||||||
|
int16_t semiperiod2 = span.note[1] == SILENCE ? semiperiod1 :
|
||||||
|
((base_freq2 * i * 2) / sample_rate) >> (10 - octave2);
|
||||||
|
self->buffer[i] = ((semiperiod1 % 2 + semiperiod2 % 2) - 1) * LOUDNESS;
|
||||||
|
}
|
||||||
|
*buffer = (uint8_t *)self->buffer;
|
||||||
|
|
||||||
|
return self->next_span >= self->total_spans ?
|
||||||
|
GET_BUFFER_DONE : GET_BUFFER_MORE_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
void synthio_miditrack_get_buffer_structure(synthio_miditrack_obj_t *self, bool single_channel,
|
||||||
|
bool *single_buffer, bool *samples_signed, uint32_t *max_buffer_length, uint8_t *spacing) {
|
||||||
|
|
||||||
|
*single_buffer = true;
|
||||||
|
*samples_signed = true;
|
||||||
|
*max_buffer_length = self->buffer_length;
|
||||||
|
*spacing = 1;
|
||||||
|
}
|
65
shared-module/synthio/MidiTrack.h
Normal file
65
shared-module/synthio/MidiTrack.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO_MIDITRACK_H
|
||||||
|
#define MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO_MIDITRACK_H
|
||||||
|
|
||||||
|
#include "py/obj.h"
|
||||||
|
|
||||||
|
#include "shared-module/synthio/__init__.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t dur;
|
||||||
|
uint8_t note[2];
|
||||||
|
} synthio_midi_span_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
mp_obj_base_t base;
|
||||||
|
uint32_t sample_rate;
|
||||||
|
uint16_t *buffer;
|
||||||
|
uint16_t buffer_length;
|
||||||
|
synthio_midi_span_t *track;
|
||||||
|
uint16_t next_span;
|
||||||
|
uint16_t total_spans;
|
||||||
|
} synthio_miditrack_obj_t;
|
||||||
|
|
||||||
|
|
||||||
|
// These are not available from Python because it may be called in an interrupt.
|
||||||
|
void synthio_miditrack_reset_buffer(synthio_miditrack_obj_t *self,
|
||||||
|
bool single_channel,
|
||||||
|
uint8_t channel);
|
||||||
|
|
||||||
|
audioio_get_buffer_result_t synthio_miditrack_get_buffer(synthio_miditrack_obj_t *self,
|
||||||
|
bool single_channel,
|
||||||
|
uint8_t channel,
|
||||||
|
uint8_t **buffer,
|
||||||
|
uint32_t *buffer_length); // length in bytes
|
||||||
|
|
||||||
|
void synthio_miditrack_get_buffer_structure(synthio_miditrack_obj_t *self, bool single_channel,
|
||||||
|
bool *single_buffer, bool *samples_signed,
|
||||||
|
uint32_t *max_buffer_length, uint8_t *spacing);
|
||||||
|
|
||||||
|
#endif // MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO_MIDITRACK_H
|
0
shared-module/synthio/__init__.c
Normal file
0
shared-module/synthio/__init__.c
Normal file
32
shared-module/synthio/__init__.h
Normal file
32
shared-module/synthio/__init__.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Artyom Skrobov
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO__INIT__H
|
||||||
|
#define MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO__INIT__H
|
||||||
|
|
||||||
|
#include "shared-module/audiocore/__init__.h"
|
||||||
|
|
||||||
|
#endif // MICROPY_INCLUDED_SHARED_MODULE_SYNTHIO__INIT__H
|
BIN
tests/circuitpython-manual/audiocore/single-track.midi
Executable file
BIN
tests/circuitpython-manual/audiocore/single-track.midi
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user