Hi, everybody
I'm starting to use a Teensy 4.0 with Audio Shield (SGTL5000) and Microphone and i'm interested in understanding how the library works and in particular the queues.
I'm trying to use the incoming and outgoing queue object to try to create a delay effect.
So my purpose is to reproduce correctly the input stream after a defined number of milliseconds.
I already know that an object that already allows this is implemented but I would like to try to get a "similar" effect by manipulating the buffers at a lower level for undestanding the functions of the Audio Library Objects.
This is the code i'm working on.
What it should do is delay playback by 500 ms but it doesn't seem to work.
In the headphones the sound is reproduced without a perceptible delay.
What am I doing wrong?
Are there any examples I can take my cue from?
Thanks
I'm starting to use a Teensy 4.0 with Audio Shield (SGTL5000) and Microphone and i'm interested in understanding how the library works and in particular the queues.
I'm trying to use the incoming and outgoing queue object to try to create a delay effect.
So my purpose is to reproduce correctly the input stream after a defined number of milliseconds.
I already know that an object that already allows this is implemented but I would like to try to get a "similar" effect by manipulating the buffers at a lower level for undestanding the functions of the Audio Library Objects.
This is the code i'm working on.
What it should do is delay playback by 500 ms but it doesn't seem to work.
In the headphones the sound is reproduced without a perceptible delay.
What am I doing wrong?
Are there any examples I can take my cue from?
Thanks
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
AudioRecordQueue queueRecord; //xy=208,57
AudioPlayQueue queuePlay; //xy=327,55
AudioInputI2S i2s1Record; //xy=149,228
AudioAnalyzePeak peak1; //xy=407,266
//AudioAnalyzePeak peak2; //xy=407,266
AudioOutputI2S i2s2Play; //xy=447,200
AudioConnection patchCord1(i2s1Record, 0, queueRecord, 0);
AudioConnection patchCord2(queuePlay, 0, i2s2Play, 1);
AudioConnection patchCord3(i2s1Record, 0, peak1, 0);
//AudioConnection patchCord4(i2s1, 0, peak2, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=339,338
#define MAX_SAMPLES 128
#define MAX_QUEUE_SIZE 1000
uint16_t buffer[MAX_SAMPLES * MAX_QUEUE_SIZE];
uint8_t record_index = 0;
uint8_t play_index = 0;
uint8_t record_offset = 0;
uint8_t play_offset = 0;
unsigned long millis_buffer[MAX_QUEUE_SIZE];
elapsedMillis mymillis;
// INPUT OUTPUT
// i2s1 L >> i2s2 L
// i2s1 R >> i2s2 R
void setup()
{
Serial.begin(115200);
AudioMemory(100);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.micGain(36);
//sgtl5000_1.lineInLevel(0);
sgtl5000_1.lineOutLevel(13);
queueRecord.begin();
mymillis = 0;
}
void printPeak()
{
Serial.println(peak1.read());
delay(5);
}
void loop()
{
//printPeak();
//delay(1000);
if(queueRecord.available() >= 2)
{ //input buffer
memcpy(buffer + record_offset, queueRecord.readBuffer(), MAX_SAMPLES);
queueRecord.freeBuffer();
millis_buffer[record_index] = mymillis;
record_offset+=128;
record_index+=1;
if (record_index >= MAX_QUEUE_SIZE)
{
record_offset=0;
record_index=0;
}
}
if (mymillis - millis_buffer[play_index] > 500)
{
memcpy(queuePlay.getBuffer(), buffer + play_offset, MAX_SAMPLES);
queuePlay.playBuffer();
millis_buffer[play_index] = 0;
play_offset+=128;
play_index+=1;
if (play_index >= MAX_QUEUE_SIZE)
{
play_index=0;
play_offset=0;
}
}
}