-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
150 lines (127 loc) · 5.03 KB
/
main.cpp
File metadata and controls
150 lines (127 loc) · 5.03 KB
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
/*
MIT License
Copyright (c) 2021 Alessandro Orlando
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVEN
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
Description:
Simple I2S INMP441 MEMS microphone Audio Streaming via UDP transmitter
Needs a UDP listener like netcat on port 16500 on listener PC
Needs a SoX with mp3 handler for Recorder
Under Linux for listener:
netcat -u -p 16500 -l | play -t s16 -r 48000 -c 2 -
Under Linux for recorder (give for file.mp3 the name you prefer) :
netcat -u -p 16500 -l | rec -t s16 -r 48000 -c 2 - file.mp3
*/
#include <Arduino.h>
#include "WiFi.h"
#include "AsyncUDP.h"
#include <driver/i2s.h>
#include <soc/i2s_reg.h>
//Set youy WiFi network name and password:
const char* ssid = "ssid";
const char* pswd = "password";
// Set your listener PC's IP here in according with your DHCP network. In my case is 192.168.1.40:
IPAddress udpAddress(192, 168, 0, 158);
const int udpPort = 16500; //UDP Listener Port:
boolean connected = false; //UDP State:
AsyncUDP udp; //Class UDP:
const i2s_port_t I2S_PORT = I2S_NUM_0;
const int block_size = 128;
void setup() {
Serial.begin(115200);
Serial.println("Configuring WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pswd);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while (1) {
delay(1000);
}
}
// I2S CONFIG
Serial.println("Configuring I2S port");
esp_err_t err;
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 96000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
.dma_buf_len = block_size,
};
// ESP32 GPIO PINS > I2S MIC PINS:
const i2s_pin_config_t pin_config = {
.bck_io_num = 26, // > SCK
.ws_io_num = 25, // > WS
.data_out_num = -1, // Serial Data Out is no connected
.data_in_num = 32, // > SD
};
// CONFIGURE I2S DRIVER AND PINS.
err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
if (err != ESP_OK) {
Serial.printf("Failed installing driver: %d\n", err);
while (true);
}
err = i2s_set_pin(I2S_PORT, &pin_config);
if (err != ESP_OK) {
Serial.printf("Failed setting pin: %d\n", err);
while (true);
}
Serial.println("I2S driver OK");
}
int32_t buffer[512]; // Buffer
volatile uint16_t rpt = 0; // Pointer
void i2s_mic()
{
size_t num_bytes_read;
int err = i2s_read(I2S_PORT, (char*)buffer + rpt, block_size, &num_bytes_read, portMAX_DELAY);
if (err != ESP_OK) {
Serial.printf("Failed to read i2s_mic: %d\n", err);
while (true);
}
rpt = rpt + num_bytes_read;
if (rpt > 2043) rpt = 0;
}
void loop() {
static uint8_t state = 0;
i2s_mic();
if (!connected) {
if (udp.connect(udpAddress, udpPort)) {
connected = true;
Serial.println("Connected to UDP Listener");
Serial.println("Under Linux for listener use: netcat -u -p 16500 -l | play -t s16 -r 48000 -c 2 -");
Serial.println("Under Linux for recorder use: netcat -u -p 16500 -l | rec -t s16 -r 48000 -c 2 - file.mp3");
}
}
else {
switch (state) {
case 0: // wait for index to pass halfway
if (rpt > 1023) {
state = 1;
}
break;
case 1: // send the first half of the buffer
state = 2;
udp.write( (uint8_t *)buffer, 1024);
break;
case 2: // wait for index to wrap
if (rpt < 1023) {
state = 3;
}
break;
case 3: // send second half of the buffer
state = 0;
udp.write((uint8_t*)buffer+1024, 1024);
break;
}
}
}