Skip to content

Distribute and run LLMs with a single file.

whisperfile server: convert files without ffmpeg (#568)

CJ Pais7517a5f175ad
diff --git a/whisper.cpp/common.cpp b/whisper.cpp/common.cpp
index 0cf9cc12..816edce8 100644
--- a/whisper.cpp/common.cpp
+++ b/whisper.cpp/common.cpp
@@ -37,21 +37,6 @@ static void on_exit(void) {
}
}
-bool is_wav_buffer(const std::string buf) {
- // RIFF ref: https://en.wikipedia.org/wiki/Resource_Interchange_File_Format
- // WAV ref: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
- if (buf.size() < 12 || buf.substr(0, 4) != "RIFF" || buf.substr(8, 4) != "WAVE") {
- return false;
- }
-
- uint32_t chunk_size = *reinterpret_cast<const uint32_t*>(buf.data() + 4);
- if (chunk_size + 8 != buf.size()) {
- return false;
- }
-
- return true;
-}
-
static ma_result perform_audio_conversion(ma_decoder* pDecoder, ma_encoder* pEncoder) {
ma_result rc = MA_SUCCESS;
for (;;) {
@@ -173,12 +158,6 @@ TryAgain:
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, wav_data.size());
}
- else if (is_wav_buffer(fname)) {
- if (drwav_init_memory(&wav, fname.c_str(), fname.size(), nullptr) == false) {
- fprintf(stderr, "error: failed to open WAV file from fname buffer\n");
- return false;
- }
- }
else if (drwav_init_file(&wav, fname.c_str(), nullptr) == false) {
tinylogf("%s: converting to wav...\n", fname.c_str());
TRY_CONVERSION;
diff --git a/whisper.cpp/server.cpp b/whisper.cpp/server.cpp
index 4a75db68..2daa4398 100644
--- a/whisper.cpp/server.cpp
+++ b/whisper.cpp/server.cpp
@@ -732,13 +732,16 @@ int whisper_server_main(int argc, char ** argv) {
std::vector<float> pcmf32; // mono-channel F32 PCM
std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
+ // write incoming buffer to temporary file
+ std::string temp_filename = __get_tmpdir();
+ temp_filename += "/whisperfile.";
+ temp_filename += std::to_string(_rand64());
+
+ std::ofstream temp_file{temp_filename, std::ios::binary};
+ temp_file << audio_file.content;
+ temp_file.close();
+
if (sparams.ffmpeg_converter) {
- // if file is not wav, convert to wav
- // write to temporary file
- const std::string temp_filename = "whisper_server_temp_file.wav";
- std::ofstream temp_file{temp_filename, std::ios::binary};
- temp_file << audio_file.content;
- temp_file.close();
std::string error_resp = "{\"error\":\"Failed to execute ffmpeg command.\"}";
const bool is_converted = convert_to_wav(temp_filename, error_resp);
@@ -756,10 +759,8 @@ int whisper_server_main(int argc, char ** argv) {
std::remove(temp_filename.c_str());
return;
}
- // remove temp file
- std::remove(temp_filename.c_str());
} else {
- if (!::read_wav(audio_file.content, pcmf32, pcmf32s, params.diarize))
+ if (!::read_wav(temp_filename, pcmf32, pcmf32s, params.diarize))
{
fprintf(stderr, "error: failed to read WAV file\n");
const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
@@ -767,6 +768,8 @@ int whisper_server_main(int argc, char ** argv) {
return;
}
}
+ // remove temp file
+ std::remove(temp_filename.c_str());
printf("Successfully loaded %s\n", filename.c_str());