]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
stream : fix data race on bool + avoid division-by-zero
authorGeorgi Gerganov <redacted>
Mon, 2 Jan 2023 08:20:50 +0000 (10:20 +0200)
committerGeorgi Gerganov <redacted>
Mon, 2 Jan 2023 08:20:50 +0000 (10:20 +0200)
examples/stream/stream.cpp

index 1752fff83cd6067e7fe0413235ec6f1351f0905a..9caa6148d890df7e22e1d7719f419d9a9b9454a2 100644 (file)
@@ -8,6 +8,7 @@
 #include <SDL.h>
 #include <SDL_audio.h>
 
+#include <atomic>
 #include <cassert>
 #include <cstdio>
 #include <string>
@@ -144,8 +145,8 @@ private:
     int m_len_ms = 0;
     int m_sample_rate = 0;
 
-    bool       m_running = false;
-    std::mutex m_mutex;
+    std::atomic_bool m_running;
+    std::mutex       m_mutex;
 
     std::vector<float> m_audio;
     std::vector<float> m_audio_new;
@@ -155,6 +156,8 @@ private:
 
 audio_async::audio_async(int len_ms) {
     m_len_ms = len_ms;
+
+    m_running = false;
 }
 
 audio_async::~audio_async() {
@@ -427,10 +430,10 @@ int main(int argc, char ** argv) {
     const int n_samples_keep = (params.keep_ms  *1e-3)*WHISPER_SAMPLE_RATE;
     const int n_samples_30s  = (30000           *1e-3)*WHISPER_SAMPLE_RATE;
 
-    const int n_new_line = params.length_ms / params.step_ms - 1; // number of steps to print new line
-
     const bool use_vad = n_samples_step <= 0; // sliding window mode uses VAD
 
+    const int n_new_line = !use_vad ? params.length_ms / params.step_ms - 1 : 1; // number of steps to print new line
+
     params.no_timestamps = !use_vad;
     params.no_context    = use_vad;
     params.max_tokens    = 0;