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_audio_handler.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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // tag ('fmt ' or 'data') and 4 bytes of chunk length. | 26 // tag ('fmt ' or 'data') and 4 bytes of chunk length. |
27 const size_t kChunkHeaderSize = 8; | 27 const size_t kChunkHeaderSize = 8; |
28 | 28 |
29 // The minimum size of 'fmt' chunk. | 29 // The minimum size of 'fmt' chunk. |
30 const size_t kFmtChunkMinimumSize = 16; | 30 const size_t kFmtChunkMinimumSize = 16; |
31 | 31 |
32 // The offsets of 'fmt' fields. | 32 // The offsets of 'fmt' fields. |
33 const size_t kAudioFormatOffset = 0; | 33 const size_t kAudioFormatOffset = 0; |
34 const size_t kChannelOffset = 2; | 34 const size_t kChannelOffset = 2; |
35 const size_t kSampleRateOffset = 4; | 35 const size_t kSampleRateOffset = 4; |
36 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 WavAudioHandler::WavAudioHandler(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), | |
61 bits_per_sample_(0) { | 60 bits_per_sample_(0) { |
62 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; | 61 CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; |
63 CHECK(wav_data.starts_with(kChunkId) && | 62 CHECK(wav_data.starts_with(kChunkId) && |
64 memcmp(wav_data.data() + 8, kFormat, 4) == 0) | 63 memcmp(wav_data.data() + 8, kFormat, 4) == 0) |
65 << "incorrect wav header"; | 64 << "incorrect wav header"; |
66 | 65 |
67 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), | 66 uint32 total_length = std::min(ReadInt<uint32>(wav_data, 4), |
68 static_cast<uint32>(wav_data.size())); | 67 static_cast<uint32>(wav_data.size())); |
69 uint32 offset = kWavFileHeaderSize; | 68 uint32 offset = kWavFileHeaderSize; |
70 while (offset < total_length) { | 69 while (offset < total_length) { |
71 const int length = ParseSubChunk(wav_data.substr(offset)); | 70 const int length = ParseSubChunk(wav_data.substr(offset)); |
72 CHECK_LE(0, length) << "can't parse wav sub-chunk"; | 71 CHECK_LE(0, length) << "can't parse wav sub-chunk"; |
73 offset += length; | 72 offset += length; |
74 } | 73 } |
| 74 |
| 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_), |
| 78 sample_rate_, |
| 79 bits_per_sample_, |
| 80 frame_count); |
75 } | 81 } |
76 | 82 |
77 WavAudioHandler::~WavAudioHandler() { | 83 WavAudioHandler::~WavAudioHandler() {} |
78 } | |
79 | 84 |
80 bool WavAudioHandler::AtEnd(size_t cursor) const { | 85 bool WavAudioHandler::AtEnd(size_t cursor) const { |
81 return data_.size() <= cursor; | 86 return data_.size() <= cursor; |
82 } | 87 } |
83 | 88 |
84 bool WavAudioHandler::CopyTo(AudioBus* bus, | 89 bool WavAudioHandler::CopyTo(AudioBus* bus, |
85 size_t cursor, | 90 size_t cursor, |
86 size_t* bytes_written) const { | 91 size_t* bytes_written) const { |
87 if (!bus) | 92 if (!bus) |
88 return false; | 93 return false; |
89 if (bus->channels() != num_channels_) { | 94 if (bus->channels() != params_.channels()) { |
90 DLOG(ERROR) << "Number of channel mismatch."; | 95 DVLOG(1) << "Number of channel mismatch."; |
91 return false; | 96 return false; |
92 } | 97 } |
93 if (AtEnd(cursor)) { | 98 if (AtEnd(cursor)) { |
94 bus->Zero(); | 99 bus->Zero(); |
95 return true; | 100 return true; |
96 } | 101 } |
97 const int remaining_frames = (data_.size() - cursor) / bytes_per_frame_; | 102 const int remaining_frames = |
| 103 (data_.size() - cursor) / params_.GetBytesPerFrame(); |
98 const int frames = std::min(bus->frames(), remaining_frames); | 104 const int frames = std::min(bus->frames(), remaining_frames); |
99 bus->FromInterleaved(data_.data() + cursor, frames, bytes_per_sample_); | 105 bus->FromInterleaved(data_.data() + cursor, frames, |
100 *bytes_written = frames * bytes_per_frame_; | 106 params_.bits_per_sample() / 8); |
| 107 *bytes_written = frames * params_.GetBytesPerFrame(); |
101 bus->ZeroFramesPartial(frames, bus->frames() - frames); | 108 bus->ZeroFramesPartial(frames, bus->frames() - frames); |
102 return true; | 109 return true; |
103 } | 110 } |
104 | 111 |
105 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { | 112 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { |
106 if (data.size() < kChunkHeaderSize) | 113 if (data.size() < kChunkHeaderSize) |
107 return data.size(); | 114 return data.size(); |
108 uint32 chunk_length = ReadInt<uint32>(data, 4); | 115 uint32 chunk_length = ReadInt<uint32>(data, 4); |
109 if (data.starts_with(kSubchunk1Id)) { | 116 if (data.starts_with(kSubchunk1Id)) { |
110 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) | 117 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) |
111 return -1; | 118 return -1; |
112 } else if (data.starts_with(kSubchunk2Id)) { | 119 } else if (data.starts_with(kSubchunk2Id)) { |
113 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) | 120 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) |
114 return -1; | 121 return -1; |
115 } else { | 122 } else { |
116 DVLOG(1) << "Unknown data chunk: " << data.substr(0, 4) << "."; | 123 DVLOG(1) << "Unknown data chunk: " << data.substr(0, 4) << "."; |
117 } | 124 } |
118 return chunk_length + kChunkHeaderSize; | 125 return chunk_length + kChunkHeaderSize; |
119 } | 126 } |
120 | 127 |
121 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { | 128 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { |
122 if (data.size() < kFmtChunkMinimumSize) { | 129 if (data.size() < kFmtChunkMinimumSize) { |
123 DLOG(ERROR) << "Data size " << data.size() << " is too short."; | 130 DLOG(ERROR) << "Data size " << data.size() << " is too short."; |
124 return false; | 131 return false; |
125 } | 132 } |
126 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); | 133 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); |
127 num_channels_ = ReadInt<uint16>(data, kChannelOffset); | 134 num_channels_ = ReadInt<uint16>(data, kChannelOffset); |
128 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); | 135 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); |
129 byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); | |
130 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); | 136 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); |
131 bytes_per_sample_ = bits_per_sample_ >> 3; | |
132 bytes_per_frame_ = num_channels_ * bytes_per_sample_; | |
133 return true; | 137 return true; |
134 } | 138 } |
135 | 139 |
136 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { | 140 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { |
137 data_ = data; | 141 data_ = data; |
138 return true; | 142 return true; |
139 } | 143 } |
140 | 144 |
141 } // namespace media | 145 } // namespace media |
OLD | NEW |