Code:
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include "Arduino.h"
#include "arm_math.h"
// GUItool: begin automatically generated code
AudioSynthWaveform sine1; //xy=111,149
AudioPlayQueue queue2; //xy=282,571
AudioRecordQueue queue1; //xy=312,488
AudioOutputI2S i2s1; //xy=412,572
AudioAnalyzeFFT1024 fft1024_1; //xy=434,618
AudioConnection patchCord21(sine1, 0, i2s1, 0);
AudioConnection patchCord24(sine1, 0, i2s1, 1);
AudioConnection patchCord22(queue2, fft1024_1);
AudioConnection patchCord23(sine1, queue1);
AudioControlSGTL5000 sgtl5000_1; //xy=111,635
// GUItool: end automatically generated cod
#define MAX_BLOCKSIZE 256
#define NO_COMPONENT 20
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
elapsedMillis press_time;
int modePlay = 0;
int modeRecord = 0;
//double coeff_play[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
double coeff_play[] = { 0,0,0,0,0,0.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int offsets = 0;
int i = 0;
int ii = 0;
byte bufferblock[512];
elapsedMillis timeElapsed;
int on = 1;
float32_t sinFloatComponent[NO_COMPONENT];
float32_t sinCoef[NO_COMPONENT];
float32_t sinFloat[MAX_BLOCKSIZE + 1];
int16_t sinInt[MAX_BLOCKSIZE + 1];
// Void setup
void setup() {
Serial.begin(9600);
Serial1.begin(38400);
AudioMemory(100);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
//sgtl5000_1.lineOutLevel(23);
SPI.setMOSI(7);
SPI.setSCK(14);
sine1.begin(0.5, 0.8, WAVEFORM_ARBITRARY);
int16_t *saw = GetSawtoothWave();
sine1.arbitraryWaveform(saw, 0.8);
delay(20);
modePlay=1;
startFFT();
}
void loop() {
// switch (on) {
//
// case 0: // Main menu
//
// break;
//
// case 1:
//
// if (modePlay == 0 ) {
// startFFT();
// }
// if (modePlay == 1 ) {
// stopFFT();
// }
// break;
//
//
// }
if (modePlay == 1) continueFFT();
}
//
// float vol = analogRead(15);
// vol = vol / 1024;
// sgtl5000_1.volume(vol);
//
/*AudioWindowHanning1024 (default)
//AudioWindowBartlett1024
//AudioWindowBlackman1024
//AudioWindowFlattop1024
//AudioWindowBlackmanHarris1024
//AudioWindowNuttall1024
//AudioWindowBlackmanNuttall1024
//AudioWindowWelch1024***
//AudioWindowHamming1024
//AudioWindowCosine1024
//AudioWindowTukey1024*/
void startFFT() {
modePlay = 1;
queue1.begin();
fft1024_1.windowFunction(AudioWindowHamming1024);
}
void continueFFT() {
if (queue1.available() >= 2) {
byte buff[512];
memcpy(buff, queue1.readBuffer(), 256);
queue1.freeBuffer();
memcpy(buff + 256, queue1.readBuffer(), 256);
queue1.freeBuffer();
while (i < 256 && 215 * i + offsets < 256) {
memcpy(bufferblock + ii * 2, buff + 2 * (215 * i + offsets), 2);
//Serial.println(ii);
i ++;
ii++;
}
offsets = 215 * i + offsets - 256;
i = 0;
if (ii >= 256 && queue2.available()) {
memcpy(queue2.getBuffer(), bufferblock, 256);
queue2.playBuffer();
memcpy(queue2.getBuffer(), bufferblock + 256, 256);
queue2.playBuffer();
ii = 0;
printtft();
//Serial.println(queue2.available());
}
}
// if(1) {
// if(millis() - last_time >= 5000) {
// Serial.print("Proc = ");
// Serial.print(AudioProcessorUsage());
// Serial.print(" (");
// Serial.print(AudioProcessorUsageMax());
// Serial.print("), Mem = ");
// Serial.print(AudioMemoryUsage());
// Serial.print(" (");
// Serial.print(AudioMemoryUsageMax());
// Serial.println(")");
// last_time = millis();
// }
//}
}
void stopFFT() {
queue1.end();
//Serial.println("stopPlaying");
//tft.fillRect(5, 30, 320, 240, BLACK);
modePlay = 0;
}
/*
huge arguement list
&d name of your display object
x = x data point
y = y datapont
gx = x graph location (lower left)
gy = y graph location (lower left)
w = width of graph
h = height of graph
xlo = lower bound of x axis
xhi = upper bound of x asis
xinc = division of x axis (distance not count)
ylo = lower bound of y axis
yhi = upper bound of y asis
yinc = division of y axis (distance not count)
title = title of graph
xlabel = x asis label
ylabel = y asis label
gcolor = graph line colors
acolor = axi ine colors
pcolor = color of your plotted data
tcolor = text color
bcolor = background color
&redraw = flag to redraw graph on fist call only
*/
void printNumber(float n) {
if (n >= 0.0005) {
long a = (n*100000);
Serial1.print("<");
Serial1.print(a);
Serial1.print(">");
} else {
Serial1.print("<");
Serial1.print(0);
Serial1.print(">");
}
}
void printtft() {
Serial1.print("A");
printNumber(fft1024_1.read(0));
printNumber(fft1024_1.read(1));
printNumber(fft1024_1.read(2));
printNumber(fft1024_1.read(3));
printNumber(fft1024_1.read(4));
printNumber(fft1024_1.read(5));
printNumber(fft1024_1.read(6));
printNumber(fft1024_1.read(7));
printNumber(fft1024_1.read(8));
printNumber(fft1024_1.read(9));
printNumber(fft1024_1.read(10));
printNumber(fft1024_1.read(11));
printNumber(fft1024_1.read(12));
printNumber(fft1024_1.read(13));
printNumber(fft1024_1.read(14));
printNumber(fft1024_1.read(15));
printNumber(fft1024_1.read(16));
printNumber(fft1024_1.read(17));
printNumber(fft1024_1.read(18));
printNumber(fft1024_1.read(19));
printNumber(fft1024_1.read(20));
printNumber(fft1024_1.read(21));
printNumber(fft1024_1.read(22));
printNumber(fft1024_1.read(23));
printNumber(fft1024_1.read(24));
printNumber(fft1024_1.read(25));
printNumber(fft1024_1.read(26));
printNumber(fft1024_1.read(27));
printNumber(fft1024_1.read(28));
printNumber(fft1024_1.read(29));
printNumber(fft1024_1.read(30));
printNumber(fft1024_1.read(31));
printNumber(fft1024_1.read(32));
printNumber(fft1024_1.read(33));
printNumber(fft1024_1.read(34));
printNumber(fft1024_1.read(35));
printNumber(fft1024_1.read(36));
printNumber(fft1024_1.read(37));
printNumber(fft1024_1.read(38));
printNumber(fft1024_1.read(39));
printNumber(fft1024_1.read(40));
Serial1.print("B");
//
// Serial.println();
// Serial.print("all=");
// Serial.print(AudioProcessorUsage());
// Serial.print(",");
// Serial.print(AudioProcessorUsageMax());
// Serial.print(" ");
// Serial.print("Memory: ");
// Serial.print(AudioMemoryUsage());
// Serial.print(",");
// Serial.print(AudioMemoryUsageMax());
// Serial.print(" ");
// Serial.println();
}
float dspSin(float x) {
float result = arm_sin_f32(x);
return result;
}
float radian(int i)
{
float radianStep = 2 * PI * i / MAX_BLOCKSIZE;
return radianStep;
}
int16_t * GetSawtoothWave() {
for (int i = 0; i < NO_COMPONENT; i++)
{
sinCoef[i] = -0.5705 * (coeff_play[i]);
//sinCoef[i] = 1;
//Serial.println(sinCoef[i], 5);
}
for (int i = 0; i <= MAX_BLOCKSIZE; i++)
{
sinFloat[i] = 0;
for (int j = 0; j < NO_COMPONENT; j++)
{
sinFloatComponent[j] = dspSin(radian(i) * (j + 1));
sinFloat[i] += sinCoef[j] * sinFloatComponent[j];
//Serial.println(sinFloatComponent[j], 5);
//Serial.println(sinFloat[i], 5);
}
}
arm_float_to_q15 ( &sinFloat[0], &sinInt[0], MAX_BLOCKSIZE);
// for (int i = 0; i <= MAX_BLOCKSIZE; i++)
// {
// //Serial.println(sinFloat[i], 5);
// //Serial.println(sinInt[i]);
// }
return sinInt;
}