ESP_IOT v2.5
IOT ESP Coding
AudioModule.cpp
Go to the documentation of this file.
1#include "AudioModule.h"
2#include "../../Defines.h"
3#ifdef USE_AUDIO_MODULE
4
5#include <driver/i2s.h>
6
7#define PIN_CLK 0
8#define PIN_DATA 34
9#define READ_LEN (2 * 256)
10#define GAIN_FACTOR 3
11uint8_t _BUFFER[READ_LEN] = {0};
12
13int16_t *_adcBuffer = NULL;
14
15#ifdef CALLBACK
16void _callbackFunction;
17
18//! call the callback..
19void callCallback(int volume)
20{
21 if (_callbackFunction)
22 {
23 void (*callbackFunction)(int) = _callbackFunction;
24 (*callbackFunction)(volume);
25 }
26}
27#endif
28
29//! the loud threshhold (after some testing)
30int _loudThreshhold = 4000; //8000;
31
32//!check signal
33void showSignal_AudioModule()
34{
35 int y;
36 int max = 0;
37 for (int n = 0; n < 160; n++) {
38 y = _adcBuffer[n] * GAIN_FACTOR;
39 if (y > max)
40 max = y;
41 }
42 if (max > _loudThreshhold)
43 {
44 SerialDebug.printf("** HIGH = %d\n", max);
45 }
46 else
47 SerialDebug.printf("low = %d\n", max);
48
49}
50
51//!check mic sound
52void checkMicSound_AudioModule()
53{
54 size_t bytesread;
55 i2s_read(I2S_NUM_0, (char *)_BUFFER, READ_LEN, &bytesread,
56 (100 / portTICK_RATE_MS));
57 _adcBuffer = (int16_t *)_BUFFER;
58 showSignal_AudioModule();
59 //vTaskDelay(100 / portTICK_RATE_MS);
60}
61
62//!initialize
63void i2sInit_AudioModule()
64{
65 i2s_config_t i2s_config = {
66 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
67 .sample_rate = 44100,
68 .bits_per_sample =
69 I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
70 .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,
71#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 1, 0)
72 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
73#else
74 .communication_format = I2S_COMM_FORMAT_I2S,
75#endif
76 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
77 .dma_buf_count = 2,
78 .dma_buf_len = 128,
79 };
80
81 i2s_pin_config_t pin_config;
82
83#if (ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0))
84 pin_config.mck_io_num = I2S_PIN_NO_CHANGE;
85#endif
86
87 pin_config.bck_io_num = I2S_PIN_NO_CHANGE;
88 pin_config.ws_io_num = PIN_CLK;
89 pin_config.data_out_num = I2S_PIN_NO_CHANGE;
90 pin_config.data_in_num = PIN_DATA;
91
92 i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
93 i2s_set_pin(I2S_NUM_0, &pin_config);
94 i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
95}
96
97int _delayStartMillis_AudioModule;
98int _delaySeconds_AudioModule;
99//!init the timer
100void startTimer_AudioModule()
101{
102 _delayStartMillis_AudioModule = millis(); // start delay
103 _delaySeconds_AudioModule = 1 * portTICK_RATE_MS;
104}
105
106//!see if the times up
107boolean timesUp_AudioModule()
108{
109 boolean timesUp = false;
110 int currentTimeMillis = millis();
111 //!substract the seconds from the set delay
112 int currentCounterSeconds = _delaySeconds_AudioModule - (currentTimeMillis - _delayStartMillis_AudioModule)/1000;
113
114 SerialLots.printf("delay = %d, counter = %d, delaySeconds = %d\n", _delayStartMillis_AudioModule, currentCounterSeconds, _delaySeconds_AudioModule);
115 if (currentCounterSeconds <= 0)
116 {
117 SerialLots.printf("delayFinished_AudioModule\n");
118
119 startTimer_AudioModule();
120 timesUp = true;
121 }
122 return timesUp;
123}
124
125//!setup ..
126//void setup_AudioModule(void (*loudCallback)(int))
128{
129 //_callbackFunction = loudCallback;
130 //!initialize
131 i2sInit_AudioModule();
132
133 //!start timer
134 startTimer_AudioModule();
135}
136
137//!loop if times up then check the mic sound
138void loop_AudioModule()
139{
140 if (timesUp_AudioModule())
141 {
142 checkMicSound_AudioModule();
143 }
144}
145
146#endif
147
void loop_AudioModule()
called for the loop() of this plugin
void setup_AudioModule()