| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/speech/audio_buffer.h" | 5 #include "content/browser/speech/audio_buffer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace content { | 9 namespace content { |
| 10 | 10 |
| 11 AudioChunk::AudioChunk(int bytes_per_sample) | 11 AudioChunk::AudioChunk(int bytes_per_sample) |
| 12 : bytes_per_sample_(bytes_per_sample) { | 12 : bytes_per_sample_(bytes_per_sample) { |
| 13 } | 13 } |
| 14 | 14 |
| 15 AudioChunk::AudioChunk(size_t length, int bytes_per_sample) | 15 AudioChunk::AudioChunk(size_t length, int bytes_per_sample) |
| 16 : data_string_(length, '\0'), bytes_per_sample_(bytes_per_sample) { | 16 : data_string_(length, '\0'), bytes_per_sample_(bytes_per_sample) { |
| 17 DCHECK_EQ(length % bytes_per_sample, 0U); | 17 DCHECK_EQ(length % bytes_per_sample, 0U); |
| 18 } | 18 } |
| 19 | 19 |
| 20 AudioChunk::AudioChunk(const uint8* data, size_t length, int bytes_per_sample) | 20 AudioChunk::AudioChunk(const uint8_t* data, size_t length, int bytes_per_sample) |
| 21 : data_string_(reinterpret_cast<const char*>(data), length), | 21 : data_string_(reinterpret_cast<const char*>(data), length), |
| 22 bytes_per_sample_(bytes_per_sample) { | 22 bytes_per_sample_(bytes_per_sample) { |
| 23 DCHECK_EQ(length % bytes_per_sample, 0U); | 23 DCHECK_EQ(length % bytes_per_sample, 0U); |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool AudioChunk::IsEmpty() const { | 26 bool AudioChunk::IsEmpty() const { |
| 27 return data_string_.empty(); | 27 return data_string_.empty(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 size_t AudioChunk::NumSamples() const { | 30 size_t AudioChunk::NumSamples() const { |
| 31 return data_string_.size() / bytes_per_sample_; | 31 return data_string_.size() / bytes_per_sample_; |
| 32 } | 32 } |
| 33 | 33 |
| 34 const std::string& AudioChunk::AsString() const { | 34 const std::string& AudioChunk::AsString() const { |
| 35 return data_string_; | 35 return data_string_; |
| 36 } | 36 } |
| 37 | 37 |
| 38 int16 AudioChunk::GetSample16(size_t index) const { | 38 int16_t AudioChunk::GetSample16(size_t index) const { |
| 39 DCHECK(index < (data_string_.size() / sizeof(int16))); | 39 DCHECK(index < (data_string_.size() / sizeof(int16_t))); |
| 40 return SamplesData16()[index]; | 40 return SamplesData16()[index]; |
| 41 } | 41 } |
| 42 | 42 |
| 43 const int16* AudioChunk::SamplesData16() const { | 43 const int16_t* AudioChunk::SamplesData16() const { |
| 44 return reinterpret_cast<const int16*>(data_string_.data()); | 44 return reinterpret_cast<const int16_t*>(data_string_.data()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 AudioBuffer::AudioBuffer(int bytes_per_sample) | 47 AudioBuffer::AudioBuffer(int bytes_per_sample) |
| 48 : bytes_per_sample_(bytes_per_sample) { | 48 : bytes_per_sample_(bytes_per_sample) { |
| 49 DCHECK(bytes_per_sample == 1 || | 49 DCHECK(bytes_per_sample == 1 || |
| 50 bytes_per_sample == 2 || | 50 bytes_per_sample == 2 || |
| 51 bytes_per_sample == 4); | 51 bytes_per_sample == 4); |
| 52 } | 52 } |
| 53 | 53 |
| 54 AudioBuffer::~AudioBuffer() { | 54 AudioBuffer::~AudioBuffer() { |
| 55 Clear(); | 55 Clear(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void AudioBuffer::Enqueue(const uint8* data, size_t length) { | 58 void AudioBuffer::Enqueue(const uint8_t* data, size_t length) { |
| 59 chunks_.push_back(new AudioChunk(data, length, bytes_per_sample_)); | 59 chunks_.push_back(new AudioChunk(data, length, bytes_per_sample_)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 scoped_refptr<AudioChunk> AudioBuffer::DequeueSingleChunk() { | 62 scoped_refptr<AudioChunk> AudioBuffer::DequeueSingleChunk() { |
| 63 DCHECK(!chunks_.empty()); | 63 DCHECK(!chunks_.empty()); |
| 64 scoped_refptr<AudioChunk> chunk(chunks_.front()); | 64 scoped_refptr<AudioChunk> chunk(chunks_.front()); |
| 65 chunks_.pop_front(); | 65 chunks_.pop_front(); |
| 66 return chunk; | 66 return chunk; |
| 67 } | 67 } |
| 68 | 68 |
| 69 scoped_refptr<AudioChunk> AudioBuffer::DequeueAll() { | 69 scoped_refptr<AudioChunk> AudioBuffer::DequeueAll() { |
| 70 size_t resulting_length = 0; | 70 size_t resulting_length = 0; |
| 71 ChunksContainer::const_iterator it; | 71 ChunksContainer::const_iterator it; |
| 72 // In order to improve performance, calulate in advance the total length | 72 // In order to improve performance, calulate in advance the total length |
| 73 // and then copy the chunks. | 73 // and then copy the chunks. |
| 74 for (it = chunks_.begin(); it != chunks_.end(); ++it) { | 74 for (it = chunks_.begin(); it != chunks_.end(); ++it) { |
| 75 resulting_length += (*it)->AsString().length(); | 75 resulting_length += (*it)->AsString().length(); |
| 76 } | 76 } |
| 77 scoped_refptr<AudioChunk> chunk( | 77 scoped_refptr<AudioChunk> chunk( |
| 78 new AudioChunk(resulting_length, bytes_per_sample_)); | 78 new AudioChunk(resulting_length, bytes_per_sample_)); |
| 79 uint8* dest = chunk->writable_data(); | 79 uint8_t* dest = chunk->writable_data(); |
| 80 for (it = chunks_.begin(); it != chunks_.end(); ++it) { | 80 for (it = chunks_.begin(); it != chunks_.end(); ++it) { |
| 81 memcpy(dest, (*it)->AsString().data(), (*it)->AsString().length()); | 81 memcpy(dest, (*it)->AsString().data(), (*it)->AsString().length()); |
| 82 dest += (*it)->AsString().length(); | 82 dest += (*it)->AsString().length(); |
| 83 } | 83 } |
| 84 Clear(); | 84 Clear(); |
| 85 return chunk; | 85 return chunk; |
| 86 } | 86 } |
| 87 | 87 |
| 88 void AudioBuffer::Clear() { | 88 void AudioBuffer::Clear() { |
| 89 chunks_.clear(); | 89 chunks_.clear(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 bool AudioBuffer::IsEmpty() const { | 92 bool AudioBuffer::IsEmpty() const { |
| 93 return chunks_.empty(); | 93 return chunks_.empty(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace content | 96 } // namespace content |
| OLD | NEW |