record.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. --- 录音处理
  2. -- @module record
  3. -- @author 朱天华、小强
  4. -- @license MIT
  5. -- @copyright openLuat.com
  6. -- @release 2017.11.23
  7. require "log"
  8. require "ril"
  9. module(..., package.seeall)
  10. local ID, FILE = 1, '/RecDir/rec001'
  11. local recording
  12. local stoping
  13. local duration
  14. local recordCallback
  15. --- record.getdata 读取录音数据
  16. -- @param offset 偏移位置
  17. -- @param len 长度
  18. -- @return data 录音数据
  19. -- @usage data = record.getdata(0, 1024)
  20. function getdata(offset, len)
  21. local f = io.open(FILE, "rb")
  22. if not f then log.error('record.getdata', 'open failed') return "" end
  23. if not f:seek("set", offset) then log.error('record.getdata', 'seek failed') f:close() return "" end
  24. local data = f:read(len)
  25. f:close()
  26. log.info("record.getdata", data and data:len() or 0)
  27. return data or ""
  28. end
  29. --- record.getsize 读取录音文件总长度,录音时长
  30. -- @return filesize 录音文件大小
  31. -- @return duration 录音时长
  32. -- @usage filesize, duration = record.getsize()
  33. function getsize()
  34. return io.filesize(FILE), duration and (duration-1)/1000+1 or 0
  35. end
  36. --- record.delete 删除录音
  37. -- @usage record.delete()
  38. function delete()
  39. os.remove(FILE)
  40. end
  41. --- record.exists 判断是否存在录音
  42. -- @return result true - 有录音 false - 无录音
  43. -- @usage result = record.exists()
  44. function exists()
  45. return io.exists(FILE)
  46. end
  47. --- record.isBusy 是否正在处理录音
  48. -- @return result true - 正在处理 false - 空闲
  49. -- @usage result = record.isBusy()
  50. function isBusy()
  51. return recording or stoping
  52. end
  53. --- record.start 开始录音
  54. -- @param seconds 录音时长,单位:秒
  55. -- @param cb 录音结果回调
  56. -- @return result true - 开始录音 其他 - 失败
  57. -- @usage result = record.start()
  58. function start(seconds, cb)
  59. if recording or stoping or seconds <= 0 or seconds > 50 then
  60. log.error('record.start', recording, stoping, seconds)
  61. return
  62. end
  63. delete()
  64. duration = seconds * 1000
  65. ril.request("AT+AUDREC=0,0,1," .. ID .. "," .. duration)
  66. recording = true
  67. recordCallback = cb
  68. return true
  69. end
  70. --- record.stop 停止录音
  71. -- @usage record.stop()
  72. function stop()
  73. if not recording or stoping then return end
  74. ril.request("AT+AUDREC=0,0,0," .. ID .. "," .. duration)
  75. stoping = true
  76. end
  77. ril.regurc("+AUDREC", function(data)
  78. local action, size = data:match("(%d),(%d+)")
  79. if action and size then
  80. size = tonumber(size)
  81. if action == "1" then
  82. local result = size > 0 and recording
  83. if not result then os.remove(FILE) size = 0 end
  84. duration = size
  85. if recordCallback then recordCallback(result, size) recordCallback = nil end
  86. recording = false
  87. stoping = false
  88. end
  89. end
  90. end)
  91. ril.regrsp("+AUDREC", function(cmd, success)
  92. local action = cmd:match("AUDREC=%d,%d,(%d)")
  93. if action == "1" then
  94. if not success then
  95. if recordCallback then
  96. recordCallback(false, 0)
  97. recordCallback = nil
  98. end
  99. recording = false
  100. end
  101. elseif action == '0' then
  102. if stoping and not success then stoping = false end -- 失败直接结束,成功则等到+AUDREC上报才判定停止录音成功
  103. end
  104. end)