Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / audio / sounds / wav_audio_handler.cc
blobc9808394aea0c12c2906b64b2e18f7c9d7769b99
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"
7 #include <algorithm>
8 #include <cstring>
10 #include "base/logging.h"
11 #include "base/sys_byteorder.h"
12 #include "media/base/audio_bus.h"
14 namespace {
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|.
42 template <typename T>
43 T ReadInt(const base::StringPiece& data, size_t offset) {
44 CHECK_LE(offset + sizeof(T), data.size());
45 T result;
46 memcpy(&result, data.data() + offset, sizeof(T));
47 #if !defined(ARCH_CPU_LITTLE_ENDIAN)
48 result = base::ByteSwap(result);
49 #endif
50 return result;
53 } // namespace
55 namespace media {
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";
70 offset += length;
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,
83 size_t cursor,
84 size_t* bytes_written) const {
85 if (!bus)
86 return false;
87 if (bus->channels() != num_channels_) {
88 DVLOG(1) << "Number of channels mismatch.";
89 return false;
91 if (AtEnd(cursor)) {
92 bus->Zero();
93 return true;
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);
102 return true;
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)
112 return data.size();
113 uint32 chunk_length = ReadInt<uint32>(data, 4);
114 if (data.starts_with(kSubchunk1Id)) {
115 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length)))
116 return -1;
117 } else if (data.starts_with(kSubchunk2Id)) {
118 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length)))
119 return -1;
120 } else {
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.";
129 return false;
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);
135 return true;
138 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) {
139 data_ = data;
140 return true;
143 } // namespace media