Skip to content

Commit 64e4f5b

Browse files
committed
add: 将录音数据写入文件,再播放出来
1 parent 7818faa commit 64e4f5b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

demo/record/EC618/es8218e.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949

5050

5151

52-
function record_task()
52+
local function record_task()
5353
sys.wait(5000)
5454
uart.setup(1, 115200) -- 开启串口1
5555
log.info("i2c initial",i2c.setup(0, i2c.FAST)) -- 开启i2c

demo/record/EC618/es8311.lua

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
audio.on(0, function(id, event)
2+
--使用play来播放文件时只有播放完成回调
3+
local succ,stop,file_cnt = audio.getError(0)
4+
if not succ then
5+
if stop then
6+
log.info("用户停止播放")
7+
else
8+
log.info("", file_cnt, "个文件解码失败")
9+
end
10+
end
11+
-- log.info("播放完成一个音频")
12+
sys.publish("AUDIO_PLAY_DONE")
13+
end)
14+
15+
116
-- es8311器件地址
217
local es8311_address = 0x18
318

@@ -47,6 +62,10 @@ local amr_buff = zbuff.create(10240)
4762
--创建一个amr的encoder
4863
local encoder = codec.create(codec.AMR, false)
4964

65+
66+
-- 录音文件路径
67+
local recordPath = "/record.amr"
68+
5069
-- i2s数据接收回调
5170
local function record_cb(id, buff)
5271
if buff then
@@ -57,9 +76,10 @@ end
5776

5877

5978

60-
function record_task()
79+
local function record_task()
80+
audio.config(0, 25, 1, 6, 200)
6181
sys.wait(5000)
62-
uart.setup(1, 115200) -- 开启串口1
82+
pm.power(pm.DAC_EN, true) -- 打开es8311芯片供电
6383
log.info("i2c initial",i2c.setup(0, i2c.FAST)) -- 开启i2c
6484
i2s.setup(0, 0, 8000, 16, 1, i2s.MODE_I2S) -- 开启i2s
6585
i2s.on(0, record_cb) -- 注册i2s接收回调
@@ -73,9 +93,20 @@ function record_task()
7393
i2s.stop(0) -- 停止接收
7494

7595
log.info("录音5秒结束")
76-
uart.write(1, "#!AMR\n") -- 向串口发送amr文件标识数据
77-
sys.wait(5) -- 等待5ms确保发送成功
78-
uart.write(1, amr_buff:query()) -- 向串口发送编码后的amr数据
96+
io.writeFile(recordPath, "#!AMR\n") -- 向文件写入amr文件标识数据
97+
io.writeFile(recordPath, amr_buff:query(), "a+b") -- 向文件写入编码后的amr数据
98+
99+
i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
100+
101+
local result = audio.play(0, {recordPath}) -- 请求音频播放
102+
if result then
103+
sys.waitUntil("AUDIO_PLAY_DONE") -- 等待音频播放完毕
104+
else
105+
-- 音频播放出错
106+
end
107+
108+
uart.setup(1, 115200) -- 开启串口1
109+
uart.write(1, io.readFile(recordPath)) -- 向串口发送录音文件
79110
end
80-
pm.power(pm.DAC_EN, true) -- 打开es8311芯片供电
111+
81112
sys.taskInit(record_task)

0 commit comments

Comments
 (0)