22 static void advanceADCStep();
23 static uint8_t adc_count = 0;
26 static void startSecondADCReadOnCurrentChannel();
29 #if BYPASS_MOZZI_OUTPUT_BUFFER == true 30 uint64_t samples_written_to_buffer = 0;
32 inline void bufferAudioOutput(
const AudioOutput_t f) {
34 ++samples_written_to_buffer;
37 # if (STEREO_HACK == true) 39 CircularBuffer<StereoOutput> output_buffer;
43 # define canBufferAudioOutput() (!output_buffer.isFull()) 44 # define bufferAudioOutput(f) output_buffer.write(f) 46 # if (USE_AUDIO_INPUT == true) 48 startSecondADCReadOnCurrentChannel();
50 audioOutput(output_buffer.read());
58 # include "MozziGuts_impl_AVR.hpp" 60 # include "MozziGuts_impl_STM32.hpp" 62 # include "MozziGuts_impl_ESP32.hpp" 64 # include "MozziGuts_impl_ESP8266.hpp" 66 # include "MozziGuts_impl_TEENSY.hpp" 68 # include "MozziGuts_impl_SAMD.hpp" 70 # include "MozziGuts_impl_RP2040.hpp" 72 # error "Platform not (yet) supported. Check MozziGuts_impl_template.hpp and existing implementations for a blueprint for adding your favorite MCU." 83 static volatile int analog_readings[NUM_ANALOG_INPUTS];
84 static Stack <
volatile int8_t,NUM_ANALOG_INPUTS> adc_channels_to_read;
85 volatile static int8_t current_channel = -1;
89 void adcReadSelectedChannels() {
91 if((current_channel = adc_channels_to_read.pop()) >= 0) adcStartConversion(current_channel);
97 __attribute__((noinline))
void adcStartReadCycle() {
98 if (current_channel < 0)
100 #if (USE_AUDIO_INPUT == true) 101 adc_channels_to_read.push(AUDIO_INPUT_PIN);
103 adcReadSelectedChannels();
110 #if defined(MOZZI_FAST_ANALOG_IMPLEMENTED) 111 pin = adcPinToChannelNum(pin);
112 adc_channels_to_read.push(pin);
113 return analog_readings[channelNumToIndex(pin)];
115 # warning Asynchronouos analog reads not implemented for this platform 116 return analogRead(pin);
120 #if (USE_AUDIO_INPUT == true) 122 CircularBuffer<
unsigned int> input_buffer;
124 static int audio_input;
126 int getAudioInput() {
return audio_input; }
129 inline void advanceADCStep() {
134 input_buffer.write(getADCReading());
135 adcReadSelectedChannels();
141 startSecondADCReadOnCurrentChannel();
146 analog_readings[channelNumToIndex(current_channel)] = getADCReading();
147 adcStartConversion(adcPinToChannelNum(AUDIO_INPUT_PIN));
159 inline void advanceADCStep() {
162 startSecondADCReadOnCurrentChannel();
166 analog_readings[channelNumToIndex(current_channel)] = getADCReading();
167 adcReadSelectedChannels();
177 static uint16_t update_control_timeout;
178 static uint16_t update_control_counter;
180 inline void advanceControlLoop() {
181 if (!update_control_counter) {
182 update_control_counter = update_control_timeout;
186 --update_control_counter;
193 #if (USE_AUDIO_INPUT == true) 194 if (!input_buffer.isEmpty())
195 audio_input = input_buffer.read();
198 if (canBufferAudioOutput()) {
199 advanceControlLoop();
200 #if (STEREO_HACK == true) 202 bufferAudioOutput(StereoOutput(audio_out_1, audio_out_2));
204 bufferAudioOutput(updateAudio());
207 #if defined(LOOP_YIELD) 220 #if (BYPASS_MOZZI_OUTPUT_BUFFER != true) 221 return output_buffer.count();
222 #elif defined(AUDIOTICK_ADJUSTMENT) 223 return samples_written_to_buffer - (AUDIOTICK_ADJUSTMENT);
225 return samples_written_to_buffer;
237 setupFastAnalogRead();
239 update_control_timeout =
AUDIO_RATE / control_rate_hz;
unsigned long mozziMicros()
An alternative for Arduino time functions like micros() and millis().
int mozziAnalogRead(uint8_t pin)
Reads the analog input of a chosen channel, without blocking other operations from running...
void updateControl()
This is where you put your control code.
#define AUDIO_RATE
Holds the audio rate setting.
void audioHook()
This is required in Arduino's loop().
unsigned long audioTicks()
An alternative for Arduino time functions like micros() and millis().
#define AudioOutput_t
Representation of an single audio output sample/frame.
#define MICROS_PER_AUDIO_TICK
#define CACHED_FUNCTION_ATTR
void startMozzi(int control_rate_hz=CONTROL_RATE)
Sets up the timers for audio and control rate processes, storing the timer registers so they can be r...