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 21 matching lines...) Expand all Loading... | |
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; | 36 const size_t kByteRateOffset = 8; |
37 const size_t kBitsPerSampleOffset = 14; | 37 const size_t kBitsPerSampleOffset = 14; |
38 | 38 |
39 // Some constants for audio format. | 39 // Some constants for audio format. |
40 const int kAudioFormatPCM = 1; | 40 const int kAudioFormatPCM = 1; |
41 | 41 |
42 // The number of frames each OnMoreData() call will request. | |
43 const int kDefaultFrameCount = 1024; | |
44 | |
42 // Reads an integer from |data| with |offset|. | 45 // Reads an integer from |data| with |offset|. |
43 template<typename T> T ReadInt(const base::StringPiece& data, size_t offset) { | 46 template <typename T> |
47 T ReadInt(const base::StringPiece& data, size_t offset) { | |
44 CHECK_LE(offset + sizeof(T), data.size()); | 48 CHECK_LE(offset + sizeof(T), data.size()); |
45 T result; | 49 T result; |
46 memcpy(&result, data.data() + offset, sizeof(T)); | 50 memcpy(&result, data.data() + offset, sizeof(T)); |
47 #if !defined(ARCH_CPU_LITTLE_ENDIAN) | 51 #if !defined(ARCH_CPU_LITTLE_ENDIAN) |
48 result = base::ByteSwap(result); | 52 result = base::ByteSwap(result); |
49 #endif | 53 #endif |
50 return result; | 54 return result; |
51 } | 55 } |
52 | 56 |
53 } // namespace | 57 } // namespace |
54 | 58 |
55 namespace media { | 59 namespace media { |
56 | 60 |
57 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) | 61 WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) { |
58 : num_channels_(0), | |
59 sample_rate_(0), | |
60 byte_rate_(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(); | |
DaleCurtis
2013/12/18 21:20:21
If you set AudioBuffer::frames_per_buffer = data_.
ygorshenin1
2013/12/19 15:42:29
frames_per_buffer is used in creation of AudioStre
DaleCurtis
2013/12/19 19:44:08
Yes, you shouldn't use the AudioParameters directl
ygorshenin1
2013/12/20 09:05:05
Done.
| |
77 const int64 rate = params_.GetBytesPerSecond(); | |
78 if (rate) | |
79 duration_ = base::TimeDelta::FromMicroseconds(size * 1000000 / rate); | |
75 } | 80 } |
76 | 81 |
77 WavAudioHandler::~WavAudioHandler() { | 82 WavAudioHandler::~WavAudioHandler() {} |
78 } | |
79 | 83 |
80 bool WavAudioHandler::AtEnd(size_t cursor) const { | 84 bool WavAudioHandler::AtEnd(size_t cursor) const { |
81 return data_.size() <= cursor; | 85 return data_.size() <= cursor; |
82 } | 86 } |
83 | 87 |
84 bool WavAudioHandler::CopyTo(AudioBus* bus, | 88 bool WavAudioHandler::CopyTo(AudioBus* bus, |
85 size_t cursor, | 89 size_t cursor, |
86 size_t* bytes_written) const { | 90 size_t* bytes_written) const { |
87 if (!bus) | 91 if (!bus) |
88 return false; | 92 return false; |
89 if (bus->channels() != num_channels_) { | 93 if (bus->channels() != params_.channels()) { |
90 LOG(ERROR) << "Number of channel mismatch."; | 94 VLOG(1) << "Number of channel mismatch."; |
91 return false; | 95 return false; |
92 } | 96 } |
93 if (AtEnd(cursor)) { | 97 if (AtEnd(cursor)) { |
94 bus->Zero(); | 98 bus->Zero(); |
95 return true; | 99 return true; |
96 } | 100 } |
97 const int remaining_frames = (data_.size() - cursor) / bytes_per_frame_; | 101 const int remaining_frames = |
102 (data_.size() - cursor) / params_.GetBytesPerFrame(); | |
98 const int frames = std::min(bus->frames(), remaining_frames); | 103 const int frames = std::min(bus->frames(), remaining_frames); |
99 bus->FromInterleaved(data_.data() + cursor, frames, bytes_per_sample_); | 104 bus->FromInterleaved(data_.data() + cursor, frames, |
100 *bytes_written = frames * bytes_per_frame_; | 105 params_.bits_per_sample() >> 3); |
DaleCurtis
2013/12/18 21:20:21
Just use * 8, the compiler will optimize as necess
ygorshenin1
2013/12/19 15:42:29
Done.
| |
106 *bytes_written = frames * params_.GetBytesPerFrame(); | |
101 bus->ZeroFramesPartial(frames, bus->frames() - frames); | 107 bus->ZeroFramesPartial(frames, bus->frames() - frames); |
102 return true; | 108 return true; |
103 } | 109 } |
104 | 110 |
105 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { | 111 int WavAudioHandler::ParseSubChunk(const base::StringPiece& data) { |
106 if (data.size() < kChunkHeaderSize) | 112 if (data.size() < kChunkHeaderSize) |
107 return data.size(); | 113 return data.size(); |
108 uint32 chunk_length = ReadInt<uint32>(data, 4); | 114 uint32 chunk_length = ReadInt<uint32>(data, 4); |
109 if (data.starts_with(kSubchunk1Id)) { | 115 if (data.starts_with(kSubchunk1Id)) { |
110 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) | 116 if (!ParseFmtChunk(data.substr(kChunkHeaderSize, chunk_length))) |
111 return -1; | 117 return -1; |
112 } else if (data.starts_with(kSubchunk2Id)) { | 118 } else if (data.starts_with(kSubchunk2Id)) { |
113 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) | 119 if (!ParseDataChunk(data.substr(kChunkHeaderSize, chunk_length))) |
114 return -1; | 120 return -1; |
115 } else { | 121 } else { |
116 LOG(ERROR) << "Unknown data chunk: " << data.substr(0, 4) << "."; | 122 LOG(ERROR) << "Unknown data chunk: " << data.substr(0, 4) << "."; |
117 } | 123 } |
118 return chunk_length + kChunkHeaderSize; | 124 return chunk_length + kChunkHeaderSize; |
119 } | 125 } |
120 | 126 |
121 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { | 127 bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { |
122 if (data.size() < kFmtChunkMinimumSize) { | 128 if (data.size() < kFmtChunkMinimumSize) { |
123 LOG(ERROR) << "Data size " << data.size() << " is too short."; | 129 LOG(ERROR) << "Data size " << data.size() << " is too short."; |
124 return false; | 130 return false; |
125 } | 131 } |
126 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); | 132 DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); |
127 num_channels_ = ReadInt<uint16>(data, kChannelOffset); | 133 int num_channels = ReadInt<uint16>(data, kChannelOffset); |
128 sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); | 134 int sample_rate = ReadInt<uint32>(data, kSampleRateOffset); |
129 byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); | 135 int bits_per_sample = ReadInt<uint16>(data, kBitsPerSampleOffset); |
130 bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); | 136 params_ = AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
131 bytes_per_sample_ = bits_per_sample_ >> 3; | 137 GuessChannelLayout(num_channels), |
132 bytes_per_frame_ = num_channels_ * bytes_per_sample_; | 138 sample_rate, |
139 bits_per_sample, | |
140 kDefaultFrameCount); | |
133 return true; | 141 return true; |
134 } | 142 } |
135 | 143 |
136 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { | 144 bool WavAudioHandler::ParseDataChunk(const base::StringPiece& data) { |
137 data_ = data; | 145 data_ = data; |
138 return true; | 146 return true; |
139 } | 147 } |
140 | 148 |
141 } // namespace media | 149 } // namespace media |
OLD | NEW |