main.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "testtts"
  3. VERSION = "2.0.0"
  4. --[[
  5. 本demo当前仅支持Ar780E和Air600E
  6. ## 提醒:
  7. 1. 本demo需要2022.12.21及之后的源码所编译的LuatOS固件
  8. 2. 本demo若使用外置TTS资源的LuatOS固件, 就必须有外挂的SPI Flash, 起码1M字节, 后面有刷写说明
  9. 3. 下载脚本时, 把txt也加上一起下载
  10. 4. 本demo需要音频扩展板, 780E只有I2S输出, 需要codec和PA才能驱动喇叭
  11. 5. 内置TTS资源的LuatOS最低版本是V1104,且去掉了很多库, 尤其是UI方面的库
  12. ## 使用本demo前,如果是外置TTS资源的LuatOS固件, 必须先刷tts.binpkg进行SPI Flash的刷写
  13. 1. 下载链接 https://gitee.com/openLuat/luatos-soc-2022/attach_files
  14. 2. 在LuaTools主界面, 用"下载固件"按钮进行下载.
  15. 3. 下载前需要接好SPI Flash!!
  16. 4. 下载前选日志模式 4G USB, luatools版本号2.1.85或以上
  17. ## SPI Flash布局, 以1M字节为例,供参考:
  18. -----------------------------------
  19. 64 k 保留空间, 用户自行分配
  20. -----------------------------------
  21. 704k TTS数据
  22. -----------------------------------
  23. 剩余空间, 256k,用户自行分配
  24. -----------------------------------
  25. ## 基本流程:
  26. 1. 初始化sfud, 本demo使用SPI0 + GPIO8
  27. 2. 使用 audio.tts播放文本
  28. 3. 等待 播放结束事件
  29. 4. 从第二步重新下一个循环
  30. ## 接线说明
  31. 以780E开发板为例, 需要1.5版本或以上,团购版本均为1.5或以上.
  32. 1.4版本SPI分布有所不同, 注意区分.
  33. https://wiki.luatos.com/chips/air780e/board.html
  34. xx脚指开发板pinout图上的顺序编号, 非GPIO编号
  35. Flash -- 开发板
  36. GND -- 16脚, GND
  37. VCC -- 15脚, 3.3V
  38. CLK -- 14脚, GPIO11/SPI0_CLK/LCD_CLK, 时钟. 如果是1.4版本的开发板, 接05脚的GPIO11/UART2_TXD
  39. MOSI -- 13脚, GPIO09/SPI0_MOSI/LCD_OUT,主控数据输出
  40. MISO -- 11脚, GPIO10/SPI0_MISO/LCD_RS,主控数据输入. 如果是1.4版本的开发板, 接06脚的GPIO10/UART2_RXD
  41. CS -- 10脚, GPIO08/SPI0_CS/LCD_CS,片选.
  42. 注意: 12脚是跳过的, 接线完毕后请检查好再通电!!
  43. ]]
  44. -- sys库是标配
  45. _G.sys = require("sys")
  46. _G.sysplus = require("sysplus")
  47. local taskName = "task_audio"
  48. local MSG_MD = "moreData" -- 播放缓存有空余
  49. local MSG_PD = "playDone" -- 播放完成所有数据
  50. audio.on(0, function(id, event)
  51. --使用play来播放文件时只有播放完成回调
  52. local succ,stop,file_cnt = audio.getError(0)
  53. if not succ then
  54. if stop then
  55. log.info("用户停止播放")
  56. else
  57. log.info("第", file_cnt, "个文件解码失败")
  58. end
  59. end
  60. sysplus.sendMsg(taskName, MSG_PD)
  61. end)
  62. local function audio_task()
  63. local result
  64. --Air780E开发板配套
  65. i2s.setup(0, 0, 0, 0, 0, i2s.MODE_MSB)
  66. audio.config(0, 25, 1, 6, 200)
  67. gpio.setup(24, 0)
  68. gpio.setup(23, 0)
  69. gpio.setup(27, 0)
  70. gpio.setup(2, 0)
  71. -- 初始化spi flash, 如果是极限版TTS_ONCHIP,就不需要初始化
  72. if sfud then
  73. spi_flash = spi.deviceSetup(0,8,0,0,8,25600000,spi.MSB,1,0)
  74. local ret = sfud.init(spi_flash)
  75. if ret then
  76. log.info("sfud.init ok")
  77. else
  78. log.info("sfud.init error", ret)
  79. return
  80. end
  81. else
  82. log.info("tts", "TTS_ONCHIP?? skip sfud")
  83. end
  84. -- 本例子是按行播放 "千字文", 文本来源自wiki百科
  85. local fd = nil
  86. local line = nil
  87. while true do
  88. log.info("开始播放")
  89. line = nil
  90. if not fd then
  91. fd = io.open("/luadb/qianzw.txt")
  92. end
  93. if fd then
  94. line = fd:read("*l")
  95. if line == nil then
  96. fd:close()
  97. fd = nil
  98. end
  99. end
  100. if line == nil then
  101. line = "一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十"
  102. end
  103. line = line:trim()
  104. log.info("播放内容", line)
  105. result = audio.tts(0, line)
  106. if result then
  107. --等待音频通道的回调消息,或者切换歌曲的消息
  108. while true do
  109. msg = sysplus.waitMsg(taskName, nil)
  110. if type(msg) == 'table' then
  111. if msg[1] == MSG_PD then
  112. log.info("播放结束")
  113. break
  114. end
  115. else
  116. log.error(type(msg), msg)
  117. end
  118. end
  119. else
  120. log.debug("解码失败!")
  121. sys.wait(1000)
  122. end
  123. if not audio.isEnd(0) then
  124. log.info("手动关闭")
  125. audio.playStop(0)
  126. end
  127. log.info("mem", "sys", rtos.meminfo("sys"))
  128. log.info("mem", "lua", rtos.meminfo("lua"))
  129. sys.wait(1000)
  130. end
  131. sysplus.taskDel(taskName)
  132. end
  133. sysplus.taskInitEx(audio_task, taskName, task_cb)
  134. -- 用户代码已结束---------------------------------------------
  135. -- 结尾总是这一句
  136. sys.run()
  137. -- sys.run()之后后面不要加任何语句!!!!!