Skip to content

Commit 5b481a2

Browse files
authored
common : fix audio loading by miniaudio (#2862)
1 parent fc7b1ee commit 5b481a2

File tree

3 files changed

+13
-36
lines changed

3 files changed

+13
-36
lines changed

examples/common-whisper.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,25 @@ bool read_audio_data(const std::string & fname, std::vector<float>& pcmf32, std:
7676

7777
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, audio_data.size());
7878
}
79-
else if (is_wav_buffer(fname)) {
80-
if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) {
81-
fprintf(stderr, "Error: failed to open audio data from fname buffer (%s)\n", ma_result_description(result));
82-
83-
return false;
84-
}
85-
}
86-
else if ((result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder)) != MA_SUCCESS) {
79+
else if (((result = ma_decoder_init_file(fname.c_str(), &decoder_config, &decoder)) != MA_SUCCESS)) {
8780
#if defined(WHISPER_FFMPEG)
88-
if (ffmpeg_decode_audio(fname, audio_data) != 0) {
89-
fprintf(stderr, "error: failed to ffmpeg decode '%s'\n", fname.c_str());
81+
if (ffmpeg_decode_audio(fname, audio_data) != 0) {
82+
fprintf(stderr, "error: failed to ffmpeg decode '%s'\n", fname.c_str());
9083

91-
return false;
92-
}
84+
return false;
85+
}
9386

94-
if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) {
95-
fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result));
87+
if ((result = ma_decoder_init_memory(audio_data.data(), audio_data.size(), &decoder_config, &decoder)) != MA_SUCCESS) {
88+
fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result));
9689

97-
return false;
98-
}
90+
return false;
91+
}
9992
#else
100-
fprintf(stderr, "error: failed to open '%s' file (%s)\n", fname.c_str(), ma_result_description(result));
93+
if ((result = ma_decoder_init_memory(fname.c_str(), fname.size(), &decoder_config, &decoder)) != MA_SUCCESS) {
94+
fprintf(stderr, "error: failed to read audio data as wav (%s)\n", ma_result_description(result));
10195

102-
return false;
96+
return false;
97+
}
10398
#endif
10499
}
105100

examples/common.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -609,21 +609,6 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat(
609609

610610
}
611611

612-
bool is_wav_buffer(const std::string buf) {
613-
// RIFF ref: https://en.wikipedia.org/wiki/Resource_Interchange_File_Format
614-
// WAV ref: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
615-
if (buf.size() < 12 || buf.substr(0, 4) != "RIFF" || buf.substr(8, 4) != "WAVE") {
616-
return false;
617-
}
618-
619-
uint32_t chunk_size = *reinterpret_cast<const uint32_t*>(buf.data() + 4);
620-
if (chunk_size + 8 != buf.size()) {
621-
return false;
622-
}
623-
624-
return true;
625-
}
626-
627612
void high_pass_filter(std::vector<float> & data, float cutoff, float sample_rate) {
628613
const float rc = 1.0f / (2.0f * M_PI * cutoff);
629614
const float dt = 1.0f / sample_rate;

examples/common.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat(
134134
// Audio utils
135135
//
136136

137-
// Check if a buffer is a WAV audio file
138-
bool is_wav_buffer(const std::string buf);
139-
140137
// Write PCM data into WAV audio file
141138
class wav_writer {
142139
private:

0 commit comments

Comments
 (0)