From 5b481a27a62978225a352abba1f871db40bfea19 Mon Sep 17 00:00:00 2001 From: Dmitry Atamanov Date: Tue, 4 Mar 2025 22:05:21 +0500 Subject: [PATCH] common : fix audio loading by miniaudio (#2862) --- examples/common-whisper.cpp | 31 +++++++++++++------------------ examples/common.cpp | 15 --------------- examples/common.h | 3 --- 3 files changed, 13 insertions(+), 36 deletions(-) diff --git a/examples/common-whisper.cpp b/examples/common-whisper.cpp index 2bc610d1..6a9b58e0 100644 --- a/examples/common-whisper.cpp +++ b/examples/common-whisper.cpp @@ -76,30 +76,25 @@ bool read_audio_data(const std::string & fname, std::vector& pcmf32, std: fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, audio_data.size()); } - else if (is_wav_buffer(fname)) { - if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) { - fprintf(stderr, "Error: failed to open audio data from fname buffer (%s)\n", ma_result_description(result)); - - return false; - } - } - else if ((result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder)) != MA_SUCCESS) { + else if (((result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder)) != MA_SUCCESS)) { #if defined(WHISPER_FFMPEG) - if (ffmpeg_decode_audio(fname, audio_data) != 0) { - fprintf(stderr, "error: failed to ffmpeg decode '%s'\n", fname.c_str()); + if (ffmpeg_decode_audio(fname, audio_data) != 0) { + fprintf(stderr, "error: failed to ffmpeg decode '%s'\n", fname.c_str()); - return false; - } + return false; + } - if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) { - fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result)); + if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) { + fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result)); - return false; - } + return false; + } #else - fprintf(stderr, "error: failed to open '%s' file (%s)\n", fname.c_str(), ma_result_description(result)); + if ((result = ma_decoder_init_memory(fname.c_str(), fname.size(), &decoder_config, &decoder)) != MA_SUCCESS) { + fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result)); - return false; + return false; + } #endif } diff --git a/examples/common.cpp b/examples/common.cpp index 484cb773..f40bcf6d 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -609,21 +609,6 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat( } -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(buf.data() + 4); - if (chunk_size + 8 != buf.size()) { - return false; - } - - return true; -} - void high_pass_filter(std::vector & data, float cutoff, float sample_rate) { const float rc = 1.0f / (2.0f * M_PI * cutoff); const float dt = 1.0f / sample_rate; diff --git a/examples/common.h b/examples/common.h index 7d2219d7..f6bf0a9e 100644 --- a/examples/common.h +++ b/examples/common.h @@ -134,9 +134,6 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat( // Audio utils // -// Check if a buffer is a WAV audio file -bool is_wav_buffer(const std::string buf); - // Write PCM data into WAV audio file class wav_writer { private: