Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/sounds/wav_audio_handler.h" | 5 #include "media/audio/sounds/wav_parser.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/sys_byteorder.h" | 11 #include "base/sys_byteorder.h" |
| 12 #include "media/base/audio_bus.h" | |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 const char kChunkId[] = "RIFF"; | 15 const char kChunkId[] = "RIFF"; |
| 17 const char kFormat[] = "WAVE"; | 16 const char kFormat[] = "WAVE"; |
| 18 const char kSubchunk1Id[] = "fmt "; | 17 const char kSubchunk1Id[] = "fmt "; |
| 19 const char kSubchunk2Id[] = "data"; | 18 const char kSubchunk2Id[] = "data"; |
| 20 | 19 |
| 21 // The size of the header of a wav file. The header consists of 'RIFF', 4 bytes | 20 // The size of the header of a wav file. The header consists of 'RIFF', 4 bytes |
| 22 // of total data length, and 'WAVE'. | 21 // of total data length, and 'WAVE'. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 33 const size_t kAudioFormatOffset = 0; | 32 const size_t kAudioFormatOffset = 0; |
| 34 const size_t kChannelOffset = 2; | 33 const size_t kChannelOffset = 2; |
| 35 const size_t kSampleRateOffset = 4; | 34 const size_t kSampleRateOffset = 4; |
| 36 const size_t kByteRateOffset = 8; | 35 const size_t kByteRateOffset = 8; |
| 37 const size_t kBitsPerSampleOffset = 14; | 36 const size_t kBitsPerSampleOffset = 14; |
| 38 | 37 |
| 39 // Some constants for audio format. | 38 // Some constants for audio format. |
| 40 const int kAudioFormatPCM = 1; | 39 const int kAudioFormatPCM = 1; |
| 41 | 40 |
| 42 // Reads an integer from |data| with |offset|. | 41 // Reads an integer from |data| with |offset|. |
| 43 template<typename T> T ReadInt(const base::StringPiece& data, size_t offset) { | 42 template <typename T> |
| 43 T ReadInt(const base::StringPiece& data, size_t offset) { | |
| 44 CHECK_LE(offset + sizeof(T), data.size()); | 44 CHECK_LE(offset + sizeof(T), data.size()); |
| 45 T result; | 45 T result; |
| 46 memcpy(&result, data.data() + offset, sizeof(T)); | 46 memcpy(&result, data.data() + offset, sizeof(T)); |
| 47 #if !defined(ARCH_CPU_LITTLE_ENDIAN) | 47 #if !defined(ARCH_CPU_LITTLE_ENDIAN) |
| 48 result = base::ByteSwap(result); | 48 result = base::ByteSwap(result); |
| 49 #endif | 49 #endif |
| 50 return result; | 50 return result; |
| 51 } | 51 } |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 namespace media { | 55 namespace media { |
| 56 | 56 |
| 57 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) | 57 WavParser::WavParser(const base::StringPiece& wav_data) |
| 58 : num_channels_(0), | 58 : num_channels_(0), |
| 59 sample_rate_(0), | 59 sample_rate_(0), |
| 60 byte_rate_(0), | 60 byte_rate_(0), |
|
DaleCurtis
2013/12/17 19:28:52
rename this to bytes_per_second_ or bytes_per_ms_
ygorshenin1
2013/12/18 14:33:12
After introduction of AudioParameters field these
| |
| 61 bits_per_sample_(0) { | 61 bits_per_sample_(0) { |
| 62 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; | 62 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; |
| 63 CHECK(wav_data.starts_with(kChunkId) && | 63 CHECK(wav_data.starts_with(kChunkId) && |
| 64 memcmp(wav_data.data() + 8, kFormat, 4) == 0) | 64 memcmp(wav_data.data() + 8, kFormat, 4) == 0) |
| 65 << "incorrect wav header"; | 65 << "incorrect wav header"; |
| 66 | 66 |
| 67 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), | 67 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), |
| 68 static_cast<uint32>(wav_data.size())); | 68 static_cast<uint32>(wav_data.size())); |
| 69 uint32 offset = kWavFileHeaderSize; | 69 uint32 offset = kWavFileHeaderSize; |
| 70 while (offset < total_length) { | 70 while (offset < total_length) { |
| 71 const int length = ParseSubChunk(wav_data.substr(offset)); | 71 const int length = ParseSubChunk(wav_data.substr(offset)); |
| 72 CHECK_LE(0, length) << "can't parse wav sub-chunk"; | 72 CHECK_LE(0, length) << "can't parse wav sub-chunk"; |
| 73 offset += length; | 73 offset += length; |
| 74 } | 74 } |
| 75 | |
| 76 const int64 size = data_.size(); | |
| 77 const int64 rate = byte_rate_; | |
| 78 if (rate) | |
| 79 duration_ = base::TimeDelta::FromMicroseconds(size * 1000000 / rate); | |
| 75 } | 80 } |
| 76 | 81 |
| 77 WavAudioHandler::~WavAudioHandler() { | 82 WavParser::~WavParser() {} |
| 78 } | |
| 79 | 83 |
| 80 bool WavAudioHandler::AtEnd(size_t cursor) const { | 84 int WavParser::ParseSubChunk(const base::StringPiece& data) { |
| 81 return data_.size() <= cursor; | |
| 82 } | |
| 83 | |
| 84 bool WavAudioHandler::CopyTo(AudioBus* bus, | |
| 85 size_t cursor, | |
| 86 size_t* bytes_written) const { | |
| 87 if (!bus) | |
| 88 return false; | |
| 89 if (bus->channels() != num_channels_) { | |
| 90 LOG(ERROR) << "Number of channel mismatch."; | |
| 91 return false; | |
| 92 } | |
| 93 if (AtEnd(cursor)) { | |
| 94 bus->Zero(); | |
| 95 return true; | |
| 96 } | |
| 97 const int remaining_frames = (data_.size() - cursor) / bytes_per_frame_; | |
| 98 const int frames = std::min(bus->frames(), remaining_frames); | |
| 99 bus->FromInterleaved(data_.data() + cursor, frames, bytes_per_sample_); | |
| 100 *bytes_written = frames * bytes_per_frame_; | |
| 101 bus->ZeroFramesPartial(frames, bus->frames() - frames); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { | |
| 106 if (data.size() < kChunkHeaderSize) | 85 if (data.size() < kChunkHeaderSize) |
| 107 return data.size(); | 86 return data.size(); |
| 108 uint32 chunk_length = ReadInt<uint32>(data, 4); | 87 uint32 chunk_length = ReadInt<uint32>(data, 4); |
| 109 if (data.starts_with(kSubchunk1Id)) { | 88 if (data.starts_with(kSubchunk1Id)) { |
| 110 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) | 89 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) |
| 111 return -1; | 90 return -1; |
| 112 } else if (data.starts_with(kSubchunk2Id)) { | 91 } else if (data.starts_with(kSubchunk2Id)) { |
| 113 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) | 92 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) |
| 114 return -1; | 93 return -1; |
| 115 } else { | 94 } else { |
| 116 LOG(ERROR) << "Unknown data chunk: " << data.substr(0, 4) << "."; | 95 LOG(ERROR) << "Unknown data chunk: " << data.substr(0, 4) << "."; |
| 117 } | 96 } |
| 118 return chunk_length + kChunkHeaderSize; | 97 return chunk_length + kChunkHeaderSize; |
| 119 } | 98 } |
| 120 | 99 |
| 121 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { | 100 bool WavParser::ParseFmtChunk(const base::StringPiece& data) { |
| 122 if (data.size() < kFmtChunkMinimumSize) { | 101 if (data.size() < kFmtChunkMinimumSize) { |
| 123 LOG(ERROR) << "Data size " << data.size() << " is too short."; | 102 LOG(ERROR) << "Data size " << data.size() << " is too short."; |
| 124 return false; | 103 return false; |
| 125 } | 104 } |
| 126 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); | 105 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); |
| 127 num_channels_ = ReadInt<uint16>(data, kChannelOffset); | 106 num_channels_ = ReadInt<uint16>(data, kChannelOffset); |
| 128 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); | 107 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); |
| 129 byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); | 108 byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); |
| 130 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); | 109 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); |
| 131 bytes_per_sample_ = bits_per_sample_ >> 3; | 110 bytes_per_sample_ = bits_per_sample_ >> 3; |
| 132 bytes_per_frame_ = num_channels_ * bytes_per_sample_; | 111 bytes_per_frame_ = num_channels_ * bytes_per_sample_; |
| 133 return true; | 112 return true; |
| 134 } | 113 } |
| 135 | 114 |
| 136 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { | 115 bool WavParser::ParseDataChunk(const base::StringPiece& data) { |
| 137 data_ = data; | 116 data_ = data; |
| 138 return true; | 117 return true; |
| 139 } | 118 } |
| 140 | 119 |
| 141 } // namespace media | 120 } // namespace media |
| OLD | NEW |