14 # error "Wrong implementation included for this platform" 19 #define getADCReading() 0
20 #define channelNumToIndex(channel) channel 21 uint8_t adcPinToChannelNum(uint8_t pin) {
24 void adcStartConversion(uint8_t channel) {
25 #warning Fast analog read not implemented on this platform 27 void startSecondADCReadOnCurrentChannel() {
28 #warning Fast analog read not implemented on this platform 31 #warning Fast analog read not implemented on this platform 33 void setupMozziADC(int8_t speed) {
34 #warning Fast analog read not implemented on this platform 42 #include <driver/i2s.h> 43 #include <driver/timer.h> 45 #if (EXTERNAL_AUDIO_OUTPUT != true) 46 # include "AudioConfigESP32.h" 50 static bool _esp32_can_buffer_next =
true;
52 static uint16_t _esp32_prev_sample[2];
53 # define ESP_SAMPLE_SIZE (2
*sizeof(uint16_t)) 54 # elif (ESP32_AUDIO_OUT_MODE == PT8211_DAC) 55 static int16_t _esp32_prev_sample[2];
56 # define ESP_SAMPLE_SIZE (2
*sizeof(int16_t)) 57 # elif (ESP32_AUDIO_OUT_MODE == PDM_VIA_I2S) 58 static uint32_t _esp32_prev_sample[PDM_RESOLUTION];
59 # define ESP_SAMPLE_SIZE (PDM_RESOLUTION*sizeof(uint32_t)) 62 inline bool esp32_tryWriteSample() {
64 i2s_write(i2s_num, &_esp32_prev_sample, ESP_SAMPLE_SIZE, &bytes_written, 0);
65 return (bytes_written != 0);
68 inline bool canBufferAudioOutput() {
69 if (_esp32_can_buffer_next)
return true;
70 _esp32_can_buffer_next = esp32_tryWriteSample();
71 return _esp32_can_buffer_next;
74 inline void audioOutput(
const AudioOutput f) {
76 _esp32_prev_sample[0] = (f.l() +
AUDIO_BIAS) << 8;
77 # if (AUDIO_CHANNELS > 1
) 78 _esp32_prev_sample[1] = (f.r() + AUDIO_BIAS) << 8;
81 _esp32_prev_sample[1] = _esp32_prev_sample[0];
83 # elif (ESP32_AUDIO_OUT_MODE == PDM_VIA_I2S) 84 for (uint8_t i=0; i<PDM_RESOLUTION; ++i) {
85 _esp32_prev_sample[i] = pdmCode32(f.l() + AUDIO_BIAS);
89 _esp32_prev_sample[0] = f.l();
90 _esp32_prev_sample[1] = f.r();
92 _esp32_can_buffer_next = esp32_tryWriteSample();
97 void CACHED_FUNCTION_ATTR timer0_audio_output_isr(
void *) {
98 TIMERG0.int_clr_timers.t0 = 1;
99 TIMERG0.hw_timer[0].config.alarm_en = 1;
100 defaultAudioOutput();
104 static void startAudio() {
106 static intr_handle_t s_timer_handle;
108 timer_config_t config = {
109 .alarm_en = (timer_alarm_t)
true,
110 .counter_en = (timer_start_t)
false,
111 .intr_type = (timer_intr_mode_t) TIMER_INTR_LEVEL,
112 .counter_dir = TIMER_COUNT_UP,
113 .auto_reload = (timer_autoreload_t)
true,
117 timer_init(TIMER_GROUP_0, TIMER_0, &config);
118 timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
119 timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 80000000UL / AUDIO_RATE / div);
120 timer_enable_intr(TIMER_GROUP_0, TIMER_0);
121 timer_isr_register(TIMER_GROUP_0, TIMER_0, &timer0_audio_output_isr, nullptr, 0, &s_timer_handle);
122 timer_start(TIMER_GROUP_0, TIMER_0);
125 static const i2s_config_t i2s_config = {
127 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
129 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
132 .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
133 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
134 .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB),
135 .intr_alloc_flags = 0,
141 i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
143 static const i2s_pin_config_t pin_config = {
144 .bck_io_num = ESP32_I2S_BCK_PIN,
145 .ws_io_num = ESP32_I2S_WS_PIN,
146 .data_out_num = ESP32_I2S_DATA_PIN,
149 i2s_set_pin((i2s_port_t)i2s_num, &pin_config);
151 i2s_set_pin((i2s_port_t)i2s_num, NULL);
152 i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
154 i2s_zero_dma_buffer((i2s_port_t)i2s_num);
#define BYPASS_MOZZI_OUTPUT_BUFFER
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
void setupFastAnalogRead(int8_t speed)
NOTE: Code needed to set up faster than usual analog reads, e.g.
#define ESP32_AUDIO_OUT_MODE