Merge branch 'master' of https://gitee.com/openLuat/LuatOS
[LuatOS.git] / demo / multimedia / main.lua
blobd44f2c3a31dd8953188d96de6a9a6917bb0e27aa
2 -- LuaTools需要PROJECT和VERSION这两个信息
3 PROJECT = "audiotest"
4 VERSION = "2.0.1"
6 -- sys库是标配
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)
14 end
16 log.style(1)
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)
24 --创建一个amr的encoder
25 encoder = nil
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")
33 else
34 local succ,stop,file_cnt = audio.getError(0)
35 if not succ then
36 if stop then
37 log.info("用户停止播放")
38 else
39 log.info("第", file_cnt, "个文件解码失败")
40 end
41 end
42 -- log.info("播放完成一个音频")
43 sysplus.sendMsg(taskName, MSG_PD)
44 end
45 end)
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)
54 --如果用TM8211,打开下面的注释
55 -- i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
57 --如果用软件DAC,打开下面的注释
58 -- if audio.setBus then
59 -- audio.setBus(0, audio.BUS_SOFT_DAC)
60 -- end
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
66 local i2s_id = 0
67 local i2s_mode = 0
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
74 local pa_pin = 25
75 local pa_on_level = 1
76 local pa_delay = 100
77 local power_pin = 16 -- Air780EPVH 请修改成17
78 local power_on_level = 1
79 local power_delay = 3
80 local power_time_delay = 100
82 local voice_vol = 70
83 local mic_vol = 80
85 local find_es8311 = false
86 --内置8311的模块(比如air780epa, air780epvh)打开下面的代码
87 --power_pin = 255
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控制上下电
93 sys.wait(10)
94 pm.power(pm.LDO_CTL, true) --开发板上ES8311由LDO_CTL控制上下电
95 sys.wait(10)
96 if i2c.send(0, 0x18, 0xfd) == true then
97 log.info("音频小板或内置ES8311", "codec on i2c0")
98 i2c_id = 0
99 find_es8311 = true
100 else
101 if i2c.send(1, 0x18, 0xfd) == true then
102 log.info("云喇叭开发板", "codec on i2c1")
103 find_es8311 = true
104 power_pin = nil
105 i2c_id = 1
109 if not find_es8311 then
110 while true do
111 log.info("not find es8311")
112 sys.wait(1000)
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)
123 -- audio.debug(true)
125 --带TM8211的云喇叭开发板参考下面的配置
126 --[[
127 local multimedia_id = 0
128 local i2s_id = 0
129 local i2s_mode = 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
136 local pa_pin = 25
137 local pa_on_level = 1
138 local pa_delay = 100
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)
153 else
154 -- 其他板子未支持
155 while 1 do
156 log.info("audio", "尚未支持的BSP")
157 sys.wait(1000)
160 sys.publish("AUDIO_READY")
163 -- 配置好audio外设
164 sys.taskInit(audio_setup)
166 local function audio_task()
167 sys.waitUntil("AUDIO_READY")
168 local result
170 --下面为录音demo,根据适配情况选择性开启
171 -- local recordPath = "/record.amr"
173 -- -- 直接录音到文件
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})
178 -- while true do
179 -- msg = sysplus.waitMsg(taskName, nil)
180 -- if type(msg) == 'table' then
181 -- if msg[1] == MSG_PD then
182 -- log.info("播放结束")
183 -- break
184 -- end
185 -- else
186 -- log.error(type(msg), msg)
187 -- end
188 -- end
190 -- -- 录音到内存自行编码
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})
201 -- while true do
202 -- msg = sysplus.waitMsg(taskName, nil)
203 -- if type(msg) == 'table' then
204 -- if msg[1] == MSG_PD then
205 -- log.info("播放结束")
206 -- break
207 -- end
208 -- else
209 -- log.error(type(msg), msg)
210 -- end
211 -- end
213 -- amr 可播放采样率 8k/16k
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"}
220 local counter = 0
221 while true do
222 log.info("开始播放")
223 -- 两个列表前后播放
224 if rtos.bsp() == "AIR105" then
225 result = audio.play(0, "/luadb/test_32k.mp3")
226 else
227 result = audio.play(0, counter % 2 == 1 and amrs or mp3s)
229 counter = counter + 1
230 if result then
231 --等待音频通道的回调消息,或者切换歌曲的消息
232 while true do
233 msg = sysplus.waitMsg(taskName, nil)
234 if type(msg) == 'table' then
235 if msg[1] == MSG_PD then
236 log.info("播放结束")
237 break
239 else
240 log.error(type(msg), msg)
243 else
244 log.debug("解码失败!")
245 sys.wait(1000)
247 if not audio.isEnd(0) then
248 log.info("手动关闭")
249 audio.playStop(0)
251 audio.pm(0,audio.SHUTDOWN)
252 --低功耗测试打开下面的代码
253 --[[
254 audio.pm(0,audio.POWEROFF) --低功耗可以选择SHUTDOWN或者POWEROFF,如果codec无法断电用SHUTDOWN
255 pm.power(pm.USB, false)
256 mobile.flymode(0, true)
257 pm.request(pm.LIGHT)
258 sys.wait(20000)
259 mobile.flymode(0, false)
261 log.info(rtos.meminfo("sys"))
262 log.info(rtos.meminfo("lua"))
263 sys.wait(1000)
265 sysplus.taskDel(taskName)
268 sysplus.taskInitEx(audio_task, taskName, task_cb)
270 -- 用户代码已结束---------------------------------------------
271 -- 结尾总是这一句
272 sys.run()
273 -- sys.run()之后后面不要加任何语句!!!!!