本教程展示了如何使用ESP32开发板与INMP441麦克风模块进行音频采集,并通过UDP将音频数据传输到Windows主机进行播放。通过简单的代码,你可以实时接收和播放麦克风输入的音频。
小提示:确保ESP32和Windows主机连接在同一个WiFi网络下,以便成功传输数据。
硬件连接
材料:
ESP32开发板
INMP441麦克风模块
连接线
连接方式:
INMP441 VCC → ESP32的3.3V
INMP441 GND → ESP32的GND
INMP441 SCK → ESP32的GPIO 17
INMP441 WS → ESP32的GPIO 18
INMP441 SD → ESP32的GPIO 16
- 硬件端代码
开发板:ESP32S3 Dev Module
IDE:Arduino IDE
#include <Arduino.h>
#include <WiFi.h>
#include <driver/i2s.h>
#include <WiFiUdp.h>
#define I2S_WS 18
#define I2S_SD 16
#define I2S_SCK 17
#define I2S_PORT I2S_NUM_0
#define bufferLen 1024 // 调整缓冲区大小为1024
const char* ssid = "NBWIFI";
const char* password = "z7758521";
const char* host = "192.168.3.18"; // 电脑的IP地址
const int port = 8888; // 监听的端口
WiFiUDP udp; // 使用 UDP 协议进行数据传输
int16_t sBuffer[bufferLen];
void setup() {
Serial.begin(115200);
Serial.println("Setup I2S ...");
// 连接WiFi
setup_wifi();
delay(1000);
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
delay(500);
}
void loop() {
size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_PORT, &sBuffer, bufferLen * sizeof(int16_t), &bytesIn, portMAX_DELAY);
if (result == ESP_OK && bytesIn > 0) {
// 发送音频数据到服务器
udp.beginPacket(host, port);
udp.write((uint8_t*)sBuffer, bytesIn);
udp.endPacket();
}
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("正在连接到WIFI ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(600);
Serial.print("-");
}
Serial.println("WiFi 已连接");
Serial.println("IP 地址: ");
Serial.println(WiFi.localIP());
}
void i2s_install() {
const i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 48000, // 增加采样率以提高音频质量
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.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 = 16, // 增加DMA缓冲区数量
.dma_buf_len = bufferLen,
.use_apll = false
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
void i2s_setpin() {
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_SD
};
i2s_set_pin(I2S_PORT, &pin_config);
}
python 接收端
import socket
import pyaudio
import numpy as np
# 设置音频参数
CHUNK = 1024 # 每个数据包的大小
FORMAT = pyaudio.paInt16 # 数据格式为 16 位整型
CHANNELS = 1 # 单声道
RATE = 48000 # 采样率 44.1kHz
# 创建 PyAudio 对象
p = pyaudio.PyAudio()
# 打开音频流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
output=True)
# 设置 UDP 服务器
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', 8888)) # 监听所有可用接口,端口 8888
# 获取连接的 IP 地址和端口
ip_address, port = server_socket.getsockname()
print(f"IP 地址: {ip_address}, 端口: {port}")
# 放大音量的因子
volume_factor = 100.0
try:
print("等待音频数据流...")
while True:
# 接收数据和客户端地址
data, addr = server_socket.recvfrom(CHUNK * CHANNELS * 2) # 接收数据包(每个 int16 占用 2 个字节)
if not data:
break
# 将接收到的字节数据转换为 NumPy 数组
audio_data = np.frombuffer(data, dtype=np.int16)
# 放大音量
audio_data = np.clip(audio_data * volume_factor, -32768, 32767).astype(np.int16)
# 播放放大音量后的音频数据
stream.write(audio_data.tobytes())
except KeyboardInterrupt:
pass
finally:
stream.stop_stream()
stream.close()
p.terminate()
server_socket.close()
c# 接收播放
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using NAudio.Wave;
class Program
{
static void Main(string[] args)
{
// 设置音频参数
int chunkSize = 1024; // 每个数据包的大小
int sampleRate = 48000; // 采样率 48kHz
int channels = 1; // 单声道
int bytesPerSample = 2; // 每个样本的字节数 (16 位整型)
// 创建 UDP 服务器
UdpClient udpClient = new UdpClient(8888);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 8888);
// 获取连接的 IP 地址和端口
IPEndPoint localEndPoint = (IPEndPoint)udpClient.Client.LocalEndPoint;
Console.WriteLine($"IP 地址: {localEndPoint.Address}, 端口: {localEndPoint.Port}");
// 放大音量的因子
float volumeFactor = 100.0f;
// 创建 NAudio WaveOut 设备
WaveOutEvent waveOut = new WaveOutEvent();
BufferedWaveProvider waveProvider = new BufferedWaveProvider(new WaveFormat(sampleRate, 16, channels));
waveOut.Init(waveProvider);
waveOut.Play();
Console.WriteLine("等待音频数据流...");
try
{
while (true)
{
// 接收数据和客户端地址
byte[] data = udpClient.Receive(ref remoteEndPoint);
if (data.Length == 0)
break;
// 将接收到的字节数据转换为短整型数组
short[] audioData = new short[data.Length / bytesPerSample];
Buffer.BlockCopy(data, 0, audioData, 0, data.Length);
// 放大音量
for (int i = 0; i < audioData.Length; i++)
{
audioData[i] = (short)Math.Max(short.MinValue, Math.Min(audioData[i] * volumeFactor, short.MaxValue));
}
// 播放放大音量后的音频数据
byte[] amplifiedData = new byte[audioData.Length * bytesPerSample];
Buffer.BlockCopy(audioData, 0, amplifiedData, 0, amplifiedData.Length);
waveProvider.AddSamples(amplifiedData, 0, amplifiedData.Length);
}
}
catch (SocketException ex)
{
Console.WriteLine($"SocketException: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
finally
{
waveOut.Stop();
waveOut.Dispose();
udpClient.Close();
}
}
}
参考:https://blog.csdn.net/m0_74276051/article/details/143181595
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询