Skip to content

Commit

Permalink
whisperfile server: convert files without ffmpeg (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpais authored Sep 28, 2024
1 parent 70e3dcd commit 7517a5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
21 changes: 0 additions & 21 deletions whisper.cpp/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (;;) {
Expand Down Expand Up @@ -173,12 +158,6 @@ bool read_wav(const std::string & fname_, std::vector<float>& pcmf32, std::vecto

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;
Expand Down
21 changes: 12 additions & 9 deletions whisper.cpp/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -756,17 +759,17 @@ 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\"}";
res.set_content(error_resp, "application/json");
return;
}
}
// remove temp file
std::remove(temp_filename.c_str());


printf("Successfully loaded %s\n", filename.c_str());
Expand Down

0 comments on commit 7517a5f

Please sign in to comment.