es8311.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. local i2c_id = 0
  15. local i2s_id = 0
  16. -- es8311器件地址
  17. local es8311_address = 0x18 -- 注意硬件差异,自己的实际地址
  18. local record_cnt = 0
  19. -- es8311初始化寄存器配置
  20. local es8311_reg = {
  21. {0x45,0x00},
  22. {0x01,0x30},
  23. {0x02,0x10},
  24. {0x02,0x00},
  25. {0x03,0x10},
  26. {0x16,0x24},
  27. {0x04,0x20},
  28. {0x05,0x00},
  29. {0x06,3},
  30. {0x07,0x00},
  31. {0x08,0xFF},
  32. {0x09,0x0C},
  33. {0x0A,0x0C},
  34. {0x0B,0x00},
  35. {0x0C,0x00},
  36. {0x10,0x03},
  37. {0x11,0x7F},
  38. {0x00,0x80},
  39. {0x0D,0x01},
  40. {0x01,0x3F},
  41. {0x14,0x1a},
  42. {0x12,0x28},
  43. {0x13,0x00},
  44. {0x0E,0x02},
  45. {0x0F,0x44},
  46. {0x15,0x00},
  47. {0x1B,0x0A},
  48. {0x1C,0x6A},
  49. {0x37,0x48},
  50. {0x44,(0 <<7)},
  51. {0x17,210},
  52. {0x32,200},
  53. }
  54. -- i2s数据接收buffer
  55. local rx_buff = zbuff.create(3200)
  56. -- amr数据存放buffer,尽可能地给大一些,对于MR475编码等级来说,一秒文件大小为0.6k左右,理论上录音五秒需要的空间为5 * 0.6 = 3k, 这里尽可能给大一点
  57. local amr_buff = zbuff.create(5 * 1024)
  58. --创建一个amr的encoder
  59. local encoder = codec.create(codec.AMR, false)
  60. -- 录音文件路径
  61. local recordPath = "/record.amr"
  62. -- i2s数据接收回调
  63. local function record_cb(id, buff)
  64. if buff then
  65. log.info("I2S", id, "接收了", rx_buff:used())
  66. log.info("编码结果", codec.encode(encoder, rx_buff, amr_buff)) -- 对录音数据进行amr编码,成功的话这个接口会返回true, 默认编码等级为MR475
  67. record_cnt = record_cnt + 1
  68. if record_cnt >= 25 then --超过5秒后停止
  69. log.info("I2S", "stop")
  70. i2s.stop(i2s_id)
  71. end
  72. end
  73. end
  74. ---- MultipartForm上传文件
  75. -- url string 请求URL地址
  76. -- filename string 上传服务器的文件名
  77. -- filePath string 待上传文件的路径
  78. local function postMultipartFormData(url, filename, filePath)
  79. local boundary = "----WebKitFormBoundary"..os.time()
  80. local req_headers = {
  81. ["Content-Type"] = "multipart/form-data; boundary=" .. boundary,
  82. }
  83. local body = {}
  84. table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\"file\"; filename=\"".. filename .."\"\r\n\r\n")
  85. table.insert(body, io.readFile(filePath))
  86. table.insert(body, "\r\n")
  87. table.insert(body, "--"..boundary.."--\r\n")
  88. body = table.concat(body)
  89. log.info("headers: ", "\r\n" .. json.encode(req_headers), type(body))
  90. log.info("body: " .. body:len() .. "\r\n" .. body)
  91. local code, headers, body = http.request("POST",url,
  92. req_headers,
  93. body
  94. ).wait()
  95. log.info("http.post", code, headers, body)
  96. end
  97. local function record_task()
  98. os.remove(recordPath)
  99. gpio.setup(26, 1) -- 打开录音开发板mic供电
  100. audio.config(0, 25, 1, 6, 200)
  101. pm.power(pm.DAC_EN, true) -- 打开es8311芯片供电
  102. log.info("i2c initial", i2c.setup(i2c_id, i2c.FAST)) -- 开启i2c
  103. for i, v in pairs(es8311_reg) do -- 初始化es8311
  104. i2c.send(i2c_id, es8311_address, v, 1)
  105. end
  106. i2s.setup(i2s_id, 0, 8000, 16, 1, i2s.MODE_I2S) -- 开启i2s
  107. i2s.on(i2s_id, record_cb) -- 注册i2s接收回调
  108. i2s.recv(i2s_id, rx_buff, 3200)
  109. i2c.send(i2c_id, es8311_address, {0x00, 0xc0}, 1)
  110. sys.wait(6000)
  111. i2c.send(i2c_id, es8311_address, {0x00, 0x80}, 1) -- ES8311停止录音
  112. log.info("录音5秒结束")
  113. io.writeFile(recordPath, "#!AMR\n") -- 向文件写入amr文件标识数据
  114. if rx_buff:len() then
  115. io.writeFile(recordPath, amr_buff:query(), "a+b") -- 向文件写入编码后的amr数据
  116. else
  117. log.info("--------------> buff nil")
  118. end
  119. i2s.setup(i2s_id, 0, 0, 0, 0, i2s.MODE_MSB)
  120. local result = audio.play(0, {recordPath}) -- 请求音频播放
  121. log.info("音频播放结果", result)
  122. if result then
  123. sys.waitUntil("AUDIO_PLAY_DONE") -- 等待音频播放完毕
  124. else
  125. -- 音频播放出错
  126. end
  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. uart.setup(1, 115200) -- 开启串口1
  135. uart.write(1, io.readFile(recordPath)) -- 向串口发送录音文件
  136. end
  137. sys.taskInit(record_task)