exaudio.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 audio_play_queue = {
  64. data = {}, -- 存储字符串的数组
  65. sequenceIndex = 1 -- 用于跟踪插入顺序的索引
  66. }
  67. -- 向队列中添加字符串(按调用顺序插入)
  68. local function audio_play_queue_push(str)
  69. if type(str) == "string" then
  70. -- 存储格式: {index = 顺序索引, value = 字符串值}
  71. table.insert(audio_play_queue.data, {
  72. index = audio_play_queue.sequenceIndex,
  73. value = str
  74. })
  75. audio_play_queue.sequenceIndex = audio_play_queue.sequenceIndex + 1
  76. return true
  77. end
  78. return false
  79. end
  80. -- 从队列中取出最早插入的字符串(按顺序取出)
  81. local function audio_play_queue_pop()
  82. if #audio_play_queue.data > 0 then
  83. -- 取出并移除第一个元素
  84. local item = table.remove(audio_play_queue.data, 1)
  85. return item.value -- 返回值
  86. end
  87. return nil
  88. end
  89. -- 清空队列中所有数据
  90. function audio_queue_clear()
  91. -- 清空数组
  92. audio_play_queue.data = {}
  93. -- 重置顺序索引
  94. audio_play_queue.sequenceIndex = 1
  95. return true
  96. end
  97. -- 工具函数:参数检查
  98. local function check_param(param, expected_type, name)
  99. if type(param) ~= expected_type then
  100. log.error(string.format("参数错误: %s 应为 %s 类型", name, expected_type))
  101. return false
  102. end
  103. return true
  104. end
  105. -- 音频回调处理
  106. local function audio_callback(id, event, point)
  107. -- log.info("audio_callback", "event:", event,
  108. -- "MORE_DATA:", audio.MORE_DATA,
  109. -- "DONE:", audio.DONE,
  110. -- "RECORD_DATA:", audio.RECORD_DATA,
  111. -- "RECORD_DONE:", audio.RECORD_DONE)
  112. if event == audio.MORE_DATA then
  113. audio.write(MULTIMEDIA_ID,audio_play_queue_pop())
  114. elseif event == audio.DONE then
  115. if type(audio_play_param.cbfnc) == "function" then
  116. audio_play_param.cbfnc(exaudio.PLAY_DONE)
  117. end
  118. audio_queue_clear() -- 清空流式播放数据队列
  119. sys.publish(EX_MSG_PLAY_DONE)
  120. elseif event == audio.RECORD_DATA then
  121. if type(audio_record_param.path) == "function" then
  122. local buff, len = point == 0 and pcm_buff0 or pcm_buff1,
  123. point == 0 and pcm_buff0:used() or pcm_buff1:used()
  124. audio_record_param.path(buff, len)
  125. end
  126. elseif event == audio.RECORD_DONE then
  127. if type(audio_record_param.cbfnc) == "function" then
  128. audio_record_param.cbfnc(exaudio.RECORD_DONE)
  129. end
  130. end
  131. end
  132. -- 读取ES8311芯片ID
  133. local function read_es8311_id()
  134. -- 发送读取请求
  135. local send_ok = i2c.send(audio_setup_param.i2c_id, ES8311_ADDR, CHIP_ID_REG)
  136. if not send_ok then
  137. log.error("发送芯片ID读取请求失败")
  138. return false
  139. end
  140. -- 读取数据
  141. local data = i2c.recv(audio_setup_param.i2c_id, ES8311_ADDR, 1)
  142. if data and #data == 1 then
  143. return true
  144. end
  145. log.error("读取ES8311芯片ID失败")
  146. return false
  147. end
  148. -- 音频硬件初始化
  149. local function audio_setup()
  150. -- I2C配置
  151. if not i2c.setup(audio_setup_param.i2c_id, i2c.FAST) then
  152. log.error("I2C初始化失败")
  153. return false
  154. end
  155. -- 初始化I2S
  156. local result, data = i2s.setup(
  157. I2S_ID,
  158. I2S_MODE,
  159. I2S_SAMPLE_RATE,
  160. audio_setup_param.bits_per_sample,
  161. I2S_CHANNEL_FORMAT,
  162. I2S_COMM_FORMAT,
  163. I2S_CHANNEL_BITS
  164. )
  165. if not result then
  166. log.error("I2S设置失败")
  167. return false
  168. end
  169. -- 配置音频通道
  170. audio.config(
  171. MULTIMEDIA_ID,
  172. audio_setup_param.pa_ctrl,
  173. audio_setup_param.pa_on_level,
  174. audio_setup_param.dac_delay,
  175. audio_setup_param.pa_delay,
  176. audio_setup_param.dac_ctrl,
  177. 1, -- power_on_level
  178. audio_setup_param.dac_time_delay
  179. )
  180. -- 设置总线
  181. audio.setBus(
  182. MULTIMEDIA_ID,
  183. audio.BUS_I2S,
  184. {
  185. chip = audio_setup_param.model,
  186. i2cid = audio_setup_param.i2c_id,
  187. i2sid = I2S_ID,
  188. voltage = audio.VOLTAGE_1800
  189. }
  190. )
  191. -- 设置音量
  192. audio.vol(MULTIMEDIA_ID, voice_vol)
  193. audio.micVol(MULTIMEDIA_ID, mic_vol)
  194. audio.pm(MULTIMEDIA_ID, audio.RESUME)
  195. -- 检查芯片连接
  196. if audio_setup_param.model == "es8311" and not read_es8311_id() then
  197. log.error("ES8311通讯失败,请检查硬件")
  198. return false
  199. end
  200. -- 注册回调
  201. audio.on(MULTIMEDIA_ID, audio_callback)
  202. return true
  203. end
  204. -- 模块接口:初始化
  205. function exaudio.setup(audioConfigs)
  206. -- 检查必要参数
  207. if not audio then
  208. log.error("不支持audio 库,请选择支持audio 的core")
  209. return false
  210. end
  211. if not audioConfigs or type(audioConfigs) ~= "table" then
  212. log.error("配置参数必须为table类型")
  213. return false
  214. end
  215. -- 检查codec型号
  216. if not audioConfigs.model or
  217. (audioConfigs.model ~= "es8311" and audioConfigs.model ~= "es8211") then
  218. log.error("请指定正确的codec型号(es8311或es8211)")
  219. return false
  220. end
  221. audio_setup_param.model = audioConfigs.model
  222. -- 针对ES8311的特殊检查
  223. if audioConfigs.model == "es8311" then
  224. if not check_param(audioConfigs.i2c_id, "number", "i2c_id") then
  225. return false
  226. end
  227. audio_setup_param.i2c_id = audioConfigs.i2c_id
  228. end
  229. -- 检查功率放大器控制管脚
  230. if audioConfigs.pa_ctrl == nil then
  231. log.warn("pa_ctrl(功率放大器控制管脚)是控制pop 音的重要管脚,建议硬件设计加上")
  232. end
  233. audio_setup_param.pa_ctrl = audioConfigs.pa_ctrl
  234. -- 检查功率放大器控制管脚
  235. if audioConfigs.dac_ctrl == nil then
  236. log.warn("dac_ctrl(音频编解码控制管脚)是控制pop 音的重要管脚,建议硬件设计加上")
  237. end
  238. audio_setup_param.dac_ctrl = audioConfigs.dac_ctrl
  239. -- 处理可选参数
  240. local optional_params = {
  241. {name = "dac_delay", type = "number"},
  242. {name = "pa_delay", type = "number"},
  243. {name = "dac_time_delay", type = "number"},
  244. {name = "bits_per_sample", type = "number"},
  245. {name = "pa_on_level", type = "number"}
  246. }
  247. for _, param in ipairs(optional_params) do
  248. if audioConfigs[param.name] ~= nil then
  249. if check_param(audioConfigs[param.name], param.type, param.name) then
  250. audio_setup_param[param.name] = audioConfigs[param.name]
  251. else
  252. return false
  253. end
  254. end
  255. end
  256. -- 确保采样位数有默认值
  257. audio_setup_param.bits_per_sample = audio_setup_param.bits_per_sample or 16
  258. return audio_setup()
  259. end
  260. -- 模块接口:开始播放
  261. function exaudio.play_start(playConfigs)
  262. if not playConfigs or type(playConfigs) ~= "table" then
  263. log.error("播放配置必须为table类型")
  264. return false
  265. end
  266. -- 检查播放类型
  267. if not check_param(playConfigs.type, "number", "type") then
  268. log.error("type必须为数值(0:文件,1:TTS,2:流式)")
  269. return false
  270. end
  271. audio_play_param.type = playConfigs.type
  272. -- 处理优先级
  273. if playConfigs.priority ~= nil then
  274. if check_param(playConfigs.priority, "number", "priority") then
  275. if playConfigs.priority > audio_play_param.priority then
  276. log.error("是否完成播放",audio.isEnd(MULTIMEDIA_ID))
  277. if not audio.isEnd(MULTIMEDIA_ID) then
  278. if audio.play(MULTIMEDIA_ID) ~= true then
  279. return false
  280. end
  281. sys.waitUntil(EX_MSG_PLAY_DONE)
  282. end
  283. audio_play_param.priority = playConfigs.priority
  284. end
  285. else
  286. return false
  287. end
  288. end
  289. -- 处理不同播放类型
  290. local play_type = audio_play_param.type
  291. if play_type == 0 then -- 文件播放
  292. if not playConfigs.content then
  293. log.error("文件播放需要指定content(文件路径或路径表)")
  294. return false
  295. end
  296. local content_type = type(playConfigs.content)
  297. if content_type == "table" then
  298. for _, path in ipairs(playConfigs.content) do
  299. if type(path) ~= "string" then
  300. log.error("播放列表元素必须为字符串路径")
  301. return false
  302. end
  303. end
  304. elseif content_type ~= "string" then
  305. log.error("文件播放content必须为字符串或路径表")
  306. return false
  307. end
  308. audio_play_param.content = playConfigs.content
  309. if audio.play(MULTIMEDIA_ID, audio_play_param.content) ~= true then
  310. return false
  311. end
  312. elseif play_type == 1 then -- TTS播放
  313. if not audio.tts then
  314. log.error("本固件不支持TTS,请更换支持TTS 的固件")
  315. return false
  316. end
  317. if not check_param(playConfigs.content, "string", "content") then
  318. log.error("TTS播放content必须为字符串")
  319. return false
  320. end
  321. audio_play_param.content = playConfigs.content
  322. if audio.tts(MULTIMEDIA_ID, audio_play_param.content) ~= true then
  323. return false
  324. end
  325. elseif play_type == 2 then -- 流式播放
  326. if not check_param(playConfigs.sampling_rate, "number", "sampling_rate") then
  327. return false
  328. end
  329. if not check_param(playConfigs.sampling_depth, "number", "sampling_depth") then
  330. return false
  331. end
  332. audio_play_param.content = playConfigs.content
  333. audio_play_param.sampling_rate = playConfigs.sampling_rate
  334. audio_play_param.sampling_depth = playConfigs.sampling_depth
  335. if playConfigs.signed_or_unsigned ~= nil then
  336. audio_play_param.signed_or_unsigned = playConfigs.signed_or_unsigned
  337. end
  338. audio.start(
  339. MULTIMEDIA_ID,
  340. audio.PCM,
  341. 1,
  342. playConfigs.sampling_rate,
  343. playConfigs.sampling_depth,
  344. audio_play_param.signed_or_unsigned
  345. )
  346. -- 发送初始数据
  347. if audio.write(MULTIMEDIA_ID, string.rep("\0", 512)) ~= true then
  348. return false
  349. end
  350. end
  351. -- 处理回调函数
  352. if playConfigs.cbfnc ~= nil then
  353. if check_param(playConfigs.cbfnc, "function", "cbfnc") then
  354. audio_play_param.cbfnc = playConfigs.cbfnc
  355. else
  356. return false
  357. end
  358. else
  359. audio_play_param.cbfnc = nil
  360. end
  361. return true
  362. end
  363. -- 模块接口:流式播放数据写入
  364. function exaudio.play_stream_write(data)
  365. audio_play_queue_push(data)
  366. return true
  367. end
  368. -- 模块接口:停止播放
  369. function exaudio.play_stop()
  370. return audio.play(MULTIMEDIA_ID)
  371. end
  372. -- 模块接口:检查播放是否结束
  373. function exaudio.is_end()
  374. return audio.isEnd(MULTIMEDIA_ID)
  375. end
  376. -- 模块接口:获取错误信息
  377. function exaudio.get_error()
  378. return audio.getError(MULTIMEDIA_ID)
  379. end
  380. -- 模块接口:开始录音
  381. function exaudio.record_start(recodConfigs)
  382. if not recodConfigs or type(recodConfigs) ~= "table" then
  383. log.error("录音配置必须为table类型")
  384. return false
  385. end
  386. -- 检查录音格式
  387. if recodConfigs.format == nil or type(recodConfigs.format) ~= "number" or recodConfigs.format > 5 then
  388. log.error("请指定正确的录音格式")
  389. return false
  390. end
  391. audio_record_param.format = recodConfigs.format
  392. -- 处理录音时间
  393. if recodConfigs.time ~= nil then
  394. if check_param(recodConfigs.time, "number", "time") then
  395. audio_record_param.time = recodConfigs.time
  396. else
  397. return false
  398. end
  399. else
  400. audio_record_param.time = 0
  401. end
  402. -- 处理存储路径/回调
  403. if not recodConfigs.path then
  404. log.error("必须指定录音路径或流式回调函数")
  405. return false
  406. end
  407. audio_record_param.path = recodConfigs.path
  408. -- 转换录音格式
  409. local recod_format, amr_quailty
  410. if audio_record_param.format == exaudio.AMR_NB then
  411. recod_format = audio.AMR_NB
  412. amr_quailty = 7
  413. elseif audio_record_param.format == exaudio.AMR_WB then
  414. recod_format = audio.AMR_WB
  415. amr_quailty = 8
  416. elseif audio_record_param.format == exaudio.PCM_8000 then
  417. recod_format = 8000
  418. elseif audio_record_param.format == exaudio.PCM_16000 then
  419. recod_format = 16000
  420. elseif audio_record_param.format == exaudio.PCM_24000 then
  421. recod_format = 24000
  422. elseif audio_record_param.format == exaudio.PCM_32000 then
  423. recod_format = 32000
  424. end
  425. -- 处理回调函数
  426. if recodConfigs.cbfnc ~= nil then
  427. if check_param(recodConfigs.cbfnc, "function", "cbfnc") then
  428. audio_record_param.cbfnc = recodConfigs.cbfnc
  429. else
  430. return false
  431. end
  432. else
  433. audio_record_param.cbfnc = nil
  434. end
  435. -- 开始录音
  436. local path_type = type(audio_record_param.path)
  437. if path_type == "string" then
  438. return audio.record(
  439. MULTIMEDIA_ID,
  440. recod_format,
  441. audio_record_param.time,
  442. amr_quailty,
  443. audio_record_param.path
  444. )
  445. elseif path_type == "function" then
  446. -- 初始化缓冲区
  447. if not pcm_buff0 or not pcm_buff1 then
  448. pcm_buff0 = zbuff.create(16000)
  449. pcm_buff1 = zbuff.create(16000)
  450. end
  451. return audio.record(
  452. MULTIMEDIA_ID,
  453. recod_format,
  454. audio_record_param.time,
  455. amr_quailty,
  456. nil,
  457. 3,
  458. pcm_buff0,
  459. pcm_buff1
  460. )
  461. end
  462. log.error("录音路径必须为字符串或函数")
  463. return false
  464. end
  465. -- 模块接口:停止录音
  466. function exaudio.record_stop()
  467. return audio.recordStop(MULTIMEDIA_ID)
  468. end
  469. -- 模块接口:设置音量
  470. function exaudio.vol(play_volume)
  471. if check_param(play_volume, "number", "音量值") then
  472. return audio.vol(MULTIMEDIA_ID, play_volume)
  473. end
  474. return false
  475. end
  476. -- 模块接口:设置麦克风音量
  477. function exaudio.mic_vol(record_volume)
  478. if check_param(record_volume, "number", "麦克风音量值") then
  479. return audio.micVol(MULTIMEDIA_ID, record_volume)
  480. end
  481. return false
  482. end
  483. return exaudio