/
esp32_inmp441_wifi_mic.ino
156 lines (125 loc) · 4.15 KB
/
esp32_inmp441_wifi_mic.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Olaf: Overly Lightweight Acoustic Fingerprinting
// Copyright (C) 2019-2023 Joren Six
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// *****************************************************************
//
//
// This is an Arduino patch for an ESP32 with a I2S INMP441 microphone.
//
// The patch captures samples from an I2S microphone, converts them to
// 32bit le float PCM and sends them over an UDP socket to a receiver
// with a chosen IP address.
//
// On the receiver, to listen to the microphone:
// use netcat in combination with ffplay, part of the ffmpeg suite
//
// nc -l -u 3000 | ffplay -f f32le -ar 16000 -ac 1 -
//
// To capture the audio, use netcat and ffmpeg to store the audio
// in a wav-file
//
// nc -l -u 3000 | ffmpeg -f f32le -ar 16000 -ac 1 -i pipe: microphone.wav
#include <WiFi.h>
#include <WiFiUdp.h>
// Include I2S driver
#include <driver/i2s.h>
#include <math.h>
//addres of the sound receiving computer and the UDP port
//that is used
IPAddress outIp(192, 168, 88, 253);
int outPort = 3000;
// a gain factor to amplify the sound: samples come out of the
// microphone rather quietly.
uint16_t gain_factor = 40;
// Connections to INMP441 I2S microphone
#define I2S_WS 25
#define I2S_SD 33
#define I2S_SCK 32
// Use I2S Processor 0
#define I2S_PORT I2S_NUM_0
// Define input buffer length
#define bufferLen 64
uint8_t sBuffer[bufferLen*2];
WiFiUDP Udp;
const char * ssid = "****";
const char * password = "*****";
void i2s_install() {
// Set up I2S Processor configuration
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 16000,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = bufferLen,
.use_apll = false
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
void i2s_setpin() {
// Set I2S pin configuration
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};
i2s_set_pin(I2S_PORT, &pin_config);
}
void connect_wifi(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while(1) {
delay(500);
}
}
// you are now connected:
Serial.print("You are connected to the network ");
Serial.println(ssid);
}
void setup() {
Serial.begin(115200);
Serial.println(" ");
Serial.println("Starting WiFi microphone");
connect_wifi();
// Set up I2S
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
}
void loop() {
// Get I2S data and place in data buffer
size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen, &bytesIn, portMAX_DELAY);
if (result == ESP_OK && bytesIn > 0){
int16_t * sample_buffer = (int16_t *) sBuffer;
// Read I2S data buffer
// I2S reads stereo data and one channel is zero the other contains
// data, both have 2 bytes per sample
int16_t samples_read = bytesIn / 2;
float audio_block_float[samples_read];
for(size_t i = 0 ; i < samples_read ; i++){
sample_buffer[i] = gain_factor * sample_buffer[i];
//Max for signed int16_t is 2^15
audio_block_float[i] = sample_buffer[i] / 32768.f;
}
// Send raw audio 32bit float samples over UDP
Udp.beginPacket(outIp, outPort);
Udp.write((const uint8_t*) audio_block_float,bytesIn*2);
Udp.endPacket();
}
}