Skip to content

Commit d1eca2e

Browse files
committed
Merge branch 'main' of github.com:Robitx/gp.nvim into copilot
2 parents 4d06669 + 17b775a commit d1eca2e

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

doc/gp.nvim.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*gp.nvim.txt* For NVIM v0.8.0 Last change: 2024 January 14
1+
*gp.nvim.txt* For NVIM v0.8.0 Last change: 2024 January 20
22

33
==============================================================================
44
Table of Contents *gp.nvim-table-of-contents*

lua/gp/config.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,21 @@ local config = {
213213
-- decrease this number to pick up only louder sounds as possible speech
214214
-- you can disable silence trimming by setting this a very high number (like 1000.0)
215215
whisper_silence = "1.75",
216-
-- whisper max recording time (mm:ss)
217-
whisper_max_time = "05:00",
218216
-- whisper tempo (1.0 is normal speed)
219217
whisper_tempo = "1.75",
220218
-- The language of the input audio, in ISO-639-1 format.
221219
whisper_language = "en",
220+
-- command to use for recording can be nil (unset) for automatic selection
221+
-- string ("sox", "arecord", "ffmpeg") or table with command and arguments:
222+
-- sox is the most universal, but can have start/end cropping issues caused by latency
223+
-- arecord is linux only, but has no cropping issues and is faster
224+
-- ffmpeg in the default configuration is macos only, but can be used on any platform
225+
-- (see https://trac.ffmpeg.org/wiki/Capture/Desktop for more info)
226+
-- below is the default configuration for all three commands:
227+
-- whisper_rec_cmd = {"sox", "-c", "1", "--buffer", "32", "-d", "rec.wav", "trim", "0", "60:00"},
228+
-- whisper_rec_cmd = {"arecord", "-c", "1", "-f", "S16_LE", "-r", "48000", "-d", "3600", "rec.wav"},
229+
-- whisper_rec_cmd = {"ffmpeg", "-y", "-f", "avfoundation", "-i", ":0", "-t", "3600", "rec.wav"},
230+
whisper_rec_cmd = nil,
222231

223232
-- image generation settings
224233
-- image prompt prefix for asking user for input (supports {{agent}} template variable)

lua/gp/init.lua

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ local deprecated = {
1717
chat_system_prompt = "`chat_system_prompt`\n" .. switch_to_agent,
1818
command_prompt_prefix = "`command_prompt_prefix`\nPlease use `command_prompt_prefix_template`"
1919
.. " with support for \n`{{agent}}` variable so you know which agent is currently active",
20+
whisper_max_time = "`whisper_max_time`\nPlease use fully customizable `whisper_rec_cmd`",
2021
}
2122

2223
--------------------------------------------------------------------------------
@@ -2863,41 +2864,35 @@ M.Whisper = function(callback)
28632864
return
28642865
end
28652866

2867+
local rec_file = M.config.whisper_dir .. "/rec.wav"
28662868
local rec_options = {
28672869
sox = {
28682870
cmd = "sox",
28692871
opts = {
2870-
-- single channel
28712872
"-c",
28722873
"1",
2873-
-- small buffer
28742874
"--buffer",
28752875
"32",
28762876
"-d",
2877-
-- output file
2878-
M.config.whisper_dir .. "/rec.wav",
2879-
-- max recording time
2877+
"rec.wav",
28802878
"trim",
28812879
"0",
2882-
M.config.whisper_max_time,
2880+
"3600",
28832881
},
28842882
exit_code = 0,
28852883
},
28862884
arecord = {
28872885
cmd = "arecord",
28882886
opts = {
2889-
-- single channel
28902887
"-c",
28912888
"1",
28922889
"-f",
28932890
"S16_LE",
28942891
"-r",
28952892
"48000",
2896-
-- max recording time
28972893
"-d",
28982894
3600,
2899-
-- output file
2900-
M.config.whisper_dir .. "/rec.wav",
2895+
"rec.wav",
29012896
},
29022897
exit_code = 1,
29032898
},
@@ -2911,7 +2906,7 @@ M.Whisper = function(callback)
29112906
":0",
29122907
"-t",
29132908
"3600",
2914-
M.config.whisper_dir .. "/rec.wav",
2909+
"rec.wav",
29152910
},
29162911
exit_code = 255,
29172912
},
@@ -3047,25 +3042,48 @@ M.Whisper = function(callback)
30473042
end)
30483043
end
30493044

3050-
local rec_cmd = "sox"
3051-
if vim.fn.executable("ffmpeg") == 1 then
3052-
local devices = vim.fn.system("ffmpeg -devices -v quiet | grep -i avfoundation | wc -l")
3053-
devices = string.gsub(devices, "^%s*(.-)%s*$", "%1")
3054-
if devices == "1" then
3055-
rec_cmd = "ffmpeg"
3045+
local cmd = {}
3046+
3047+
local rec_cmd = M.config.whisper_rec_cmd
3048+
-- if rec_cmd not set explicitly, try to autodetect
3049+
if not rec_cmd then
3050+
rec_cmd = "sox"
3051+
if vim.fn.executable("ffmpeg") == 1 then
3052+
local devices = vim.fn.system("ffmpeg -devices -v quiet | grep -i avfoundation | wc -l")
3053+
devices = string.gsub(devices, "^%s*(.-)%s*$", "%1")
3054+
if devices == "1" then
3055+
rec_cmd = "ffmpeg"
3056+
end
30563057
end
3058+
if vim.fn.executable("arecord") == 1 then
3059+
rec_cmd = "arecord"
3060+
end
3061+
end
3062+
3063+
if type(rec_cmd) == "table" and rec_cmd[1] and rec_options[rec_cmd[1]] then
3064+
rec_cmd = vim.deepcopy(rec_cmd)
3065+
cmd.cmd = table.remove(rec_cmd, 1)
3066+
cmd.exit_code = rec_options[cmd.cmd].exit_code
3067+
cmd.opts = rec_cmd
3068+
elseif type(rec_cmd) == "string" and rec_options[rec_cmd] then
3069+
cmd = rec_options[rec_cmd]
3070+
else
3071+
M.error(string.format("Whisper got invalid recording command: %s", rec_cmd))
3072+
close()
3073+
return
30573074
end
3058-
if vim.fn.executable("arecord") == 1 then
3059-
rec_cmd = "arecord"
3075+
for i, v in ipairs(cmd.opts) do
3076+
if v == "rec.wav" then
3077+
cmd.opts[i] = rec_file
3078+
end
30603079
end
30613080

3062-
local cmd = rec_options[rec_cmd]
30633081
M._H.process(nil, cmd.cmd, cmd.opts, function(code, signal, stdout, stderr)
30643082
close()
30653083

30663084
if code and code ~= cmd.exit_code then
30673085
M.error(
3068-
rec_cmd
3086+
cmd.cmd
30693087
.. " exited with code and signal:\ncode: "
30703088
.. code
30713089
.. ", signal: "

0 commit comments

Comments
 (0)