2 -- LuaTools需要PROJECT和VERSION这两个信息
7 _G
.sys
= require("sys")
8 _G
.sysplus
= require("sysplus")
11 -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
12 if rtos
.bsp() == "EC618" and pm
and pm
.PWK_MODE
then
13 pm
.power(pm
.PWK_MODE
, false)
17 local taskName
= "task_audio"
19 local MSG_MD
= "moreData" -- 播放缓存有空余
20 local MSG_PD
= "playDone" -- 播放完成所有数据
22 -- amr数据存放buffer,尽可能地给大一些
23 amr_buff
= zbuff
.create(20 * 1024)
27 audio
.on(0, function(id
, event
,buff
)
28 --使用play来播放文件时只有播放完成回调
29 if event
== audio
.RECORD_DATA
then -- 录音数据
30 codec
.encode(encoder
, buff
, amr_buff
)
31 elseif event
== audio
.RECORD_DONE
then -- 录音完成
32 sys
.publish("AUDIO_RECORD_DONE")
34 local succ
,stop
,file_cnt
= audio
.getError(0)
39 log.info("第", file_cnt
, "个文件解码失败")
42 -- log.info("播放完成一个音频")
43 sysplus
.sendMsg(taskName
, MSG_PD
)
47 function audio_setup()
48 local bsp
= rtos
.bsp()
49 if bsp
== "EC618" then
50 --Air780E开发板配套+音频扩展板. ES7149
51 --由于音频扩展板的PA是长供电的,有塔塔声音是正常的,做产品的话有额外的参考设计
52 i2s
.setup(0, 0, 0, 0, 0, i2s
.MODE_I2S
)
55 -- i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
58 -- if audio.setBus then
59 -- audio.setBus(0, audio.BUS_SOFT_DAC)
61 audio
.config(0, 25, 1, 3, 100)
62 elseif string.find(bsp
,"EC718") then
63 -- CORE+音频小板是这个配置/云喇叭开发板同为这个配置
64 local multimedia_id
= 0
65 local i2c_id
= 0 --云喇叭开发版是1
68 local i2s_sample_rate
= 16000
69 local i2s_bits_per_sample
= 16
70 local i2s_channel_format
= i2s
.MONO_R
71 local i2s_communication_format
= i2s
.MODE_LSB
72 local i2s_channel_bits
= 16
77 local power_pin
= 16 -- Air780EPVH 请修改成17
78 local power_on_level
= 1
80 local power_time_delay
= 100
85 local find_es8311
= false
86 --内置8311的模块(比如air780epa, air780epvh)打开下面的代码
88 --gpio.setup(17,1,nil,nil,4)
89 --自适应开发板,如果明确是I2C几就不用了
90 i2c
.setup(0, i2c
.FAST
)
91 i2c
.setup(1, i2c
.FAST
)
92 pm
.power(pm
.LDO_CTL
, false) --开发板上ES8311由LDO_CTL控制上下电
94 pm
.power(pm
.LDO_CTL
, true) --开发板上ES8311由LDO_CTL控制上下电
96 if i2c
.send(0, 0x18, 0xfd) == true then
97 log.info("音频小板或内置ES8311", "codec on i2c0")
101 if i2c
.send(1, 0x18, 0xfd) == true then
102 log.info("云喇叭开发板", "codec on i2c1")
109 if not find_es8311
then
111 log.info("not find es8311")
116 i2s
.setup(i2s_id
, i2s_mode
, i2s_sample_rate
, i2s_bits_per_sample
, i2s_channel_format
, i2s_communication_format
,i2s_channel_bits
)
118 audio
.config(multimedia_id
, pa_pin
, pa_on_level
, power_delay
, pa_delay
, power_pin
, power_on_level
, power_time_delay
)
119 audio
.setBus(multimedia_id
, audio
.BUS_I2S
,{chip
= "es8311",i2cid
= i2c_id
, i2sid
= i2s_id
}) --通道0的硬件输出通道设置为I2S
121 audio
.vol(multimedia_id
, voice_vol
)
122 audio
.micVol(multimedia_id
, mic_vol
)
125 --带TM8211的云喇叭开发板参考下面的配置
127 local multimedia_id = 0
130 local i2s_sample_rate = 0
131 local i2s_bits_per_sample = 16
132 local i2s_channel_format = i2s.STEREO
133 local i2s_communication_format = i2s.MODE_MSB
134 local i2s_channel_bits = 16
137 local pa_on_level = 1
139 local power_pin = nil
140 local power_on_level = 1
141 local power_delay = 3
142 local power_time_delay = 0
143 local voice_vol = 100
144 -- local voice_vol = 200 --默认就不放大了
145 i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format,i2s_channel_bits)
146 audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin, power_on_level, power_time_delay)
147 audio.setBus(multimedia_id, audio.BUS_I2S,{chip = "tm8211", i2sid = i2s_id}) --通道0的硬件输出通道设置为I2S
148 -- audio.vol(multimedia_id, voice_vol)
150 elseif bsp
== "AIR105" then
151 -- Air105开发板支持DAC直接输出
152 audio
.config(0, 25, 1, 3, 100)
156 log.info("audio", "尚未支持的BSP")
160 sys
.publish("AUDIO_READY")
164 sys
.taskInit(audio_setup
)
166 local function audio_task()
167 sys
.waitUntil("AUDIO_READY")
170 --下面为录音demo,根据适配情况选择性开启
171 -- local recordPath = "/record.amr"
174 -- err = audio.record(0, audio.AMR, 5, 7, recordPath)
175 -- sys.waitUntil("AUDIO_RECORD_DONE")
176 -- log.info("record","录音结束")
177 -- result = audio.play(0, {recordPath})
179 -- msg = sysplus.waitMsg(taskName, nil)
180 -- if type(msg) == 'table' then
181 -- if msg[1] == MSG_PD then
186 -- log.error(type(msg), msg)
191 -- encoder = codec.create(codec.AMR, false, 7)
192 -- print("encoder",encoder)
193 -- err = audio.record(0, audio.AMR, 5, 7)
194 -- sys.waitUntil("AUDIO_RECORD_DONE")
195 -- log.info("record","录音结束")
196 -- os.remove(recordPath)
197 -- io.writeFile(recordPath, "#!AMR\n")
198 -- io.writeFile(recordPath, amr_buff:query(), "a+b")
200 -- result = audio.play(0, {recordPath})
202 -- msg = sysplus.waitMsg(taskName, nil)
203 -- if type(msg) == 'table' then
204 -- if msg[1] == MSG_PD then
209 -- log.error(type(msg), msg)
214 local amrs
= {"/luadb/alipay.amr", "/luadb/2.amr", "/luadb/10.amr", "/luadb/yuan.amr"}
215 -- 如需在同一个table内混播, 需要使用相同的采样率
216 -- 此mp3为自由文件,无版权问题,合宙自录音频,若测试音质请使用其他高清mp3
217 -- local mp3s = {"/luadb/test_32k.mp3"}
218 -- ec618的固件需要用非full版本才能放下44k的MP3
219 local mp3s
= {"/luadb/test_44k.mp3"}
224 if rtos
.bsp() == "AIR105" then
225 result
= audio
.play(0, "/luadb/test_32k.mp3")
227 result
= audio
.play(0, counter
% 2 == 1 and amrs
or mp3s
)
229 counter
= counter
+ 1
231 --等待音频通道的回调消息,或者切换歌曲的消息
233 msg
= sysplus
.waitMsg(taskName
, nil)
234 if type(msg
) == 'table' then
235 if msg
[1] == MSG_PD
then
240 log.error(type(msg
), msg
)
247 if not audio
.isEnd(0) then
251 audio
.pm(0,audio
.SHUTDOWN
)
254 audio.pm(0,audio.POWEROFF) --低功耗可以选择SHUTDOWN或者POWEROFF,如果codec无法断电用SHUTDOWN
255 pm.power(pm.USB, false)
256 mobile.flymode(0, true)
259 mobile.flymode(0, false)
261 log.info(rtos
.meminfo("sys"))
262 log.info(rtos
.meminfo("lua"))
265 sysplus
.taskDel(taskName
)
268 sysplus
.taskInitEx(audio_task
, taskName
, task_cb
)
270 -- 用户代码已结束---------------------------------------------
273 -- sys.run()之后后面不要加任何语句!!!!!