|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 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 <stdint.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "extmod/vfs_fat.h" |
| 31 | +#include "py/gc.h" |
| 32 | +#include "py/mperrno.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "common-hal/audiopwmio/AudioOut.h" |
| 35 | +#include "common-hal/pulseio/PWMOut.h" |
| 36 | +#include "shared-bindings/audiopwmio/AudioOut.h" |
| 37 | +#include "shared-bindings/microcontroller/__init__.h" |
| 38 | +#include "shared-bindings/microcontroller/Pin.h" |
| 39 | +#include "supervisor/shared/translate.h" |
| 40 | + |
| 41 | +// TODO: This should be the same size as PWMOut.c:pwms[], but there's no trivial way to accomplish that |
| 42 | +STATIC audiopwmio_audioout_obj_t* active_audio[4]; |
| 43 | + |
| 44 | +#define F_TARGET (62500) |
| 45 | +#define F_PWM (16000000) |
| 46 | +// return the REFRESH value, store the TOP value in an out-parameter |
| 47 | +// Tested for key values (worst relative error = 0.224% = 3.84 cents) |
| 48 | +// 8000: top = 250 refresh = 7 [ 8000.0] |
| 49 | +// 22050: top = 242 refresh = 2 [22038.5] |
| 50 | +// 24000: top = 222 refresh = 2 [24024.0] |
| 51 | +// 44100: top = 181 refresh = 1 [44198.8] |
| 52 | +// 48000: top = 167 refresh = 1 [47904.1] |
| 53 | +STATIC uint32_t calculate_pwm_parameters(uint32_t sample_rate, uint32_t *top_out) { |
| 54 | + // the desired frequency is the closest integer multiple of sample_rate not less than F_TARGET |
| 55 | + uint32_t desired_frequency = (F_TARGET + sample_rate - 1) / sample_rate * sample_rate; |
| 56 | + // The top value is the PWM frequency divided by the desired frequency (round to nearest) |
| 57 | + uint32_t top = (F_PWM + desired_frequency/2) / desired_frequency; |
| 58 | + // The actual frequency is the PWM frequency divided by the top value (round to nearest) |
| 59 | + uint32_t actual_frequency = (F_PWM + top/2) / top; |
| 60 | + // The multiplier is the actual frequency divided by the sample rate (round to nearest) |
| 61 | + uint32_t multiplier = (actual_frequency + sample_rate/2) / sample_rate; |
| 62 | + *top_out = top; |
| 63 | + return multiplier - 1; |
| 64 | +} |
| 65 | + |
| 66 | +STATIC void activate_audiopwmout_obj(audiopwmio_audioout_obj_t *self) { |
| 67 | + for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { |
| 68 | + if(!active_audio[i]) { |
| 69 | + active_audio[i] = self; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +STATIC void deactivate_audiopwmout_obj(audiopwmio_audioout_obj_t *self) { |
| 75 | + for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { |
| 76 | + if(active_audio[i] == self) |
| 77 | + active_audio[i] = NULL; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void audiopwmout_reset() { |
| 82 | + for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) |
| 83 | + active_audio[i] = NULL; |
| 84 | +} |
| 85 | + |
| 86 | +STATIC void fill_buffers(audiopwmio_audioout_obj_t *self, int buf) { |
| 87 | + self->pwm->EVENTS_SEQSTARTED[1-buf] = 0; |
| 88 | + uint16_t *dev_buffer = self->buffers[buf]; |
| 89 | + uint8_t *buffer; |
| 90 | + uint32_t buffer_length; |
| 91 | + audioio_get_buffer_result_t get_buffer_result = |
| 92 | + audiosample_get_buffer(self->sample, false, 0, |
| 93 | + &buffer, &buffer_length); |
| 94 | + if (get_buffer_result == GET_BUFFER_ERROR) { |
| 95 | + common_hal_audiopwmio_audioout_stop(self); |
| 96 | + return; |
| 97 | + } |
| 98 | + uint32_t num_samples = buffer_length / self->bytes_per_sample / self->spacing; |
| 99 | + |
| 100 | + if(self->bytes_per_sample == 1) { |
| 101 | + uint8_t offset = self->signed_to_unsigned ? 0x80 : 0; |
| 102 | + uint16_t scale = self->scale; |
| 103 | + for(uint32_t i=0; i<buffer_length/self->spacing; i++) { |
| 104 | + uint8_t rawval = (*buffer++ + offset); |
| 105 | + uint16_t val = (uint16_t)(((uint32_t)rawval * (uint32_t)scale) >> 8); |
| 106 | + *dev_buffer++ = val; |
| 107 | + if(self->spacing == 1) |
| 108 | + *dev_buffer++ = val; |
| 109 | + } |
| 110 | + } else { |
| 111 | + uint16_t offset = self->signed_to_unsigned ? 0x8000 : 0; |
| 112 | + uint16_t scale = self->scale; |
| 113 | + uint16_t *buffer16 = (uint16_t*)buffer; |
| 114 | + for(uint32_t i=0; i<buffer_length/2/self->spacing; i++) { |
| 115 | + uint16_t rawval = (*buffer16++ + offset); |
| 116 | + uint16_t val = (uint16_t)((rawval * (uint32_t)scale) >> 16); |
| 117 | + *dev_buffer++ = val; |
| 118 | + if(self->spacing == 1) |
| 119 | + *dev_buffer++ = val; |
| 120 | + } |
| 121 | + } |
| 122 | + self->pwm->SEQ[buf].PTR = (intptr_t)self->buffers[buf]; |
| 123 | + self->pwm->SEQ[buf].CNT = num_samples*2; |
| 124 | + |
| 125 | + if (self->loop && get_buffer_result == GET_BUFFER_DONE) { |
| 126 | + audiosample_reset_buffer(self->sample, false, 0); |
| 127 | + } else if(get_buffer_result == GET_BUFFER_DONE) { |
| 128 | + self->pwm->LOOP = 0; |
| 129 | + self->stopping = true; |
| 130 | + } else { |
| 131 | + self->pwm->LOOP = 0xffff; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +STATIC void audiopwmout_background_obj(audiopwmio_audioout_obj_t *self) { |
| 136 | + if(!common_hal_audiopwmio_audioout_get_playing(self)) |
| 137 | + return; |
| 138 | + if(self->loop && self->single_buffer) { |
| 139 | + self->pwm->LOOP = 0xffff; |
| 140 | + } else if(self->stopping) { |
| 141 | + bool stopped = |
| 142 | + (self->pwm->EVENTS_SEQEND[0] || !self->pwm->EVENTS_SEQSTARTED[0]) && |
| 143 | + (self->pwm->EVENTS_SEQEND[1] || !self->pwm->EVENTS_SEQSTARTED[1]); |
| 144 | + if(stopped) |
| 145 | + self->pwm->TASKS_STOP = 1; |
| 146 | + } else if(!self->single_buffer) { |
| 147 | + if(self->pwm->EVENTS_SEQSTARTED[0]) fill_buffers(self, 1); |
| 148 | + if(self->pwm->EVENTS_SEQSTARTED[1]) fill_buffers(self, 0); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void audiopwmout_background() { |
| 153 | + for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { |
| 154 | + if(!active_audio[i]) continue; |
| 155 | + audiopwmout_background_obj(active_audio[i]); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void common_hal_audiopwmio_audioout_construct(audiopwmio_audioout_obj_t* self, |
| 160 | + const mcu_pin_obj_t* left_channel, const mcu_pin_obj_t* right_channel, uint16_t quiescent_value) { |
| 161 | + assert_pin_free(left_channel); |
| 162 | + assert_pin_free(right_channel); |
| 163 | + self->pwm = pwmout_allocate(256, PWM_PRESCALER_PRESCALER_DIV_1, true, NULL, NULL); |
| 164 | + if(!self->pwm) { |
| 165 | + mp_raise_RuntimeError(translate("All timers in use")); |
| 166 | + } |
| 167 | + |
| 168 | + self->pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1; |
| 169 | + // two uint16_t values per sample when Grouped |
| 170 | + // n.b. SEQ[#].CNT "counts" are 2 per sample (left and right channels) |
| 171 | + self->pwm->DECODER = PWM_DECODER_LOAD_Grouped; |
| 172 | + |
| 173 | + // we use channels 0 and 2 because these are GROUPED; it lets us save half |
| 174 | + // the space for sample data (no additional optimization is possible for |
| 175 | + // single channel) |
| 176 | + self->pwm->PSEL.OUT[0] = self->left_channel_number = left_channel->number; |
| 177 | + claim_pin(left_channel); |
| 178 | + |
| 179 | + if(right_channel) |
| 180 | + { |
| 181 | + self->pwm->PSEL.OUT[2] = self->right_channel_number = right_channel->number; |
| 182 | + claim_pin(right_channel); |
| 183 | + } |
| 184 | + |
| 185 | + self->quiescent_value = quiescent_value >> 8; |
| 186 | + |
| 187 | + self->pwm->ENABLE = 1; |
| 188 | + // TODO: Ramp from 0 to quiescent value |
| 189 | +} |
| 190 | + |
| 191 | +bool common_hal_audiopwmio_audioout_deinited(audiopwmio_audioout_obj_t* self) { |
| 192 | + return !self->pwm; |
| 193 | +} |
| 194 | + |
| 195 | +void common_hal_audiopwmio_audioout_deinit(audiopwmio_audioout_obj_t* self) { |
| 196 | + if (common_hal_audiopwmio_audioout_deinited(self)) { |
| 197 | + return; |
| 198 | + } |
| 199 | + // TODO: ramp the pwm down from quiescent value to 0 |
| 200 | + self->pwm->ENABLE = 0; |
| 201 | + |
| 202 | + if(self->left_channel_number) |
| 203 | + reset_pin_number(self->left_channel_number); |
| 204 | + if(self->right_channel_number) |
| 205 | + reset_pin_number(self->right_channel_number); |
| 206 | + |
| 207 | + pwmout_free_channel(self->pwm, 0); |
| 208 | + pwmout_free_channel(self->pwm, 2); |
| 209 | + |
| 210 | + self->pwm = NULL; |
| 211 | + |
| 212 | + m_free(self->buffers[0]); |
| 213 | + self->buffers[0] = NULL; |
| 214 | + |
| 215 | + m_free(self->buffers[1]); |
| 216 | + self->buffers[1] = NULL; |
| 217 | +} |
| 218 | + |
| 219 | +void common_hal_audiopwmio_audioout_play(audiopwmio_audioout_obj_t* self, mp_obj_t sample, bool loop) { |
| 220 | + if (common_hal_audiopwmio_audioout_get_playing(self)) { |
| 221 | + common_hal_audiopwmio_audioout_stop(self); |
| 222 | + } |
| 223 | + self->sample = sample; |
| 224 | + self->loop = loop; |
| 225 | + |
| 226 | + uint32_t sample_rate = audiosample_sample_rate(sample); |
| 227 | + uint32_t max_sample_rate = 62500; |
| 228 | + if (sample_rate > max_sample_rate) { |
| 229 | + mp_raise_ValueError_varg(translate("Sample rate too high. It must be less than %d"), max_sample_rate); |
| 230 | + } |
| 231 | + self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8; |
| 232 | + |
| 233 | + uint32_t max_buffer_length; |
| 234 | + audiosample_get_buffer_structure(sample, /* single channel */ false, |
| 235 | + &self->single_buffer, &self->signed_to_unsigned, &max_buffer_length, |
| 236 | + &self->spacing); |
| 237 | + if(max_buffer_length > UINT16_MAX) { |
| 238 | + mp_raise_ValueError_varg(translate("Buffer length %d too big. It must be less than %d"), max_buffer_length, UINT16_MAX); |
| 239 | + } |
| 240 | + self->buffer_length = (uint16_t)max_buffer_length; |
| 241 | + self->buffers[0] = m_malloc(self->buffer_length * 2 * sizeof(uint16_t), false); |
| 242 | + if(!self->single_buffer) |
| 243 | + self->buffers[1] = m_malloc(self->buffer_length * 2 * sizeof(uint16_t), false); |
| 244 | + |
| 245 | + |
| 246 | + uint32_t top; |
| 247 | + self->pwm->SEQ[0].REFRESH = self->pwm->SEQ[1].REFRESH = calculate_pwm_parameters(sample_rate, &top); |
| 248 | + self->scale = top-1; |
| 249 | + self->pwm->COUNTERTOP = top; |
| 250 | + |
| 251 | + if(!self->single_buffer) |
| 252 | + self->pwm->LOOP = 1; |
| 253 | + else if(self->loop) |
| 254 | + self->pwm->LOOP = 0xffff; |
| 255 | + else |
| 256 | + self->pwm->LOOP = 0; |
| 257 | + audiosample_reset_buffer(self->sample, false, 0); |
| 258 | + activate_audiopwmout_obj(self); |
| 259 | + fill_buffers(self, 0); |
| 260 | + self->pwm->SEQ[1].PTR = self->pwm->SEQ[0].PTR; |
| 261 | + self->pwm->SEQ[1].CNT = self->pwm->SEQ[0].CNT; |
| 262 | + self->pwm->EVENTS_SEQSTARTED[0] = 0; |
| 263 | + self->pwm->EVENTS_SEQSTARTED[1] = 0; |
| 264 | + self->pwm->TASKS_SEQSTART[0] = 1; |
| 265 | + self->pwm->EVENTS_STOPPED = 0; |
| 266 | + self->playing = true; |
| 267 | + self->stopping = false; |
| 268 | + self->paused = false; |
| 269 | +} |
| 270 | + |
| 271 | +void common_hal_audiopwmio_audioout_stop(audiopwmio_audioout_obj_t* self) { |
| 272 | + deactivate_audiopwmout_obj(self); |
| 273 | + self->pwm->TASKS_STOP = 1; |
| 274 | + self->stopping = false; |
| 275 | + self->paused = false; |
| 276 | + |
| 277 | + m_free(self->buffers[0]); |
| 278 | + self->buffers[0] = NULL; |
| 279 | + |
| 280 | + m_free(self->buffers[1]); |
| 281 | + self->buffers[1] = NULL; |
| 282 | +} |
| 283 | + |
| 284 | +bool common_hal_audiopwmio_audioout_get_playing(audiopwmio_audioout_obj_t* self) { |
| 285 | + if(self->pwm->EVENTS_STOPPED) { |
| 286 | + self->playing = false; |
| 287 | + self->pwm->EVENTS_STOPPED = 0; |
| 288 | + } |
| 289 | + return self->playing; |
| 290 | +} |
| 291 | + |
| 292 | +/* pause/resume present difficulties for the NRF PWM audio module. |
| 293 | + * |
| 294 | + * A PWM sequence can be stopped in its tracks by sending a TASKS_STOP event, |
| 295 | + * but there's no way to pick up the sequence where it was stopped; you could |
| 296 | + * start at the start of one of the two sequences, but especially for "single buffer" |
| 297 | + * sample, this seems undesirable. |
| 298 | + * |
| 299 | + * Or, you can stop at the end of a sequence so that you don't duplicate anything |
| 300 | + * when restarting, but again this is unsatisfactory for a "single buffer" sample. |
| 301 | + * |
| 302 | + * For now, I've taken the coward's way and left these methods unimplemented. |
| 303 | + * Perhaps the way forward is to divide even "single buffer" samples into tasks of |
| 304 | + * only a few ms long, so that they can be stopped/restarted quickly enough that it |
| 305 | + * feels instant. (This also saves on memory, for long in-memory "single buffer" |
| 306 | + * samples!) |
| 307 | + */ |
| 308 | +void common_hal_audiopwmio_audioout_pause(audiopwmio_audioout_obj_t* self) { |
| 309 | + self->paused = true; |
| 310 | +} |
| 311 | + |
| 312 | +void common_hal_audiopwmio_audioout_resume(audiopwmio_audioout_obj_t* self) { |
| 313 | + self->paused = false; |
| 314 | +} |
| 315 | + |
| 316 | +bool common_hal_audiopwmio_audioout_get_paused(audiopwmio_audioout_obj_t* self) { |
| 317 | + return self->paused; |
| 318 | +} |
0 commit comments