Mercurial > vim
diff plugins/voicenote/voicenote.vim @ 22:c8ad85e95854 default tip
Some general updates
| author | Luka Sitas <lsitas@avatarasoftware.com> |
|---|---|
| date | Thu, 16 Jul 2026 14:47:23 -0400 |
| parents | 203279635445 |
| children |
line wrap: on
line diff
--- a/plugins/voicenote/voicenote.vim Wed Mar 04 13:41:22 2026 -0500 +++ b/plugins/voicenote/voicenote.vim Thu Jul 16 14:47:23 2026 -0400 @@ -1,91 +1,65 @@ -let g:audio_record_file = -1 -let g:audio_record_tmp_file = -1 -let g:audio_record_cmd = expand('~/record.sh') -let g:audio_transcribe_cmd = expand('~/transcribe.sh') -let g:audio_record_pid = -1 -let g:audio_record_transcript_file = -1 +" Voicenote +" A very simple plugin for transcribing text and inserting it into vim at the +" cursor. +" Utilizes simple shell commands to record audio at the press of a key binding +" Then stops recording after another keybinding +" After recording sends the audio to an openai compatible audio transcription +" endpoint +" Finally parses the output and inserts the text at the cursor. -function! ToggleVoiceNote() - if g:audio_record_pid != -1 - call StopRecording() - call TranscribeLastRecording() - else - call StartRecording() +let g:record_job = -1 +let g:record_job_running = -1 +let g:record_file = '' + + +" Begin new recording. +function! StartRecording() + " Check for already running recording. + if g:record_job_running < 0 + let l:file = tempname() + let g:record_file = l:file + let g:record_job = job_start(['arecord', '-f', 'cd', '-r', '44100', l:file]) + let g:record_job_running = 1 endif endfunction -function! StartRecording() - if g:audio_record_pid != -1 - echo "Recording already in progress (PID " . g:audio_record_pid . ")" - return - endif - - " Temp file where record.sh will write the WAV path - let l:temp_file = tempname() +" Stop the recording and transcribe. +function! StopRecording() + " Check if we're already starting recording. + if g:record_job_running > 0 + call StopAndWait(g:record_job) - " Run record.sh; its stdout (the wav path) goes to temp_file. - " Then echo the background PID. - let l:cmd = printf('%s > %s 2>/dev/null & echo $!', shellescape(g:audio_record_cmd), shellescape(l:temp_file)) + " Transcribe the text. + let l:response = system('curl -s "http://127.0.0.1:52625/v1/audio/transcriptions" -F "model=whisper-v3" -F file=@' . g:record_file) + if !empty(l:response) + let l:data = json_decode(l:response) + if has_key(l:data, 'text') + execute 'normal! a' . escape(l:data.text, '\') + endif + endif - let l:pid = str2nr(system(l:cmd)) + " Clean up. + let g:record_job_running = -1 + let g:record_job = -1 + let g:record_file = '' - if l:pid <= 0 - echoerr "Failed to start recording" + endif +endfunction + +function! StopAndWait(job) + " If the job is already dead, nothing to do + if empty(a:job) || job_status(a:job) == 'dead' return endif - " Read the file path written by record.sh - let g:audio_record_file = trim(system('cat ' . shellescape(l:temp_file))) - let g:audio_record_tmp_file = l:temp_file - let g:audio_record_pid = l:pid - - echo "Recording started (PID " . g:audio_record_pid . ") -> " . g:audio_record_file -endfunction + " Request the job to stop + call job_stop(a:job) -function! StopRecording() - if g:audio_record_pid == -1 - echo "No recording in progress" - return - endif - - call system('kill ' . g:audio_record_pid) - - if g:audio_record_tmp_file != -1 - call system('rm -f ' . shellescape(g:audio_record_tmp_file)) - let g:audio_record_tmp_file = -1 - endif - - let g:audio_record_pid = -1 - let g:audio_record_transcript_file = -1 - - echo "Recording stopped." + " Wait until the status reports it as dead + while job_status(a:job) != 'dead' + sleep 100m " adjust granularity if needed + endwhile endfunction - -function! TranscribeLastRecording() - if g:audio_record_file == -1 - echo "No recent recording" - return - endif - - let l:cmd = printf('%s %s', shellescape(g:audio_transcribe_cmd), shellescape(g:audio_record_file)) - - " transcribe.sh echoes the .txt path - let l:transcription_file = trim(system(l:cmd)) - - if v:shell_error != 0 || empty(l:transcription_file) - echoerr "Transcription failed" - return - endif - - let g:audio_record_file = -1 - execute 'tabe' fnameescape(l:transcription_file) -endfunction - - - -" BINDINGS - -command! VoiceNote call ToggleVoiceNote() - -nnoremap <Leader>vn :VoiceNote<CR> +nnoremap <leader>s :call StartRecording()<CR> +nnoremap <leader>e :call StopRecording()<CR>
