【M5Stack 提问】Ardunio框架使用M5.Speaker类播放wav音频文件问题
-
硬件型号:M5Core2
代码框架:Ardunio
问题:
我在TF卡中准备了一个wav格式的文件,想通过M5.Speaker类进行播放,但在配置Speaker后无法正常播放音频// 我的代码 void playSpeak(File* file) { auto config = M5.Speaker.config(); config.pin_data_out = 2; config.pin_ws = 0; config.sample_rate = 16000; config.dma_buf_len = 1024; config.dma_buf_count = 2; // config.buzzer = true; config.i2s_port = I2S_NUM_0; M5.Speaker.config(config); M5.Speaker.begin(); // M5.Speaker.tone(661, 1000); M5.Speaker.setAllChannelVolume(255); M5.Speaker.setVolume(64); M5.Speaker.setChannelVolume(0, 255); // bool status = M5.Speaker.tone(2000, 1000); uint8_t* dataBuf = (uint8_t*)malloc(file->size()); file->read(dataBuf, file->size()); Serial.printf("File Content: %d", file->size()); bool status = M5.Speaker.playWav(dataBuf, file->size(), 1, 1, true); if (!status) { Serial.println("Failed to play"); } free(dataBuf); file->close(); M5.Speaker.end(); i2s_driver_uninstall(I2S_NUM_0); }
我开放了Speaker类中源官方测试代码,用来打印wav文件信息,端口打印内容如下:
Speech Start File Content: 957804[ 14851][D][Speaker_Class.cpp:772] playWav(): [wav] RIFF : RIFF [ 14851][D][Speaker_Class.cpp:773] playWav(): [wav] chunk_size : 957796 [ 14855][D][Speaker_Class.cpp:774] playWav(): [wav] WAVEfmt : WAVEfmt [ 14862][D][Speaker_Class.cpp:775] playWav(): [wav] fmt_chunk_size : 16 [ 14868][D][Speaker_Class.cpp:776] playWav(): [wav] audiofmt : 1 [ 14874][D][Speaker_Class.cpp:777] playWav(): [wav] channel : 1 [ 14880][D][Speaker_Class.cpp:778] playWav(): [wav] sample_rate : 16000 [ 14887][D][Speaker_Class.cpp:779] playWav(): [wav] byte_per_sec : 32000 [ 14894][D][Speaker_Class.cpp:780] playWav(): [wav] block_size : 2 [ 14900][D][Speaker_Class.cpp:781] playWav(): [wav] bit_per_sample : 16 Speech end
为解决该问题,我做了如下尝试:
- 测试硬件功放是否可用,我通过官方tone函数测试功放可以正常发出声音
- 阅读学习I2S相关知识,了解了基础原理
- 在网上查阅了ESP32 I2S相关使用指引
但我并没有解决我的问题,希望有人可以帮我看看是哪里的问题?非常感谢!