diff --git a/lua/gp/config.lua b/lua/gp/config.lua index cb431f00..2c426905 100644 --- a/lua/gp/config.lua +++ b/lua/gp/config.lua @@ -183,7 +183,16 @@ local config = { whisper_tempo = "1.75", -- The language of the input audio, in ISO-639-1 format. whisper_language = "en", - -- Override command to use for recording (does not support options) + -- command to use for recording can be nil (unset) for automatic selection + -- string ("sox", "arecord", "ffmpeg") or table with command and arguments: + -- sox is the most universal, but can have start/end cropping issues caused by latency + -- arecord is linux only, but has no cropping issues and is faster + -- ffmpeg in the default configuration is macos only, but can be used on any platform + -- (see https://trac.ffmpeg.org/wiki/Capture/Desktop for more info) + -- below is the default configuration for all three commands: + -- whisper_rec_cmd = {"sox", "-c", "1", "--buffer", "32", "-d", "rec.wav", "trim", "0", "60:00"}, + -- whisper_rec_cmd = {"arecord", "-c", "1", "-f", "S16_LE", "-r", "48000", "-d", "3600", "rec.wav"}, + -- whisper_rec_cmd = {"ffmpeg", "-y", "-f", "avfoundation", "-i", ":0", "-t", "3600", "rec.wav"}, whisper_rec_cmd = nil, -- image generation settings diff --git a/lua/gp/init.lua b/lua/gp/init.lua index 8359a4ad..933ff839 100644 --- a/lua/gp/init.lua +++ b/lua/gp/init.lua @@ -2785,41 +2785,35 @@ M.Whisper = function(callback) return end + local rec_file = M.config.whisper_dir .. "/rec.wav" local rec_options = { sox = { cmd = "sox", opts = { - -- single channel "-c", "1", - -- small buffer "--buffer", "32", "-d", - -- output file - M.config.whisper_dir .. "/rec.wav", - -- max recording time + "rec.wav", "trim", "0", - M.config.whisper_max_time, + "3600", }, exit_code = 0, }, arecord = { cmd = "arecord", opts = { - -- single channel "-c", "1", "-f", "S16_LE", "-r", "48000", - -- max recording time "-d", 3600, - -- output file - M.config.whisper_dir .. "/rec.wav", + "rec.wav", }, exit_code = 1, }, @@ -2833,7 +2827,7 @@ M.Whisper = function(callback) ":0", "-t", "3600", - M.config.whisper_dir .. "/rec.wav", + "rec.wav", }, exit_code = 255, }, @@ -2969,6 +2963,8 @@ M.Whisper = function(callback) end) end + local cmd = {} + local rec_cmd = M.config.whisper_rec_cmd -- if rec_cmd not set explicitly, try to autodetect if not rec_cmd then @@ -2984,18 +2980,31 @@ M.Whisper = function(callback) rec_cmd = "arecord" end end - if not rec_options[rec_cmd] then + + if type(rec_cmd) == "table" and rec_cmd[1] and rec_options[rec_cmd[1]] then + rec_cmd = vim.deepcopy(rec_cmd) + cmd.cmd = table.remove(rec_cmd, 1) + cmd.exit_code = rec_options[cmd.cmd].exit_code + cmd.opts = rec_cmd + elseif type(rec_cmd) == "string" and rec_options[rec_cmd] then + cmd = rec_options[rec_cmd] + else M.error(string.format("Whisper got invalid recording command: %s", rec_cmd)) + close() return end + for i, v in ipairs(cmd.opts) do + if v == "rec.wav" then + cmd.opts[i] = rec_file + end + end - local cmd = rec_options[rec_cmd] M._H.process(nil, cmd.cmd, cmd.opts, function(code, signal, stdout, stderr) close() if code and code ~= cmd.exit_code then M.error( - rec_cmd + cmd.cmd .. " exited with code and signal:\ncode: " .. code .. ", signal: "