Skip to content

Commit

Permalink
add:录音demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Dozingfiretruck committed Apr 29, 2024
1 parent ff08690 commit eb60d08
Showing 1 changed file with 96 additions and 40 deletions.
136 changes: 96 additions & 40 deletions demo/multimedia/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ local taskName = "task_audio"
local MSG_MD = "moreData" -- 播放缓存有空余
local MSG_PD = "playDone" -- 播放完成所有数据

audio.on(0, function(id, event)
-- amr数据存放buffer,尽可能地给大一些
amr_buff = zbuff.create(20 * 1024)
--创建一个amr的encoder
encoder = nil

audio.on(0, function(id, event,buff)
--使用play来播放文件时只有播放完成回调
local succ,stop,file_cnt = audio.getError(0)
if not succ then
if stop then
log.info("用户停止播放")
else
log.info("", file_cnt, "个文件解码失败")
if event == audio.RECORD_DATA then -- 录音数据
codec.encode(encoder, buff, amr_buff)
elseif event == audio.RECORD_DONE then -- 录音完成
sys.publish("AUDIO_RECORD_DONE")
else
local succ,stop,file_cnt = audio.getError(0)
if not succ then
if stop then
log.info("用户停止播放")
else
log.info("", file_cnt, "个文件解码失败")
end
end
-- log.info("播放完成一个音频")
sysplus.sendMsg(taskName, MSG_PD)
end
-- log.info("播放完成一个音频")
sysplus.sendMsg(taskName, MSG_PD)
end)

function audio_setup()
Expand All @@ -48,8 +59,8 @@ function audio_setup()
-- audio.setBus(0, audio.BUS_SOFT_DAC)
-- end
audio.config(0, 25, 1, 3, 100)
elseif bsp == "EC718P" then
-- CORE+音频小板是这个配置
elseif bsp == "EC718P" then
-- CORE+音频小板是这个配置

pm.power(pm.LDO_CTL, false) --开发板上ES8311由LDO_CTL控制上下电
sys.wait(100)
Expand All @@ -68,7 +79,7 @@ function audio_setup()
local pa_pin = 25
local pa_on_level = 1
local pa_delay = 100
local power_pin = 255
local power_pin = 16
local power_on_level = 1
local power_delay = 3
local power_time_delay = 100
Expand All @@ -83,32 +94,33 @@ function audio_setup()
audio.setBus(multimedia_id, audio.BUS_I2S,{chip = "es8311",i2cid = i2c_id , i2sid = i2s_id}) --通道0的硬件输出通道设置为I2S

audio.vol(multimedia_id, voice_vol)
audio.micVol(multimedia_id, mic_vol)

--带TM8211的云喇叭开发板参考下面的配置
--[[
local multimedia_id = 0
local i2s_id = 0
local i2s_mode = 0
local i2s_sample_rate = 0
local i2s_bits_per_sample = 16
local i2s_channel_format = i2s.STEREO
local i2s_communication_format = i2s.MODE_MSB
local i2s_channel_bits = 16
local pa_pin = 25
local pa_on_level = 1
local pa_delay = 100
local power_pin = nil
local power_on_level = 1
local power_delay = 3
local power_time_delay = 0
-- local voice_vol = 200 --默认就不放大了
i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format,i2s_channel_bits)
audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin, power_on_level, power_time_delay)
audio.setBus(multimedia_id, audio.BUS_I2S,{chip = "tm8211", i2sid = i2s_id}) --通道0的硬件输出通道设置为I2S
-- audio.vol(multimedia_id, voice_vol)
audio.micVol(multimedia_id, mic_vol)
-- audio.debug(true)

--带TM8211的云喇叭开发板参考下面的配置
--[[
local multimedia_id = 0
local i2s_id = 0
local i2s_mode = 0
local i2s_sample_rate = 0
local i2s_bits_per_sample = 16
local i2s_channel_format = i2s.STEREO
local i2s_communication_format = i2s.MODE_MSB
local i2s_channel_bits = 16
local pa_pin = 25
local pa_on_level = 1
local pa_delay = 100
local power_pin = nil
local power_on_level = 1
local power_delay = 3
local power_time_delay = 0
-- local voice_vol = 200 --默认就不放大了
i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format,i2s_channel_bits)
audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin, power_on_level, power_time_delay)
audio.setBus(multimedia_id, audio.BUS_I2S,{chip = "tm8211", i2sid = i2s_id}) --通道0的硬件输出通道设置为I2S
-- audio.vol(multimedia_id, voice_vol)
]]
elseif bsp == "AIR105" then
-- Air105开发板支持DAC直接输出
Expand All @@ -129,6 +141,50 @@ sys.taskInit(audio_setup)
local function audio_task()
sys.waitUntil("AUDIO_READY")
local result

--下面为录音demo,根据适配情况选择性开启
-- local recordPath = "/record.amr"

-- -- 直接录音到文件
-- err = audio.record(0, audio.AMR, 5, 7, recordPath)
-- sys.waitUntil("AUDIO_RECORD_DONE")
-- log.info("record","录音结束")
-- result = audio.play(0, {recordPath})
-- while true do
-- msg = sysplus.waitMsg(taskName, nil)
-- if type(msg) == 'table' then
-- if msg[1] == MSG_PD then
-- log.info("播放结束")
-- break
-- end
-- else
-- log.error(type(msg), msg)
-- end
-- end

-- -- 录音到内存自行编码
-- encoder = codec.create(codec.AMR, false, 7)
-- print("encoder",encoder)
-- err = audio.record(0, audio.AMR, 5, 7)
-- sys.waitUntil("AUDIO_RECORD_DONE")
-- log.info("record","录音结束")
-- os.remove(recordPath)
-- io.writeFile(recordPath, "#!AMR\n")
-- io.writeFile(recordPath, amr_buff:query(), "a+b")

-- result = audio.play(0, {recordPath})
-- while true do
-- msg = sysplus.waitMsg(taskName, nil)
-- if type(msg) == 'table' then
-- if msg[1] == MSG_PD then
-- log.info("播放结束")
-- break
-- end
-- else
-- log.error(type(msg), msg)
-- end
-- end

-- amr 可播放采样率 8k/16k
local amrs = {"/luadb/alipay.amr", "/luadb/2.amr", "/luadb/10.amr", "/luadb/yuan.amr"}
-- 如需在同一个table内混播, 需要使用相同的采样率
Expand Down Expand Up @@ -166,8 +222,8 @@ local function audio_task()
if not audio.isEnd(0) then
log.info("手动关闭")
audio.playStop(0)
end
audio.pm(0,audio.STANDBY)
end
audio.pm(0,audio.STANDBY)
-- audio.pm(0,audio.SHUTDOWN) --低功耗可以选择SHUTDOWN或者POWEROFF,如果codec无法断电用SHUTDOWN
-- log.info(rtos.meminfo("sys"))
-- log.info(rtos.meminfo("lua"))
Expand Down

0 comments on commit eb60d08

Please sign in to comment.