2021-03-19 18:22:00 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2023-04-01 11:21:22 -04:00
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
STATIC NORETURN void raise_midi_stream_error(uint32_t pos) {
|
|
|
|
mp_raise_ValueError_varg(translate("Error in MIDI stream at position %d"), pos);
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:22:00 -04:00
|
|
|
STATIC uint8_t parse_note(const uint8_t *buffer, uint32_t len, uint32_t *pos) {
|
|
|
|
if (*pos + 1 >= len) {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_midi_stream_error(*pos);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
uint8_t note = buffer[(*pos)++];
|
|
|
|
if (note > 127 || buffer[(*pos)++] > 127) {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_midi_stream_error(*pos);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2023-03-31 11:51:03 -04:00
|
|
|
STATIC void terminate_span(synthio_miditrack_obj_t *self, uint16_t *dur) {
|
2021-03-19 18:22:00 -04:00
|
|
|
if (*dur) {
|
|
|
|
self->track[self->total_spans - 1].dur = *dur;
|
|
|
|
*dur = 0;
|
|
|
|
} else {
|
|
|
|
self->total_spans--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 11:51:03 -04:00
|
|
|
STATIC void add_span(synthio_miditrack_obj_t *self, const synthio_midi_span_t *span) {
|
2023-04-01 11:19:44 -04:00
|
|
|
self->track = m_renew(synthio_midi_span_t, self->track, self->total_spans, self->total_spans + 1);
|
2023-03-31 11:51:03 -04:00
|
|
|
self->track[self->total_spans++] = *span;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void change_span_note(synthio_miditrack_obj_t *self, uint8_t old_note, uint8_t new_note, uint16_t *dur) {
|
|
|
|
synthio_midi_span_t span = self->track[self->total_spans - 1];
|
2023-04-02 21:37:23 -04:00
|
|
|
if (synthio_span_change_note(&span, old_note, new_note)) {
|
2023-03-31 11:51:03 -04:00
|
|
|
terminate_span(self, dur);
|
|
|
|
add_span(self, &span);
|
2023-03-31 17:41:05 -04:00
|
|
|
*dur = 0;
|
2023-03-31 11:51:03 -04:00
|
|
|
}
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_synthio_miditrack_construct(synthio_miditrack_obj_t *self,
|
2023-04-01 11:21:22 -04:00
|
|
|
const uint8_t *buffer, uint32_t len, uint32_t tempo, uint32_t sample_rate,
|
|
|
|
const int16_t *waveform, uint16_t waveform_length) {
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-04-02 19:54:07 -04:00
|
|
|
self->synth.sample_rate = sample_rate;
|
2021-03-19 18:22:00 -04:00
|
|
|
self->track = m_malloc(sizeof(synthio_midi_span_t), false);
|
2023-04-02 21:37:23 -04:00
|
|
|
synthio_span_init(self->track);
|
2021-03-19 18:22:00 -04:00
|
|
|
self->next_span = 0;
|
|
|
|
self->total_spans = 1;
|
2023-04-02 19:54:07 -04:00
|
|
|
self->synth.waveform = waveform;
|
|
|
|
self->synth.waveform_length = waveform_length;
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-03-31 11:51:03 -04:00
|
|
|
uint16_t dur = 0;
|
2021-03-19 18:22:00 -04:00
|
|
|
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) {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_midi_stream_error(pos);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:51:03 -04:00
|
|
|
// dur is carried over here so that if a note on/off message doesn't actually produce a change, the
|
|
|
|
// underlying "span" is extended. Otherwise, it is zeroed out in the call to `terminate_span`.
|
2021-03-19 18:22:00 -04:00
|
|
|
dur += delta * sample_rate / tempo;
|
|
|
|
|
|
|
|
switch (buffer[pos++] >> 4) {
|
|
|
|
case 8: { // Note Off
|
|
|
|
uint8_t note = parse_note(buffer, len, &pos);
|
2023-04-02 19:54:07 -04:00
|
|
|
change_span_note(self, note, SYNTHIO_SILENCE, &dur);
|
2021-03-19 18:22:00 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 9: { // Note On
|
|
|
|
uint8_t note = parse_note(buffer, len, &pos);
|
2023-04-02 19:54:07 -04:00
|
|
|
change_span_note(self, SYNTHIO_SILENCE, note, &dur);
|
2021-03-19 18:22:00 -04:00
|
|
|
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) {
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_midi_stream_error(pos);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 15: // the full syntax is too complicated, just assume it's "End of Track" event
|
|
|
|
pos = len;
|
|
|
|
break;
|
|
|
|
default: // invalid event
|
2022-05-13 15:33:43 -04:00
|
|
|
raise_midi_stream_error(pos);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 11:51:03 -04:00
|
|
|
terminate_span(self, &dur);
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-03-31 11:51:03 -04:00
|
|
|
uint16_t max_dur = 0;
|
|
|
|
for (int i = 0; i < self->total_spans; i++) {
|
|
|
|
max_dur = MAX(self->track[i].dur, max_dur);
|
|
|
|
}
|
2023-04-02 19:54:07 -04:00
|
|
|
synthio_synth_init(&self->synth, max_dur);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_synthio_miditrack_deinit(synthio_miditrack_obj_t *self) {
|
2023-04-02 19:54:07 -04:00
|
|
|
synthio_synth_deinit(&self->synth);
|
2023-04-01 11:19:44 -04:00
|
|
|
m_del(synthio_midi_span_t, self->track, self->total_spans + 1);
|
2021-03-19 18:22:00 -04:00
|
|
|
self->track = NULL;
|
|
|
|
}
|
|
|
|
bool common_hal_synthio_miditrack_deinited(synthio_miditrack_obj_t *self) {
|
2023-04-02 19:54:07 -04:00
|
|
|
return synthio_synth_deinited(&self->synth);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t common_hal_synthio_miditrack_get_sample_rate(synthio_miditrack_obj_t *self) {
|
2023-04-02 19:54:07 -04:00
|
|
|
return self->synth.sample_rate;
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
uint8_t common_hal_synthio_miditrack_get_bits_per_sample(synthio_miditrack_obj_t *self) {
|
2023-04-02 19:54:07 -04:00
|
|
|
return SYNTHIO_BITS_PER_SAMPLE;
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
|
|
|
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,
|
2021-06-30 13:11:54 -04:00
|
|
|
bool single_channel_output, uint8_t channel) {
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-04-02 19:54:07 -04:00
|
|
|
self->synth.span.dur = 0;
|
2021-03-19 18:22:00 -04:00
|
|
|
self->next_span = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
audioio_get_buffer_result_t synthio_miditrack_get_buffer(synthio_miditrack_obj_t *self,
|
2021-06-30 13:11:54 -04:00
|
|
|
bool single_channel_output, uint8_t channel, uint8_t **buffer, uint32_t *buffer_length) {
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-04-02 19:54:07 -04:00
|
|
|
if (self->synth.span.dur == 0) {
|
2023-03-31 17:41:05 -04:00
|
|
|
if (self->next_span >= self->total_spans) {
|
|
|
|
*buffer_length = 0;
|
|
|
|
return GET_BUFFER_DONE;
|
|
|
|
}
|
2023-04-02 19:54:07 -04:00
|
|
|
self->synth.span = self->track[self->next_span++];
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|
2023-03-31 11:51:03 -04:00
|
|
|
|
2023-04-02 19:54:07 -04:00
|
|
|
synthio_synth_synthesize(&self->synth, buffer, buffer_length);
|
2021-03-19 18:22:00 -04:00
|
|
|
|
2023-04-02 19:54:07 -04:00
|
|
|
return (self->synth.span.dur == 0 && self->next_span >= self->total_spans) ?
|
2021-03-19 18:22:00 -04:00
|
|
|
GET_BUFFER_DONE : GET_BUFFER_MORE_DATA;
|
|
|
|
}
|
|
|
|
|
2021-06-30 13:11:54 -04:00
|
|
|
void synthio_miditrack_get_buffer_structure(synthio_miditrack_obj_t *self, bool single_channel_output,
|
2021-03-19 18:22:00 -04:00
|
|
|
bool *single_buffer, bool *samples_signed, uint32_t *max_buffer_length, uint8_t *spacing) {
|
2023-04-02 19:54:07 -04:00
|
|
|
return synthio_synth_get_buffer_structure(&self->synth, single_channel_output, single_buffer, samples_signed, max_buffer_length, spacing);
|
2021-03-19 18:22:00 -04:00
|
|
|
}
|