Skip to content

Commit a466c34

Browse files
committed
stream : fix data race on bool + avoid division-by-zero
1 parent d629c03 commit a466c34

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

examples/stream/stream.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <SDL.h>
99
#include <SDL_audio.h>
1010

11+
#include <atomic>
1112
#include <cassert>
1213
#include <cstdio>
1314
#include <string>
@@ -144,8 +145,8 @@ class audio_async {
144145
int m_len_ms = 0;
145146
int m_sample_rate = 0;
146147

147-
bool m_running = false;
148-
std::mutex m_mutex;
148+
std::atomic_bool m_running;
149+
std::mutex m_mutex;
149150

150151
std::vector<float> m_audio;
151152
std::vector<float> m_audio_new;
@@ -155,6 +156,8 @@ class audio_async {
155156

156157
audio_async::audio_async(int len_ms) {
157158
m_len_ms = len_ms;
159+
160+
m_running = false;
158161
}
159162

160163
audio_async::~audio_async() {
@@ -427,10 +430,10 @@ int main(int argc, char ** argv) {
427430
const int n_samples_keep = (params.keep_ms *1e-3)*WHISPER_SAMPLE_RATE;
428431
const int n_samples_30s = (30000 *1e-3)*WHISPER_SAMPLE_RATE;
429432

430-
const int n_new_line = params.length_ms / params.step_ms - 1; // number of steps to print new line
431-
432433
const bool use_vad = n_samples_step <= 0; // sliding window mode uses VAD
433434

435+
const int n_new_line = !use_vad ? params.length_ms / params.step_ms - 1 : 1; // number of steps to print new line
436+
434437
params.no_timestamps = !use_vad;
435438
params.no_context = use_vad;
436439
params.max_tokens = 0;

0 commit comments

Comments
 (0)