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
)
58 : num_channels_(0), sample_rate_(0), bits_per_sample_(0), total_frames_(0) {
59 CHECK_LE(kWavFileHeaderSize
, wav_data
.size()) << "wav data is too small";
60 CHECK(wav_data
.starts_with(kChunkId
) &&
61 memcmp(wav_data
.data() + 8, kFormat
, 4) == 0)
62 << "incorrect wav header";
64 uint32 total_length
= std::min(ReadInt
<uint32
>(wav_data
, 4),
65 static_cast<uint32
>(wav_data
.size()));
66 uint32 offset
= kWavFileHeaderSize
;
67 while (offset
< total_length
) {
68 const int length
= ParseSubChunk(wav_data
.substr(offset
));
69 CHECK_LE(0, length
) << "can't parse wav sub-chunk";
73 total_frames_
= data_
.size() * 8 / num_channels_
/ bits_per_sample_
;
76 WavAudioHandler::~WavAudioHandler() {}
78 bool WavAudioHandler::AtEnd(size_t cursor
) const {
79 return data_
.size() <= cursor
;
82 bool WavAudioHandler::CopyTo(AudioBus
* bus
,
84 size_t* bytes_written
) const {
87 if (bus
->channels() != num_channels_
) {
88 DVLOG(1) << "Number of channels mismatch.";
95 const int bytes_per_frame
= num_channels_
* bits_per_sample_
/ 8;
96 const int remaining_frames
= (data_
.size() - cursor
) / bytes_per_frame
;
97 const int frames
= std::min(bus
->frames(), remaining_frames
);
99 bus
->FromInterleaved(data_
.data() + cursor
, frames
, bits_per_sample_
/ 8);
100 *bytes_written
= frames
* bytes_per_frame
;
101 bus
->ZeroFramesPartial(frames
, bus
->frames() - frames
);
105 base::TimeDelta
WavAudioHandler::GetDuration() const {
106 return base::TimeDelta::FromSecondsD(total_frames_
/
107 static_cast<double>(sample_rate_
));
110 int WavAudioHandler::ParseSubChunk(const base::StringPiece
& data
) {
111 if (data
.size() < kChunkHeaderSize
)
113 uint32 chunk_length
= ReadInt
<uint32
>(data
, 4);
114 if (data
.starts_with(kSubchunk1Id
)) {
115 if (!ParseFmtChunk(data
.substr(kChunkHeaderSize
, chunk_length
)))
117 } else if (data
.starts_with(kSubchunk2Id
)) {
118 if (!ParseDataChunk(data
.substr(kChunkHeaderSize
, chunk_length
)))
121 DVLOG(1) << "Unknown data chunk: " << data
.substr(0, 4) << ".";
123 return chunk_length
+ kChunkHeaderSize
;
126 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece
& data
) {
127 if (data
.size() < kFmtChunkMinimumSize
) {
128 DLOG(ERROR
) << "Data size " << data
.size() << " is too short.";
131 DCHECK_EQ(ReadInt
<uint16
>(data
, kAudioFormatOffset
), kAudioFormatPCM
);
132 num_channels_
= ReadInt
<uint16
>(data
, kChannelOffset
);
133 sample_rate_
= ReadInt
<uint32
>(data
, kSampleRateOffset
);
134 bits_per_sample_
= ReadInt
<uint16
>(data
, kBitsPerSampleOffset
);
138 bool WavAudioHandler::ParseDataChunk(const base::StringPiece
& data
) {