#include <Arduino.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s1; // xy=294,318
AudioSynthWaveform waveform; // xy=294,449
AudioMixer4 mixer; // xy=467,361
AudioOutputI2S i2s2; // xy=473,458
AudioRecordQueue queue; // xy=651,295
AudioConnection patchCord1(i2s1, 0, mixer, 0);
AudioConnection patchCord2(i2s1, 1, mixer, 1);
AudioConnection patchCord3(waveform, 0, i2s2, 0);
AudioConnection patchCord4(waveform, 0, i2s2, 1);
AudioConnection patchCord5(mixer, queue);
AudioControlSGTL5000 sgtl5000_1; // xy=467,541
// GUItool: end automatically generated code
const int myInput = AUDIO_INPUT_LINEIN;
int16_t data[AUDIO_BLOCK_SAMPLES * 128];
uint8_t buf_count = 0;
int16_t* dataPtr = data;
bool started = false;
uint8_t test_step = 0;
void setup()
{
Serial.begin(9600);
AudioMemory(25);
if (!sgtl5000_1.enable())
{
while(1)
{
Serial.println("Codec init fail!");
delay(1000);
}
}
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);
//sgtl5000_1.muteHeadphone();
sgtl5000_1.lineInLevel(15); // 0-15, default 5, set input sensitivity
sgtl5000_1.lineOutLevel(29); // 13-31, default 29, set output voltage
// configure the mixer to equally add left & right
mixer.gain(0, 1.0);
mixer.gain(1, 1.0);
waveform.begin(1, 10000, WAVEFORM_SINE); // Set amplitude and frequency for speaker
queue.begin();
}
void loop()
{
if (queue.available() > 0 )
{
if (buf_count < 128+5 && started)
{
if (buf_count > 5)
{
int16_t *buffer = queue.readBuffer();
memcpy(dataPtr, buffer, AUDIO_BLOCK_SAMPLES*sizeof(data[0]));
dataPtr += AUDIO_BLOCK_SAMPLES;
}
buf_count++;
}
queue.freeBuffer();
}
if (buf_count == 128)
{
for (uint8_t i = 0; i<128; i++)
{
for (uint8_t j=0; j<AUDIO_BLOCK_SAMPLES; j++)
{
Serial.print(data[i*128 + j]);
Serial.print(",");
}
Serial.println();
}
memset(data, 0, AUDIO_BLOCK_SAMPLES*sizeof(int16_t));
buf_count = 0;
dataPtr = data;
started = false;
}
if (Serial.available() && started == false)
{
uint8_t serIn = Serial.read();
if(serIn == 'R')
{
test_step = 0;
memset(data, 0, AUDIO_BLOCK_SAMPLES*sizeof(int16_t));
buf_count = 0;
dataPtr = data;
}
else if (serIn == 'S')
{
if(test_step > 3) test_step = 0;
switch(test_step)
{
case 0:
waveform.amplitude(1.0f);
// sgtl5000_1.unmuteHeadphone();
// sgtl5000_1.volume(1);
delay(50);
break;
case 1:
waveform.amplitude(0.8f);
// sgtl5000_1.unmuteHeadphone();
// sgtl5000_1.volume(0.8);
delay(50);
break;
case 2:
waveform.amplitude(0.4f);
// sgtl5000_1.muteHeadphone();
delay(50);
break;
case 3:
waveform.amplitude(0.0f);
// sgtl5000_1.unmuteHeadphone();
// sgtl5000_1.volume(0.5);
delay(50);
break;
default:
test_step = 0;
break;
}
test_step++;
started = true;
queue.clear();
}
}
}