Skip to content

Commit

Permalink
Use condition variable to signal input thread if available
Browse files Browse the repository at this point in the history
  • Loading branch information
palana committed Aug 8, 2024
1 parent e9581f3 commit a4b5c1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
48 changes: 17 additions & 31 deletions src/tests/localvocal-offline-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ create_context(int sample_rate, int channels, const std::string &whisper_model_p
gf->process_while_muted = false;
gf->buffered_output = false;
gf->fix_utf8 = true;
gf->input_cv.emplace();

for (size_t i = 0; i < gf->channels; i++) {
circlebuf_init(&gf->input_buffers[i]);
Expand Down Expand Up @@ -453,38 +454,23 @@ int wmain(int argc, wchar_t *argv[])
frames_size_bytes = frames * frame_size_bytes;
}
{
bool wait = false;
auto max_wait =
start_time_time + (window_number * window_size_in_ms);
for (;;) {
{
std::lock_guard<std::mutex> lock(
gf->whisper_buf_mutex);
wait = gf->input_buffers->size != 0;
}
if (!wait)
break;

// sleep up to window size in case whisper is processing, so the buffer builds up similar to OBS
auto now = std::chrono::system_clock::now();
if (false && now > max_wait)
break;

auto wait_start = now + std::chrono::milliseconds(1);
auto wait_until = max_wait > wait_start ? wait_start
: max_wait;
#if 0
obs_log(LOG_INFO, "sleeping %lld ms",
std::chrono::duration_cast<std::chrono::milliseconds>(
(wait_until -
std::chrono::system_clock::now()))
.count());
#endif
std::this_thread::sleep_until(wait_until);
}

{
std::lock_guard<std::mutex> lock(gf->whisper_buf_mutex);
auto max_wait = start_time_time +
(window_number * window_size_in_ms);
std::unique_lock<std::mutex> lock(gf->whisper_buf_mutex);
for (;;) {
// sleep up to window size in case whisper is processing, so the buffer builds up similar to OBS
auto now = std::chrono::system_clock::now();
if (false && now > max_wait)
break;

gf->input_cv->wait_for(
lock, std::chrono::milliseconds(10), [&] {
return gf->input_buffers->size == 0;
});
if (gf->input_buffers->size == 0)
break;
}
// push back current audio data to input circlebuf
for (size_t c = 0; c < gf->channels; c++) {
circlebuf_push_back(
Expand Down
1 change: 1 addition & 0 deletions src/transcription-filter-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ struct transcription_filter_data {
std::mutex whisper_buf_mutex;
std::mutex whisper_ctx_mutex;
std::condition_variable wshiper_thread_cv;
std::optional<std::condition_variable> input_cv;

// translation context
struct translation_context translation_ctx;
Expand Down
3 changes: 3 additions & 0 deletions src/whisper-utils/whisper-processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ void whisper_loop(void *data)
}
}

if (gf->input_cv.has_value())
gf->input_cv->notify_one();

// Sleep using the condition variable wshiper_thread_cv
// This will wake up the thread if there is new data in the input buffer
// or if the whisper context is null
Expand Down

0 comments on commit a4b5c1e

Please sign in to comment.