aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/synthio
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
commit4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch)
tree65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/shared-bindings/synthio
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/shared-bindings/synthio')
-rw-r--r--circuitpython/shared-bindings/synthio/MidiTrack.c168
-rw-r--r--circuitpython/shared-bindings/synthio/MidiTrack.h43
-rw-r--r--circuitpython/shared-bindings/synthio/__init__.c138
-rw-r--r--circuitpython/shared-bindings/synthio/__init__.h34
4 files changed, 383 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/synthio/MidiTrack.c b/circuitpython/shared-bindings/synthio/MidiTrack.c
new file mode 100644
index 0000000..7805c1a
--- /dev/null
+++ b/circuitpython/shared-bindings/synthio/MidiTrack.c
@@ -0,0 +1,168 @@
+/*
+ * 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 "shared/runtime/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 ~circuitpython_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, size_t n_kw, const mp_obj_t *all_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_kw_array(n_args, n_kw, all_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);
+
+MP_PROPERTY_GETTER(synthio_miditrack_sample_rate_obj,
+ (mp_obj_t)&synthio_miditrack_get_sample_rate_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,
+ .flags = MP_TYPE_FLAG_EXTENDED,
+ .make_new = synthio_miditrack_make_new,
+ .locals_dict = (mp_obj_dict_t *)&synthio_miditrack_locals_dict,
+ MP_TYPE_EXTENDED_FIELDS(
+ .protocol = &synthio_miditrack_proto,
+ ),
+};
diff --git a/circuitpython/shared-bindings/synthio/MidiTrack.h b/circuitpython/shared-bindings/synthio/MidiTrack.h
new file mode 100644
index 0000000..d44d4c3
--- /dev/null
+++ b/circuitpython/shared-bindings/synthio/MidiTrack.h
@@ -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
diff --git a/circuitpython/shared-bindings/synthio/__init__.c b/circuitpython/shared-bindings/synthio/__init__.c
new file mode 100644
index 0000000..72fe2eb
--- /dev/null
+++ b/circuitpython/shared-bindings/synthio/__init__.c
@@ -0,0 +1,138 @@
+/*
+ * 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,
+};
+
+MP_REGISTER_MODULE(MP_QSTR_synthio, synthio_module, CIRCUITPY_SYNTHIO);
diff --git a/circuitpython/shared-bindings/synthio/__init__.h b/circuitpython/shared-bindings/synthio/__init__.h
new file mode 100644
index 0000000..14af1a5
--- /dev/null
+++ b/circuitpython/shared-bindings/synthio/__init__.h
@@ -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