|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Artyom Skrobov |
| 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 | + |
| 27 | +#include <stdint.h> |
| 28 | + |
| 29 | +#include "lib/utils/context_manager_helpers.h" |
| 30 | +#include "py/binary.h" |
| 31 | +#include "py/objproperty.h" |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "shared-bindings/util.h" |
| 34 | +#include "shared-bindings/synthio/MidiTrack.h" |
| 35 | +#include "supervisor/shared/translate.h" |
| 36 | + |
| 37 | +//| class MidiTrack: |
| 38 | +//| """Simple square-wave MIDI synth""" |
| 39 | +//| |
| 40 | +//| def __init__(self, buffer: ReadableBuffer, tempo: int, *, sample_rate: int = 11025) -> None: |
| 41 | +//| """Create a MidiTrack from the given stream of MIDI events. Only "Note On" and "Note Off" events |
| 42 | +//| are supported; channel numbers and key velocities are ignored. Up to two notes may be on at the |
| 43 | +//| same time. |
| 44 | +//| |
| 45 | +//| :param ~_typing.ReadableBuffer buffer: Stream of MIDI events, as stored in a MIDI file track chunk |
| 46 | +//| :param int tempo: Tempo of the streamed events, in MIDI ticks per second |
| 47 | +//| :param int sample_rate: The desired playback sample rate; higher sample rate requires more memory |
| 48 | +//| |
| 49 | +//| Simple melody:: |
| 50 | +//| |
| 51 | +//| import audioio |
| 52 | +//| import board |
| 53 | +//| import synthio |
| 54 | +//| |
| 55 | +//| dac = audioio.AudioOut(board.SPEAKER) |
| 56 | +//| melody = synthio.MidiTrack(b"\\0\\x90H\\0*\\x80H\\0\\6\\x90J\\0*\\x80J\\0\\6\\x90L\\0*\\x80L\\0\\6\\x90J\\0" + |
| 57 | +//| b"*\\x80J\\0\\6\\x90H\\0*\\x80H\\0\\6\\x90J\\0*\\x80J\\0\\6\\x90L\\0T\\x80L\\0" + |
| 58 | +//| b"\\x0c\\x90H\\0T\\x80H\\0\\x0c\\x90H\\0T\\x80H\\0", tempo=640) |
| 59 | +//| dac.play(melody) |
| 60 | +//| print("playing") |
| 61 | +//| while dac.playing: |
| 62 | +//| pass |
| 63 | +//| print("stopped")""" |
| 64 | +//| ... |
| 65 | +//| |
| 66 | +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) { |
| 67 | + enum { ARG_buffer, ARG_tempo, ARG_sample_rate }; |
| 68 | + static const mp_arg_t allowed_args[] = { |
| 69 | + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 70 | + { MP_QSTR_tempo, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 71 | + { MP_QSTR_sample_rate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 11025} }, |
| 72 | + }; |
| 73 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 74 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 75 | + |
| 76 | + mp_buffer_info_t bufinfo; |
| 77 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); |
| 78 | + |
| 79 | + synthio_miditrack_obj_t *self = m_new_obj(synthio_miditrack_obj_t); |
| 80 | + self->base.type = &synthio_miditrack_type; |
| 81 | + |
| 82 | + common_hal_synthio_miditrack_construct(self, |
| 83 | + (uint8_t *)bufinfo.buf, bufinfo.len, |
| 84 | + args[ARG_tempo].u_int, |
| 85 | + args[ARG_sample_rate].u_int); |
| 86 | + |
| 87 | + return MP_OBJ_FROM_PTR(self); |
| 88 | +} |
| 89 | + |
| 90 | +//| def deinit(self) -> None: |
| 91 | +//| """Deinitialises the MidiTrack and releases any hardware resources for reuse.""" |
| 92 | +//| ... |
| 93 | +//| |
| 94 | +STATIC mp_obj_t synthio_miditrack_deinit(mp_obj_t self_in) { |
| 95 | + synthio_miditrack_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 96 | + common_hal_synthio_miditrack_deinit(self); |
| 97 | + return mp_const_none; |
| 98 | +} |
| 99 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_deinit_obj, synthio_miditrack_deinit); |
| 100 | + |
| 101 | +STATIC void check_for_deinit(synthio_miditrack_obj_t *self) { |
| 102 | + if (common_hal_synthio_miditrack_deinited(self)) { |
| 103 | + raise_deinited_error(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +//| def __enter__(self) -> MidiTrack: |
| 108 | +//| """No-op used by Context Managers.""" |
| 109 | +//| ... |
| 110 | +//| |
| 111 | +// Provided by context manager helper. |
| 112 | + |
| 113 | +//| def __exit__(self) -> None: |
| 114 | +//| """Automatically deinitializes the hardware when exiting a context. See |
| 115 | +//| :ref:`lifetime-and-contextmanagers` for more info.""" |
| 116 | +//| ... |
| 117 | +//| |
| 118 | +STATIC mp_obj_t synthio_miditrack_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 119 | + (void)n_args; |
| 120 | + common_hal_synthio_miditrack_deinit(args[0]); |
| 121 | + return mp_const_none; |
| 122 | +} |
| 123 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(synthio_miditrack___exit___obj, 4, 4, synthio_miditrack_obj___exit__); |
| 124 | + |
| 125 | +//| sample_rate: Optional[int] |
| 126 | +//| """32 bit value that tells how quickly samples are played in Hertz (cycles per second).""" |
| 127 | +//| |
| 128 | +STATIC mp_obj_t synthio_miditrack_obj_get_sample_rate(mp_obj_t self_in) { |
| 129 | + synthio_miditrack_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 130 | + check_for_deinit(self); |
| 131 | + return MP_OBJ_NEW_SMALL_INT(common_hal_synthio_miditrack_get_sample_rate(self)); |
| 132 | +} |
| 133 | +MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_get_sample_rate_obj, synthio_miditrack_obj_get_sample_rate); |
| 134 | + |
| 135 | +const mp_obj_property_t synthio_miditrack_sample_rate_obj = { |
| 136 | + .base.type = &mp_type_property, |
| 137 | + .proxy = {(mp_obj_t)&synthio_miditrack_get_sample_rate_obj, |
| 138 | + (mp_obj_t)&mp_const_none_obj, |
| 139 | + (mp_obj_t)&mp_const_none_obj}, |
| 140 | +}; |
| 141 | + |
| 142 | +STATIC const mp_rom_map_elem_t synthio_miditrack_locals_dict_table[] = { |
| 143 | + // Methods |
| 144 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&synthio_miditrack_deinit_obj) }, |
| 145 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, |
| 146 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&synthio_miditrack___exit___obj) }, |
| 147 | + |
| 148 | + // Properties |
| 149 | + { MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&synthio_miditrack_sample_rate_obj) }, |
| 150 | +}; |
| 151 | +STATIC MP_DEFINE_CONST_DICT(synthio_miditrack_locals_dict, synthio_miditrack_locals_dict_table); |
| 152 | + |
| 153 | +STATIC const audiosample_p_t synthio_miditrack_proto = { |
| 154 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_audiosample) |
| 155 | + .sample_rate = (audiosample_sample_rate_fun)common_hal_synthio_miditrack_get_sample_rate, |
| 156 | + .bits_per_sample = (audiosample_bits_per_sample_fun)common_hal_synthio_miditrack_get_bits_per_sample, |
| 157 | + .channel_count = (audiosample_channel_count_fun)common_hal_synthio_miditrack_get_channel_count, |
| 158 | + .reset_buffer = (audiosample_reset_buffer_fun)synthio_miditrack_reset_buffer, |
| 159 | + .get_buffer = (audiosample_get_buffer_fun)synthio_miditrack_get_buffer, |
| 160 | + .get_buffer_structure = (audiosample_get_buffer_structure_fun)synthio_miditrack_get_buffer_structure, |
| 161 | +}; |
| 162 | + |
| 163 | +const mp_obj_type_t synthio_miditrack_type = { |
| 164 | + { &mp_type_type }, |
| 165 | + .name = MP_QSTR_MidiTrack, |
| 166 | + .make_new = synthio_miditrack_make_new, |
| 167 | + .locals_dict = (mp_obj_dict_t *)&synthio_miditrack_locals_dict, |
| 168 | + .protocol = &synthio_miditrack_proto, |
| 169 | +}; |
0 commit comments