16 #include "internal/mozzi_rand_p.h"
28 #if (!BYPASS_MOZZI_OUTPUT_BUFFER)
31 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
38 template<byte BITS_IN, byte BITS_OUT,
typename T>
constexpr T smartShift(T value) {
39 return (BITS_IN > BITS_OUT) ? value >> (BITS_IN - BITS_OUT) : (BITS_IN < BITS_OUT) ? value << (BITS_OUT - BITS_IN) : value;
45 # include "MozziGuts_impl_AVR.hpp"
47 # include "MozziGuts_impl_STM32.hpp"
49 # include "MozziGuts_impl_STM32duino.hpp"
51 # include "MozziGuts_impl_ESP32.hpp"
53 # include "MozziGuts_impl_ESP8266.hpp"
55 # include "MozziGuts_impl_TEENSY.hpp"
57 # include "MozziGuts_impl_SAMD.hpp"
59 # include "MozziGuts_impl_RP2040.hpp"
61 # include "MozziGuts_impl_MBED.hpp"
63 # include "MozziGuts_impl_RENESAS.hpp"
65 # error "Platform not (yet) supported. Check MozziGuts_impl_template.hpp and existing implementations for a blueprint for adding your favorite MCU."
71 #if !defined(MOZZI__LEGACY_AUDIO_INPUT_IMPL)
72 # if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE)
73 # define MOZZI__LEGACY_AUDIO_INPUT_IMPL 1
75 # define MOZZI__LEGACY_AUDIO_INPUT_IMPL 0
81 #if BYPASS_MOZZI_OUTPUT_BUFFER == true
90 # define canBufferAudioOutput() (!output_buffer.isFull())
91 # define bufferAudioOutput(f) output_buffer.write(f)
94 #if MOZZI_IS(MOZZI__LEGACY_AUDIO_INPUT_IMPL, 1
)
95 MOZZI_ASSERT_NOTEQUAL(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE);
97 startSecondADCReadOnCurrentChannel();
99 audioOutput(output_buffer.read());
111 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
131 #if MOZZI_IS(MOZZI__LEGACY_AUDIO_INPUT_IMPL, 1
)
146 #if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE)
151 #if MOZZI_IS(MOZZI__LEGACY_AUDIO_INPUT_IMPL, 1
)
154 #define audioInputAvailable() (!input_buffer.isEmpty())
155 #define readAudioInput() (input_buffer.read())
215 static uint16_t update_control_counter;
217 inline void advanceControlLoop() {
218 if (!update_control_counter) {
219 update_control_counter = update_control_timeout;
221 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
225 --update_control_counter;
232 if (canBufferAudioOutput()) {
233 advanceControlLoop();
234 bufferAudioOutput(updateAudio());
236 #if defined(LOOP_YIELD)
240 #if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE)
241 if (audioInputAvailable()) audio_input = readAudioInput();
245 #if defined(AUDIO_HOOK_HOOK)
256 unsigned long audioTicks() {
257 #if (BYPASS_MOZZI_OUTPUT_BUFFER != true)
258 return output_buffer.count();
259 #elif defined(AUDIOTICK_ADJUSTMENT)
260 return samples_written_to_buffer - (AUDIOTICK_ADJUSTMENT);
262 return samples_written_to_buffer;
266 unsigned long mozziMicros() {
return audioTicks() * MICROS_PER_AUDIO_TICK; }
271 void startMozzi(
int control_rate_hz) {
272 #if !MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
273 MozziPrivate::setupMozziADC(FAST_ADC);
277 update_control_timeout = MOZZI_AUDIO_RATE / control_rate_hz;
290 #undef BYPASS_MOZZI_OUTPUT_BUFFER
291 #undef AUDIO_HOOK_HOOK
292 #undef AUDIOTICK_ADJUSTMENT
293 #undef MOZZI__LEGACY_AUDIO_INPUT_IMPL
302 void startMozzi(
int control_rate_hz) {
MozziPrivate::startMozzi(control_rate_hz); };
303 void stopMozzi() { MozziPrivate::stopMozzi(); };
304 template<byte RES> uint16_t mozziAnalogRead(uint8_t pin) {
return MozziPrivate::smartShift<MOZZI__INTERNAL_ANALOG_READ_RESOLUTION, RES>(MozziPrivate::mozziAnalogRead(pin));};
305 #if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE)
306 template<byte RES> uint16_t getAudioInput() {
return MozziPrivate::smartShift<MOZZI__INTERNAL_ANALOG_READ_RESOLUTION, RES>(MozziPrivate::getAudioInput()); };
308 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
309 void setupMozziADC(int8_t speed) { MozziPrivate::setupMozziADC(speed); };
310 void setupFastAnalogRead(int8_t speed) { MozziPrivate::setupFastAnalogRead(speed); };
311 uint8_t adcPinToChannelNum(uint8_t pin) {
return MozziPrivate::adcPinToChannelNum(pin); };
316 #if !MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
317 MOZZI_DEPRECATED(
"n/a",
"Sketch has audioOutput() function, although external output is not configured.")
void audioOutput(
const AudioOutput) {};
319 #if !MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)