Skip to content

Instantly share code, notes, and snippets.

@jpsamaroo
Created May 9, 2023 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpsamaroo/aff348ae04f392f1e8683b59cbe6bda7 to your computer and use it in GitHub Desktop.
Save jpsamaroo/aff348ae04f392f1e8683b59cbe6bda7 to your computer and use it in GitHub Desktop.
Whisper+PortAudio
using PortAudio, SampledSignals, Whisper
# Requires https://github.com/aviks/Whisper.jl/pull/8
ctx = Whisper.create_ctx("base.en")
wparams = Whisper.create_params()
wparams.n_threads = 6
queue = Channel(typemax(Int))
stream = PortAudioStream(1, 0)
function to_16k(buf)
buf_16k = SampleBuf(Float32, 16000, round(Int, length(buf)*(16000/samplerate(buf))), nchannels(buf))
write(SampleBufSink(buf_16k), SampleBufSource(buf))
return buf_16k
end
errormonitor(Threads.@spawn begin
fullbuf_16k = to_16k(read(stream, 5s)).data[:]
while true
buf = read(stream, 4s)
buf_16k = to_16k(buf)
append!(fullbuf_16k, buf_16k.data[:])
deleteat!(fullbuf_16k, 1:length(buf_16k))
put!(queue, copy(fullbuf_16k))
end
end)
full_results = ""
function merge_results!(result)
result_lower = lowercase(result)
global full_results
if endswith(lowercase(full_results), lowercase(result))
return "BAD THINGS HAPPENED "
end
full_results *= " " * result
return result
end
while true
samp = take!(queue)
result = redirect_stderr(devnull) do
Whisper.transcribe(ctx, wparams, samp)
end
diff_results = merge_results!(result)
print(diff_results)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment