@@ -23,23 +23,6 @@ const std::vector<std::string> k_colors = {
23
23
};
24
24
25
25
26
- // 500 -> 00:05.000
27
- // 6000 -> 01:00.000
28
- std::string to_timestamp (int64_t t, bool comma = false ) {
29
- int64_t msec = t * 10 ;
30
- int64_t hr = msec / (1000 * 60 * 60 );
31
- msec = msec - hr * (1000 * 60 * 60 );
32
- int64_t min = msec / (1000 * 60 );
33
- msec = msec - min * (1000 * 60 );
34
- int64_t sec = msec / 1000 ;
35
- msec = msec - sec * 1000 ;
36
-
37
- char buf[32 ];
38
- snprintf (buf, sizeof (buf), " %02d:%02d:%02d%s%03d" , (int ) hr, (int ) min, (int ) sec, comma ? " ," : " ." , (int ) msec);
39
-
40
- return std::string (buf);
41
- }
42
-
43
26
int timestamp_to_sample (int64_t t, int n_samples) {
44
27
return std::max (0 , std::min ((int ) n_samples - 1 , (int ) ((t * WHISPER_SAMPLE_RATE) / 100 )));
45
28
}
@@ -202,20 +185,20 @@ void getReqParameters(const Request &req, whisper_params ¶ms) {
202
185
if (req.has_file (" temerature" )) {
203
186
params.userdef_temp = std::stof (req.get_file_value (" temperature" ).content );
204
187
}
205
- if (req.has_file (" audio_format" )){
206
- params.audio_format = req.get_file_value (" audio_format" ).content ;
188
+ if (req.has_file (" audio_format" )) {
189
+ params.audio_format = req.get_file_value (" audio_format" ).content ;
207
190
}
208
191
}
209
192
210
193
211
194
void getReqParameters (const Request &request, whisper_params ¶ms);
212
195
213
- bool read_audio_file (std::string audio_format, std::string filename, std::vector<float > & pcmf32,
214
- std::vector<std::vector<float >> & pcmf32s, bool diarize) {
196
+ bool read_audio_file (std::string audio_format, std::string filename, std::vector<float > &pcmf32,
197
+ std::vector<std::vector<float >> &pcmf32s, bool diarize) {
215
198
216
199
// read audio content into pcmf32
217
200
if (audio_format == " mp3" ) {
218
- if (!::read_mp3 (filename, pcmf32,diarize)) {
201
+ if (!::read_mp3 (filename, pcmf32, diarize)) {
219
202
fprintf (stderr, " error: failed to read mp3 file '%s'\n " , filename.c_str ());
220
203
return false ;
221
204
}
@@ -234,7 +217,7 @@ bool read_audio_file(std::string audio_format, std::string filename, std::vector
234
217
}
235
218
236
219
bool run (std::mutex &whisper_mutex, whisper_params ¶ms, whisper_context *ctx, std::string filename,
237
- const std::vector<std::vector<float >>& pcmf32s, std::vector<float > pcmf32) {
220
+ const std::vector<std::vector<float >> & pcmf32s, std::vector<float > pcmf32) {
238
221
// print system information
239
222
{
240
223
fprintf (stderr, " \n " );
@@ -363,19 +346,19 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
363
346
if (!request.has_file (" file" )) {
364
347
fprintf (stderr, " error: no 'file' field in the request\n " );
365
348
json jres = json{
366
- {" code" ,-1 },
367
- {" msg" , " no 'file' field in the request" }
349
+ {" code" , -1 },
350
+ {" msg" , " no 'file' field in the request" }
368
351
};
369
- auto json_string = jres.dump (-1 , ' ' , false ,json::error_handler_t ::replace);
370
- response.set_content (json_string," application/json" );
352
+ auto json_string = jres.dump (-1 , ' ' , false , json::error_handler_t ::replace);
353
+ response.set_content (json_string, " application/json" );
371
354
return ;
372
355
}
373
356
auto audio_file = request.get_file_value (" file" );
374
357
std::string filename{audio_file.filename };
375
- printf (" %s: Received filename: %s \n " ,get_current_time ().c_str (),filename.c_str ());
358
+ printf (" %s: Received filename: %s \n " , get_current_time ().c_str (), filename.c_str ());
376
359
// check non-required fields
377
360
getReqParameters (request, params);
378
- printf (" %s: audio_format:%s \n " ,get_current_time ().c_str (),params.audio_format .c_str ());
361
+ printf (" %s: audio_format:%s \n " , get_current_time ().c_str (), params.audio_format .c_str ());
379
362
380
363
// audio arrays
381
364
std::vector<float > pcmf32; // mono-channel F32 PCM
@@ -385,13 +368,13 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
385
368
std::ofstream temp_file{filename, std::ios::binary};
386
369
temp_file << audio_file.content ;
387
370
388
- bool isOK= read_audio_file (params.audio_format ,filename,pcmf32,pcmf32s,params.diarize );
389
- if (!isOK){
390
- json json_obj= {
391
- {" code" ,-1 },
392
- {" msg" ," error: failed to read WAV file " }
371
+ bool isOK = read_audio_file (params.audio_format , filename, pcmf32, pcmf32s, params.diarize );
372
+ if (!isOK) {
373
+ json json_obj = {
374
+ {" code" , -1 },
375
+ {" msg" , " error: failed to read WAV file " }
393
376
};
394
- auto json_string = json_obj.dump (-1 , ' ' , false ,json::error_handler_t ::replace);
377
+ auto json_string = json_obj.dump (-1 , ' ' , false , json::error_handler_t ::replace);
395
378
response.set_content (json_string, " application/json" );
396
379
return ;
397
380
}
@@ -401,8 +384,8 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
401
384
402
385
printf (" Successfully loaded %s\n " , filename.c_str ());
403
386
404
- bool isOk= run (whisper_mutex, params, ctx, filename, pcmf32s, pcmf32);
405
- if (isOk){
387
+ bool isOk = run (whisper_mutex, params, ctx, filename, pcmf32s, pcmf32);
388
+ if (isOk) {
406
389
// return results to user
407
390
if (params.response_format == text_format) {
408
391
std::string results = output_str (ctx, params, pcmf32s);
@@ -412,18 +395,18 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
412
395
else {
413
396
auto results = get_result (ctx);
414
397
json jres = json{
415
- {" code" ,0 },
398
+ {" code" , 0 },
416
399
{" data" , results}
417
400
};
418
401
response.set_content (jres.dump (-1 , ' ' , false , json::error_handler_t ::replace),
419
402
" application/json" );
420
403
}
421
- }else {
404
+ } else {
422
405
json jres = json{
423
- {" code" ,-1 },
424
- {" msg" , " run error" }
406
+ {" code" , -1 },
407
+ {" msg" , " run error" }
425
408
};
426
- auto json_string = jres.dump (-1 , ' ' , false ,json::error_handler_t ::replace);
427
- response.set_content (json_string," application/json" );
409
+ auto json_string = jres.dump (-1 , ' ' , false , json::error_handler_t ::replace);
410
+ response.set_content (json_string, " application/json" );
428
411
}
429
412
}
0 commit comments