Chromium Code Reviews| Index: media/audio/sounds/wav_audio_handler.cc |
| diff --git a/media/audio/sounds/wav_audio_handler.cc b/media/audio/sounds/wav_audio_handler.cc |
| index 20eab8be4377f908451905f53014326ca1994c03..5479c1e2dfbe2fb843eeb846ac999fa4c77f1925 100644 |
| --- a/media/audio/sounds/wav_audio_handler.cc |
| +++ b/media/audio/sounds/wav_audio_handler.cc |
| @@ -39,8 +39,12 @@ const size_t kBitsPerSampleOffset = 14; |
| // Some constants for audio format. |
| const int kAudioFormatPCM = 1; |
| +// The number of frames each OnMoreData() call will request. |
| +const int kDefaultFrameCount = 1024; |
| + |
| // Reads an integer from |data| with |offset|. |
| -template<typename T> T ReadInt(const base::StringPiece& data, size_t offset) { |
| +template <typename T> |
| +T ReadInt(const base::StringPiece& data, size_t offset) { |
| CHECK_LE(offset + sizeof(T), data.size()); |
| T result; |
| memcpy(&result, data.data() + offset, sizeof(T)); |
| @@ -54,11 +58,7 @@ template<typename T> T ReadInt(const base::StringPiece& data, size_t offset) { |
| namespace media { |
| -WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) |
| - : num_channels_(0), |
| - sample_rate_(0), |
| - byte_rate_(0), |
| - bits_per_sample_(0) { |
| +WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) { |
| CHECK_LE(kWavFileHeaderSize, wav_data.size()) << "wav data is too small"; |
| CHECK(wav_data.starts_with(kChunkId) && |
| memcmp(wav_data.data() + 8, kFormat, 4) == 0) |
| @@ -72,11 +72,15 @@ WavAudioHandler::WavAudioHandler(const base::StringPiece& wav_data) |
| CHECK_LE(0, length) << "can't parse wav sub-chunk"; |
| offset += length; |
| } |
| -} |
| -WavAudioHandler::~WavAudioHandler() { |
| + 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.
|
| + const int64 rate = params_.GetBytesPerSecond(); |
| + if (rate) |
| + duration_ = base::TimeDelta::FromMicroseconds(size * 1000000 / rate); |
| } |
| +WavAudioHandler::~WavAudioHandler() {} |
| + |
| bool WavAudioHandler::AtEnd(size_t cursor) const { |
| return data_.size() <= cursor; |
| } |
| @@ -86,18 +90,20 @@ bool WavAudioHandler::CopyTo(AudioBus* bus, |
| size_t* bytes_written) const { |
| if (!bus) |
| return false; |
| - if (bus->channels() != num_channels_) { |
| - LOG(ERROR) << "Number of channel mismatch."; |
| + if (bus->channels() != params_.channels()) { |
| + VLOG(1) << "Number of channel mismatch."; |
| return false; |
| } |
| if (AtEnd(cursor)) { |
| bus->Zero(); |
| return true; |
| } |
| - const int remaining_frames = (data_.size() - cursor) / bytes_per_frame_; |
| + const int remaining_frames = |
| + (data_.size() - cursor) / params_.GetBytesPerFrame(); |
| const int frames = std::min(bus->frames(), remaining_frames); |
| - bus->FromInterleaved(data_.data() + cursor, frames, bytes_per_sample_); |
| - *bytes_written = frames * bytes_per_frame_; |
| + bus->FromInterleaved(data_.data() + cursor, frames, |
| + 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.
|
| + *bytes_written = frames * params_.GetBytesPerFrame(); |
| bus->ZeroFramesPartial(frames, bus->frames() - frames); |
| return true; |
| } |
| @@ -124,12 +130,14 @@ bool WavAudioHandler::ParseFmtChunk(const base::StringPiece& data) { |
| return false; |
| } |
| DCHECK_EQ(ReadInt<uint16>(data, kAudioFormatOffset), kAudioFormatPCM); |
| - num_channels_ = ReadInt<uint16>(data, kChannelOffset); |
| - sample_rate_ = ReadInt<uint32>(data, kSampleRateOffset); |
| - byte_rate_ = ReadInt<uint32>(data, kByteRateOffset); |
| - bits_per_sample_ = ReadInt<uint16>(data, kBitsPerSampleOffset); |
| - bytes_per_sample_ = bits_per_sample_ >> 3; |
| - bytes_per_frame_ = num_channels_ * bytes_per_sample_; |
| + int num_channels = ReadInt<uint16>(data, kChannelOffset); |
| + int sample_rate = ReadInt<uint32>(data, kSampleRateOffset); |
| + int bits_per_sample = ReadInt<uint16>(data, kBitsPerSampleOffset); |
| + params_ = AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| + GuessChannelLayout(num_channels), |
| + sample_rate, |
| + bits_per_sample, |
| + kDefaultFrameCount); |
| return true; |
| } |