Spaces:
Running
Running
common : fix wav buffer detection (#1819)
Browse files- examples/common.cpp +16 -1
- examples/common.h +3 -0
examples/common.cpp
CHANGED
|
@@ -615,6 +615,21 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat(
|
|
| 615 |
|
| 616 |
}
|
| 617 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector<std::vector<float>>& pcmf32s, bool stereo) {
|
| 619 |
drwav wav;
|
| 620 |
std::vector<uint8_t> wav_data; // used for pipe input from stdin
|
|
@@ -639,7 +654,7 @@ bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector
|
|
| 639 |
|
| 640 |
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, wav_data.size());
|
| 641 |
}
|
| 642 |
-
else if (
|
| 643 |
if (drwav_init_memory(&wav, fname.c_str(), fname.size(), nullptr) == false) {
|
| 644 |
fprintf(stderr, "error: failed to open WAV file from fname buffer\n");
|
| 645 |
return false;
|
|
|
|
| 615 |
|
| 616 |
}
|
| 617 |
|
| 618 |
+
bool is_wav_buffer(const std::string buf) {
|
| 619 |
+
// RIFF ref: https://en.wikipedia.org/wiki/Resource_Interchange_File_Format
|
| 620 |
+
// WAV ref: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
| 621 |
+
if (buf.size() < 12 || buf.substr(0, 4) != "RIFF" || buf.substr(8, 4) != "WAVE") {
|
| 622 |
+
return false;
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
uint32_t chunk_size = *reinterpret_cast<const uint32_t*>(buf.data() + 4);
|
| 626 |
+
if (chunk_size + 8 != buf.size()) {
|
| 627 |
+
return false;
|
| 628 |
+
}
|
| 629 |
+
|
| 630 |
+
return true;
|
| 631 |
+
}
|
| 632 |
+
|
| 633 |
bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector<std::vector<float>>& pcmf32s, bool stereo) {
|
| 634 |
drwav wav;
|
| 635 |
std::vector<uint8_t> wav_data; // used for pipe input from stdin
|
|
|
|
| 654 |
|
| 655 |
fprintf(stderr, "%s: read %zu bytes from stdin\n", __func__, wav_data.size());
|
| 656 |
}
|
| 657 |
+
else if (is_wav_buffer(fname)) {
|
| 658 |
if (drwav_init_memory(&wav, fname.c_str(), fname.size(), nullptr) == false) {
|
| 659 |
fprintf(stderr, "error: failed to open WAV file from fname buffer\n");
|
| 660 |
return false;
|
examples/common.h
CHANGED
|
@@ -135,6 +135,9 @@ gpt_vocab::id gpt_sample_top_k_top_p_repeat(
|
|
| 135 |
// Audio utils
|
| 136 |
//
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
// Read WAV audio file and store the PCM data into pcmf32
|
| 139 |
// fname can be a buffer of WAV data instead of a filename
|
| 140 |
// The sample rate of the audio must be equal to COMMON_SAMPLE_RATE
|
|
|
|
| 135 |
// Audio utils
|
| 136 |
//
|
| 137 |
|
| 138 |
+
// Check if a buffer is a WAV audio file
|
| 139 |
+
bool is_wav_buffer(const std::string buf);
|
| 140 |
+
|
| 141 |
// Read WAV audio file and store the PCM data into pcmf32
|
| 142 |
// fname can be a buffer of WAV data instead of a filename
|
| 143 |
// The sample rate of the audio must be equal to COMMON_SAMPLE_RATE
|