ggerganov commited on
Commit
03f79ff
·
unverified ·
1 Parent(s): 35a7fcb

ref #10 : add "step" argument for "stream" example

Browse files

Controls how often we run the inference.
By default, we run it every 3 seconds.

Files changed (1) hide show
  1. stream.cpp +7 -2
stream.cpp CHANGED
@@ -36,6 +36,7 @@ std::string to_timestamp(int64_t t) {
36
  struct whisper_params {
37
  int32_t seed = -1; // RNG seed, not used currently
38
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
 
39
 
40
  bool verbose = false;
41
  bool translate = false;
@@ -57,6 +58,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
57
  params.seed = std::stoi(argv[++i]);
58
  } else if (arg == "-t" || arg == "--threads") {
59
  params.n_threads = std::stoi(argv[++i]);
 
 
60
  } else if (arg == "-v" || arg == "--verbose") {
61
  params.verbose = true;
62
  } else if (arg == "--translate") {
@@ -97,6 +100,7 @@ void whisper_print_usage(int argc, char ** argv, const whisper_params & params)
97
  fprintf(stderr, " -h, --help show this help message and exit\n");
98
  fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
99
  fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
 
100
  fprintf(stderr, " -v, --verbose verbose output\n");
101
  fprintf(stderr, " --translate translate from source language to english\n");
102
  fprintf(stderr, " -ps, --print_special print special tokens\n");
@@ -197,6 +201,7 @@ int main(int argc, char ** argv) {
197
 
198
  struct whisper_context * ctx = whisper_init(params.model.c_str());
199
 
 
200
  const int n_samples_30s = 30*WHISPER_SAMPLE_RATE;
201
  std::vector<float> pcmf32(n_samples_30s, 0.0f);
202
  std::vector<float> pcmf32_old;
@@ -212,7 +217,7 @@ int main(int argc, char ** argv) {
212
  }
213
  }
214
  printf("%s: processing %d samples (%.1f sec), %d threads, lang = %s, task = %s, timestamps = %d ...\n",
215
- __func__, int(pcmf32.size()), float(pcmf32.size())/WHISPER_SAMPLE_RATE, params.n_threads,
216
  params.language.c_str(),
217
  params.translate ? "translate" : "transcribe",
218
  params.no_timestamps ? 0 : 1);
@@ -238,7 +243,7 @@ int main(int argc, char ** argv) {
238
  }
239
 
240
  // process 3 seconds of new audio
241
- while (SDL_GetQueuedAudioSize(g_dev_id_in) < 3*WHISPER_SAMPLE_RATE*sizeof(float)) {
242
  SDL_Delay(1);
243
  }
244
  const int n_samples_new = SDL_GetQueuedAudioSize(g_dev_id_in)/sizeof(float);
 
36
  struct whisper_params {
37
  int32_t seed = -1; // RNG seed, not used currently
38
  int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
39
+ int32_t step_ms = 3000;
40
 
41
  bool verbose = false;
42
  bool translate = false;
 
58
  params.seed = std::stoi(argv[++i]);
59
  } else if (arg == "-t" || arg == "--threads") {
60
  params.n_threads = std::stoi(argv[++i]);
61
+ } else if (arg == "--step") {
62
+ params.step_ms = std::stoi(argv[++i]);
63
  } else if (arg == "-v" || arg == "--verbose") {
64
  params.verbose = true;
65
  } else if (arg == "--translate") {
 
100
  fprintf(stderr, " -h, --help show this help message and exit\n");
101
  fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1)\n");
102
  fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
103
+ fprintf(stderr, " --step N audio step size in milliseconds (default: %d)\n", params.step_ms);
104
  fprintf(stderr, " -v, --verbose verbose output\n");
105
  fprintf(stderr, " --translate translate from source language to english\n");
106
  fprintf(stderr, " -ps, --print_special print special tokens\n");
 
201
 
202
  struct whisper_context * ctx = whisper_init(params.model.c_str());
203
 
204
+ const int n_samples = (params.step_ms/1000.0)*WHISPER_SAMPLE_RATE;
205
  const int n_samples_30s = 30*WHISPER_SAMPLE_RATE;
206
  std::vector<float> pcmf32(n_samples_30s, 0.0f);
207
  std::vector<float> pcmf32_old;
 
217
  }
218
  }
219
  printf("%s: processing %d samples (%.1f sec), %d threads, lang = %s, task = %s, timestamps = %d ...\n",
220
+ __func__, n_samples, float(n_samples)/WHISPER_SAMPLE_RATE, params.n_threads,
221
  params.language.c_str(),
222
  params.translate ? "translate" : "transcribe",
223
  params.no_timestamps ? 0 : 1);
 
243
  }
244
 
245
  // process 3 seconds of new audio
246
+ while (SDL_GetQueuedAudioSize(g_dev_id_in) < n_samples*sizeof(float)) {
247
  SDL_Delay(1);
248
  }
249
  const int n_samples_new = SDL_GetQueuedAudioSize(g_dev_id_in)/sizeof(float);