audio.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. --- 模块功能:音频播放.
  2. -- 支持MP3、amr文件播放;
  3. -- 支持本地TTS播放、通话中TTS播放到对端(需要使用支持TTS功能的core软件)
  4. -- @module audio
  5. -- @author openLuat
  6. -- @license MIT
  7. -- @copyright openLuat
  8. -- @release 2018.3.19
  9. require "common"
  10. require "misc"
  11. require "utils"
  12. module(..., package.seeall)
  13. local req = ril.request
  14. local stopCbFnc
  15. --tts速度,默认50
  16. local ttsSpeed = 50
  17. --喇叭音量和mic音量等级
  18. local sVolume,sMicVolume = 4,1
  19. local sCallVolume = 4
  20. --音频播放的协程ID
  21. local taskID
  22. --播放和停止请求队列,用于存储通过调用audio.play和audio.stop接口允许播放和停止播放的请求项
  23. --每个播放请求项为table类型,数据结构如下(参考本文件中的play接口注释)
  24. --priority:播放优先级
  25. --type:播放类型
  26. --path:播放音频内容
  27. --vol:播放音量
  28. --cbFnc:播放结束后的回调函数
  29. --dup:是否重复播放
  30. --dupInterval:重复播放的间隔,单位毫秒
  31. --每个停止请求项为table类型,数据结构如下(参考本文件中的stop接口注释)
  32. --type:固定为"STOP"
  33. --cbFnc:停止播放后的回调函数
  34. local audioQueue = {}
  35. --sStrategy:优先级相同时的播放策略,0(表示继续播放正在播放的音频,忽略请求播放的新音频),1(表示停止正在播放的音频,播放请求播放的新音频)
  36. local sStrategy
  37. local function isTtsStopResultValid()
  38. local version = string.match(rtos.get_version(),"(%d+)_RDA")
  39. if version then
  40. return tonumber(version)>=8
  41. else
  42. return false
  43. end
  44. end
  45. local function handleCb(item,result)
  46. log.info("audio.handleCb",item.cbFnc,result)
  47. if item.cbFnc then item.cbFnc(result) end
  48. table.remove(audioQueue,1)
  49. end
  50. local function handlePlayInd(item,key,value)
  51. log.info("audio.handlePlayInd",key,value)
  52. --播放结束
  53. if key=="RESULT" then
  54. --播放成功
  55. if value then
  56. if item.dup then
  57. if item.dupInterval>0 then
  58. log.info("audio.handlePlayInd",item.type,"dup wait LIB_AUDIO_PLAY_IND or timeout",item.dupInterval)
  59. local result,reason = sys.waitUntil("LIB_AUDIO_PLAY_IND",item.dupInterval)
  60. log.info("audio.handlePlayInd",item.type,"dup wait",reason or "timeout")
  61. if result then
  62. log.warn("audio.handlePlayInd",item.type,"dup wait error",reason)
  63. handleCb(item,reason=="NEW" and 4 or 5)
  64. end
  65. end
  66. else
  67. handleCb(item,0)
  68. end
  69. --播放失败
  70. else
  71. log.warn("audio.handlePlayInd",item.type,"play cnf error")
  72. handleCb(item,1)
  73. end
  74. --新的优先级更高的播放请求
  75. elseif key=="NEW" then
  76. log.warn("audio.handlePlayInd",item.type,"priority error")
  77. handleCb(item,4)
  78. --主动调用audio.stop
  79. elseif key=="STOP" then
  80. log.warn("audio.handlePlayInd",item.type,"stop error",result)
  81. handleCb(item,5)
  82. end
  83. end
  84. local ttsEngineInited
  85. local audioTaskWaitPlayEntry
  86. local function audioTask()
  87. while true do
  88. if #audioQueue==0 then
  89. log.info("audioTask","wait LIB_AUDIO_PLAY_ENTRY")
  90. audioTaskWaitPlayEntry = true
  91. sys.waitUntil("LIB_AUDIO_PLAY_ENTRY")
  92. audioTaskWaitPlayEntry = false
  93. end
  94. local item = audioQueue[1]
  95. log.info("audioTask",item.type,"#audioQueue",#audioQueue)
  96. if item.type=="FILE" then
  97. --队列中有优先级高的请求等待处理
  98. if #audioQueue>1 then
  99. log.warn("audioTask",item.type,"priority low")
  100. local behind = audioQueue[2]
  101. handleCb(item,behind.type=="STOP" and 5 or 4)
  102. else
  103. setVolume(item.vol)
  104. local result
  105. if type(item.path)=="table" then
  106. if (item.path[1]):match("%.amr$") or (item.path[1]):match("%.AMR$") then
  107. local dataBuf = {}
  108. for i=1,#item.path do
  109. table.insert(dataBuf,(io.readFile(item.path[i])):sub(i==1 and 1 or 7,-1))
  110. end
  111. result = audiocore.playdata(table.concat(dataBuf),audiocore.AMR)
  112. elseif (item.path[1]):match("%.pcm$") or (item.path[1]):match("%.PCM$") then
  113. local dataBuf = {}
  114. for i=1,#item.path do
  115. table.insert(dataBuf,io.readFile(item.path[i]))
  116. end
  117. result = audiocore.playdata(table.concat(dataBuf),audiocore.PCM)
  118. elseif (item.path[1]):match("%.mp3$") or (item.path[1]):match("%.MP3$") then
  119. local dataBuf = {}
  120. for i=1,#item.path do
  121. table.insert(dataBuf,io.readFile(item.path[i]))
  122. end
  123. result = audiocore.playdata(table.concat(dataBuf),audiocore.MP3)
  124. else
  125. result = false
  126. end
  127. --result = audiocore.play(unpack(item.path))
  128. else
  129. result = audiocore.play(item.path)
  130. end
  131. if result then
  132. --等待三种消息(播放结束、主动调用audio.stop、新的优先级更高的播放请求)
  133. log.info("audioTask",item.type,"wait LIB_AUDIO_PLAY_IND")
  134. local _,key,value = sys.waitUntil("LIB_AUDIO_PLAY_IND")
  135. log.info("audioTask",item.type,"recv LIB_AUDIO_PLAY_IND",key,value)
  136. audiocore.stop()
  137. handlePlayInd(item,key,value)
  138. else
  139. log.warn("audioTask",item.type,"audiocore.play error")
  140. audiocore.stop()
  141. handleCb(item,1)
  142. end
  143. end
  144. elseif item.type=="TTS" or item.type=="TTSCC" then
  145. --队列中有优先级高的请求等待处理
  146. if #audioQueue>1 then
  147. log.warn("audioTask",item.type,"priority low")
  148. local behind = audioQueue[2]
  149. handleCb(item,behind.type=="STOP" and 5 or 4)
  150. else
  151. setVolume(item.vol)
  152. if item.type=="TTS" then
  153. if not ttsEngineInited then
  154. ttsply.initEngine()
  155. ttsEngineInited = true
  156. end
  157. ttsply.setParm(0,ttsSpeed)
  158. --队列中有优先级高的请求等待处理
  159. if #audioQueue>1 then
  160. log.warn("audioTask",item.type,"priority low1")
  161. if isTtsStopResultValid() then
  162. if ttsply.stop() then
  163. sys.waitUntil("LIB_AUDIO_PLAY_IND",2000)
  164. end
  165. else
  166. ttsply.stop()
  167. sys.waitUntil("LIB_AUDIO_PLAY_IND",500)
  168. end
  169. local behind = audioQueue[2]
  170. handleCb(item,behind.type=="STOP" and 5 or 4)
  171. else
  172. ttsply.play(common.utf8ToGb2312(item.path))
  173. --等待三种消息(播放结束、主动调用audio.stop、新的优先级更高的播放请求)
  174. log.info("audioTask",item.type,"wait LIB_AUDIO_PLAY_IND")
  175. local _,key,value = sys.waitUntil("LIB_AUDIO_PLAY_IND")
  176. log.info("audioTask",item.type,"recv LIB_AUDIO_PLAY_IND",key,value)
  177. if item.type=="TTS" then
  178. if isTtsStopResultValid() then
  179. --log.info("tts 1")
  180. if ttsply.stop() then
  181. --log.info("tts 2")
  182. sys.waitUntil("LIB_AUDIO_PLAY_IND",2000)
  183. end
  184. --log.info("tts 3")
  185. else
  186. ttsply.stop()
  187. sys.waitUntil("LIB_AUDIO_PLAY_IND",500)
  188. end
  189. else
  190. end
  191. handlePlayInd(item,key,value)
  192. end
  193. else
  194. end
  195. end
  196. elseif item.type=="RECORD" then
  197. --队列中有优先级高的请求等待处理
  198. if #audioQueue>1 then
  199. log.warn("audioTask",item.type,"priority low")
  200. local behind = audioQueue[2]
  201. handleCb(item,behind.type=="STOP" and 5 or 4)
  202. else
  203. setVolume(item.vol)
  204. f,d=record.getSize()
  205. req("AT+AUDREC=1,0,2,"..item.path..","..d*1000)
  206. --等待三种消息(播放结束、主动调用audio.stop、新的优先级更高的播放请求)
  207. log.info("audioTask",item.type,"wait LIB_AUDIO_PLAY_IND")
  208. local _,key,value = sys.waitUntil("LIB_AUDIO_PLAY_IND")
  209. log.info("audioTask",item.type,"recv LIB_AUDIO_PLAY_IND",key,value)
  210. req("AT+AUDREC=1,0,3,"..item.path..","..d*1000)
  211. sys.waitUntil("LIB_AUDIO_RECORD_STOP_RESULT")
  212. handlePlayInd(item,key,value)
  213. end
  214. elseif item.type=="STOP" then
  215. if item.cbFnc then item.cbFnc(0) end
  216. table.remove(audioQueue,1)
  217. end
  218. end
  219. end
  220. --- 播放音频
  221. -- @number priority,音频优先级,数值越大,优先级越高
  222. -- 优先级高的播放请求会终止优先级低的播放
  223. -- 相同优先级的播放请求,播放策略参考:audio.setStrategy接口
  224. -- @string type,音频类型,目前仅支持"FILE"、"TTS"
  225. -- @string path,音频文件路径,跟typ有关
  226. -- typ为"FILE"时:表示音频文件路径
  227. -- typ为"TTS"时:表示要播放的UTF8编码格式的数据
  228. -- @number[opt=4] vol,播放音量,取值范围0到7,0为静音
  229. -- @function[opt=nil] cbFnc,音频播放结束时的回调函数,回调函数的调用形式如下:
  230. -- cbFnc(result)
  231. -- result表示播放结果:
  232. -- 0-播放成功结束;
  233. -- 1-播放出错
  234. -- 2-播放优先级不够,没有播放
  235. -- 3-传入的参数出错,没有播放
  236. -- 4-被新的播放请求中止
  237. -- 5-调用audio.stop接口主动停止
  238. -- @bool[opt=nil] dup,是否循环播放,true循环,false或者nil不循环
  239. -- @number[opt=0] dupInterval,循环播放间隔(单位毫秒),dup为true时,此值才有意义
  240. -- @return result,bool或者nil类型,同步调用成功返回true,否则返回false
  241. -- @usage audio.play(0,"FILE","/lua/call.mp3")
  242. -- @usage audio.play(0,"FILE","/lua/call.mp3",7)
  243. -- @usage audio.play(0,"FILE","/lua/call.mp3",7,cbFnc)
  244. -- @usage 更多用法参考demo/audio/testAudio.lua
  245. function play(priority,type,path,vol,cbFnc,dup,dupInterval)
  246. log.info("audio.play",priority,type,path,vol,cbFnc,dup,dupInterval)
  247. if not taskID then
  248. taskID = sys.taskInit(audioTask)
  249. end
  250. local item = {priority=priority,type=type,path=path,vol=vol or 4,cbFnc=cbFnc,dup=dup,dupInterval=dupInterval or 0}
  251. if #audioQueue==0 then
  252. table.insert(audioQueue,item)
  253. sys.publish("LIB_AUDIO_PLAY_ENTRY")
  254. else
  255. local front = audioQueue[#audioQueue]
  256. if front.type=="STOP" then
  257. table.insert(audioQueue,item)
  258. else
  259. if priority>front.priority or (priority==front.priority and sStrategy==1) then
  260. table.insert(audioQueue,item)
  261. if not audioTaskWaitPlayEntry then
  262. sys.publish("LIB_AUDIO_PLAY_IND","NEW")
  263. end
  264. else
  265. log.warn("audio.play","priority error")
  266. if cbFnc then cbFnc(2) end
  267. end
  268. end
  269. end
  270. return true
  271. end
  272. --- 停止音频播放
  273. -- @function[opt=nil] cbFnc,停止音频播放的回调函数(停止结果通过此函数通知用户),回调函数的调用形式为:
  274. -- cbFnc(result)
  275. -- result:number类型
  276. -- 0表示停止成功
  277. -- @return nil
  278. -- @usage audio.stop()
  279. function stop(cbFnc)
  280. log.info("audio.stop",cbFnc)
  281. if #audioQueue==0 then
  282. if cbFnc then cbFnc(0) end
  283. else
  284. table.insert(audioQueue,{type="STOP",cbFnc=cbFnc})
  285. sys.publish("LIB_AUDIO_PLAY_IND","STOP")
  286. end
  287. end
  288. local function audioMsg(msg)
  289. --log.info("audio.MSG_AUDIO",msg.play_end_ind,msg.play_error_ind)
  290. sys.publish("LIB_AUDIO_PLAY_IND","RESULT",msg.play_end_ind)
  291. end
  292. --注册core上报的rtos.MSG_AUDIO消息的处理函数
  293. rtos.on(rtos.MSG_AUDIO,audioMsg)
  294. rtos.on(rtos.MSG_TTSPLY_STATUS, function() log.info("rtos.MSG_TTSPLY_STATUS") sys.publish("LIB_AUDIO_PLAY_IND","RESULT",true) end)
  295. rtos.on(rtos.MSG_TTSPLY_ERROR, function() log.info("rtos.MSG_TTSPLY_ERROR") sys.publish("LIB_AUDIO_PLAY_IND","RESULT",false) end)
  296. --- 设置喇叭音量等级
  297. -- @number vol,音量值为0-7,0为静音
  298. -- @return bool result,设置成功返回true,失败返回false
  299. -- @usage audio.setVolume(7)
  300. function setVolume(vol)
  301. local result = audiocore.setvol(vol)
  302. if result == 1 then
  303. result = true
  304. elseif result == 0 then
  305. result = false
  306. end
  307. if result then sVolume = vol end
  308. return result
  309. end
  310. --- 设置通话音量等级
  311. -- @number vol,音量值为0-7,0为静音
  312. -- @return bool result,设置成功返回true,失败返回false
  313. -- @usage audio.setCallVolume(7)
  314. function setCallVolume(vol)
  315. --local result = audiocore.setsphvol(vol)
  316. --if result then sCallVolume = vol end
  317. --return result
  318. audiocore.setsphvol(vol)
  319. sCallVolume = vol
  320. return true
  321. end
  322. -- 设置麦克音量等级
  323. -- @number vol,音量值为0-15,0为静音
  324. -- @return bool result,设置成功返回true,失败返回false
  325. -- @usage audio.setMicVolume(14)
  326. function setMicVolume(vol)
  327. ril.request("AT+CMIC="..audiocore.LOUDSPEAKER..","..vol)
  328. return true
  329. end
  330. ril.regRsp("+CMIC",function(cmd,success)
  331. if success then
  332. sMicVolume = tonumber(cmd:match("CMIC=%d+,(%d+)"))
  333. end
  334. end)
  335. --- 设置mic增益等级
  336. -- 通话时mic增益在通话建立成功之后设置才有效
  337. -- 录音mic增益设置后实时生效
  338. -- @string mode,增益类型
  339. -- "call"表示通话中mic增益
  340. -- "record"表示录音mic增益
  341. -- @number level,增益等级,取值为0-7
  342. -- @return bool result,设置成功返回true,失败返回false
  343. -- @usage audio.setMicGain("record",7),设置录音时mic增益为7级
  344. function setMicGain(mode, level)
  345. if (mode ~= "call" and mode ~= "record") or (level > 7 and level < 0) then
  346. return false
  347. else
  348. local gainHex
  349. if level == 7 then
  350. gainHex = string.format("%02X%02X%02X%02X", 7, 0, 15, 0)
  351. else
  352. gainHex = string.format("%02X%02X%02X%02X", level, 0, level * 2, 0)
  353. end
  354. if mode == "call" then
  355. ril.request("AT+CACCP=5,1,0," .. gainHex)
  356. ril.request("AT+CACCP=0,1,0," .. gainHex)
  357. elseif mode == "record" then
  358. ril.request("AT+CACCP=2,1,6," .. gainHex)
  359. end
  360. return true
  361. end
  362. end
  363. --- 获取喇叭音量等级
  364. -- @return number vol,喇叭音量等级
  365. -- @usage audio.getVolume()
  366. function getVolume()
  367. return sVolume
  368. end
  369. --- 获取通话音量等级
  370. -- @return number vol,通话音量等级
  371. -- @usage audio.getCallVolume()
  372. function getCallVolume()
  373. return sCallVolume
  374. end
  375. -- 获取麦克音量等级
  376. -- @return number vol,麦克音量等级
  377. -- @usage audio.getMicVolume()
  378. function getMicVolume()
  379. return sMicVolume
  380. end
  381. --- 设置优先级相同时的播放策略
  382. -- @number strategy,优先级相同时的播放策略;
  383. -- 0:表示继续播放正在播放的音频,忽略请求播放的新音频
  384. -- 1:表示停止正在播放的音频,播放请求播放的新音频
  385. -- @return nil
  386. -- @usage audio.setStrategy(0)
  387. -- @usage audio.setStrategy(1)
  388. function setStrategy(strategy)
  389. sStrategy=strategy
  390. end
  391. --- 设置TTS朗读速度
  392. -- @number speed,速度范围为0-100,默认50
  393. -- @return bool result,设置成功返回true,失败返回false
  394. -- @usage audio.setTTSSpeed(70)
  395. function setTTSSpeed(speed)
  396. if type(speed) == "number" and speed >= 0 and speed <= 100 then
  397. ttsSpeed = speed
  398. return true
  399. end
  400. end
  401. --- 设置音频输入、输出通道
  402. -- 设置后实时生效
  403. -- @number[opt=2] output,0:earphone听筒 1:headphone耳机 2:speaker喇叭
  404. -- @number[opt=0] input, 0:主mic 3:耳机mic
  405. -- @return nil
  406. -- @usage
  407. -- 设置为听筒输出:audio.setChannel(0)
  408. -- 设置为耳机输出:audio.setChannel(1)
  409. -- 设置为喇叭输出:audio.setChannel(2)
  410. -- 设置为喇叭输出、耳机mic输入:audio.setChannel(2,3)
  411. function setChannel(output, input)
  412. local version = string.match(rtos.get_version(), "(%d+)_RDA")
  413. if not version or tonumber(version) >= 9 then --匹配不到,兼容其它版本 或者大于版本9
  414. audiocore.setchannel(output or 2, input or 0)
  415. else
  416. ril.request("AT+AUDCH="..(output==1 and 1 or 2))
  417. end
  418. end
  419. --默认音频通道设置为LOUDSPEAKER,因为目前的模块只支持LOUDSPEAKER通道
  420. audiocore.setchannel(audiocore.LOUDSPEAKER)
  421. --默认音量等级设置为4级,4级是中间等级,最低为0级,最高为7级
  422. setVolume(sVolume)
  423. setCallVolume(sCallVolume)
  424. --默认MIC音量等级设置为1级,最低为0级,最高为15级
  425. setMicVolume(sMicVolume)