vimamsa 0.1.26 → 0.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/appimage.yml +93 -0
- data/.gitignore +4 -1
- data/Dockerfile +7 -4
- data/README.md +62 -16
- data/custom_example.rb +43 -13
- data/doc/TODO.md +461 -0
- data/doc/bundling_plan.md +569 -0
- data/doc/key_bindings_report_plan.md +168 -0
- data/doc/missing_vim_bindings.md +71 -0
- data/doc/modules_settings_plan.md +433 -0
- data/doc/refactor_dedup_plan.md +117 -0
- data/doc/unused_code_plan.md +120 -0
- data/doc/vim_compatibility_plan.md +296 -0
- data/doc/vimlike_vim_conflicts.md +254 -0
- data/doc/vm_snapshots_plan.md +146 -0
- data/exe/run_tests.rb +117 -3
- data/exe/vimamsa +24 -4
- data/fonts/OFL-Oxanium.txt +93 -0
- data/fonts/Oxanium[wght].ttf +0 -0
- data/icon.png +0 -0
- data/img/demo.gif +0 -0
- data/lang/hyperplaintext.lang +6 -0
- data/lib/vimamsa/ack.rb +11 -1
- data/lib/vimamsa/actions.rb +4 -1
- data/lib/vimamsa/appimage_env.rb +35 -0
- data/lib/vimamsa/audio.rb +2 -5
- data/lib/vimamsa/bookmarks.rb +302 -0
- data/lib/vimamsa/buffer.rb +593 -82
- data/lib/vimamsa/buffer_changetext.rb +102 -42
- data/lib/vimamsa/buffer_cursor.rb +7 -0
- data/lib/vimamsa/buffer_list.rb +4 -0
- data/lib/vimamsa/buffer_manager.rb +4 -1
- data/lib/vimamsa/bundled_fonts.rb +32 -0
- data/lib/vimamsa/clipboard.rb +3 -0
- data/lib/vimamsa/conf.rb +59 -3
- data/lib/vimamsa/constants.rb +3 -0
- data/lib/vimamsa/crash_handler.rb +149 -0
- data/lib/vimamsa/debug.rb +4 -1
- data/lib/vimamsa/desktop_install.rb +96 -0
- data/lib/vimamsa/diff_buffer.rb +39 -10
- data/lib/vimamsa/easy_jump.rb +3 -0
- data/lib/vimamsa/editor.rb +263 -57
- data/lib/vimamsa/encrypt.rb +78 -29
- data/lib/vimamsa/file_finder.rb +4 -19
- data/lib/vimamsa/file_history.rb +3 -0
- data/lib/vimamsa/file_manager.rb +30 -15
- data/lib/vimamsa/fold.rb +226 -0
- data/lib/vimamsa/gui.rb +520 -106
- data/lib/vimamsa/gui_command_line.rb +208 -0
- data/lib/vimamsa/gui_dialog.rb +94 -42
- data/lib/vimamsa/gui_file_panel.rb +356 -25
- data/lib/vimamsa/gui_form_generator.rb +59 -35
- data/lib/vimamsa/gui_func_panel.rb +67 -22
- data/lib/vimamsa/gui_image.rb +3 -0
- data/lib/vimamsa/gui_keylog_panel.rb +246 -0
- data/lib/vimamsa/gui_menu.rb +23 -11
- data/lib/vimamsa/gui_select_window.rb +19 -48
- data/lib/vimamsa/gui_settings.rb +78 -37
- data/lib/vimamsa/gui_sourceview.rb +273 -19
- data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
- data/lib/vimamsa/gui_text.rb +63 -2
- data/lib/vimamsa/gui_theme.rb +606 -0
- data/lib/vimamsa/hook.rb +3 -0
- data/lib/vimamsa/hyper_plain_text.rb +3 -0
- data/lib/vimamsa/key_actions.rb +112 -13
- data/lib/vimamsa/key_binding_tree.rb +242 -215
- data/lib/vimamsa/key_bindings_notepad.rb +43 -0
- data/lib/vimamsa/key_bindings_report.rb +261 -0
- data/lib/vimamsa/key_bindings_vim.rb +354 -0
- data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
- data/lib/vimamsa/langservp.rb +271 -54
- data/lib/vimamsa/macro.rb +3 -0
- data/lib/vimamsa/main.rb +14 -8
- data/lib/vimamsa/rbvma.rb +26 -1
- data/lib/vimamsa/search.rb +3 -0
- data/lib/vimamsa/search_replace.rb +11 -10
- data/lib/vimamsa/string_util.rb +120 -40
- data/lib/vimamsa/terminal.rb +3 -0
- data/lib/vimamsa/test_framework.rb +28 -2
- data/lib/vimamsa/tests.rb +3 -0
- data/lib/vimamsa/text_objects.rb +353 -0
- data/lib/vimamsa/text_transforms.rb +14 -0
- data/lib/vimamsa/util.rb +46 -7
- data/lib/vimamsa/version.rb +1 -1
- data/lib/vimamsa.rb +0 -1
- data/modules/color_highlight/color_highlight.rb +118 -0
- data/modules/color_highlight/color_highlight_info.rb +3 -0
- data/modules/dictation/dictation.rb +1054 -0
- data/modules/dictation/dictation_info.rb +3 -0
- data/modules/dictation/whisper_worker.py +273 -0
- data/modules/ollama/ollama.rb +107 -0
- data/modules/ollama/ollama_info.rb +3 -0
- data/modules/tts/tts.rb +119 -0
- data/modules/tts/tts_info.rb +3 -0
- data/run_e2e_vm.sh +28 -0
- data/run_tests_vm.sh +130 -0
- data/scripts/appimage/AppRun +54 -0
- data/scripts/appimage/Dockerfile +18 -0
- data/scripts/appimage/build.sh +19 -0
- data/scripts/appimage/build_in_container.sh +192 -0
- data/scripts/appimage/smoke_test.sh +27 -0
- data/scripts/demo/demo_steps.txt +59 -0
- data/scripts/demo/record_demo.rb +177 -0
- data/scripts/demo/shapes.rb +28 -0
- data/styles/134272-molokai.xml +3 -0
- data/styles/molokai_cyber.xml +48 -0
- data/styles/molokai_edit.xml +1 -0
- data/tests/support/virtual_keyboard.rb +254 -0
- data/tests/support/virtual_mic.rb +67 -0
- data/tests/test_ack.rb +22 -0
- data/tests/test_auto_format.rb +122 -0
- data/tests/test_autocomplete.rb +211 -0
- data/tests/test_autosave.rb +145 -0
- data/tests/test_basic_editing.rb +33 -3
- data/tests/test_bookmarks.rb +206 -0
- data/tests/test_bracket_match.rb +85 -0
- data/tests/test_bundling.rb +92 -0
- data/tests/test_color_highlight.rb +92 -0
- data/tests/test_command_line.rb +79 -0
- data/tests/test_command_line_file.rb +27 -0
- data/tests/test_context_menu_file.rb +129 -0
- data/tests/test_copy_paste.rb +80 -0
- data/tests/test_crash_handler.rb +39 -0
- data/tests/test_custom_two_phase.rb +92 -0
- data/tests/test_dialog_callback_errors.rb +44 -0
- data/tests/test_dictation.rb +294 -0
- data/tests/test_diff_buffer_name.rb +51 -0
- data/tests/test_double_click.rb +41 -0
- data/tests/test_drag_selection.rb +125 -0
- data/tests/test_e2e_cmdline.rb +116 -0
- data/tests/test_e2e_dictation.rb +98 -0
- data/tests/test_e2e_replace_char.rb +58 -0
- data/tests/test_extract_file_links.rb +55 -0
- data/tests/test_file_format.rb +217 -0
- data/tests/test_file_manager.rb +72 -0
- data/tests/test_file_panel.rb +269 -0
- data/tests/test_flash.rb +78 -0
- data/tests/test_fold.rb +111 -0
- data/tests/test_font_refresh.rb +56 -0
- data/tests/test_func_panel.rb +68 -0
- data/tests/test_grep_apply.rb +46 -0
- data/tests/test_gtk_sync.rb +126 -0
- data/tests/test_hpt_highlight_visible.rb +75 -0
- data/tests/test_hpt_link_highlight.rb +82 -0
- data/tests/test_jump_to_file.rb +41 -0
- data/tests/test_key_bindings.rb +208 -0
- data/tests/test_keylog_panel.rb +101 -0
- data/tests/test_lsp_clangd.rb +281 -0
- data/tests/test_lsp_jump.rb +87 -0
- data/tests/test_lsp_position_encoding.rb +49 -0
- data/tests/test_lsp_symbols.rb +68 -0
- data/tests/test_macro.rb +137 -0
- data/tests/test_markdown_highlight.rb +57 -0
- data/tests/test_mode_badge.rb +41 -0
- data/tests/test_notepad_bindings.rb +67 -0
- data/tests/test_popup_queue.rb +51 -0
- data/tests/test_random_edit.rb +109 -0
- data/tests/test_revert.rb +127 -0
- data/tests/test_startup_file.rb +58 -0
- data/tests/test_status_area.rb +50 -0
- data/tests/test_text_objects.rb +163 -0
- data/tests/test_text_transforms.rb +329 -0
- data/tests/test_tts.rb +68 -0
- data/tests/test_vim_bindings.rb +252 -0
- data/tests/test_vim_conflicts.rb +63 -0
- data/tests/test_window_layout.rb +219 -0
- data/vimamsa.gemspec +3 -1
- data/vm_guest_run.sh +109 -0
- metadata +112 -10
|
@@ -0,0 +1,1054 @@
|
|
|
1
|
+
require "gstreamer"
|
|
2
|
+
require "open3"
|
|
3
|
+
require "tmpdir"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
# Voice Dictation module
|
|
7
|
+
#
|
|
8
|
+
# Records from the microphone with GStreamer and transcribes with a persistent
|
|
9
|
+
# faster-whisper worker, inserting text at the cursor.
|
|
10
|
+
#
|
|
11
|
+
# Real-time (hybrid) flow:
|
|
12
|
+
# C , k -> start fast (no dialog), reusing the last-used initial prompt.
|
|
13
|
+
# C , ; k -> start with the prompt-selection dialog.
|
|
14
|
+
# (either, while recording) -> stop.
|
|
15
|
+
# While you speak, a *preliminary* transcript is inserted incrementally (every few
|
|
16
|
+
# seconds, only the new audio is transcribed, so it scales to long dictations). On
|
|
17
|
+
# stop, the whole recording is re-transcribed once at best quality and replaces the
|
|
18
|
+
# preliminary text in place (a single undo step), ending with a newline.
|
|
19
|
+
#
|
|
20
|
+
# Note: don't edit elsewhere in the buffer while a dictation is in progress — the
|
|
21
|
+
# inserted span is tracked by position, so concurrent edits would misplace the
|
|
22
|
+
# final replacement.
|
|
23
|
+
#
|
|
24
|
+
# Configuration (set in ~/.vimamsa/settings.rb via cnf, all optional):
|
|
25
|
+
# cnf.dictation.python python interpreter (default "python3")
|
|
26
|
+
# cnf.dictation.worker worker script path (default bundled whisper_worker.py)
|
|
27
|
+
# cnf.dictation.model faster-whisper model (default "large-v3")
|
|
28
|
+
# cnf.dictation.language language code (default "en")
|
|
29
|
+
# cnf.dictation.device auto / cuda / cpu (default "auto")
|
|
30
|
+
# cnf.dictation.compute_type compute type (default "float16"; use "int8" on CPU)
|
|
31
|
+
# cnf.dictation.normalize ffmpeg level norm (default true; final pass)
|
|
32
|
+
# cnf.dictation.initial_prompt default prompt (default none; usually set per-dictation
|
|
33
|
+
# in the start dialog)
|
|
34
|
+
# cnf.dictation.chunk_secs preliminary cadence (s) (default 4)
|
|
35
|
+
# cnf.dictation.idle_timeout worker self-exits after (default 300; <=0 disables)
|
|
36
|
+
# this many idle seconds to
|
|
37
|
+
# release VRAM
|
|
38
|
+
# cnf.dictation.startup_timeout give up waiting for the (default 600; <=0 waits
|
|
39
|
+
# model to load after this forever)
|
|
40
|
+
# many seconds
|
|
41
|
+
# cnf.dictation.source GStreamer mic source (default "autoaudiosrc")
|
|
42
|
+
#
|
|
43
|
+
# Requires faster-whisper (pip install faster-whisper) and ffmpeg on PATH.
|
|
44
|
+
|
|
45
|
+
# Fixed capture format (what whisper expects). Used by the recorder and by the
|
|
46
|
+
# preliminary tail-slicer that writes its own chunk WAVs.
|
|
47
|
+
DICT_RATE = 16000
|
|
48
|
+
DICT_CH = 1
|
|
49
|
+
DICT_BITS = 16
|
|
50
|
+
DICT_FRAME = DICT_CH * DICT_BITS / 8
|
|
51
|
+
|
|
52
|
+
# Return the byte offset where PCM samples begin (just past the "data" chunk
|
|
53
|
+
# header), walking the chunk list. Returns nil if not a recognizable WAV.
|
|
54
|
+
def vma_wav_data_offset(bytes)
|
|
55
|
+
return nil unless bytes.bytesize >= 12 && bytes[0, 4] == "RIFF" && bytes[8, 4] == "WAVE"
|
|
56
|
+
off = 12
|
|
57
|
+
while off + 8 <= bytes.bytesize
|
|
58
|
+
cid = bytes[off, 4]
|
|
59
|
+
csz = bytes[off + 4, 4].unpack1("V")
|
|
60
|
+
return off + 8 if cid == "data"
|
|
61
|
+
off += 8 + csz + (csz.odd? ? 1 : 0)
|
|
62
|
+
end
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Canonical 44-byte PCM WAV header for the fixed capture format + given data size.
|
|
67
|
+
def vma_wav_header(data_size)
|
|
68
|
+
byte_rate = DICT_RATE * DICT_FRAME
|
|
69
|
+
block_align = DICT_FRAME
|
|
70
|
+
"RIFF" + [36 + data_size].pack("V") + "WAVE" +
|
|
71
|
+
"fmt " + [16, 1, DICT_CH, DICT_RATE, byte_rate, block_align, DICT_BITS].pack("VvvVVvv") +
|
|
72
|
+
"data" + [data_size].pack("V")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Write raw PCM bytes as a standalone WAV in the fixed capture format.
|
|
76
|
+
def vma_write_wav(path, pcm)
|
|
77
|
+
File.binwrite(path, vma_wav_header(pcm.bytesize) + pcm)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Rewrite the RIFF + data chunk sizes of a WAV to match its real length. Needed
|
|
81
|
+
# because the GStreamer Ruby binding can't send EOS, so wavenc leaves streaming
|
|
82
|
+
# placeholders in the header. All samples are present; only the sizes are wrong.
|
|
83
|
+
def vma_wav_fix_header!(path)
|
|
84
|
+
data = File.binread(path)
|
|
85
|
+
data_content = vma_wav_data_offset(data) or return false
|
|
86
|
+
data_off = data_content - 8
|
|
87
|
+
real_data = data.bytesize - data_content
|
|
88
|
+
data[4, 4] = [data.bytesize - 8].pack("V")
|
|
89
|
+
data[data_off + 4, 4] = [real_data].pack("V")
|
|
90
|
+
File.binwrite(path, data)
|
|
91
|
+
true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Records mic audio to a WAV file (fixed S16LE / 16 kHz / mono).
|
|
95
|
+
class VmaDictationRecorder
|
|
96
|
+
attr_reader :recording, :wav_path
|
|
97
|
+
|
|
98
|
+
def initialize
|
|
99
|
+
@pipeline = nil
|
|
100
|
+
@recording = false
|
|
101
|
+
@wav_path = nil
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def start
|
|
105
|
+
return false if @recording
|
|
106
|
+
|
|
107
|
+
src = cnf.dictation.source! || "autoaudiosrc"
|
|
108
|
+
@wav_path = File.join(Dir.tmpdir, "vma_dictation_#{Process.pid}_#{Time.now.to_i}.wav")
|
|
109
|
+
|
|
110
|
+
desc = "#{src} ! audioconvert ! audioresample ! " \
|
|
111
|
+
"audio/x-raw,format=S16LE,rate=#{DICT_RATE},channels=#{DICT_CH} ! " \
|
|
112
|
+
"wavenc ! filesink location=#{@wav_path}"
|
|
113
|
+
|
|
114
|
+
@pipeline = Gst.parse_launch(desc)
|
|
115
|
+
if @pipeline.nil?
|
|
116
|
+
message("Dictation: failed to build GStreamer pipeline")
|
|
117
|
+
return false
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
@pipeline.set_state(:playing)
|
|
121
|
+
# Block until the pipeline actually reaches PLAYING (e.g. mic opened ok).
|
|
122
|
+
res, = @pipeline.get_state(3 * Gst::SECOND)
|
|
123
|
+
if res == Gst::StateChangeReturn::FAILURE
|
|
124
|
+
message("Dictation: could not start recording — no microphone?")
|
|
125
|
+
@pipeline.set_state(:null)
|
|
126
|
+
@pipeline = nil
|
|
127
|
+
return false
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
@recording = true
|
|
131
|
+
true
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Stop capturing, right now. Split from #finalize so the mic can be released the
|
|
135
|
+
# moment the user asks for it, even while a preliminary pass is still reading
|
|
136
|
+
# the file (rewriting the header under a concurrent reader is what's unsafe,
|
|
137
|
+
# not stopping the pipeline).
|
|
138
|
+
def stop_capture
|
|
139
|
+
return false unless @recording
|
|
140
|
+
@pipeline.set_state(:null)
|
|
141
|
+
@pipeline = nil
|
|
142
|
+
@recording = false
|
|
143
|
+
true
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Fix up the streaming placeholder header of the stopped recording and return
|
|
147
|
+
# its path (or nil). Call only once no one else is reading the file.
|
|
148
|
+
def finalize
|
|
149
|
+
return nil unless @wav_path && File.exist?(@wav_path)
|
|
150
|
+
vma_wav_fix_header!(@wav_path)
|
|
151
|
+
@wav_path
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Stop recording, finalize the WAV header, and return its path (or nil).
|
|
155
|
+
def stop
|
|
156
|
+
return nil unless stop_capture
|
|
157
|
+
finalize
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Persistent faster-whisper worker.
|
|
162
|
+
#
|
|
163
|
+
# Spawns whisper_worker.py once and keeps it alive, so the model is loaded a
|
|
164
|
+
# single time and reused. Communication is line-based JSON over stdin/stdout (see
|
|
165
|
+
# whisper_worker.py). Requests are serialized with a mutex; a dead worker is
|
|
166
|
+
# restarted on the next request.
|
|
167
|
+
class VmaWhisperWorker
|
|
168
|
+
# Why the last start/transcribe attempt failed, for display (nil when fine).
|
|
169
|
+
attr_reader :start_error
|
|
170
|
+
|
|
171
|
+
# proc {|phase, fraction| } called as the worker reports progress of a long
|
|
172
|
+
# phase ("download"/"load"/"transcribe"; fraction is 0..1 or nil). Runs on
|
|
173
|
+
# whichever thread is talking to the worker, never the main loop.
|
|
174
|
+
attr_accessor :on_progress
|
|
175
|
+
|
|
176
|
+
def initialize
|
|
177
|
+
@mutex = Mutex.new
|
|
178
|
+
@stdin = nil
|
|
179
|
+
@stdout = nil
|
|
180
|
+
@wait = nil
|
|
181
|
+
@ready = false
|
|
182
|
+
@start_error = nil
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Start the worker and wait for the model to finish loading. Blocks the calling
|
|
186
|
+
# thread (never the main loop): call it from a background thread at dictation
|
|
187
|
+
# start so the load overlaps with the user speaking. Safe to call repeatedly.
|
|
188
|
+
def ensure_started
|
|
189
|
+
@mutex.synchronize do
|
|
190
|
+
_spawn unless _alive?
|
|
191
|
+
_await_ready
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# True when a request would be served without waiting for the model to load.
|
|
196
|
+
# Deliberately lock-free so a preliminary pass can cheaply skip itself during a
|
|
197
|
+
# cold start instead of queueing on the mutex behind the load. Racing with
|
|
198
|
+
# _spawn/_await_ready is benign: at worst one preliminary pass is skipped, or
|
|
199
|
+
# one is attempted a moment early and blocks as it used to.
|
|
200
|
+
def ready?
|
|
201
|
+
@ready && _alive?
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Transcribe a WAV file with optional per-request overrides:
|
|
205
|
+
# :initial_prompt, :beam_size, :normalize, :vad
|
|
206
|
+
# Returns { ok: true, text: "..." } or { ok: false, error: "..." }.
|
|
207
|
+
def transcribe(wav, opts = {})
|
|
208
|
+
@mutex.synchronize do
|
|
209
|
+
_spawn unless _alive?
|
|
210
|
+
return { ok: false, error: @start_error || "worker not running" } unless _alive?
|
|
211
|
+
return { ok: false, error: @start_error || "model not ready" } unless _await_ready
|
|
212
|
+
|
|
213
|
+
begin
|
|
214
|
+
req = { "path" => wav }
|
|
215
|
+
req["initial_prompt"] = opts[:initial_prompt] if opts[:initial_prompt]
|
|
216
|
+
req["beam_size"] = opts[:beam_size] if opts[:beam_size]
|
|
217
|
+
req["normalize"] = opts[:normalize] if opts.key?(:normalize)
|
|
218
|
+
req["vad"] = opts[:vad] if opts.key?(:vad)
|
|
219
|
+
|
|
220
|
+
@stdin.puts(JSON.generate(req))
|
|
221
|
+
@stdin.flush
|
|
222
|
+
resp = _read_response
|
|
223
|
+
return { ok: false, error: "worker closed unexpectedly" } if resp.nil?
|
|
224
|
+
if resp["ok"]
|
|
225
|
+
{ ok: true, text: (resp["text"] || "").strip }
|
|
226
|
+
else
|
|
227
|
+
{ ok: false, error: resp["error"] || "unknown error" }
|
|
228
|
+
end
|
|
229
|
+
rescue => e
|
|
230
|
+
{ ok: false, error: "#{e.class}: #{e.message}" }
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def stop
|
|
236
|
+
@mutex.synchronize do
|
|
237
|
+
begin
|
|
238
|
+
@stdin&.close
|
|
239
|
+
rescue
|
|
240
|
+
nil
|
|
241
|
+
end
|
|
242
|
+
@stdin = @stdout = @wait = nil
|
|
243
|
+
@ready = false
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
private
|
|
248
|
+
|
|
249
|
+
def _alive?
|
|
250
|
+
@wait && @wait.alive?
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def _spawn
|
|
254
|
+
@ready = false
|
|
255
|
+
@start_error = nil
|
|
256
|
+
# Close stale handles from a previous (now-dead) worker before respawning.
|
|
257
|
+
begin; @stdin&.close; rescue; end
|
|
258
|
+
begin; @stdout&.close; rescue; end
|
|
259
|
+
|
|
260
|
+
python = cnf.dictation.python! || "python3"
|
|
261
|
+
worker = cnf.dictation.worker! || ppath("modules/dictation/whisper_worker.py")
|
|
262
|
+
idle = cnf.dictation.idle_timeout!
|
|
263
|
+
idle = 300 if idle.nil?
|
|
264
|
+
|
|
265
|
+
cmd = [python, worker,
|
|
266
|
+
"--model", (cnf.dictation.model! || "large-v3"),
|
|
267
|
+
"--language", (cnf.dictation.language! || "en"),
|
|
268
|
+
"--device", (cnf.dictation.device! || "auto"),
|
|
269
|
+
"--compute-type", (cnf.dictation.compute_type! || "float16"),
|
|
270
|
+
"--idle-timeout", idle.to_s]
|
|
271
|
+
cmd << "--no-normalize" if cnf.dictation.normalize! == false
|
|
272
|
+
ip = cnf.dictation.initial_prompt!
|
|
273
|
+
cmd += ["--initial-prompt", ip.to_s] if ip && !ip.to_s.empty?
|
|
274
|
+
|
|
275
|
+
@stdin, @stdout, @wait = Open3.popen2(*cmd)
|
|
276
|
+
rescue => e
|
|
277
|
+
@start_error = "could not start worker (#{e.class}: #{e.message})"
|
|
278
|
+
@stdin = @stdout = @wait = nil
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Read protocol lines until a terminating (non-progress) one arrives, handing
|
|
282
|
+
# each {"progress": …} line to the observer. Returns the parsed terminating
|
|
283
|
+
# line, or nil if the worker closed its pipe first.
|
|
284
|
+
def _read_response
|
|
285
|
+
loop do
|
|
286
|
+
line = @stdout.gets
|
|
287
|
+
return nil if line.nil?
|
|
288
|
+
resp = JSON.parse(line) rescue nil
|
|
289
|
+
next if resp.nil? # ignore anything that isn't protocol JSON
|
|
290
|
+
if (p = resp["progress"])
|
|
291
|
+
_notify_progress(p)
|
|
292
|
+
next
|
|
293
|
+
end
|
|
294
|
+
return resp
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Fan a progress report out to whoever registered interest. Called on the
|
|
299
|
+
# worker-facing thread, so the observer is responsible for getting itself onto
|
|
300
|
+
# the main thread before touching any widget.
|
|
301
|
+
def _notify_progress(p)
|
|
302
|
+
@on_progress&.call(p["phase"].to_s, p["fraction"])
|
|
303
|
+
rescue => e
|
|
304
|
+
debug "Dictation: progress handler error: #{e}"
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Block until the worker prints its readiness line (model loaded). Cached.
|
|
308
|
+
#
|
|
309
|
+
# Bounded, because this runs while holding @mutex: a worker that never becomes
|
|
310
|
+
# ready (model download stalled, wedged CUDA init) would otherwise hang every
|
|
311
|
+
# later request behind it forever, leaving the editor stuck on "finalizing…"
|
|
312
|
+
# with nothing to report. A first-ever run legitimately downloads several GB,
|
|
313
|
+
# so the default is generous.
|
|
314
|
+
def _await_ready
|
|
315
|
+
return true if @ready
|
|
316
|
+
return false unless @stdout
|
|
317
|
+
limit = cnf.dictation.startup_timeout! || 600
|
|
318
|
+
|
|
319
|
+
loop do
|
|
320
|
+
# The limit is per line, not for the whole startup: progress lines prove
|
|
321
|
+
# the worker is alive and downloading, so they legitimately extend the
|
|
322
|
+
# wait. Only silence for the full period counts as wedged.
|
|
323
|
+
if limit > 0 && !@stdout.wait_readable(limit)
|
|
324
|
+
@start_error = "no progress from the speech model for #{limit}s (download stalled?)"
|
|
325
|
+
return false
|
|
326
|
+
end
|
|
327
|
+
line = @stdout.gets
|
|
328
|
+
if line.nil?
|
|
329
|
+
@start_error = "worker exited before becoming ready (check faster-whisper/ffmpeg)"
|
|
330
|
+
return false
|
|
331
|
+
end
|
|
332
|
+
resp = JSON.parse(line) rescue nil
|
|
333
|
+
next if resp.nil?
|
|
334
|
+
if (p = resp["progress"])
|
|
335
|
+
_notify_progress(p)
|
|
336
|
+
next
|
|
337
|
+
end
|
|
338
|
+
if resp["ready"]
|
|
339
|
+
@ready = true
|
|
340
|
+
return true
|
|
341
|
+
end
|
|
342
|
+
@start_error = resp["error"] || "worker failed to load model"
|
|
343
|
+
return false
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
def dictation_worker
|
|
349
|
+
$vma_dictation_worker ||= VmaWhisperWorker.new
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# Drives one real-time dictation: recording + incremental preliminary inserts +
|
|
353
|
+
# final high-quality replacement.
|
|
354
|
+
class VmaDictationSession
|
|
355
|
+
def initialize
|
|
356
|
+
@active = false
|
|
357
|
+
@finalizing = false
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
def active?
|
|
361
|
+
@active
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Begin a dictation. `prompt` (or nil) is the whisper initial_prompt.
|
|
365
|
+
def start(prompt)
|
|
366
|
+
return false if @active
|
|
367
|
+
@recorder = VmaDictationRecorder.new
|
|
368
|
+
return false unless @recorder.start
|
|
369
|
+
|
|
370
|
+
@prompt = (prompt && !prompt.empty?) ? prompt : nil
|
|
371
|
+
@buf = vma.buf
|
|
372
|
+
@dict_start = @buf.pos
|
|
373
|
+
@dict_len = 0
|
|
374
|
+
@prelim_text = ""
|
|
375
|
+
@pcm_off = nil
|
|
376
|
+
@active = true
|
|
377
|
+
@buf.new_undo_group
|
|
378
|
+
|
|
379
|
+
# The mic is already live either way; what differs is whether anything can be
|
|
380
|
+
# transcribed yet. Say which, because a cold start is silent for as long as
|
|
381
|
+
# the model takes to load (seconds warm, minutes on a first download) and
|
|
382
|
+
# otherwise looks like dictation simply not working.
|
|
383
|
+
if dictation_worker.ready?
|
|
384
|
+
_status(:listening)
|
|
385
|
+
message("Dictation: listening… (toggle again to stop)")
|
|
386
|
+
else
|
|
387
|
+
_status(:loading)
|
|
388
|
+
message("Dictation: loading speech model… (already listening; text appears once loaded)")
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# ensure_started blocks until the model is loaded, so report the outcome
|
|
392
|
+
# back on the main thread once it lands. Progress arrives meanwhile.
|
|
393
|
+
dictation_worker.on_progress = method(:_progress_update)
|
|
394
|
+
Thread.new do
|
|
395
|
+
ok = dictation_worker.ensure_started
|
|
396
|
+
GLib::Idle.add { _on_worker_ready(ok); false }
|
|
397
|
+
end
|
|
398
|
+
@running = true
|
|
399
|
+
@prelim_thread = Thread.new { _prelim_loop }
|
|
400
|
+
true
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Stop recording and produce the final high-quality transcript, replacing the
|
|
404
|
+
# preliminary text. Runs the heavy work off the main thread.
|
|
405
|
+
# add_newline: end the finalized text with a trailing "\n" (default). The space
|
|
406
|
+
# toggle in dictation mode passes false so pausing doesn't break the line.
|
|
407
|
+
# restart: begin a fresh dictation segment once the finalize lands (used by the
|
|
408
|
+
# Enter key to continue speaking on the new line).
|
|
409
|
+
def stop(add_newline: true, restart: false)
|
|
410
|
+
return unless @active
|
|
411
|
+
@active = false
|
|
412
|
+
@running = false
|
|
413
|
+
@finalize_newline = add_newline
|
|
414
|
+
@restart_after = restart
|
|
415
|
+
@finalizing = true
|
|
416
|
+
# On a cold start the final pass just queues behind the model load, so don't
|
|
417
|
+
# claim to be transcribing while we are really still waiting for the model
|
|
418
|
+
# (a first-use download can sit here for minutes). _on_worker_ready promotes
|
|
419
|
+
# this to :transcribing once the model actually lands.
|
|
420
|
+
if dictation_worker.ready?
|
|
421
|
+
_status(:transcribing)
|
|
422
|
+
message("Dictation: finalizing…")
|
|
423
|
+
else
|
|
424
|
+
_status(:queued)
|
|
425
|
+
message("Dictation: waiting for the speech model before transcribing…")
|
|
426
|
+
end
|
|
427
|
+
# Release the mic here, not after the join below: on a cold start the
|
|
428
|
+
# preliminary pass can still be blocked waiting for the model to load, and
|
|
429
|
+
# recording through that wait would append seconds of audio the user
|
|
430
|
+
# recorded after they asked to stop.
|
|
431
|
+
@recorder.stop_capture
|
|
432
|
+
|
|
433
|
+
Thread.new do
|
|
434
|
+
@prelim_thread&.join # no more preliminary file reads / inserts
|
|
435
|
+
wav = @recorder.finalize # rewrite header (safe: no concurrent reader)
|
|
436
|
+
if wav.nil?
|
|
437
|
+
GLib::Idle.add { @finalizing = false; _status(nil); message("Dictation: nothing recorded"); false }
|
|
438
|
+
else
|
|
439
|
+
res = dictation_worker.transcribe(wav, _final_opts)
|
|
440
|
+
File.delete(wav) if wav && File.exist?(wav)
|
|
441
|
+
GLib::Idle.add { _finalize_replace(res); false }
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
private
|
|
447
|
+
|
|
448
|
+
# Status-area slot key, and the text/style for each phase of a dictation.
|
|
449
|
+
STATUS_KEY = :dictation
|
|
450
|
+
# Base labels, without trailing ellipsis: #_status appends either "…" or a
|
|
451
|
+
# percentage, so a phase reads "DOWNLOADING MODEL 42%" once the worker starts
|
|
452
|
+
# reporting and "DOWNLOADING MODEL…" before it can.
|
|
453
|
+
STATUS_PHASES = {
|
|
454
|
+
loading: ["🎙 LOADING MODEL", "status-busy"],
|
|
455
|
+
downloading: ["⬇ DOWNLOADING MODEL", "status-busy"],
|
|
456
|
+
listening: ["🎙 LISTENING", "status-active"],
|
|
457
|
+
queued: ["⏳ WAITING FOR MODEL", "status-busy"],
|
|
458
|
+
transcribing: ["⏳ TRANSCRIBING", "status-busy"],
|
|
459
|
+
failed: ["🎙 MODEL FAILED", "status-error"],
|
|
460
|
+
}.freeze
|
|
461
|
+
|
|
462
|
+
# Steady states rather than work in progress: no ellipsis, no percentage.
|
|
463
|
+
STATUS_STEADY = %i[listening failed].freeze
|
|
464
|
+
|
|
465
|
+
# (main thread) Show a dictation phase in the status area, or clear the slot
|
|
466
|
+
# when phase is nil. Unlike the minibuf messages this stays put, so the user
|
|
467
|
+
# can see at a glance whether it is still listening, still loading, or done.
|
|
468
|
+
# `fraction` (0..1) renders as a percentage.
|
|
469
|
+
def _status(phase, fraction = nil)
|
|
470
|
+
if phase.nil?
|
|
471
|
+
vma.gui&.set_status_indicator(STATUS_KEY, nil)
|
|
472
|
+
return
|
|
473
|
+
end
|
|
474
|
+
text, css = STATUS_PHASES[phase]
|
|
475
|
+
return if text.nil?
|
|
476
|
+
if fraction
|
|
477
|
+
text = "#{text} #{(fraction.to_f * 100).round}%"
|
|
478
|
+
elsif !STATUS_STEADY.include?(phase)
|
|
479
|
+
text = "#{text}…"
|
|
480
|
+
end
|
|
481
|
+
vma.gui&.set_status_indicator(STATUS_KEY, text, css_class: css)
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# (worker thread) A progress report from the worker. Hop onto the main thread,
|
|
485
|
+
# then map the phase onto whichever status this session is currently in — a
|
|
486
|
+
# download can be running while still listening or while already finalizing,
|
|
487
|
+
# and the two must not show the same thing.
|
|
488
|
+
def _progress_update(phase, fraction)
|
|
489
|
+
GLib::Idle.add do
|
|
490
|
+
case phase
|
|
491
|
+
when "download"
|
|
492
|
+
_status(:downloading, fraction) if @active || @finalizing
|
|
493
|
+
when "load"
|
|
494
|
+
_status(@active ? :loading : (@finalizing ? :queued : nil))
|
|
495
|
+
when "transcribe"
|
|
496
|
+
# Preliminary passes report progress too, but they run while listening
|
|
497
|
+
# and must not overwrite LISTENING with TRANSCRIBING.
|
|
498
|
+
_status(:transcribing, fraction) if @finalizing && !@active
|
|
499
|
+
end
|
|
500
|
+
false
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# (main thread) The model finished loading, or failed to. Which state that
|
|
505
|
+
# lands in depends on whether the user has stopped in the meantime: still
|
|
506
|
+
# listening, or already stopped with a finalize queued behind the load. Once
|
|
507
|
+
# the finalize has completed, neither holds and the slot is left alone.
|
|
508
|
+
def _on_worker_ready(ok)
|
|
509
|
+
if @active
|
|
510
|
+
if ok
|
|
511
|
+
_status(:listening)
|
|
512
|
+
message("Dictation: model loaded — listening")
|
|
513
|
+
else
|
|
514
|
+
_status(:failed)
|
|
515
|
+
message("Dictation: could not load speech model — #{dictation_worker.start_error}")
|
|
516
|
+
end
|
|
517
|
+
elsif @finalizing
|
|
518
|
+
if ok
|
|
519
|
+
_status(:transcribing)
|
|
520
|
+
message("Dictation: model loaded — transcribing…")
|
|
521
|
+
else
|
|
522
|
+
_status(:failed)
|
|
523
|
+
message("Dictation: could not load speech model — #{dictation_worker.start_error}")
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
def _chunk_secs
|
|
529
|
+
(cnf.dictation.chunk_secs! || 4).to_f
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
# Require at least ~1s of new audio before transcribing a preliminary chunk.
|
|
533
|
+
def _min_new_bytes
|
|
534
|
+
DICT_RATE * DICT_FRAME
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
def _prelim_loop
|
|
538
|
+
elapsed = 0.0
|
|
539
|
+
while @running
|
|
540
|
+
sleep 0.2
|
|
541
|
+
elapsed += 0.2
|
|
542
|
+
next if elapsed < _chunk_secs
|
|
543
|
+
elapsed = 0.0
|
|
544
|
+
_process_tail
|
|
545
|
+
end
|
|
546
|
+
rescue => e
|
|
547
|
+
debug "Dictation prelim loop error: #{e}"
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# Transcribe the not-yet-processed tail of the growing recording and append it.
|
|
551
|
+
def _process_tail
|
|
552
|
+
# Cold start: the model is still loading. Preliminary passes are best-effort,
|
|
553
|
+
# so skip rather than block — a blocked pass holds the worker mutex, which is
|
|
554
|
+
# what the final pass then has to wait on, turning a slow start into a stall
|
|
555
|
+
# with the editor sitting on "finalizing…". The audio isn't lost: @pcm_off
|
|
556
|
+
# only advances on a pass that actually runs, so the next tick picks up the
|
|
557
|
+
# whole tail.
|
|
558
|
+
return unless dictation_worker.ready?
|
|
559
|
+
|
|
560
|
+
path = @recorder.wav_path
|
|
561
|
+
return unless path && File.exist?(path)
|
|
562
|
+
|
|
563
|
+
if @pcm_off.nil?
|
|
564
|
+
head = File.binread(path, 4096)
|
|
565
|
+
doff = vma_wav_data_offset(head) or return
|
|
566
|
+
@pcm_off = doff
|
|
567
|
+
end
|
|
568
|
+
|
|
569
|
+
fsize = File.size(path)
|
|
570
|
+
endpos = fsize - ((fsize - @pcm_off) % DICT_FRAME) # frame-align
|
|
571
|
+
newlen = endpos - @pcm_off
|
|
572
|
+
return if newlen < _min_new_bytes
|
|
573
|
+
|
|
574
|
+
pcm = File.open(path, "rb") { |f| f.seek(@pcm_off); f.read(newlen) }
|
|
575
|
+
return if pcm.nil? || pcm.bytesize < newlen # short read; retry next tick
|
|
576
|
+
@pcm_off = endpos
|
|
577
|
+
|
|
578
|
+
tmp = File.join(Dir.tmpdir, "vma_dict_chunk_#{Process.pid}_#{(Time.now.to_f * 1000).to_i}.wav")
|
|
579
|
+
vma_write_wav(tmp, pcm)
|
|
580
|
+
res = dictation_worker.transcribe(tmp, _prelim_opts)
|
|
581
|
+
File.delete(tmp) if File.exist?(tmp)
|
|
582
|
+
|
|
583
|
+
return unless res[:ok] && res[:text] && !res[:text].empty?
|
|
584
|
+
text = res[:text]
|
|
585
|
+
GLib::Idle.add { _append_prelim(text); false }
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
# Fast, low-context settings for the live preliminary passes. Carry the tail of
|
|
589
|
+
# the accumulated text forward as a prompt to improve cross-chunk continuity.
|
|
590
|
+
def _prelim_opts
|
|
591
|
+
ctx = [@prompt, @prelim_text[-200..]].compact.join(" ").strip
|
|
592
|
+
opts = { beam_size: 1, normalize: false, vad: false }
|
|
593
|
+
opts[:initial_prompt] = ctx unless ctx.empty?
|
|
594
|
+
opts
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
# High-quality settings for the final pass.
|
|
598
|
+
def _final_opts
|
|
599
|
+
opts = { beam_size: 5, vad: true, normalize: cnf.dictation.normalize! != false }
|
|
600
|
+
opts[:initial_prompt] = @prompt if @prompt
|
|
601
|
+
opts
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
# True once the target buffer has been closed out from under an async pass:
|
|
605
|
+
# `Buffer#view` returns nil (gui_close_buffer removed it), so any GTK sync
|
|
606
|
+
# would crash. Text mutation would also be pointless once the buffer is gone.
|
|
607
|
+
def _target_gone?
|
|
608
|
+
@buf.nil? || @buf.view.nil?
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
# (main thread) Append a preliminary phrase to the tracked span.
|
|
612
|
+
def _append_prelim(text)
|
|
613
|
+
return if _target_gone?
|
|
614
|
+
t = (@dict_len > 0 ? " " : "") + text
|
|
615
|
+
@buf.insert_txt_at(t, @dict_start + @dict_len)
|
|
616
|
+
@dict_len += t.size
|
|
617
|
+
@prelim_text = (@prelim_text + " " + text).strip
|
|
618
|
+
@buf.view.handle_deltas
|
|
619
|
+
end
|
|
620
|
+
|
|
621
|
+
# (main thread) Replace the preliminary span with the final transcript.
|
|
622
|
+
def _finalize_replace(res)
|
|
623
|
+
# Transcription is done however this turns out; clear the slot up front so
|
|
624
|
+
# every path below (including the early returns) leaves it empty. A restart
|
|
625
|
+
# at the end re-populates it via start.
|
|
626
|
+
@finalizing = false
|
|
627
|
+
_status(nil)
|
|
628
|
+
if !res[:ok]
|
|
629
|
+
message("Dictation: transcription failed — #{res[:error]}")
|
|
630
|
+
return
|
|
631
|
+
end
|
|
632
|
+
if _target_gone?
|
|
633
|
+
message("Dictation: target buffer closed — transcript discarded")
|
|
634
|
+
return
|
|
635
|
+
end
|
|
636
|
+
final = res[:text].to_s
|
|
637
|
+
@buf.delete_range(@dict_start, @dict_start + @dict_len - 1) if @dict_len > 0
|
|
638
|
+
@dict_len = 0
|
|
639
|
+
|
|
640
|
+
if final.empty?
|
|
641
|
+
# Nothing recognized: still honor an explicit newline request (e.g. Enter)
|
|
642
|
+
# so the caret moves to the next line, then optionally continue dictating.
|
|
643
|
+
if @finalize_newline
|
|
644
|
+
@buf.insert_txt_at("\n", @dict_start)
|
|
645
|
+
@buf.view.handle_deltas
|
|
646
|
+
@buf.set_pos(@dict_start + 1)
|
|
647
|
+
else
|
|
648
|
+
@buf.view.handle_deltas
|
|
649
|
+
end
|
|
650
|
+
@buf.new_undo_group
|
|
651
|
+
message("Dictation: no speech recognized")
|
|
652
|
+
start(@prompt) if @restart_after
|
|
653
|
+
return
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
# Optionally append a newline so the dictation ends a line; leave the cursor
|
|
657
|
+
# after the inserted text (start of the next line when a newline was added).
|
|
658
|
+
text = final + (@finalize_newline ? "\n" : "")
|
|
659
|
+
@buf.insert_txt_at(text, @dict_start)
|
|
660
|
+
@buf.view.handle_deltas
|
|
661
|
+
@buf.set_pos(@dict_start + text.size)
|
|
662
|
+
@buf.new_undo_group
|
|
663
|
+
message("Dictation: done (#{final.length} chars)")
|
|
664
|
+
|
|
665
|
+
# Enter continues the dictation on the new line: begin a fresh segment now that
|
|
666
|
+
# the span replace is done (main thread, @active already false — span tracking
|
|
667
|
+
# for the new segment starts clean at the new caret position).
|
|
668
|
+
start(@prompt) if @restart_after
|
|
669
|
+
end
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
def dictation_session
|
|
673
|
+
$vma_dict_session ||= VmaDictationSession.new
|
|
674
|
+
end
|
|
675
|
+
|
|
676
|
+
# ── Saved prompts ───────────────────────────────────────────────────────────────
|
|
677
|
+
|
|
678
|
+
def vma_dict_prompts_path
|
|
679
|
+
get_dot_path("dictation_prompts.json")
|
|
680
|
+
end
|
|
681
|
+
|
|
682
|
+
def vma_dict_load_prompts
|
|
683
|
+
path = vma_dict_prompts_path
|
|
684
|
+
if File.exist?(path)
|
|
685
|
+
(JSON.parse(File.read(path)) rescue nil) || { "prompts" => [], "last" => "" }
|
|
686
|
+
else
|
|
687
|
+
{ "prompts" => [], "last" => "" }
|
|
688
|
+
end
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
def vma_dict_save_prompts(data)
|
|
692
|
+
File.write(vma_dict_prompts_path, JSON.pretty_generate(data))
|
|
693
|
+
rescue => e
|
|
694
|
+
debug "Dictation: could not save prompts: #{e}"
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
# Modal dialog shown when starting a dictation: pick a saved prompt, edit it, or
|
|
698
|
+
# add a new one. Calls `on_start` with the chosen prompt (or nil) on Start.
|
|
699
|
+
# ── Model selection ─────────────────────────────────────────────────────────────
|
|
700
|
+
|
|
701
|
+
# Presets offered by Modules ▸ "Select Dictation Model…", as
|
|
702
|
+
# [model id, label, language]. All are CTranslate2 format, which faster-whisper
|
|
703
|
+
# loads straight from the HuggingFace id with no conversion step.
|
|
704
|
+
#
|
|
705
|
+
# The language travels with the model on purpose: a Finnish fine-tune transcribes
|
|
706
|
+
# badly while the language stays "en", and nothing in the UI would show that
|
|
707
|
+
# mismatch, so choosing a preset sets both.
|
|
708
|
+
VMA_DICT_MODELS = [
|
|
709
|
+
["large-v3", "large-v3 — multilingual, general purpose (default)", "en"],
|
|
710
|
+
["Finnish-NLP/whisper-large-finnish-v3-ct2", "Finnish — large-v3 fine-tune (best Finnish accuracy)", "fi"],
|
|
711
|
+
["RASMUS/whisper-large-v3-turbo-finnish-ct2", "Finnish — large-v3-turbo fine-tune (faster)", "fi"],
|
|
712
|
+
["mpasila/faster-whisper-large-finnish-v3", "Finnish — large-v3 fine-tune (alternate packaging)", "fi"],
|
|
713
|
+
["tiny.en", "tiny.en — English only, fast and low quality", "en"],
|
|
714
|
+
].freeze
|
|
715
|
+
|
|
716
|
+
# Apply a model choice: update the live config, remember it, and retire the
|
|
717
|
+
# running worker so the next dictation loads the new weights (the worker holds
|
|
718
|
+
# one model for its whole life).
|
|
719
|
+
def vma_dict_apply_model(model, language)
|
|
720
|
+
return if model.nil? || model.to_s.strip.empty?
|
|
721
|
+
cnf.dictation.model = model
|
|
722
|
+
cnf.dictation.language = language unless language.nil? || language.to_s.empty?
|
|
723
|
+
|
|
724
|
+
data = ($vma_dict_prompts ||= vma_dict_load_prompts)
|
|
725
|
+
data["model"] = model
|
|
726
|
+
data["language"] = cnf.dictation.language!
|
|
727
|
+
vma_dict_save_prompts(data)
|
|
728
|
+
|
|
729
|
+
$vma_dictation_worker&.stop
|
|
730
|
+
message("Dictation model: #{model} (#{cnf.dictation.language!}) — loads on next dictation")
|
|
731
|
+
end
|
|
732
|
+
|
|
733
|
+
# Restore the stored model choice at init, so it survives a restart without the
|
|
734
|
+
# user having to hand-edit settings.rb.
|
|
735
|
+
def vma_dict_restore_model
|
|
736
|
+
data = ($vma_dict_prompts ||= vma_dict_load_prompts)
|
|
737
|
+
return if data["model"].nil? || data["model"].to_s.empty?
|
|
738
|
+
cnf.dictation.model = data["model"]
|
|
739
|
+
cnf.dictation.language = data["language"] if data["language"]
|
|
740
|
+
end
|
|
741
|
+
|
|
742
|
+
# Pick the speech model (and its language). Presets in a dropdown, plus a free
|
|
743
|
+
# text field for any other CTranslate2 model id or local path.
|
|
744
|
+
def vma_dictation_model_dialog
|
|
745
|
+
current = cnf.dictation.model! || "large-v3"
|
|
746
|
+
|
|
747
|
+
window = Gtk::Window.new
|
|
748
|
+
window.set_transient_for($vmag.window) if $vmag&.window
|
|
749
|
+
window.modal = true
|
|
750
|
+
window.title = "Select dictation model"
|
|
751
|
+
|
|
752
|
+
frame = Gtk::Frame.new
|
|
753
|
+
window.set_child(frame)
|
|
754
|
+
vbox = Gtk::Box.new(:vertical, 8)
|
|
755
|
+
vbox.margin = 12
|
|
756
|
+
frame.set_child(vbox)
|
|
757
|
+
|
|
758
|
+
vbox.append(Gtk::Label.new("Speech recognition model:"))
|
|
759
|
+
|
|
760
|
+
labels = VMA_DICT_MODELS.map { |_id, label, _lang| label }
|
|
761
|
+
dropdown = Gtk::DropDown.new(Gtk::StringList.new(labels), nil)
|
|
762
|
+
vbox.append(dropdown)
|
|
763
|
+
|
|
764
|
+
entry = Gtk::Entry.new
|
|
765
|
+
entry.text = current
|
|
766
|
+
entry.hexpand = true
|
|
767
|
+
vbox.append(entry)
|
|
768
|
+
|
|
769
|
+
lang_box = Gtk::Box.new(:horizontal, 8)
|
|
770
|
+
lang_box.append(Gtk::Label.new("Language code:"))
|
|
771
|
+
lang_entry = Gtk::Entry.new
|
|
772
|
+
lang_entry.text = (cnf.dictation.language! || "en").to_s
|
|
773
|
+
lang_box.append(lang_entry)
|
|
774
|
+
vbox.append(lang_box)
|
|
775
|
+
|
|
776
|
+
note = Gtk::Label.new("Any CTranslate2 model id or local path works. " \
|
|
777
|
+
"A model not yet downloaded is fetched on first use.")
|
|
778
|
+
note.wrap = true
|
|
779
|
+
note.xalign = 0
|
|
780
|
+
vbox.append(note)
|
|
781
|
+
|
|
782
|
+
sel = VMA_DICT_MODELS.index { |id, _l, _lang| id == current }
|
|
783
|
+
dropdown.selected = sel if sel
|
|
784
|
+
dropdown.signal_connect("notify::selected") do
|
|
785
|
+
id, _label, lang = VMA_DICT_MODELS[dropdown.selected]
|
|
786
|
+
if id
|
|
787
|
+
entry.text = id
|
|
788
|
+
lang_entry.text = lang.to_s
|
|
789
|
+
end
|
|
790
|
+
end
|
|
791
|
+
|
|
792
|
+
hbox = Gtk::Box.new(:horizontal, 8)
|
|
793
|
+
hbox.halign = :end
|
|
794
|
+
cancel_btn = Gtk::Button.new(:label => "Cancel")
|
|
795
|
+
ok_btn = Gtk::Button.new(:label => "Use model")
|
|
796
|
+
hbox.append(cancel_btn)
|
|
797
|
+
hbox.append(ok_btn)
|
|
798
|
+
vbox.append(hbox)
|
|
799
|
+
|
|
800
|
+
apply = proc do
|
|
801
|
+
vma_dict_apply_model(entry.text.to_s.strip, lang_entry.text.to_s.strip)
|
|
802
|
+
window.destroy
|
|
803
|
+
end
|
|
804
|
+
ok_btn.signal_connect("clicked") { apply.call }
|
|
805
|
+
cancel_btn.signal_connect("clicked") { window.destroy }
|
|
806
|
+
|
|
807
|
+
press = Gtk::EventControllerKey.new
|
|
808
|
+
press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
|
|
809
|
+
window.add_controller(press)
|
|
810
|
+
press.signal_connect "key-pressed" do |_g, keyval, _kc, _y|
|
|
811
|
+
if keyval == Gdk::Keyval::KEY_Return
|
|
812
|
+
apply.call
|
|
813
|
+
true
|
|
814
|
+
elsif keyval == Gdk::Keyval::KEY_Escape
|
|
815
|
+
window.destroy
|
|
816
|
+
true
|
|
817
|
+
else
|
|
818
|
+
false
|
|
819
|
+
end
|
|
820
|
+
end
|
|
821
|
+
|
|
822
|
+
window.show
|
|
823
|
+
end
|
|
824
|
+
|
|
825
|
+
def vma_dictation_prompt_dialog(&on_start)
|
|
826
|
+
data = ($vma_dict_prompts ||= vma_dict_load_prompts)
|
|
827
|
+
prompts = data["prompts"] || []
|
|
828
|
+
last = data["last"] || ""
|
|
829
|
+
|
|
830
|
+
window = Gtk::Window.new
|
|
831
|
+
window.set_transient_for($vmag.window) if $vmag&.window
|
|
832
|
+
window.modal = true
|
|
833
|
+
window.title = "Start dictation"
|
|
834
|
+
|
|
835
|
+
frame = Gtk::Frame.new
|
|
836
|
+
window.set_child(frame)
|
|
837
|
+
vbox = Gtk::Box.new(:vertical, 8)
|
|
838
|
+
vbox.margin = 12
|
|
839
|
+
frame.set_child(vbox)
|
|
840
|
+
|
|
841
|
+
vbox.append(Gtk::Label.new("Initial prompt (biases recognition toward these words):"))
|
|
842
|
+
|
|
843
|
+
items = ["(none)"] + prompts
|
|
844
|
+
strlist = Gtk::StringList.new(items)
|
|
845
|
+
dropdown = Gtk::DropDown.new(strlist, nil)
|
|
846
|
+
vbox.append(dropdown)
|
|
847
|
+
|
|
848
|
+
entry = Gtk::Entry.new
|
|
849
|
+
entry.text = last
|
|
850
|
+
entry.hexpand = true
|
|
851
|
+
vbox.append(entry)
|
|
852
|
+
|
|
853
|
+
sel = items.index(last)
|
|
854
|
+
dropdown.selected = sel if sel
|
|
855
|
+
dropdown.signal_connect("notify::selected") do
|
|
856
|
+
i = dropdown.selected
|
|
857
|
+
entry.text = (i && i > 0) ? items[i] : ""
|
|
858
|
+
end
|
|
859
|
+
|
|
860
|
+
hbox = Gtk::Box.new(:horizontal, 8)
|
|
861
|
+
hbox.halign = :end
|
|
862
|
+
save_btn = Gtk::Button.new(:label => "Save as new")
|
|
863
|
+
cancel_btn = Gtk::Button.new(:label => "Cancel")
|
|
864
|
+
start_btn = Gtk::Button.new(:label => "Start")
|
|
865
|
+
hbox.append(save_btn)
|
|
866
|
+
hbox.append(cancel_btn)
|
|
867
|
+
hbox.append(start_btn)
|
|
868
|
+
vbox.append(hbox)
|
|
869
|
+
|
|
870
|
+
do_start = proc do
|
|
871
|
+
prompt = entry.text.to_s
|
|
872
|
+
data["last"] = prompt
|
|
873
|
+
vma_dict_save_prompts(data)
|
|
874
|
+
window.destroy
|
|
875
|
+
on_start.call(prompt.empty? ? nil : prompt)
|
|
876
|
+
end
|
|
877
|
+
|
|
878
|
+
save_btn.signal_connect("clicked") do
|
|
879
|
+
t = entry.text.to_s.strip
|
|
880
|
+
unless t.empty? || prompts.include?(t)
|
|
881
|
+
prompts << t
|
|
882
|
+
data["prompts"] = prompts
|
|
883
|
+
vma_dict_save_prompts(data)
|
|
884
|
+
strlist.append(t)
|
|
885
|
+
items << t
|
|
886
|
+
end
|
|
887
|
+
end
|
|
888
|
+
start_btn.signal_connect("clicked") { do_start.call }
|
|
889
|
+
cancel_btn.signal_connect("clicked") { window.destroy }
|
|
890
|
+
|
|
891
|
+
press = Gtk::EventControllerKey.new
|
|
892
|
+
press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
|
|
893
|
+
window.add_controller(press)
|
|
894
|
+
press.signal_connect "key-pressed" do |_g, keyval, _kc, _y|
|
|
895
|
+
if keyval == Gdk::Keyval::KEY_Return
|
|
896
|
+
do_start.call
|
|
897
|
+
true
|
|
898
|
+
elsif keyval == Gdk::Keyval::KEY_Escape
|
|
899
|
+
window.destroy
|
|
900
|
+
true
|
|
901
|
+
else
|
|
902
|
+
false
|
|
903
|
+
end
|
|
904
|
+
end
|
|
905
|
+
|
|
906
|
+
window.show
|
|
907
|
+
end
|
|
908
|
+
|
|
909
|
+
# ── Toggle / lifecycle ──────────────────────────────────────────────────────────
|
|
910
|
+
|
|
911
|
+
# Fast toggle: start immediately (no dialog) reusing the last-used prompt, or stop.
|
|
912
|
+
def dictation_toggle
|
|
913
|
+
s = dictation_session
|
|
914
|
+
if s.active?
|
|
915
|
+
s.stop
|
|
916
|
+
else
|
|
917
|
+
s.start(dictation_last_prompt)
|
|
918
|
+
end
|
|
919
|
+
end
|
|
920
|
+
|
|
921
|
+
# The last-used whisper prompt (nil when unset/blank).
|
|
922
|
+
def dictation_last_prompt
|
|
923
|
+
last = $vma_dict_prompts && $vma_dict_prompts["last"]
|
|
924
|
+
(last.nil? || last.to_s.strip.empty?) ? nil : last
|
|
925
|
+
end
|
|
926
|
+
|
|
927
|
+
# `, k`: switch into the dedicated dictation keyboard mode and start listening.
|
|
928
|
+
# Already in the mode (only reachable from the menu: in the mode `, k` is
|
|
929
|
+
# bound to dictation_exit_mode), stop and leave it, so the "Start/Stop
|
|
930
|
+
# Dictation" menu item toggles like the keys do.
|
|
931
|
+
def dictation_enter_mode
|
|
932
|
+
return dictation_exit_mode if vma.kbd.get_mode == :dictation
|
|
933
|
+
vma.kbd.set_mode(:dictation)
|
|
934
|
+
s = dictation_session
|
|
935
|
+
s.start(dictation_last_prompt) unless s.active?
|
|
936
|
+
end
|
|
937
|
+
|
|
938
|
+
# Dictation mode Space: toggle listening on/off without leaving the mode and
|
|
939
|
+
# without ending the line (so a pause doesn't insert a newline).
|
|
940
|
+
def dictation_toggle_stay
|
|
941
|
+
s = dictation_session
|
|
942
|
+
if s.active?
|
|
943
|
+
s.stop(add_newline: false)
|
|
944
|
+
else
|
|
945
|
+
s.start(dictation_last_prompt)
|
|
946
|
+
end
|
|
947
|
+
end
|
|
948
|
+
|
|
949
|
+
# Dictation mode Enter: start a new line. If listening, finalize the current
|
|
950
|
+
# segment with a newline and resume on the next line; otherwise just insert one.
|
|
951
|
+
def dictation_newline
|
|
952
|
+
s = dictation_session
|
|
953
|
+
if s.active?
|
|
954
|
+
s.stop(add_newline: true, restart: true)
|
|
955
|
+
else
|
|
956
|
+
b = vma.buf
|
|
957
|
+
b.insert_txt_at("\n", b.pos)
|
|
958
|
+
b.set_pos(b.pos + 1)
|
|
959
|
+
b.view.handle_deltas
|
|
960
|
+
end
|
|
961
|
+
end
|
|
962
|
+
|
|
963
|
+
# Dictation mode Esc / Ctrl-tap / `, k`: finalize any listening (with a trailing
|
|
964
|
+
# newline) and return to the previous mode.
|
|
965
|
+
def dictation_exit_mode
|
|
966
|
+
s = dictation_session
|
|
967
|
+
s.stop(add_newline: true) if s.active?
|
|
968
|
+
vma.kbd.to_previous_mode
|
|
969
|
+
end
|
|
970
|
+
|
|
971
|
+
# Toggle with the prompt-selection dialog, or stop.
|
|
972
|
+
def dictation_toggle_dialog
|
|
973
|
+
s = dictation_session
|
|
974
|
+
if s.active?
|
|
975
|
+
s.stop
|
|
976
|
+
else
|
|
977
|
+
vma_dictation_prompt_dialog { |prompt| s.start(prompt) }
|
|
978
|
+
end
|
|
979
|
+
end
|
|
980
|
+
|
|
981
|
+
# Stop the worker now to free its VRAM. It respawns automatically on the next
|
|
982
|
+
# dictation. Refuses while a dictation is in progress.
|
|
983
|
+
def dictation_release_vram
|
|
984
|
+
if $vma_dict_session&.active?
|
|
985
|
+
message("Dictation: stop the current dictation first")
|
|
986
|
+
return
|
|
987
|
+
end
|
|
988
|
+
if $vma_dictation_worker
|
|
989
|
+
$vma_dictation_worker.stop
|
|
990
|
+
message("Dictation: model unloaded (VRAM released)")
|
|
991
|
+
else
|
|
992
|
+
message("Dictation: worker not running")
|
|
993
|
+
end
|
|
994
|
+
end
|
|
995
|
+
|
|
996
|
+
def dictation_init
|
|
997
|
+
$vma_dict_prompts = vma_dict_load_prompts
|
|
998
|
+
vma_dict_restore_model
|
|
999
|
+
reg_act(:dictation_select_model, proc { vma_dictation_model_dialog },
|
|
1000
|
+
"Voice dictation: choose speech model")
|
|
1001
|
+
reg_act(:dictation_toggle, proc { dictation_toggle },
|
|
1002
|
+
"Voice dictation: start/stop (fast, last prompt)")
|
|
1003
|
+
reg_act(:dictation_toggle_dialog, proc { dictation_toggle_dialog },
|
|
1004
|
+
"Voice dictation: start/stop (choose prompt)")
|
|
1005
|
+
reg_act(:dictation_release_vram, proc { dictation_release_vram },
|
|
1006
|
+
"Voice dictation: unload model to free VRAM")
|
|
1007
|
+
reg_act(:dictation_enter_mode, proc { dictation_enter_mode },
|
|
1008
|
+
"Voice dictation: start + enter dictation mode (exit if in it)")
|
|
1009
|
+
reg_act(:dictation_toggle_stay, proc { dictation_toggle_stay },
|
|
1010
|
+
"Dictation mode: toggle listening (stay in mode)")
|
|
1011
|
+
reg_act(:dictation_newline, proc { dictation_newline },
|
|
1012
|
+
"Dictation mode: new line")
|
|
1013
|
+
reg_act(:dictation_exit_mode, proc { dictation_exit_mode },
|
|
1014
|
+
"Dictation mode: exit")
|
|
1015
|
+
|
|
1016
|
+
# A dedicated dictation input mode that inherits command mode (like `audio`),
|
|
1017
|
+
# so unoverridden command chords stay available while dictating.
|
|
1018
|
+
vma.kbd.add_minor_mode("dictation", :dictation, :command)
|
|
1019
|
+
add_keys "dictation", {
|
|
1020
|
+
"C , k" => :dictation_enter_mode, # start + enter dictation mode
|
|
1021
|
+
"C , ; k" => :dictation_toggle_dialog, # plain toggle (choose prompt), no mode
|
|
1022
|
+
"dictation space" => :dictation_toggle_stay,
|
|
1023
|
+
"dictation enter || dictation return" => :dictation_newline,
|
|
1024
|
+
"dictation esc || dictation ctrl! || dictation , k" => :dictation_exit_mode,
|
|
1025
|
+
}
|
|
1026
|
+
# Same action as `, k`, so the menu shows that binding
|
|
1027
|
+
vma.gui.menu.add_module_action(:dictation_enter_mode, "Start/Stop Dictation")
|
|
1028
|
+
vma.gui.menu.add_module_action(:dictation_toggle_dialog, "Start Dictation (choose prompt)")
|
|
1029
|
+
vma.gui.menu.add_module_action(:dictation_release_vram, "Release Dictation VRAM")
|
|
1030
|
+
vma.gui.menu.add_module_action(:dictation_select_model, "Select Dictation Model…")
|
|
1031
|
+
end
|
|
1032
|
+
|
|
1033
|
+
def dictation_disable
|
|
1034
|
+
dictation_session.stop if $vma_dict_session&.active?
|
|
1035
|
+
$vma_dictation_worker&.stop
|
|
1036
|
+
# The module is going away; don't leave a stale indicator behind. (stop above
|
|
1037
|
+
# only queues the finalize that would normally clear it.)
|
|
1038
|
+
vma.gui&.set_status_indicator(VmaDictationSession::STATUS_KEY, nil)
|
|
1039
|
+
unreg_act(:dictation_toggle)
|
|
1040
|
+
unreg_act(:dictation_toggle_dialog)
|
|
1041
|
+
unreg_act(:dictation_release_vram)
|
|
1042
|
+
unreg_act(:dictation_enter_mode)
|
|
1043
|
+
unreg_act(:dictation_toggle_stay)
|
|
1044
|
+
unreg_act(:dictation_newline)
|
|
1045
|
+
unreg_act(:dictation_exit_mode)
|
|
1046
|
+
unreg_act(:dictation_select_model)
|
|
1047
|
+
unbindkey "C , k"
|
|
1048
|
+
unbindkey "C , ; k"
|
|
1049
|
+
unbindkey "dictation", include_child_nodes: true
|
|
1050
|
+
vma.gui.menu.remove_module_action(:dictation_enter_mode)
|
|
1051
|
+
vma.gui.menu.remove_module_action(:dictation_toggle_dialog)
|
|
1052
|
+
vma.gui.menu.remove_module_action(:dictation_release_vram)
|
|
1053
|
+
vma.gui.menu.remove_module_action(:dictation_select_model)
|
|
1054
|
+
end
|