es8311_loop.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. audio.on(0, function(id, event)
  2. --使用play来播放文件时只有播放完成回调
  3. local succ,stop,file_cnt = audio.getError(0)
  4. if not succ then
  5. if stop then
  6. log.info("用户停止播放")
  7. else
  8. log.info("第", file_cnt, "个文件解码失败")
  9. end
  10. end
  11. -- log.info("播放完成一个音频")
  12. sys.publish("AUDIO_PLAY_DONE")
  13. end)
  14. -- es8311器件地址
  15. local es8311_address = 0x18
  16. local record_cnt = 0
  17. -- es8311初始化寄存器配置
  18. local es8311_reg = {
  19. {0x45,0x00},
  20. {0x01,0x30},
  21. {0x02,0x10},
  22. {0x02,0x00},
  23. {0x03,0x10},
  24. {0x16,0x24},
  25. {0x04,0x20},
  26. {0x05,0x00},
  27. {0x06,3},
  28. {0x07,0x00},
  29. {0x08,0xFF},
  30. {0x09,0x0C},
  31. {0x0A,0x0C},
  32. {0x0B,0x00},
  33. {0x0C,0x00},
  34. {0x10,0x03},
  35. {0x11,0x7F},
  36. {0x00,0x80},
  37. {0x0D,0x01},
  38. {0x01,0x3F},
  39. {0x14,0x1a},
  40. {0x12,0x28},
  41. {0x13,0x00},
  42. {0x0E,0x02},
  43. {0x0F,0x44},
  44. {0x15,0x00},
  45. {0x1B,0x0A},
  46. {0x1C,0x6A},
  47. {0x37,0x48},
  48. {0x44,(0 <<7)},
  49. {0x17,210},
  50. {0x32,200},
  51. }
  52. -- i2s数据接收buffer
  53. local rx_buff = zbuff.create(3200)
  54. -- amr数据存放buffer,尽可能地给大一些,对于MR475编码等级来说,一秒文件大小为0.6k左右,理论上录音五秒需要的空间为5 * 0.6 = 3k, 这里尽可能给大一点
  55. local amr_buff = zbuff.create(5 * 1024)
  56. --创建一个amr的encoder
  57. local encoder = codec.create(codec.AMR, false)
  58. -- 录音文件路径
  59. local recordPath = "/record.amr"
  60. -- i2s数据接收回调
  61. local function record_cb(id, buff)
  62. if buff then
  63. log.info("I2S", id, "接收了", rx_buff:used())
  64. codec.encode(encoder, rx_buff, amr_buff) -- 对录音数据进行amr编码,成功的话这个接口会返回true, 默认编码等级为MR475
  65. record_cnt = record_cnt + 1
  66. if record_cnt >= 25 then --超过5秒后停止
  67. log.info("I2S", "stop")
  68. i2s.stop(0)
  69. end
  70. end
  71. end
  72. ---- MultipartForm上传文件
  73. -- url string 请求URL地址
  74. -- filename string 上传服务器的文件名
  75. -- filePath string 待上传文件的路径
  76. local function postMultipartFormData(url, filename, filePath)
  77. local boundary = "----WebKitFormBoundary"..os.time()
  78. local req_headers = {
  79. ["Content-Type"] = "multipart/form-data; boundary="..boundary,
  80. }
  81. local body = {}
  82. table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\"file\"; filename=\"".. filename .."\"\r\n\r\n")
  83. table.insert(body, io.readFile(filePath))
  84. table.insert(body, "\r\n")
  85. table.insert(body, "--"..boundary.."--\r\n")
  86. body = table.concat(body)
  87. log.info("headers: ", "\r\n" .. json.encode(req_headers), type(body))
  88. log.info("body: " .. body:len() .. "\r\n" .. body)
  89. local code, headers, body = http.request("POST",url,
  90. req_headers,
  91. body
  92. ).wait()
  93. log.info("http.post", code, headers, body)
  94. end
  95. local function record_task()
  96. os.remove(recordPath)
  97. gpio.setup(26, 1) -- 打开录音开发板mic供电
  98. audio.config(0, 25, 1, 6, 200)
  99. -- audio.debug(true)
  100. pm.power(pm.DAC_EN, true) -- 打开es8311芯片供电
  101. log.info("i2c initial",i2c.setup(0, i2c.FAST)) -- 开启i2c
  102. for i, v in pairs(es8311_reg) do -- 初始化es8311
  103. i2c.send(0,es8311_address,v,1)
  104. end
  105. i2s.setup(0, 0, 8000, 16, 1, i2s.MODE_I2S) -- 开启i2s
  106. i2s.on(0, record_cb) -- 注册i2s接收回调
  107. i2s.recv(0, rx_buff, 3200)
  108. i2c.send(0,es8311_address,{0x00, 0xc0},1)
  109. sys.wait(6000)
  110. i2c.send(0, es8311_address,{0x00, 0x80},1) -- ES8311停止录音
  111. log.info("录音5秒结束")
  112. io.writeFile(recordPath, "#!AMR\n") -- 向文件写入amr文件标识数据
  113. io.writeFile(recordPath, amr_buff:query(), "a+b") -- 向文件写入编码后的amr数据
  114. i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
  115. local result = audio.play(0, {recordPath}) -- 请求音频播放
  116. if result then
  117. sys.waitUntil("AUDIO_PLAY_DONE")
  118. log.info("音频播放结果", result) -- 等待音频播放完毕
  119. else
  120. -- 音频播放出错
  121. end
  122. i2c.close(0)
  123. gpio.setup(26, 0) --录音完毕关闭mic和es8311电源,关闭i2c
  124. pm.power(pm.DAC_EN, false)
  125. -- uart.setup(1, 115200) -- 开启串口1
  126. -- uart.write(1, io.readFile(recordPath))
  127. -- 下面的演示是将音频文件发送到服务器上,如有需要,可以将下面代码注释打开,这里的url是合宙的文件上传测试服务器,上传的文件到http://tools.openluat.com/tools/device-upload-test查看
  128. --[[
  129. local timeTable = os.date("*t", os.time())
  130. local nowTime = string.format("%4d%02d%02d_%02d%02d%02d", timeTable.year, timeTable.month, timeTable.day, timeTable.hour, timeTable.min, timeTable.sec)
  131. local filename = mobile.imei() .. "_" .. nowTime .. ".amr"
  132. postMultipartFormData("http://tools.openluat.com/api/site/device_upload_file", filename, recordPath)
  133. ]]
  134. while true do
  135. sys.wait(2000)
  136. record_cnt = 0 --录音开始清除amr数据存放buffer数据,设置光标位置,设置录音时间归零,删除文件系统录音文件,打开mic和es8311电源
  137. amr_buff:clear(0)
  138. amr_buff:seek(0)
  139. os.remove(recordPath)
  140. gpio.setup(26, 1)
  141. pm.power(pm.DAC_EN, true)
  142. log.info("i2c initial",i2c.setup(0, i2c.FAST)) -- 开启i2c
  143. for i, v in pairs(es8311_reg) do -- 初始化es8311
  144. i2c.send(0,es8311_address,v,1)
  145. end
  146. i2s.setup(0, 0, 8000, 16, 1, i2s.MODE_I2S) -- 开启i2s
  147. i2s.on(0, record_cb) -- 注册i2s接收回调
  148. i2s.recv(0, rx_buff, 3200)
  149. i2c.send(0,es8311_address,{0x00, 0xc0},1)
  150. sys.wait(6000)
  151. i2c.send(0, es8311_address,{0x00, 0x80},1) -- ES8311停止录音
  152. log.info("录音5秒结束")
  153. io.writeFile(recordPath, "#!AMR\n") -- 向文件写入amr文件标识数据
  154. io.writeFile(recordPath, amr_buff:query(), "a+b") -- 向文件写入编码后的amr数据
  155. i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
  156. local result = audio.play(0, {recordPath}) -- 请求音频播放
  157. if result then
  158. sys.waitUntil("AUDIO_PLAY_DONE")
  159. log.info("音频播放结果", result) -- 等待音频播放完毕
  160. else
  161. end
  162. i2c.close(0)
  163. gpio.setup(26, 0) --录音完毕关闭mic和es8311电源,关闭i2c
  164. pm.power(pm.DAC_EN, false)
  165. -- uart.write(1, io.readFile(recordPath))
  166. end
  167. end
  168. sys.taskInit(record_task)