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;
31int _lastTimeMillis;
32
33//!check signal
34void showSignal_AudioModule()
35{
36
37
38 int y;
39 int max = 0;
40 for (int n = 0; n < 160; n++) {
41 y = _adcBuffer[n] * GAIN_FACTOR;
42 if (y > max)
43 max = y;
44 }
45
46 int currentTimeMillis = millis();
47 int timeDiff = currentTimeMillis - _lastTimeMillis;
48
49 if (max > _loudThreshhold)
50 {
51
52 _lastTimeMillis = currentTimeMillis;
53
54 SerialDebug.printf("** HIGH = %d - diffTime=%d\n", max, timeDiff);
55 }
56// else
57// SerialDebug.printf("low = %d\n", max);
58
59}
60
61//!check mic sound
62void checkMicSound_AudioModule()
63{
64 size_t bytesread;
65 i2s_read(I2S_NUM_0, (char *)_BUFFER, READ_LEN, &bytesread,
66 (100 / portTICK_RATE_MS));
67 _adcBuffer = (int16_t *)_BUFFER;
68 showSignal_AudioModule();
69 //vTaskDelay(100 / portTICK_RATE_MS);
70}
71
72//!initialize
73void i2sInit_AudioModule()
74{
75 i2s_config_t i2s_config = {
76 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
77 .sample_rate = 44100,
78 .bits_per_sample =
79 I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
80 .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,
81#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 1, 0)
82 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
83#else
84 .communication_format = I2S_COMM_FORMAT_I2S,
85#endif
86 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
87 .dma_buf_count = 2,
88 .dma_buf_len = 128,
89 };
90
91 i2s_pin_config_t pin_config;
92
93#if (ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0))
94 pin_config.mck_io_num = I2S_PIN_NO_CHANGE;
95#endif
96
97 pin_config.bck_io_num = I2S_PIN_NO_CHANGE;
98 pin_config.ws_io_num = PIN_CLK;
99 pin_config.data_out_num = I2S_PIN_NO_CHANGE;
100 pin_config.data_in_num = PIN_DATA;
101
102 i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
103 i2s_set_pin(I2S_NUM_0, &pin_config);
104 i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
105}
106
107int _delayStartMillis_AudioModule;
108int _delaySeconds_AudioModule;
109//!init the timer
110void startTimer_AudioModule()
111{
112 _delayStartMillis_AudioModule = millis(); // start delay
113 _delaySeconds_AudioModule = 1 * portTICK_RATE_MS;
114}
115
116//!see if the times up
117boolean timesUp_AudioModule()
118{
119 return true;
120
121 boolean timesUp = false;
122 int currentTimeMillis = millis();
123 //!substract the seconds from the set delay
124 int currentCounterSeconds = _delaySeconds_AudioModule - (currentTimeMillis - _delayStartMillis_AudioModule)/1000;
125
126 SerialLots.printf("delay = %d, counter = %d, delaySeconds = %d\n", _delayStartMillis_AudioModule, currentCounterSeconds, _delaySeconds_AudioModule);
127 if (currentCounterSeconds <= 0)
128 {
129 SerialLots.printf("delayFinished_AudioModule\n");
130
131 startTimer_AudioModule();
132 timesUp = true;
133 }
134 return timesUp;
135}
136
137//!setup ..
138//void setup_AudioModule(void (*loudCallback)(int))
140{
141 //_callbackFunction = loudCallback;
142 //!initialize
143 i2sInit_AudioModule();
144
145 //!start timer
146 startTimer_AudioModule();
147}
148
149//!loop if times up then check the mic sound
150void loop_AudioModule()
151{
152 if (timesUp_AudioModule())
153 {
154 checkMicSound_AudioModule();
155
156 //try checking mic again..
157// delay(100 / portTICK_RATE_MS);
158// checkMicSound_AudioModule();
159
160 }
161}
162
163#endif
164
void loop_AudioModule()
called for the loop() of this plugin
void setup_AudioModule()
unsigned long millis()
Definition: TinyGPS.cpp:35