|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Jeff Epler for Adafruit Industries |
| 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 <string.h> |
| 28 | + |
| 29 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +#include "common-hal/audiobusio/__init__.h" |
| 32 | +#include "bindings/espidf/__init__.h" |
| 33 | +#include "freertos/FreeRTOS.h" |
| 34 | +#include "freertos/task.h" |
| 35 | + |
| 36 | +#include "shared-module/audiocore/__init__.h" |
| 37 | + |
| 38 | +#define I2S_QUEUE_SIZE (3) |
| 39 | + |
| 40 | +static i2s_t *i2s_instance[I2S_NUM_MAX]; |
| 41 | +static QueueHandle_t i2s_queues[I2S_NUM_MAX]; |
| 42 | +static TaskHandle_t i2s_tasks[I2S_NUM_MAX]; |
| 43 | + |
| 44 | +static int8_t port_i2s_allocate(void) { |
| 45 | +#if defined(I2S_NUM_1) |
| 46 | + if(!i2s_instance[1]) return 1; |
| 47 | +#endif |
| 48 | + if(!i2s_instance[0]) return 0; |
| 49 | + |
| 50 | + mp_raise_RuntimeError(translate("Peripheral in use")); |
| 51 | +} |
| 52 | + |
| 53 | +void port_i2s_reset_instance(int i) { |
| 54 | + assert(i >= 0 && i < I2S_NUM_MAX); |
| 55 | + if (i2s_tasks[i]) { |
| 56 | + vTaskDelete(i2s_tasks[i]); |
| 57 | + } |
| 58 | + i2s_tasks[i] = NULL; |
| 59 | + |
| 60 | + (void)i2s_driver_uninstall(i); |
| 61 | + i2s_instance[i] = NULL; |
| 62 | +} |
| 63 | + |
| 64 | +void i2s_reset(void) { |
| 65 | + for (int i=0; i < I2S_NUM_MAX; i++) { |
| 66 | + port_i2s_reset_instance(i); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +static void i2s_fill_buffer(i2s_t *self) { |
| 71 | + if (self->instance < 0 || self->instance >= I2S_NUM_MAX) { |
| 72 | + return; |
| 73 | + } |
| 74 | +#define STACK_BUFFER_SIZE (512) |
| 75 | + int16_t signed_samples[STACK_BUFFER_SIZE / sizeof(int16_t)]; |
| 76 | + |
| 77 | + if (!self->playing || self->paused || !self->sample || self->stopping) { |
| 78 | + memset(signed_samples, 0, sizeof(signed_samples)); |
| 79 | + |
| 80 | + size_t bytes_written = 0; |
| 81 | + do { |
| 82 | + CHECK_ESP_RESULT(i2s_write(self->instance, signed_samples, sizeof(signed_samples), &bytes_written, 0)); |
| 83 | + } while (bytes_written != 0); |
| 84 | + return; |
| 85 | + } |
| 86 | + while (!self->stopping) { |
| 87 | + if (self->sample_data == self->sample_end) { |
| 88 | + uint32_t sample_buffer_length; |
| 89 | + audioio_get_buffer_result_t get_buffer_result = |
| 90 | + audiosample_get_buffer(self->sample, false, 0, |
| 91 | + &self->sample_data, &sample_buffer_length); |
| 92 | + self->sample_end = self->sample_data + sample_buffer_length; |
| 93 | + if (get_buffer_result == GET_BUFFER_DONE) { |
| 94 | + if (self->loop) { |
| 95 | + audiosample_reset_buffer(self->sample, false, 0); |
| 96 | + } else { |
| 97 | + self->stopping = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + if (get_buffer_result == GET_BUFFER_ERROR || sample_buffer_length == 0) { |
| 102 | + self->stopping = true; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + size_t bytes_written = 0; |
| 107 | + size_t bytecount = self->sample_end - self->sample_data; |
| 108 | + if (self->samples_signed && self->channel_count == 2) { |
| 109 | + if (self->bytes_per_sample == 2) { |
| 110 | + CHECK_ESP_RESULT(i2s_write(self->instance, self->sample_data, bytecount, &bytes_written, 0)); |
| 111 | + } else { |
| 112 | + CHECK_ESP_RESULT(i2s_write_expand(self->instance, self->sample_data, bytecount, 8, 16, &bytes_written, 0)); |
| 113 | + } |
| 114 | + } else { |
| 115 | + const size_t bytes_per_output_frame = 4; |
| 116 | + size_t bytes_per_input_frame = self->channel_count * self->bytes_per_sample; |
| 117 | + size_t framecount = MIN(STACK_BUFFER_SIZE / bytes_per_output_frame, bytecount / bytes_per_input_frame); |
| 118 | + if (self->samples_signed) { |
| 119 | + assert(self->channel_count == 1); |
| 120 | + if (self->bytes_per_sample == 1) { |
| 121 | + audiosample_convert_s8m_s16s(signed_samples, (int8_t*)(void*)self->sample_data, framecount); |
| 122 | + } else { |
| 123 | + audiosample_convert_s16m_s16s(signed_samples, (int16_t*)(void*)self->sample_data, framecount); |
| 124 | + } |
| 125 | + } else { |
| 126 | + if (self->channel_count == 1) { |
| 127 | + if (self->bytes_per_sample == 1) { |
| 128 | + audiosample_convert_u8m_s16s(signed_samples, (uint8_t*)(void*)self->sample_data, framecount); |
| 129 | + } else { |
| 130 | + audiosample_convert_u16m_s16s(signed_samples, (uint16_t*)(void*)self->sample_data, framecount); |
| 131 | + } |
| 132 | + } else { |
| 133 | + if (self->bytes_per_sample == 1) { |
| 134 | + audiosample_convert_u8s_s16s(signed_samples, (uint8_t*)(void*)self->sample_data, framecount); |
| 135 | + } else { |
| 136 | + audiosample_convert_u16s_s16s(signed_samples, (uint16_t*)(void*)self->sample_data, framecount); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + size_t expanded_bytes_written = 0; |
| 141 | + CHECK_ESP_RESULT(i2s_write(self->instance, signed_samples, bytes_per_output_frame*framecount, &expanded_bytes_written, 0)); |
| 142 | + assert(expanded_bytes_written % 4 == 0); |
| 143 | + bytes_written = expanded_bytes_written / bytes_per_output_frame * bytes_per_input_frame; |
| 144 | + } |
| 145 | + self->sample_data += bytes_written; |
| 146 | + // We have filled the DMA buffer |
| 147 | + if (!bytes_written) { |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +static void i2s_callback_fun(void *self_in) { |
| 154 | + i2s_t *self = self_in; |
| 155 | + i2s_fill_buffer(self); |
| 156 | +} |
| 157 | + |
| 158 | +static void i2s_event_task(void *self_in) { |
| 159 | + i2s_t *self = self_in; |
| 160 | + while(true) { |
| 161 | + i2s_event_type_t event; |
| 162 | + BaseType_t result = xQueueReceive(i2s_queues[self->instance], &event, portMAX_DELAY); |
| 163 | + if (result && event == I2S_EVENT_TX_DONE) { |
| 164 | + background_callback_add(&self->callback, i2s_callback_fun, self_in); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +void port_i2s_allocate_init(i2s_t *self, bool left_justified) { |
| 170 | + self->instance = port_i2s_allocate(); |
| 171 | + |
| 172 | + i2s_config_t i2s_config = { |
| 173 | + .mode = I2S_MODE_MASTER | I2S_MODE_TX, |
| 174 | + .sample_rate = 44100, |
| 175 | + .bits_per_sample = 16, |
| 176 | + .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, |
| 177 | + .communication_format = left_justified ? I2S_COMM_FORMAT_STAND_I2S : I2S_COMM_FORMAT_STAND_I2S, |
| 178 | + .dma_buf_count = 2, |
| 179 | + .dma_buf_len = 128, // in _frames_, so 128 is 512 bytes per dma buf |
| 180 | + .use_apll = false, |
| 181 | + }; |
| 182 | + CHECK_ESP_RESULT(i2s_driver_install(self->instance, &i2s_config, I2S_QUEUE_SIZE, &i2s_queues[self->instance])); |
| 183 | + |
| 184 | + if (!xTaskCreate(i2s_event_task, "I2S_task", 3 * configMINIMAL_STACK_SIZE, self, CONFIG_PTHREAD_TASK_PRIO_DEFAULT, &i2s_tasks[self->instance])) { |
| 185 | + mp_raise_OSError_msg(translate("xTaskCreate failed")); |
| 186 | + } |
| 187 | + i2s_instance[self->instance] = self; |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | +void port_i2s_play(i2s_t *self, mp_obj_t sample, bool loop) { |
| 193 | + self->sample = sample; |
| 194 | + self->loop = loop; |
| 195 | + self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8; |
| 196 | + self->channel_count = audiosample_channel_count(sample); |
| 197 | + bool single_buffer; |
| 198 | + bool samples_signed; |
| 199 | + uint32_t max_buffer_length; |
| 200 | + uint8_t spacing; |
| 201 | + audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed, |
| 202 | + &max_buffer_length, &spacing); |
| 203 | + self->samples_signed = samples_signed; |
| 204 | + self->playing = true; |
| 205 | + self->paused = false; |
| 206 | + self->stopping = false; |
| 207 | + self->sample_data = self->sample_end = NULL; |
| 208 | + // We always output stereo so output twice as many bits. |
| 209 | + // uint16_t bits_per_sample_output = bits_per_sample * 2; |
| 210 | + |
| 211 | + audiosample_reset_buffer(self->sample, false, 0); |
| 212 | + |
| 213 | + CHECK_ESP_RESULT(i2s_set_sample_rates(self->instance, audiosample_sample_rate(sample))); |
| 214 | + |
| 215 | + background_callback_add(&self->callback, i2s_callback_fun, self); |
| 216 | +} |
| 217 | + |
| 218 | +bool port_i2s_playing(i2s_t *self) { |
| 219 | + return self->playing && !self->stopping; |
| 220 | +} |
| 221 | + |
| 222 | +bool port_i2s_paused(i2s_t *self) { |
| 223 | + return self->paused; |
| 224 | +} |
| 225 | + |
| 226 | +void port_i2s_stop(i2s_t *self) { |
| 227 | + self->sample = NULL; |
| 228 | + self->paused = false; |
| 229 | + self->playing = false; |
| 230 | + self->stopping = false; |
| 231 | +} |
| 232 | + |
| 233 | +void port_i2s_pause(i2s_t *self) { |
| 234 | + if (!self->paused) { |
| 235 | + self->paused = true; |
| 236 | + CHECK_ESP_RESULT(i2s_stop(self->instance)); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +void port_i2s_resume(i2s_t *self) { |
| 241 | + if (self->paused) { |
| 242 | + self->paused = false; |
| 243 | + CHECK_ESP_RESULT(i2s_start(self->instance)); |
| 244 | + } |
| 245 | +} |
0 commit comments