From cf83412d0abb373afc9e004bebf4dc2a57f6ac1f Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Fri, 24 May 2024 11:07:44 +0800 Subject: [PATCH] Support reading waves from NAudio. (#914) --- sherpa-onnx/csrc/wave-reader.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sherpa-onnx/csrc/wave-reader.cc b/sherpa-onnx/csrc/wave-reader.cc index 66f1cb12e..316b2846f 100644 --- a/sherpa-onnx/csrc/wave-reader.cc +++ b/sherpa-onnx/csrc/wave-reader.cc @@ -118,6 +118,30 @@ std::vector ReadWaveImpl(std::istream &is, int32_t *sampling_rate, return {}; } + if (header.subchunk1_size == 18) { + // this is for NAudio. It puts extra bytes after bits_per_sample + // See + // https://github.com/naudio/NAudio/blob/master/NAudio.Core/Wave/WaveFormats/WaveFormat.cs#L223 + + is.seekg(36, std::istream::beg); + + int16_t extra_size = -1; + is.read(reinterpret_cast(&extra_size), sizeof(int16_t)); + if (extra_size != 0) { + SHERPA_ONNX_LOGE( + "Extra size should be 0 for wave from NAudio. Current extra size " + "%d\n", + extra_size); + *is_ok = false; + return {}; + } + + is.read(reinterpret_cast(&header.subchunk2_id), + sizeof(header.subchunk2_id)); + is.read(reinterpret_cast(&header.subchunk2_size), + sizeof(header.subchunk2_size)); + } + header.SeekToDataChunk(is); if (!is) { *is_ok = false;