Advertisement
Ewgeniy

Speech Synthesizer

Jul 7th, 2024 (edited)
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local event = require("event")
  4. local fs = require("filesystem")
  5.  
  6. local function loadCommands(filePath)
  7.     local commands = {}
  8.     if not fs.exists(filePath) then
  9.         return commands
  10.     end
  11.  
  12.     local file = io.open(filePath, "r")
  13.     for line in file:lines() do
  14.         local freq, time, state = line:match("([^,]+),([^,]+),([^,]+)")
  15.         if freq and time and state then
  16.             table.insert(commands, {tonumber(freq), tonumber(time), state})
  17.         end
  18.     end
  19.     file:close()
  20.     return commands
  21. end
  22.  
  23. local function playCommands(commands)
  24.     local lastTime = 0
  25.     local startTime = computer.uptime()
  26.    
  27.     for i, command in ipairs(commands) do
  28.         local currentTime = computer.uptime()
  29.         local elapsedTime = currentTime - startTime
  30.        
  31.         local freq, time, state = command[1], command[2], command[3]
  32.         local delay = time - lastTime
  33.        
  34.         while elapsedTime < time do
  35.             os.sleep(0.05)
  36.             elapsedTime = computer.uptime() - startTime
  37.         end
  38.        
  39.         if state == 'on' then
  40.             computer.beep(freq, 0.05)  -- Включаем ноту на короткий промежуток времени
  41.         end
  42.        
  43.         lastTime = time
  44.        
  45.         -- Вставляем вызов event.pull(0) каждые 10 команд, чтобы избежать ошибки "too long without yielding"
  46.         if i % 10 == 0 then
  47.             event.pull(0)
  48.         end
  49.     end
  50. end
  51.  
  52. local commands = loadCommands("/home/commands.txt")
  53. playCommands(commands)
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement