Code:
IntervalTimer timer;
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputAnalog adc1; //xy=205.00000381469727,195.00000286102295
AudioMixer4 mixer1; //xy=366.25,208.25
AudioPlayQueue queuePlay; //xy=488.25,435.25
AudioRecordQueue queueRecord; //xy=509.25,209.25
AudioMixer4 mixer2; //xy=644.25,454.25
AudioOutputI2S i2s2Play; //xy=828.25,454.25
AudioConnection patchCord1(adc1, 0, mixer1, 0);
//AudioConnection patchCord2(adc1, 0, mixer1, 1);
AudioConnection patchCord3(mixer1, queueRecord);
AudioConnection patchCord4(mixer1, 0, mixer2, 1);
AudioConnection patchCord5(queuePlay, 0, mixer2, 0);
AudioConnection patchCord6(mixer2, 0, i2s2Play, 0);
AudioConnection patchCord7(mixer2, 0, i2s2Play, 1);
// GUItool: end automatically generated code
#define MAX_SAMPLES 128
#define MAX_QUEUE_SIZE 16
short DMAMEM bufferIn[MAX_SAMPLES * MAX_QUEUE_SIZE];
short DMAMEM bufferOut[MAX_SAMPLES * MAX_QUEUE_SIZE];
int32_t record_offset = 0;
int32_t play_offset = 0;
int16_t audioDataInput = 0, audioDataOutput = 0;
unsigned int pointer = 0, speed = 0;
unsigned int time = 0;
void setup() {
Serial.begin(115200);
AudioMemory(30);
timer.begin(effect, 22.676);
timer.priority(128);
queuePlay.setBehaviour(AudioPlayQueue::NON_STALLING);
queuePlay.setMaxBuffers(16);
mixer1.gain(0, 1.0);
mixer1.gain(1, 0);
mixer1.gain(2, 0);
mixer1.gain(3, 0);
mixer2.gain(0, 1.0); // Fx signal
mixer2.gain(1, 0); // Dry signal
mixer2.gain(2, 0);
mixer2.gain(3, 0);
queueRecord.begin();
}
void ADC_to_buffer() {
memcpy(bufferIn + record_offset, queueRecord.readBuffer(), MAX_SAMPLES * 2);
queueRecord.freeBuffer();
record_offset += MAX_SAMPLES;
if (record_offset >= (MAX_SAMPLES * MAX_QUEUE_SIZE)) record_offset = 0;
}
void buffer_to_i2s() {
memcpy(queuePlay.getBuffer(), bufferOut + play_offset , MAX_SAMPLES * 2);
queuePlay.playBuffer();
play_offset += MAX_SAMPLES;
if (play_offset >= (MAX_SAMPLES * MAX_QUEUE_SIZE)) play_offset = 0;
}
void loop() {}
void effect() {
if (queueRecord.available() >= 2) ADC_to_buffer(), buffer_to_i2s();
clean();
//ring_mod();
}
void clean() {
pointer++;
audioDataInput = bufferIn[pointer];
bufferOut[pointer] = audioDataInput;
if (pointer >= (MAX_SAMPLES * MAX_QUEUE_SIZE)) pointer = 0;
}
void ring_mod() {
pointer++;
audioDataInput = bufferIn[pointer];
bufferOut[pointer] = audioDataOutput;
if (pointer >= (MAX_SAMPLES * MAX_QUEUE_SIZE)) pointer = 0;
time = 15;//map(analogRead(A15), 0, 1023, 0, 60);
speed++;
if (speed >= time) {
speed = 0;
audioDataOutput = audioDataInput;
}
}