Skip to content

Commit 7517a5f

Browse files
authored
whisperfile server: convert files without ffmpeg (#568)
1 parent 70e3dcd commit 7517a5f

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

whisper.cpp/common.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ static void on_exit(void) {
3737
}
3838
}
3939

40-
bool is_wav_buffer(const std::string buf) {
41-
// RIFF ref: https://en.wikipedia.org/wiki/Resource_Interchange_File_Format
42-
// WAV ref: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
43-
if (buf.size() < 12 || buf.substr(0, 4) != "RIFF" || buf.substr(8, 4) != "WAVE") {
44-
return false;
45-
}
46-
47-
uint32_t chunk_size = *reinterpret_cast<const uint32_t*>(buf.data() + 4);
48-
if (chunk_size + 8 != buf.size()) {
49-
return false;
50-
}
51-
52-
return true;
53-
}
54-
5540
static ma_result perform_audio_conversion(ma_decoder* pDecoder, ma_encoder* pEncoder) {
5641
ma_result rc = MA_SUCCESS;
5742
for (;;) {
@@ -173,12 +158,6 @@ bool read_wav(const std::string & fname_, std::vector<float>& pcmf32, std::vecto
173158

174159
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, wav_data.size());
175160
}
176-
else if (is_wav_buffer(fname)) {
177-
if (drwav_init_memory(&wav, fname.c_str(), fname.size(), nullptr) == false) {
178-
fprintf(stderr, "error: failed to open WAV file from fname buffer\n");
179-
return false;
180-
}
181-
}
182161
else if (drwav_init_file(&wav, fname.c_str(), nullptr) == false) {
183162
tinylogf("%s: converting to wav...\n", fname.c_str());
184163
TRY_CONVERSION;

whisper.cpp/server.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)