Created
September 21, 2022 07:40
-
-
Save bboyho/6dc50a92bda1ec53465db871082ddc97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int analogPin1 = A1; | |
#include "AudioCodec.h" | |
#include "FFT.h" | |
#define SAMPLERATE 16000 | |
#define SAMPLECOUNT 128 | |
int16_t audio_buffer[SAMPLECOUNT] = {0}; | |
float fft_buffer[SAMPLECOUNT/2] = {0}; | |
uint16_t freq_bins[SAMPLECOUNT/2] = {0}; | |
int i = 0; | |
int val = 0; | |
FFT fft; | |
void setup() { | |
Serial.begin(2000000); | |
fft.setWindow(HANN, SAMPLECOUNT); | |
fft.getFrequencyBins(freq_bins, SAMPLECOUNT, SAMPLERATE); | |
for (i = 0; i < (SAMPLECOUNT/2); i++) { | |
Serial.print(freq_bins[i]); | |
Serial.print(" Hz"); | |
Serial.print(","); | |
} | |
Serial.println(); | |
Codec.setSampleRate(SAMPLERATE); | |
Codec.begin(TRUE, FALSE); | |
} | |
void loop() { | |
if(Codec.readAvaliable()) { | |
Codec.readDataPage(audio_buffer, SAMPLECOUNT); // read latest received data from buffer | |
fft.calculate(audio_buffer, fft_buffer, SAMPLECOUNT); | |
for (i = 0; i < (SAMPLECOUNT/2); i++) { | |
//Sample rate = 16000, SAMPLECOUNT = 128, i = 4 (i.e 500Hz) | |
if(i==4){ | |
val = fft_buffer[i]*100; | |
val = map(val, 0, 10, 0, 255); | |
analogWrite(analogPin1, val); | |
} | |
if (fft_buffer[i] > 0.01) { | |
Serial.print(fft_buffer[i]*100,2); | |
Serial.print(","); | |
//Serial.print(" | "); | |
} else { | |
Serial.print(0); | |
Serial.print(","); | |
//Serial.print(" - |"); | |
} | |
} | |
Serial.println(); | |
} | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment