forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.c
347 lines (312 loc) · 15 KB
/
__init__.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Artyom Skrobov
//
// SPDX-License-Identifier: MIT
#include <math.h>
#include <string.h>
#include "py/enum.h"
#include "py/mperrno.h"
#include "py/obj.h"
#include "py/objnamedtuple.h"
#include "py/runtime.h"
#include "extmod/vfs_fat.h"
#include "extmod/vfs_posix.h"
#include "shared-bindings/synthio/__init__.h"
#include "shared-bindings/synthio/Biquad.h"
#include "shared-bindings/synthio/BlockBiquad.h"
#include "shared-bindings/synthio/LFO.h"
#include "shared-bindings/synthio/Math.h"
#include "shared-bindings/synthio/MidiTrack.h"
#include "shared-bindings/synthio/Note.h"
#include "shared-bindings/synthio/Synthesizer.h"
#include "shared-module/synthio/LFO.h"
//|
//| """Support for multi-channel audio synthesis
//|
//| At least 2 simultaneous notes are supported. samd5x, mimxrt10xx and rp2040 platforms support up to 12 notes.
//| """
//|
//| AnyBiquad = Union["Biquad", "BlockBiquad"]
//|
//|
//| class EnvelopeState:
//| ATTACK: EnvelopeState
//| """The note is in its attack phase"""
//| DECAY: EnvelopeState
//| """The note is in its decay phase"""
//| SUSTAIN: EnvelopeState
//| """The note is in its sustain phase"""
//| RELEASE: EnvelopeState
//| """The note is in its release phase"""
//|
//|
MAKE_ENUM_VALUE(synthio_note_state_type, note_state, ATTACK, SYNTHIO_ENVELOPE_STATE_ATTACK);
MAKE_ENUM_VALUE(synthio_note_state_type, note_state, DECAY, SYNTHIO_ENVELOPE_STATE_DECAY);
MAKE_ENUM_VALUE(synthio_note_state_type, note_state, SUSTAIN, SYNTHIO_ENVELOPE_STATE_SUSTAIN);
MAKE_ENUM_VALUE(synthio_note_state_type, note_state, RELEASE, SYNTHIO_ENVELOPE_STATE_RELEASE);
MAKE_ENUM_MAP(synthio_note_state) {
MAKE_ENUM_MAP_ENTRY(note_state, ATTACK),
MAKE_ENUM_MAP_ENTRY(note_state, DECAY),
MAKE_ENUM_MAP_ENTRY(note_state, SUSTAIN),
MAKE_ENUM_MAP_ENTRY(note_state, RELEASE),
};
static MP_DEFINE_CONST_DICT(synthio_note_state_locals_dict, synthio_note_state_locals_table);
MAKE_PRINTER(synthio, synthio_note_state);
MAKE_ENUM_TYPE(synthio, EnvelopeState, synthio_note_state);
#define default_attack_time (MICROPY_FLOAT_CONST(0.1))
#define default_decay_time (MICROPY_FLOAT_CONST(0.05))
#define default_release_time (MICROPY_FLOAT_CONST(0.2))
#define default_attack_level (MICROPY_FLOAT_CONST(1.))
#define default_sustain_level (MICROPY_FLOAT_CONST(0.8))
static const mp_arg_t envelope_properties[] = {
{ MP_QSTR_attack_time, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_decay_time, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_release_time, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_attack_level, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL } },
{ MP_QSTR_sustain_level, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NULL } },
};
//| BlockInput = Union["Math", "LFO", float, None]
//| """Blocks and Notes can take any of these types as inputs on certain attributes
//|
//| A BlockInput can be any of the following types: `Math`, `LFO`, `builtins.float`, `None` (treated same as 0).
//| """
//|
//|
//| class Envelope:
//| def __init__(
//| self,
//| *,
//| attack_time: Optional[float] = 0.1,
//| decay_time: Optional[float] = 0.05,
//| release_time: Optional[float] = 0.2,
//| attack_level: Optional[float] = 1.0,
//| sustain_level: Optional[float] = 0.8,
//| ) -> None:
//| """Construct an Envelope object
//|
//| The Envelope defines an ADSR (Attack, Decay, Sustain, Release) envelope with linear amplitude ramping. A note starts at 0 volume, then increases to ``attack_level`` over ``attack_time`` seconds; then it decays to ``sustain_level`` over ``decay_time`` seconds. Finally, when the note is released, it decreases to ``0`` volume over ``release_time``.
//|
//| If the ``sustain_level`` of an envelope is 0, then the decay and sustain phases of the note are always omitted. The note is considered to be released as soon as the envelope reaches the end of the attack phase. The ``decay_time`` is ignored. This is similar to how a plucked or struck instrument behaves.
//|
//| If a note is released before it reaches its sustain phase, it decays with the same slope indicated by ``sustain_level/release_time`` (or ``attack_level/release_time`` for plucked envelopes)
//|
//| :param float attack_time: The time in seconds it takes to ramp from 0 volume to attack_volume
//| :param float decay_time: The time in seconds it takes to ramp from attack_volume to sustain_volume
//| :param float release_time: The time in seconds it takes to ramp from sustain_volume to release_volume. When a note is released before it has reached the sustain phase, the release is done with the same slope indicated by ``release_time`` and ``sustain_level``. If the ``sustain_level`` is ``0.0`` then the release slope calculations use the ``attack_level`` instead.
//| :param float attack_level: The level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase
//| :param float sustain_level: The level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase relative to the attack level
//| """
//|
//| attack_time: float
//| """The time in seconds it takes to ramp from 0 volume to attack_volume"""
//|
//| decay_time: float
//| """The time in seconds it takes to ramp from attack_volume to sustain_volume"""
//|
//| release_time: float
//| """The time in seconds it takes to ramp from sustain_volume to release_volume. When a note is released before it has reached the sustain phase, the release is done with the same slope indicated by ``release_time`` and ``sustain_level``"""
//|
//| attack_level: float
//| """The level, in the range ``0.0`` to ``1.0`` of the peak volume of the attack phase"""
//|
//| sustain_level: float
//| """The level, in the range ``0.0`` to ``1.0`` of the volume of the sustain phase relative to the attack level"""
//|
//|
static mp_obj_t synthio_envelope_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
mp_arg_val_t args[MP_ARRAY_SIZE(envelope_properties)];
enum { ARG_attack_time, ARG_decay_time, ARG_release_time, ARG_attack_level, ARG_sustain_level };
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(envelope_properties), envelope_properties, args);
if (args[ARG_attack_time].u_obj == MP_OBJ_NULL) {
args[ARG_attack_time].u_obj = mp_obj_new_float(default_attack_time);
}
if (args[ARG_decay_time].u_obj == MP_OBJ_NULL) {
args[ARG_decay_time].u_obj = mp_obj_new_float(default_decay_time);
}
if (args[ARG_release_time].u_obj == MP_OBJ_NULL) {
args[ARG_release_time].u_obj = mp_obj_new_float(default_release_time);
}
if (args[ARG_attack_level].u_obj == MP_OBJ_NULL) {
args[ARG_attack_level].u_obj = mp_obj_new_float(default_attack_level);
}
if (args[ARG_sustain_level].u_obj == MP_OBJ_NULL) {
args[ARG_sustain_level].u_obj = mp_obj_new_float(default_sustain_level);
}
mp_arg_validate_obj_float_non_negative(args[ARG_attack_time].u_obj, 0., MP_QSTR_attack_time);
mp_arg_validate_obj_float_non_negative(args[ARG_decay_time].u_obj, 0., MP_QSTR_decay_time);
mp_arg_validate_obj_float_non_negative(args[ARG_release_time].u_obj, 0., MP_QSTR_release_time);
mp_arg_validate_obj_float_range(args[ARG_attack_level].u_obj, 0, 1, MP_QSTR_attack_level);
mp_arg_validate_obj_float_range(args[ARG_sustain_level].u_obj, 0, 1, MP_QSTR_sustain_level);
MP_STATIC_ASSERT(sizeof(mp_arg_val_t) == sizeof(mp_obj_t));
return namedtuple_make_new(type_in, MP_ARRAY_SIZE(args), 0, &args[0].u_obj);
};
const mp_obj_namedtuple_type_t synthio_envelope_type_obj = {
NAMEDTUPLE_TYPE_BASE_AND_SLOTS_MAKE_NEW(MP_QSTR_Envelope, synthio_envelope_make_new),
.n_fields = 5,
.fields = {
MP_QSTR_attack_time,
MP_QSTR_decay_time,
MP_QSTR_release_time,
MP_QSTR_attack_level,
MP_QSTR_sustain_level,
},
};
//| def from_file(
//| file: typing.BinaryIO,
//| *,
//| sample_rate: int = 11025,
//| waveform: Optional[ReadableBuffer] = None,
//| envelope: Optional[Envelope] = None,
//| ) -> 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
//| :param ReadableBuffer waveform: A single-cycle waveform. Default is a 50% duty cycle square wave. If specified, must be a ReadableBuffer of type 'h' (signed 16 bit)
//| :param Envelope envelope: An object that defines the loudness of a note over time. The default envelope provides no ramping, voices turn instantly on and off.
//|
//| 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, ARG_waveform, ARG_envelope };
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_QSTR_waveform, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
{ MP_QSTR_envelope, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
};
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_vfs_fat_fileio)) {
mp_raise_TypeError(MP_ERROR_TEXT("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_arg_error_invalid(MP_QSTR_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_arg_error_invalid(MP_QSTR_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);
if (f_read(&file->fp, buffer, track_size, &bytes_read) != FR_OK) {
mp_raise_OSError(MP_EIO);
}
if (bytes_read != track_size) {
mp_arg_error_invalid(MP_QSTR_file);
}
synthio_miditrack_obj_t *result = mp_obj_malloc(synthio_miditrack_obj_t, &synthio_miditrack_type);
common_hal_synthio_miditrack_construct(result, buffer, track_size,
tempo, args[ARG_sample_rate].u_int, args[ARG_waveform].u_obj,
mp_const_none,
args[ARG_envelope].u_obj
);
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
m_free(buffer, track_size);
#else
m_free(buffer);
#endif
return MP_OBJ_FROM_PTR(result);
}
MP_DEFINE_CONST_FUN_OBJ_KW(synthio_from_file_obj, 1, synthio_from_file);
//| def midi_to_hz(midi_note: float) -> float:
//| """Converts the given midi note (60 = middle C, 69 = concert A) to Hz"""
//|
//|
static mp_obj_t midi_to_hz(mp_obj_t arg) {
mp_float_t note = mp_arg_validate_type_float(arg, MP_QSTR_note);
return mp_obj_new_float(common_hal_synthio_midi_to_hz_float(note));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_midi_to_hz_obj, midi_to_hz);
//| def voct_to_hz(ctrl: float) -> float:
//| """Converts a 1v/octave signal to Hz.
//|
//| 24/12 (2.0) corresponds to middle C, 33/12 (2.75) is concert A."""
//|
//|
static mp_obj_t voct_to_hz(mp_obj_t arg) {
mp_float_t note = mp_arg_validate_obj_float_range(arg, -11, 11, MP_QSTR_ctrl);
return mp_obj_new_float(common_hal_synthio_voct_to_hz_float(note));
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_voct_to_hz_obj, voct_to_hz);
//|
//| waveform_max_length: int
//| """The maximum number of samples permitted in a waveform"""
//|
#if CIRCUITPY_AUDIOCORE_DEBUG
static mp_obj_t synthio_lfo_tick(size_t n, const mp_obj_t *args) {
shared_bindings_synthio_lfo_tick(48000, SYNTHIO_MAX_DUR);
mp_obj_t result[n];
for (size_t i = 0; i < n; i++) {
synthio_block_slot_t slot;
synthio_block_assign_slot(args[i], &slot, MP_QSTR_arg);
mp_float_t value = synthio_block_slot_get(&slot);
result[i] = mp_obj_new_float(value);
}
return mp_obj_new_tuple(n, result);
}
MP_DEFINE_CONST_FUN_OBJ_VAR(synthio_lfo_tick_obj, 1, synthio_lfo_tick);
#endif
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_Biquad), MP_ROM_PTR(&synthio_biquad_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_BlockBiquad), MP_ROM_PTR(&synthio_block_biquad_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_FilterMode), MP_ROM_PTR(&synthio_filter_mode_type) },
{ MP_ROM_QSTR(MP_QSTR_Math), MP_ROM_PTR(&synthio_math_type) },
{ MP_ROM_QSTR(MP_QSTR_MathOperation), MP_ROM_PTR(&synthio_math_operation_type) },
{ MP_ROM_QSTR(MP_QSTR_MidiTrack), MP_ROM_PTR(&synthio_miditrack_type) },
{ MP_ROM_QSTR(MP_QSTR_Note), MP_ROM_PTR(&synthio_note_type) },
{ MP_ROM_QSTR(MP_QSTR_EnvelopeState), MP_ROM_PTR(&synthio_note_state_type) },
{ MP_ROM_QSTR(MP_QSTR_LFO), MP_ROM_PTR(&synthio_lfo_type) },
{ MP_ROM_QSTR(MP_QSTR_Synthesizer), MP_ROM_PTR(&synthio_synthesizer_type) },
{ MP_ROM_QSTR(MP_QSTR_from_file), MP_ROM_PTR(&synthio_from_file_obj) },
{ MP_ROM_QSTR(MP_QSTR_Envelope), MP_ROM_PTR(&synthio_envelope_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_midi_to_hz), MP_ROM_PTR(&synthio_midi_to_hz_obj) },
{ MP_ROM_QSTR(MP_QSTR_voct_to_hz), MP_ROM_PTR(&synthio_voct_to_hz_obj) },
{ MP_ROM_QSTR(MP_QSTR_waveform_max_length), MP_ROM_INT(SYNTHIO_WAVEFORM_SIZE) },
#if CIRCUITPY_AUDIOCORE_DEBUG
{ MP_ROM_QSTR(MP_QSTR_lfo_tick), MP_ROM_PTR(&synthio_lfo_tick_obj) },
#endif
};
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);