Skip to content

Commit eba50e4

Browse files
KarelVesely84danpovey
authored andcommitted
[src] Enable reading wavs produced on iPhones with extra RIFF tags. (kaldi-asr#2293)
1 parent c6b3588 commit eba50e4

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

src/feat/wave-reader.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,23 @@ void WaveInfo::Read(std::istream &is) {
132132
uint32 riff_chunk_read = 0;
133133
riff_chunk_read += 4; // WAVE included in riff_chunk_size.
134134

135-
reader.Expect4ByteTag("fmt ");
135+
// Possibly skip any RIFF tags between 'WAVE' and 'fmt '.
136+
// Apple devices produce a filler tag 'JUNK' for memory alignment.
137+
reader.Read4ByteTag();
138+
riff_chunk_read += 4;
139+
while (strcmp(reader.tag,"fmt ") != 0) {
140+
uint32 filler_size = reader.ReadUint32();
141+
riff_chunk_read += 4;
142+
for (uint32 i = 0; i < filler_size; i++) {
143+
is.get(); // read 1 byte,
144+
}
145+
riff_chunk_read += filler_size;
146+
// get next RIFF tag,
147+
reader.Read4ByteTag();
148+
riff_chunk_read += 4;
149+
}
150+
151+
KALDI_ASSERT(strcmp(reader.tag,"fmt ") == 0);
136152
uint32 subchunk1_size = reader.ReadUint32();
137153
uint16 audio_format = reader.ReadUint16();
138154
num_channels_ = reader.ReadUint16();
@@ -190,9 +206,8 @@ void WaveInfo::Read(std::istream &is) {
190206
KALDI_ERR << "Unexpected block_align: " << block_align << " vs. "
191207
<< num_channels_ << " * " << (bits_per_sample/8);
192208

193-
riff_chunk_read += 8 + subchunk1_size;
194-
// size of what we just read, 4 bytes for "fmt " + 4
195-
// for subchunk1_size + subchunk1_size itself.
209+
riff_chunk_read += 4 + subchunk1_size;
210+
// size of what we just read, 4 for subchunk1_size + subchunk1_size itself.
196211

197212
// We support an optional "fact" chunk (which is useless but which
198213
// we encountered), and then a single "data" chunk.
@@ -217,10 +232,7 @@ void WaveInfo::Read(std::istream &is) {
217232
riff_chunk_read += 4;
218233
}
219234

220-
if (strcmp(reader.tag, "data"))
221-
KALDI_ERR << "WaveData: expected data chunk, got instead "
222-
<< reader.tag;
223-
235+
KALDI_ASSERT(strcmp(reader.tag, "data") == 0);
224236
uint32 data_chunk_size = reader.ReadUint32();
225237
riff_chunk_read += 4;
226238

0 commit comments

Comments
 (0)