@@ -23,23 +23,6 @@ const std::vector<std::string> k_colors = {
2323};
2424
2525
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-
4326int timestamp_to_sample (int64_t t, int n_samples) {
4427 return std::max (0 , std::min ((int ) n_samples - 1 , (int ) ((t * WHISPER_SAMPLE_RATE) / 100 )));
4528}
@@ -202,20 +185,20 @@ void getReqParameters(const Request &req, whisper_params ¶ms) {
202185 if (req.has_file (" temerature" )) {
203186 params.userdef_temp = std::stof (req.get_file_value (" temperature" ).content );
204187 }
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 ;
207190 }
208191}
209192
210193
211194void getReqParameters (const Request &request, whisper_params ¶ms);
212195
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) {
215198
216199 // read audio content into pcmf32
217200 if (audio_format == " mp3" ) {
218- if (!::read_mp3 (filename, pcmf32,diarize)) {
201+ if (!::read_mp3 (filename, pcmf32, diarize)) {
219202 fprintf (stderr, " error: failed to read mp3 file '%s'\n " , filename.c_str ());
220203 return false ;
221204 }
@@ -234,7 +217,7 @@ bool read_audio_file(std::string audio_format, std::string filename, std::vector
234217}
235218
236219bool 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) {
238221 // print system information
239222 {
240223 fprintf (stderr, " \n " );
@@ -363,19 +346,19 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
363346 if (!request.has_file (" file" )) {
364347 fprintf (stderr, " error: no 'file' field in the request\n " );
365348 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" }
368351 };
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" );
371354 return ;
372355 }
373356 auto audio_file = request.get_file_value (" file" );
374357 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 ());
376359 // check non-required fields
377360 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 ());
379362
380363 // audio arrays
381364 std::vector<float > pcmf32; // mono-channel F32 PCM
@@ -385,13 +368,13 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
385368 std::ofstream temp_file{filename, std::ios::binary};
386369 temp_file << audio_file.content ;
387370
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 " }
393376 };
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);
395378 response.set_content (json_string, " application/json" );
396379 return ;
397380 }
@@ -401,8 +384,8 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
401384
402385 printf (" Successfully loaded %s\n " , filename.c_str ());
403386
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) {
406389 // return results to user
407390 if (params.response_format == text_format) {
408391 std::string results = output_str (ctx, params, pcmf32s);
@@ -412,18 +395,18 @@ void handleInference(const Request &request, Response &response, std::mutex &whi
412395 else {
413396 auto results = get_result (ctx);
414397 json jres = json{
415- {" code" ,0 },
398+ {" code" , 0 },
416399 {" data" , results}
417400 };
418401 response.set_content (jres.dump (-1 , ' ' , false , json::error_handler_t ::replace),
419402 " application/json" );
420403 }
421- }else {
404+ } else {
422405 json jres = json{
423- {" code" ,-1 },
424- {" msg" , " run error" }
406+ {" code" , -1 },
407+ {" msg" , " run error" }
425408 };
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" );
428411 }
429412}
0 commit comments