1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/sounds/wav_audio_handler.h"
10 #include "base/logging.h"
11 #include "base/sys_byteorder.h"
12 #include "media/base/audio_bus.h"
16 const char kChunkId
[] = "RIFF";
17 const char kFormat
[] = "WAVE";
18 const char kSubchunk1Id
[] = "fmt ";
19 const char kSubchunk2Id
[] = "data";
21 // The size of the header of a wav file. The header consists of 'RIFF', 4 bytes
22 // of total data length, and 'WAVE'.
23 const size_t kWavFileHeaderSize
= 12;
25 // The size of a chunk header in wav file format. A chunk header consists of a
26 // tag ('fmt ' or 'data') and 4 bytes of chunk length.
27 const size_t kChunkHeaderSize
= 8;
29 // The minimum size of 'fmt' chunk.
30 const size_t kFmtChunkMinimumSize
= 16;
32 // The offsets of 'fmt' fields.
33 const size_t kAudioFormatOffset
= 0;
34 const size_t kChannelOffset
= 2;
35 const size_t kSampleRateOffset
= 4;
36 const size_t kBitsPerSampleOffset
= 14;
38 // Some constants for audio format.
39 const int kAudioFormatPCM
= 1;
41 // Reads an integer from |data| with |offset|.
43 T
ReadInt(const base::StringPiece
& data
, size_t offset
) {
44 CHECK_LE(offset
+ sizeof(T
), data
.size());
46 memcpy(&result
, data
.data() + offset
, sizeof(T
));
47 #if !defined(ARCH_CPU_LITTLE_ENDIAN)
48 result
= base::ByteSwap(result
);
57 WavAudioHandler::WavAudioHandler(const base::StringPiece
& wav_data
)
61 CHECK_LE(kWavFileHeaderSize
, wav_data
.size()) << "wav data is too small";
62 CHECK(wav_data
.starts_with(kChunkId
) &&
63 memcmp(wav_data
.data() + 8, kFormat
, 4) == 0)
64 << "incorrect wav header";
66 uint32 total_length
= std::min(ReadInt
<uint32
>(wav_data
, 4),
67 static_cast<uint32
>(wav_data
.size()));
68 uint32 offset
= kWavFileHeaderSize
;
69 while (offset
< total_length
) {
70 const int length
= ParseSubChunk(wav_data
.substr(offset
));
71 CHECK_LE(0, length
) << "can't parse wav sub-chunk";
75 const int frame_count
= data_
.size() * 8 / num_channels_
/ bits_per_sample_
;
76 params_
= AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY
,
77 GuessChannelLayout(num_channels_
),
83 WavAudioHandler::~WavAudioHandler() {}
85 bool WavAudioHandler::AtEnd(size_t cursor
) const {
86 return data_
.size() <= cursor
;
89 bool WavAudioHandler::CopyTo(AudioBus
* bus
,
91 size_t* bytes_written
) const {
94 if (bus
->channels() != params_
.channels()) {
95 DVLOG(1) << "Number of channel mismatch.";
102 const int remaining_frames
=
103 (data_
.size() - cursor
) / params_
.GetBytesPerFrame();
104 const int frames
= std::min(bus
->frames(), remaining_frames
);
105 bus
->FromInterleaved(data_
.data() + cursor
, frames
,
106 params_
.bits_per_sample() / 8);
107 *bytes_written
= frames
* params_
.GetBytesPerFrame();
108 bus
->ZeroFramesPartial(frames
, bus
->frames() - frames
);
112 int WavAudioHandler::ParseSubChunk(const base::StringPiece
& data
) {
113 if (data
.size() < kChunkHeaderSize
)
115 uint32 chunk_length
= ReadInt
<uint32
>(data
, 4);
116 if (data
.starts_with(kSubchunk1Id
)) {
117 if (!ParseFmtChunk(data
.substr(kChunkHeaderSize
, chunk_length
)))
119 } else if (data
.starts_with(kSubchunk2Id
)) {
120 if (!ParseDataChunk(data
.substr(kChunkHeaderSize
, chunk_length
)))
123 DVLOG(1) << "Unknown data chunk: " << data
.substr(0, 4) << ".";
125 return chunk_length
+ kChunkHeaderSize
;
128 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece
& data
) {
129 if (data
.size() < kFmtChunkMinimumSize
) {
130 DLOG(ERROR
) << "Data size " << data
.size() << " is too short.";
133 DCHECK_EQ(ReadInt
<uint16
>(data
, kAudioFormatOffset
), kAudioFormatPCM
);
134 num_channels_
= ReadInt
<uint16
>(data
, kChannelOffset
);
135 sample_rate_
= ReadInt
<uint32
>(data
, kSampleRateOffset
);
136 bits_per_sample_
= ReadInt
<uint16
>(data
, kBitsPerSampleOffset
);
140 bool WavAudioHandler::ParseDataChunk(const base::StringPiece
& data
) {