exaudio.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. --[[
  2. @module exaudio
  3. @summary exaudio扩展库
  4. @version 1.1
  5. @date 2025.09.01
  6. @author 梁健
  7. @usage
  8. ]]
  9. local exaudio = {}
  10. -- 常量定义
  11. local I2S_ID = 0
  12. local I2S_MODE = 0 -- 0:主机 1:从机
  13. local I2S_SAMPLE_RATE = 16000
  14. local I2S_CHANNEL_FORMAT = i2s.MONO_R
  15. local I2S_COMM_FORMAT = i2s.MODE_LSB -- 可选MODE_I2S, MODE_LSB, MODE_MSB
  16. local I2S_CHANNEL_BITS = 16
  17. local MULTIMEDIA_ID = 0
  18. local EX_MSG_PLAY_DONE = "playDone"
  19. local ES8311_ADDR = 0x18 -- 7位地址
  20. local CHIP_ID_REG = 0x00 -- 芯片ID寄存器地址
  21. -- 模块常量
  22. exaudio.PLAY_DONE = 1 -- 音频播放完毕的事件之一
  23. exaudio.RECORD_DONE = 1 -- 音频录音完毕的事件之一
  24. exaudio.AMR_NB = 0
  25. exaudio.AMR_WB = 1
  26. exaudio.PCM_8000 = 2
  27. exaudio.PCM_16000 = 3
  28. exaudio.PCM_24000 = 4
  29. exaudio.PCM_32000 = 5
  30. -- 默认配置参数
  31. local audio_setup_param = {
  32. model = "es8311", -- dac类型: "es8311","es8211"
  33. i2c_id = 0, -- i2c_id: 0,1
  34. pa_ctrl = 0, -- 音频放大器电源控制管脚
  35. dac_ctrl = 0, -- 音频编解码芯片电源控制管脚
  36. dac_delay = 3, -- DAC启动前冗余时间(100ms)
  37. pa_delay = 100, -- DAC启动后延迟打开PA的时间(ms)
  38. dac_time_delay = 600, -- 播放完毕后PA与DAC关闭间隔(ms)
  39. bits_per_sample = 16, -- 采样位数
  40. pa_on_level = 1 -- PA打开电平 1:高 0:低
  41. }
  42. local audio_play_param = {
  43. type = 0, -- 0:文件 1:TTS 2:流式
  44. content = nil, -- 内容/回调函数
  45. cbfnc = nil, -- 播放完毕回调
  46. priority = 0, -- 优先级(数值越大越高)
  47. sampling_rate = 16000, -- 采样率(仅流式)
  48. sampling_depth = 16, -- 采样位深(仅流式)
  49. signed_or_unsigned = true -- PCM是否有符号(仅流式)
  50. }
  51. local audio_record_param = {
  52. format = 0, -- 录制格式,支持exaudio.AMR_NB,exaudio.AMR_WB,exaudio.PCM_8000,exaudio.PCM_16000,exaudio.PCM_24000,exaudio.PCM_32000
  53. time = 5, -- 录制时间(秒)
  54. path = nil, -- 文件路径或流式回调
  55. cbfnc = nil -- 录音完毕回调
  56. }
  57. -- 内部变量
  58. local pcm_buff0 = nil
  59. local pcm_buff1 = nil
  60. local voice_vol = 55
  61. local mic_vol = 80
  62. -- 工具函数:参数检查
  63. local function check_param(param, expected_type, name)
  64. if type(param) ~= expected_type then
  65. log.error(string.format("参数错误: %s 应为 %s 类型", name, expected_type))
  66. return false
  67. end
  68. return true
  69. end
  70. -- 音频回调处理
  71. local function audio_callback(id, event, point)
  72. -- log.info("audio_callback", "event:", event,
  73. -- "MORE_DATA:", audio.MORE_DATA,
  74. -- "DONE:", audio.DONE,
  75. -- "RECORD_DATA:", audio.RECORD_DATA,
  76. -- "RECORD_DONE:", audio.RECORD_DONE)
  77. if event == audio.MORE_DATA then
  78. if type(audio_play_param.content) == "function" then
  79. local data = audio_play_param.content()
  80. if data ~= nil and type(data) == "string" then
  81. audio.write(MULTIMEDIA_ID,data)
  82. end
  83. end
  84. elseif event == audio.DONE then
  85. if type(audio_play_param.cbfnc) == "function" then
  86. audio_play_param.cbfnc(exaudio.PLAY_DONE)
  87. end
  88. sys.publish(EX_MSG_PLAY_DONE)
  89. elseif event == audio.RECORD_DATA then
  90. if type(audio_record_param.path) == "function" then
  91. local buff, len = point == 0 and pcm_buff0 or pcm_buff1,
  92. point == 0 and pcm_buff0:used() or pcm_buff1:used()
  93. audio_record_param.path(buff, len)
  94. end
  95. elseif event == audio.RECORD_DONE then
  96. if type(audio_record_param.cbfnc) == "function" then
  97. audio_record_param.cbfnc(exaudio.RECORD_DONE)
  98. end
  99. end
  100. end
  101. -- 读取ES8311芯片ID
  102. local function read_es8311_id()
  103. -- 发送读取请求
  104. local send_ok = i2c.send(audio_setup_param.i2c_id, ES8311_ADDR, CHIP_ID_REG)
  105. if not send_ok then
  106. log.error("发送芯片ID读取请求失败")
  107. return false
  108. end
  109. -- 读取数据
  110. local data = i2c.recv(audio_setup_param.i2c_id, ES8311_ADDR, 1)
  111. if data and #data == 1 then
  112. return true
  113. end
  114. log.error("读取ES8311芯片ID失败")
  115. return false
  116. end
  117. -- 音频硬件初始化
  118. local function audio_setup()
  119. -- I2C配置
  120. if not i2c.setup(audio_setup_param.i2c_id, i2c.FAST) then
  121. log.error("I2C初始化失败")
  122. return false
  123. end
  124. -- 初始化I2S
  125. local result, data = i2s.setup(
  126. I2S_ID,
  127. I2S_MODE,
  128. I2S_SAMPLE_RATE,
  129. audio_setup_param.bits_per_sample,
  130. I2S_CHANNEL_FORMAT,
  131. I2S_COMM_FORMAT,
  132. I2S_CHANNEL_BITS
  133. )
  134. if not result then
  135. log.error("I2S设置失败")
  136. return false
  137. end
  138. -- 配置音频通道
  139. audio.config(
  140. MULTIMEDIA_ID,
  141. audio_setup_param.pa_ctrl,
  142. audio_setup_param.pa_on_level,
  143. audio_setup_param.dac_delay,
  144. audio_setup_param.pa_delay,
  145. audio_setup_param.dac_ctrl,
  146. 1, -- power_on_level
  147. audio_setup_param.dac_time_delay
  148. )
  149. -- 设置总线
  150. audio.setBus(
  151. MULTIMEDIA_ID,
  152. audio.BUS_I2S,
  153. {
  154. chip = audio_setup_param.model,
  155. i2cid = audio_setup_param.i2c_id,
  156. i2sid = I2S_ID,
  157. voltage = audio.VOLTAGE_1800
  158. }
  159. )
  160. -- 设置音量
  161. audio.vol(MULTIMEDIA_ID, voice_vol)
  162. audio.micVol(MULTIMEDIA_ID, mic_vol)
  163. audio.pm(MULTIMEDIA_ID, audio.RESUME)
  164. -- 检查芯片连接
  165. if audio_setup_param.model == "es8311" and not read_es8311_id() then
  166. log.error("ES8311通讯失败,请检查硬件")
  167. return false
  168. end
  169. -- 注册回调
  170. audio.on(MULTIMEDIA_ID, audio_callback)
  171. return true
  172. end
  173. -- 模块接口:初始化
  174. function exaudio.setup(audioConfigs)
  175. -- 检查必要参数
  176. if not audio then
  177. log.error("不支持audio 库,请选择支持audio 的core")
  178. return false
  179. end
  180. if not audioConfigs or type(audioConfigs) ~= "table" then
  181. log.error("配置参数必须为table类型")
  182. return false
  183. end
  184. -- 检查codec型号
  185. if not audioConfigs.model or
  186. (audioConfigs.model ~= "es8311" and audioConfigs.model ~= "es8211") then
  187. log.error("请指定正确的codec型号(es8311或es8211)")
  188. return false
  189. end
  190. audio_setup_param.model = audioConfigs.model
  191. -- 针对ES8311的特殊检查
  192. if audioConfigs.model == "es8311" then
  193. if not check_param(audioConfigs.i2c_id, "number", "i2c_id") then
  194. return false
  195. end
  196. audio_setup_param.i2c_id = audioConfigs.i2c_id
  197. end
  198. -- 检查功率放大器控制管脚
  199. if audioConfigs.pa_ctrl == nil then
  200. log.warn("pa_ctrl(功率放大器控制管脚)是控制pop 音的重要管脚,建议硬件设计加上")
  201. end
  202. audio_setup_param.pa_ctrl = audioConfigs.pa_ctrl
  203. -- 检查功率放大器控制管脚
  204. if audioConfigs.dac_ctrl == nil then
  205. log.warn("dac_ctrl(音频编解码控制管脚)是控制pop 音的重要管脚,建议硬件设计加上")
  206. end
  207. audio_setup_param.dac_ctrl = audioConfigs.dac_ctrl
  208. -- 处理可选参数
  209. local optional_params = {
  210. {name = "dac_delay", type = "number"},
  211. {name = "pa_delay", type = "number"},
  212. {name = "dac_time_delay", type = "number"},
  213. {name = "bits_per_sample", type = "number"},
  214. {name = "pa_on_level", type = "number"}
  215. }
  216. for _, param in ipairs(optional_params) do
  217. if audioConfigs[param.name] ~= nil then
  218. if check_param(audioConfigs[param.name], param.type, param.name) then
  219. audio_setup_param[param.name] = audioConfigs[param.name]
  220. else
  221. return false
  222. end
  223. end
  224. end
  225. -- 确保采样位数有默认值
  226. audio_setup_param.bits_per_sample = audio_setup_param.bits_per_sample or 16
  227. return audio_setup()
  228. end
  229. -- 模块接口:开始播放
  230. function exaudio.play_start(playConfigs)
  231. if not playConfigs or type(playConfigs) ~= "table" then
  232. log.error("播放配置必须为table类型")
  233. return false
  234. end
  235. -- 检查播放类型
  236. if not check_param(playConfigs.type, "number", "type") then
  237. log.error("type必须为数值(0:文件,1:TTS,2:流式)")
  238. return false
  239. end
  240. audio_play_param.type = playConfigs.type
  241. -- 处理优先级
  242. if playConfigs.priority ~= nil then
  243. if check_param(playConfigs.priority, "number", "priority") then
  244. if playConfigs.priority > audio_play_param.priority then
  245. log.error("是否完成播放",audio.isEnd(MULTIMEDIA_ID))
  246. if not audio.isEnd(MULTIMEDIA_ID) then
  247. if audio.play(MULTIMEDIA_ID) ~= true then
  248. return false
  249. end
  250. sys.waitUntil(EX_MSG_PLAY_DONE)
  251. end
  252. audio_play_param.priority = playConfigs.priority
  253. end
  254. else
  255. return false
  256. end
  257. end
  258. -- 处理不同播放类型
  259. local play_type = audio_play_param.type
  260. if play_type == 0 then -- 文件播放
  261. if not playConfigs.content then
  262. log.error("文件播放需要指定content(文件路径或路径表)")
  263. return false
  264. end
  265. local content_type = type(playConfigs.content)
  266. if content_type == "table" then
  267. for _, path in ipairs(playConfigs.content) do
  268. if type(path) ~= "string" then
  269. log.error("播放列表元素必须为字符串路径")
  270. return false
  271. end
  272. end
  273. elseif content_type ~= "string" then
  274. log.error("文件播放content必须为字符串或路径表")
  275. return false
  276. end
  277. audio_play_param.content = playConfigs.content
  278. if audio.play(MULTIMEDIA_ID, audio_play_param.content) ~= true then
  279. return false
  280. end
  281. elseif play_type == 1 then -- TTS播放
  282. if not audio.tts then
  283. log.error("本固件不支持TTS,请更换支持TTS 的固件")
  284. return false
  285. end
  286. if not check_param(playConfigs.content, "string", "content") then
  287. log.error("TTS播放content必须为字符串")
  288. return false
  289. end
  290. audio_play_param.content = playConfigs.content
  291. if audio.tts(MULTIMEDIA_ID, audio_play_param.content) ~= true then
  292. return false
  293. end
  294. elseif play_type == 2 then -- 流式播放
  295. if not check_param(playConfigs.content, "function", "content") then
  296. log.error("流式播放content必须为回调函数")
  297. return false
  298. end
  299. if not check_param(playConfigs.sampling_rate, "number", "sampling_rate") then
  300. return false
  301. end
  302. if not check_param(playConfigs.sampling_depth, "number", "sampling_depth") then
  303. return false
  304. end
  305. audio_play_param.content = playConfigs.content
  306. audio_play_param.sampling_rate = playConfigs.sampling_rate
  307. audio_play_param.sampling_depth = playConfigs.sampling_depth
  308. if playConfigs.signed_or_unsigned ~= nil then
  309. audio_play_param.signed_or_unsigned = playConfigs.signed_or_unsigned
  310. end
  311. audio.start(
  312. MULTIMEDIA_ID,
  313. audio.PCM,
  314. 1,
  315. playConfigs.sampling_rate,
  316. playConfigs.sampling_depth,
  317. audio_play_param.signed_or_unsigned
  318. )
  319. -- 发送初始数据
  320. if audio.write(MULTIMEDIA_ID, string.rep("\0", 512)) ~= true then
  321. return false
  322. end
  323. end
  324. -- 处理回调函数
  325. if playConfigs.cbfnc ~= nil then
  326. if check_param(playConfigs.cbfnc, "function", "cbfnc") then
  327. audio_play_param.cbfnc = playConfigs.cbfnc
  328. else
  329. return false
  330. end
  331. else
  332. audio_play_param.cbfnc = nil
  333. end
  334. return true
  335. end
  336. -- 模块接口:停止播放
  337. function exaudio.play_stop()
  338. return audio.play(MULTIMEDIA_ID)
  339. end
  340. -- 模块接口:检查播放是否结束
  341. function exaudio.is_end()
  342. return audio.isEnd(MULTIMEDIA_ID)
  343. end
  344. -- 模块接口:获取错误信息
  345. function exaudio.get_error()
  346. return audio.getError(MULTIMEDIA_ID)
  347. end
  348. -- 模块接口:开始录音
  349. function exaudio.record_start(recodConfigs)
  350. if not recodConfigs or type(recodConfigs) ~= "table" then
  351. log.error("录音配置必须为table类型")
  352. return false
  353. end
  354. -- 检查录音格式
  355. if recodConfigs.format == nil or type(recodConfigs.format) ~= "number" or recodConfigs.format > 5 then
  356. log.error("请指定正确的录音格式")
  357. return false
  358. end
  359. audio_record_param.format = recodConfigs.format
  360. -- 处理录音时间
  361. if recodConfigs.time ~= nil then
  362. if check_param(recodConfigs.time, "number", "time") then
  363. audio_record_param.time = recodConfigs.time
  364. else
  365. return false
  366. end
  367. else
  368. audio_record_param.time = 0
  369. end
  370. -- 处理存储路径/回调
  371. if not recodConfigs.path then
  372. log.error("必须指定录音路径或流式回调函数")
  373. return false
  374. end
  375. audio_record_param.path = recodConfigs.path
  376. -- 转换录音格式
  377. local recod_format, amr_quailty
  378. if audio_record_param.format == exaudio.AMR_NB then
  379. recod_format = audio.AMR_NB
  380. amr_quailty = 7
  381. elseif audio_record_param.format == exaudio.AMR_WB then
  382. recod_format = audio.AMR_WB
  383. amr_quailty = 8
  384. elseif audio_record_param.format == exaudio.PCM_8000 then
  385. recod_format = 8000
  386. elseif audio_record_param.format == exaudio.PCM_16000 then
  387. recod_format = 16000
  388. elseif audio_record_param.format == exaudio.PCM_24000 then
  389. recod_format = 24000
  390. elseif audio_record_param.format == exaudio.PCM_32000 then
  391. recod_format = 32000
  392. end
  393. -- 处理回调函数
  394. if recodConfigs.cbfnc ~= nil then
  395. if check_param(recodConfigs.cbfnc, "function", "cbfnc") then
  396. audio_record_param.cbfnc = recodConfigs.cbfnc
  397. else
  398. return false
  399. end
  400. else
  401. audio_record_param.cbfnc = nil
  402. end
  403. -- 开始录音
  404. local path_type = type(audio_record_param.path)
  405. if path_type == "string" then
  406. return audio.record(
  407. MULTIMEDIA_ID,
  408. recod_format,
  409. audio_record_param.time,
  410. amr_quailty,
  411. audio_record_param.path
  412. )
  413. elseif path_type == "function" then
  414. -- 初始化缓冲区
  415. if not pcm_buff0 or not pcm_buff1 then
  416. pcm_buff0 = zbuff.create(16000)
  417. pcm_buff1 = zbuff.create(16000)
  418. end
  419. return audio.record(
  420. MULTIMEDIA_ID,
  421. recod_format,
  422. audio_record_param.time,
  423. amr_quailty,
  424. nil,
  425. 3,
  426. pcm_buff0,
  427. pcm_buff1
  428. )
  429. end
  430. log.error("录音路径必须为字符串或函数")
  431. return false
  432. end
  433. -- 模块接口:停止录音
  434. function exaudio.record_stop()
  435. return audio.recordStop(MULTIMEDIA_ID)
  436. end
  437. -- 模块接口:设置音量
  438. function exaudio.vol(play_volume)
  439. if check_param(number, "number", "音量值") then
  440. return audio.vol(MULTIMEDIA_ID, play_volume)
  441. end
  442. return false
  443. end
  444. -- 模块接口:设置麦克风音量
  445. function exaudio.mic_vol(record_volume)
  446. if check_param(number, "number", "麦克风音量值") then
  447. return audio.micVol(MULTIMEDIA_ID, record_volume)
  448. end
  449. return false
  450. end
  451. return exaudio