@@ -732,13 +732,16 @@ int whisper_server_main(int argc, char ** argv) {
732732 std::vector<float > pcmf32; // mono-channel F32 PCM
733733 std::vector<std::vector<float >> pcmf32s; // stereo-channel F32 PCM
734734
735+ // write incoming buffer to temporary file
736+ std::string temp_filename = __get_tmpdir ();
737+ temp_filename += " /whisperfile." ;
738+ temp_filename += std::to_string (_rand64 ());
739+
740+ std::ofstream temp_file{temp_filename, std::ios::binary};
741+ temp_file << audio_file.content ;
742+ temp_file.close ();
743+
735744 if (sparams.ffmpeg_converter ) {
736- // if file is not wav, convert to wav
737- // write to temporary file
738- const std::string temp_filename = " whisper_server_temp_file.wav" ;
739- std::ofstream temp_file{temp_filename, std::ios::binary};
740- temp_file << audio_file.content ;
741- temp_file.close ();
742745
743746 std::string error_resp = " {\" error\" :\" Failed to execute ffmpeg command.\" }" ;
744747 const bool is_converted = convert_to_wav (temp_filename, error_resp);
@@ -756,17 +759,17 @@ int whisper_server_main(int argc, char ** argv) {
756759 std::remove (temp_filename.c_str ());
757760 return ;
758761 }
759- // remove temp file
760- std::remove (temp_filename.c_str ());
761762 } else {
762- if (!::read_wav (audio_file. content , pcmf32, pcmf32s, params.diarize ))
763+ if (!::read_wav (temp_filename , pcmf32, pcmf32s, params.diarize ))
763764 {
764765 fprintf (stderr, " error: failed to read WAV file\n " );
765766 const std::string error_resp = " {\" error\" :\" failed to read WAV file\" }" ;
766767 res.set_content (error_resp, " application/json" );
767768 return ;
768769 }
769770 }
771+ // remove temp file
772+ std::remove (temp_filename.c_str ());
770773
771774
772775 printf (" Successfully loaded %s\n " , filename.c_str ());
0 commit comments