Spaces:
Sleeping
Sleeping
File size: 5,155 Bytes
402daee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
--[[
ReaSpeechUI.lua - ReaSpeech user interface
]]--
ReaSpeechUI = Polo {
VERSION = "unknown (development)",
-- Set to show ImGui Metrics/Debugger window
METRICS = false,
TITLE = 'ReaSpeech',
WIDTH = 1000,
HEIGHT = 600,
ITEM_WIDTH = 125,
}
function ReaSpeechUI:init()
self.onerror = function (e)
self:log(e)
end
self.requests = {}
self.responses = {}
self.logs = {}
ReaSpeechAPI:init('http://' .. Script.host)
self.worker = ReaSpeechWorker.new({
requests = self.requests,
responses = self.responses,
logs = self.logs,
})
self.product_activation = ReaSpeechProductActivation.new()
self.product_activation_ui = ReaSpeechProductActivationUI.new {
product_activation = self.product_activation
}
self.controls_ui = ReaSpeechControlsUI.new()
self.actions_ui = ReaSpeechActionsUI.new({
worker = self.worker
})
self.transcript = Transcript.new()
self.transcript_ui = TranscriptUI.new { transcript = self.transcript }
self.failure = AlertPopup.new { title = 'Transcription Failed' }
self.react_handlers = self:get_react_handlers()
end
ReaSpeechUI.config_flags = function ()
return ImGui.ConfigFlags_DockingEnable()
end
ReaSpeechUI.log_time = function ()
return os.date('%Y-%m-%d %H:%M:%S')
end
function ReaSpeechUI:log(msg)
table.insert(self.logs, {msg, false})
end
function ReaSpeechUI:debug(msg)
table.insert(self.logs, {msg, true})
end
function ReaSpeechUI:trap(f)
return xpcall(f, self.onerror)
end
function ReaSpeechUI:has_js_ReaScriptAPI()
if reaper.JS_Dialog_BrowseForSaveFile then
return true
end
return false
end
function ReaSpeechUI:show_file_dialog(options)
local title = options.title or 'Save file'
local folder = options.folder or ''
local file = options.file or ''
local ext = options.ext or ''
local save = options.save or false
local multi = options.multi or false
if self:has_js_ReaScriptAPI() then
if save then
return reaper.JS_Dialog_BrowseForSaveFile(title, folder, file, ext)
else
return reaper.JS_Dialog_BrowseForOpenFiles(title, folder, file, ext, multi)
end
else
return nil
end
end
function ReaSpeechUI:tooltip(text)
if not ImGui.IsItemHovered(ctx, ImGui.HoveredFlags_DelayNormal()) or
not ImGui.BeginTooltip(ctx)
then return end
self:trap(function()
ImGui.PushTextWrapPos(ctx, ImGui.GetFontSize(ctx) * 42)
self:trap(function()
ImGui.Text(ctx, text)
end)
ImGui.PopTextWrapPos(ctx)
end)
ImGui.EndTooltip(ctx)
end
function ReaSpeechUI:react()
for _, handler in pairs(self.react_handlers) do
self:trap(handler)
end
end
function ReaSpeechUI:get_react_handlers()
return {
function() self:react_to_worker_response() end,
function() self:react_to_logging() end,
function() self.worker:react() end,
function() self:render() end
}
end
function ReaSpeechUI:react_to_worker_response()
local response = table.remove(self.responses, 1)
if not response then
return
end
self:debug('Response: ' .. dump(response))
if response.error then
self.failure:show(response.error)
self.worker:cancel()
return
end
if not response.segments then
return
end
for _, segment in pairs(response.segments) do
for _, s in pairs(
TranscriptSegment.from_whisper(segment, response._job.item, response._job.take)
) do
if s:get('text') then
self.transcript:add_segment(s)
end
end
end
self.transcript:update()
end
function ReaSpeechUI:react_to_logging()
for _, log in pairs(self.logs) do
local msg, dbg = table.unpack(log)
if dbg and self.controls_ui.log_enable and self.controls_ui.log_debug then
reaper.ShowConsoleMsg(self:log_time() .. ' [DBG] ' .. tostring(msg) .. '\n')
elseif not dbg and self.controls_ui.log_enable then
reaper.ShowConsoleMsg(self:log_time() .. ' [LOG] ' .. tostring(msg) .. '\n')
end
end
self.logs = {}
end
function ReaSpeechUI:render()
ImGui.PushItemWidth(ctx, self.ITEM_WIDTH)
self:trap(function ()
if not self.product_activation:is_activated() then
self.product_activation_ui:render()
return
end
self.controls_ui:render()
self.actions_ui:render()
self.transcript_ui:render()
self.failure:render()
end)
ImGui.PopItemWidth(ctx)
end
function ReaSpeechUI:new_jobs(jobs)
local request = self.controls_ui:get_request_data()
request.jobs = jobs
self:debug('Request: ' .. dump(request))
self.transcript:clear()
table.insert(self.requests, request)
end
function ReaSpeechUI.png_from_bytes(image_key)
if not IMAGES[image_key] or not IMAGES[image_key].bytes then
return
end
local image = IMAGES[image_key]
if not ImGui.ValidatePtr(image.imgui_image, 'ImGui_Image*') then
image.imgui_image = ImGui.CreateImageFromMem(image.bytes)
end
ImGui.Image(ctx, image.imgui_image, image.width, image.height)
end
function ReaSpeechUI.get_source_path(take)
local source = reaper.GetMediaItemTake_Source(take)
if source then
local source_path = reaper.GetMediaSourceFileName(source)
return source_path
end
return nil
end
|