airtts.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. local airtts = {}
  2. local i2c_id = 0 -- i2c_id 0
  3. local play_string = "支持 4G,卫星定位,WiFi,蓝牙,5秒极速联网,"
  4. local play_string1 = "51个可编程IO/4个UART/4个通用ADC/1个CAN接口,"
  5. local play_string2 = "支持LuatOS二次开发,源码开放例程丰富,"
  6. local play_string3 = "支持485/232/充电/以太网驱动/多网融合/VoLTE通话,"
  7. local airaudio = require "airaudio"
  8. local taskName = "airtts"
  9. local run_state = 0
  10. local function audio_play()
  11. local result
  12. -- 本例子是按行播放 "千字文", 文本来源自wiki百科
  13. local fd = nil
  14. local line = nil
  15. log.info("开始播放")
  16. line = play_string .. play_string1 .. play_string2 .. play_string3
  17. line = line:trim()
  18. log.info("播放内容", line)
  19. result = audio.tts(0, line)
  20. if result then
  21. --等待音频通道的回调消息,或者切换歌曲的消息
  22. while true do
  23. msg = sysplus.waitMsg(taskName, nil)
  24. if type(msg) == 'table' then
  25. if msg[1] == MSG_PD then
  26. log.info("播放结束")
  27. break
  28. end
  29. else
  30. log.error(type(msg), msg)
  31. end
  32. end
  33. else
  34. log.debug("解码失败!")
  35. sys.wait(1000)
  36. end
  37. if not audio.isEnd(0) then
  38. log.info("手动关闭")
  39. audio.playStop(0)
  40. end
  41. if audio.pm then
  42. audio.pm(0,audio.STANDBY)
  43. end
  44. -- audio.pm(0,audio.SHUTDOWN) --低功耗可以选择SHUTDOWN或者POWEROFF,如果codec无法断电用SHUTDOWN
  45. log.info("mem", "sys", rtos.meminfo("sys"))
  46. log.info("mem", "lua", rtos.meminfo("lua"))
  47. -- sys.wait(1000)
  48. sysplus.taskDel(taskName)
  49. run_state = 0
  50. sysplus.sendMsg(taskName, "PALY END")
  51. end
  52. function audio_stop()
  53. sysplus.sendMsg(taskName, MSG_PD)
  54. end
  55. local function audio_task()
  56. airaudio.init()
  57. if fonts.list then
  58. log.info("fonts", "u8g2", json.encode(fonts.list("u8g2")))
  59. end
  60. audio_play()
  61. end
  62. function airtts.run() -- TTS 播放主程序
  63. if run_state == 0 then
  64. lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
  65. sysplus.taskInitEx(audio_task, taskName)
  66. run_state = 1
  67. end
  68. lcd.drawStr(0,80,play_string)
  69. lcd.drawStr(0,130,play_string1)
  70. lcd.drawStr(0,180,play_string2)
  71. lcd.drawStr(0,230,play_string3)
  72. lcd.showImage(120,300,"/luadb/back.jpg")
  73. lcd.showImage(0,448,"/luadb/Lbottom.jpg")
  74. lcd.flush()
  75. while true do
  76. sys.wait(30)
  77. if run_state == 0 then -- 等待结束
  78. return true
  79. end
  80. end
  81. end
  82. function airtts.tp_handal(x,y,event) -- 判断是否需要停止播放
  83. if x > 120 and x < 200 and y > 300 and y < 380 then
  84. audio_stop()
  85. end
  86. end
  87. return airtts