-
Notifications
You must be signed in to change notification settings - Fork 11
/
Frequencies.ino
73 lines (55 loc) · 2.62 KB
/
Frequencies.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Frequencies.ino
By Shea Ivey
Reads I2S microphone data into samples[], processes them into frequency buckets and then outputs it to the serial plotter for viewing.
Try wistling differrent tones to see which frequency buckets they fall into.
TIP: uncomment the audioInfo.autoLevel() to see how the loud and quiet noises are handled.
*/
#include <AudioInI2S.h>
#define SAMPLE_SIZE 1024 // Buffer size of read samples
#define SAMPLE_RATE 44100 // Audio Sample Rate
/* Required defines for audio analysis */
#define BAND_SIZE 8 // powers of 2 up to 64, defaults to 8
#include <AudioAnalysis.h>
AudioAnalysis audioInfo;
// ESP32 S2 Mini
// #define MIC_BCK_PIN 4 // Clock pin from the mic.
// #define MIC_WS_PIN 39 // WS pin from the mic.
// #define MIC_DATA_PIN 5 // SD pin data from the mic.
// #define MIC_CHANNEL_SELECT_PIN 40 // Left/Right pin to select the channel output from the mic.
// ESP32 TTGO T-Display
#define MIC_BCK_PIN 32 // Clock pin from the mic.
#define MIC_WS_PIN 25 // WS pin from the mic.
#define MIC_DATA_PIN 33 // SD pin data from the mic.
#define MIC_CHANNEL_SELECT_PIN 27 // Left/Right pin to select the channel output from the mic.
AudioInI2S mic(MIC_BCK_PIN, MIC_WS_PIN, MIC_DATA_PIN, MIC_CHANNEL_SELECT_PIN); // defaults to RIGHT channel.
int32_t samples[SAMPLE_SIZE]; // I2S sample data is stored here
void setup()
{
Serial.begin(115200);
mic.begin(SAMPLE_SIZE, SAMPLE_RATE); // Starts the I2S DMA port.
// audio analysis setup
audioInfo.setNoiseFloor(10); // sets the noise floor
audioInfo.normalize(true, 0, 255); // normalize all values to range provided.
// audioInfo.autoLevel(AudioAnalysis::ACCELERATE_FALLOFF, 1); // uncomment this line to set auto level falloff rate
audioInfo.bandPeakFalloff(AudioAnalysis::EXPONENTIAL_FALLOFF, 0.05); // set the band peak fall off rate
audioInfo.vuPeakFalloff(AudioAnalysis::EXPONENTIAL_FALLOFF, 0.05); // set the volume unit peak fall off rate
}
void loop()
{
mic.read(samples); // Stores the current I2S port buffer into samples.
audioInfo.computeFFT(samples, SAMPLE_SIZE, SAMPLE_RATE);
audioInfo.computeFrequencies(BAND_SIZE);
float *bands = audioInfo.getBands();
float *peaks = audioInfo.getPeaks();
float vuMeter = audioInfo.getVolumeUnit();
float vuMeterPeak = audioInfo.getVolumeUnitPeak();
// Send data to serial plotter
for (int i = 0; i < BAND_SIZE; i++)
{
Serial.printf("%dHz:%.1f,", audioInfo.getBandName(i), peaks[i]);
}
// also send the vu meter data
Serial.printf("vuValue:%.1f,vuPeak:%.2f", vuMeter, vuMeterPeak);
Serial.println();
}