文章目录[隐藏]
在决定使用INMP441作为麦克风的时候,我们对麦克风的相关知识并不是很了解,所以过程中出了很多问题。出现问题,那就针对debug的结果和自己的猜想一个个去查,去实验,最后终于解决了问题,不过这个过程真的蛮令人抓狂的。本项目的代码都有参考网上的代码,参考列表会给出。本项目是基于Arduino平台开发的,因为简单,但是Arduino调试比较麻烦。在做的过程中,我学会了查阅官方文档,esp32的和INMP441的。
INMP441的连接
INMP441麦克风有6个引脚,长这样:
INMP441是芯片,麦克风是基于这个芯片的。
和网上PCM编码的图片对比之后,发现应该是PCM编码的麦克风。
这是一个数字麦克风,这很重要。数字麦克风本身包含ADC,它传递进来的是数字量,如果要播放,或者是上传到语音识别API中,必须要经过一次DAC。将输入的数字信号转换成电压模拟信号输出。
官方文档里对其引脚有详尽的描述:
再结合esp32-wrover板子的技术规格文档里的引脚信息,本次项目我们给出的连接方案是
INMP441 | ESP32 |
---|---|
SCK | 2 |
WS | 15 |
SD | 13 |
L/R | 无连接 |
GND | GND (对所有元件都是一样的) |
VDD | 3V3 |
INMP441录音
买的时候商家会有几个示例代码。注意:
- INMP441不需要额外的库,只需要esp32本身的<driver/i2s.h>库
- I2S的初始化可以参考esp32官方API指南,不再赘述。
在串口绘图器打印录音波形代码如下:
/**
* ESP32 I2S Serial Plotter Example.
* 在串口绘图器中显示声音波形
*/
#include <driver/i2s.h>
const i2s_port_t I2S_PORT = I2S_NUM_0;
void setup() {
Serial.begin(115200);
Serial.println("Configuring I2S...");
esp_err_t err;
// The I2S config
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive mode
.sample_rate = 16000, // 16KHz
// could only get it to work with 32bits/24bits
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
// leave L/R unconnected when using Left channel
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
// Philips standard | MSB first standard
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S| I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Interrupt level 1
.dma_buf_count = 4, // number of buffers
.dma_buf_len = 8 // 8 samples per buffer (minimum)
};
// The pin config
const i2s_pin_config_t pin_config = {
.bck_io_num = 2, // BCKL
.ws_io_num = 15, // LRCL
.data_out_num = -1, // not used (only for speakers)
.data_in_num = 13 // DOUT
};
// Configuring the I2S driver and pins.
// This function must be called before any I2S driver read/write operations.
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 installed.");
}
void loop() {
// Read a single sample and log it for the Serial Plotter.
int32_t sample = 0;
int bytes_read = i2s_pop_sample(I2S_PORT, (char *)&sample, portMAX_DELAY); // no timeout
if (bytes_read > 0) {
Serial.println(sample);
}
delay(100);
}
运行结果:
串口打印的是采样的每一个样本,设置为32bit位深时,一个样本应该是4byte。
遇到的问题:
- 录音一直显示0…我起初以为是焊接的时候把麦克风焊坏了,所以又买了两个新的。。。。结果还是不行。。。几近崩溃。仔细观察自己的麦克风,我有了一个猜想:是不是因为焊的时候焊锡接触了外围的金属圈,然后我重新焊了一次,然后,就可以录音啦!!!!!
但是,还有几个限制,不知道为什么会有,可能是元件工艺问题:
(1)只能在24bit和32bit深度的时候工作(采样率是16khz,为了后续进行语音识别)
(2) 单通道,左通道(参考上图引脚描述),这个并没有出现右通道才能工作的问题。
版权声明:本文为CSDN博主「枯叶酿茶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Laura_Luo/article/details/117776909
暂无评论