summaryrefslogtreecommitdiffstats
path: root/content/browser/speech/audio_buffer.cc
diff options
context:
space:
mode:
authorprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 23:57:51 +0000
committerprimiano@chromium.org <primiano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-13 23:57:51 +0000
commitfad64e7a123b6ddd2ba8af13441c74f8f37966ee (patch)
tree28f09e77787e4a77ed30d45743086f12b62f58ee /content/browser/speech/audio_buffer.cc
parent4a5aebb91b0784ef133a926773b0b9e517f288d9 (diff)
downloadchromium_src-fad64e7a123b6ddd2ba8af13441c74f8f37966ee.zip
chromium_src-fad64e7a123b6ddd2ba8af13441c74f8f37966ee.tar.gz
chromium_src-fad64e7a123b6ddd2ba8af13441c74f8f37966ee.tar.bz2
Added AudioBuffer/AudioChunk abstractions for speech recognition and improved speech_recognizer_impl_unittest.
audio_encoder - Introduced AudioBuffer class in order to hide the current string-based implementation (which involved a lot of dirty and distributed casts) and make room for future implementations based on a circular buffer. speech_recognizer_impl_unittest - Created MockAudioManager class, in order to avoid using the true audio manager on trybots, which could lead to errors accessing the audio device. BUG=116954 TEST=speech_recognizer_impl_uinittest should never raise errors related to the audio driver (e.g, device in use, no microphone attached, etc). Review URL: http://codereview.chromium.org/9646031 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126512 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/speech/audio_buffer.cc')
-rw-r--r--content/browser/speech/audio_buffer.cc91
1 files changed, 91 insertions, 0 deletions
diff --git a/content/browser/speech/audio_buffer.cc b/content/browser/speech/audio_buffer.cc
new file mode 100644
index 0000000..5b887d7
--- /dev/null
+++ b/content/browser/speech/audio_buffer.cc
@@ -0,0 +1,91 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/logging.h"
+#include "base/stl_util.h"
+#include "content/browser/speech/audio_buffer.h"
+
+namespace speech {
+
+AudioChunk::AudioChunk(int bytes_per_sample)
+ : bytes_per_sample_(bytes_per_sample) {
+}
+
+AudioChunk::AudioChunk(const uint8* data, size_t length, int bytes_per_sample)
+ : data_string_(reinterpret_cast<const char*>(data), length),
+ bytes_per_sample_(bytes_per_sample) {
+ DCHECK_EQ(length % bytes_per_sample, 0U);
+}
+
+bool AudioChunk::IsEmpty() const {
+ return data_string_.empty();
+}
+
+size_t AudioChunk::NumSamples() const {
+ return data_string_.size() / bytes_per_sample_;
+}
+
+const std::string& AudioChunk::AsString() const {
+ return data_string_;
+}
+
+int16 AudioChunk::GetSample16(size_t index) const {
+ DCHECK(index < (data_string_.size() / sizeof(int16)));
+ return SamplesData16()[index];
+}
+
+const int16* AudioChunk::SamplesData16() const {
+ return reinterpret_cast<const int16*>(data_string_.data());
+}
+
+
+AudioBuffer::AudioBuffer(int bytes_per_sample)
+ : bytes_per_sample_(bytes_per_sample) {
+ DCHECK(bytes_per_sample == 1 ||
+ bytes_per_sample == 2 ||
+ bytes_per_sample == 4);
+}
+
+AudioBuffer::~AudioBuffer() {
+ Clear();
+}
+
+void AudioBuffer::Enqueue(const uint8* data, size_t length) {
+ AudioChunk* chunk = new AudioChunk(data, length, bytes_per_sample_);
+ chunks_.push_back(chunk);
+}
+
+scoped_ptr<AudioChunk> AudioBuffer::DequeueSingleChunk() {
+ DCHECK(!chunks_.empty());
+ AudioChunk* chunk = *chunks_.begin();
+ chunks_.weak_erase(chunks_.begin());
+ return scoped_ptr<AudioChunk>(chunk);
+}
+
+scoped_ptr<AudioChunk> AudioBuffer::DequeueAll() {
+ AudioChunk* chunk = new AudioChunk(bytes_per_sample_);
+ size_t resulting_length = 0;
+ ChunksContainer::const_iterator it;
+ // In order to improve performance, calulate in advance the total length
+ // and then copy the chunks.
+ for (it = chunks_.begin(); it != chunks_.end(); ++it) {
+ resulting_length += (*it)->data_string_.length();
+ }
+ chunk->data_string_.reserve(resulting_length);
+ for (it = chunks_.begin(); it != chunks_.end(); ++it) {
+ chunk->data_string_.append((*it)->data_string_);
+ }
+ Clear();
+ return scoped_ptr<AudioChunk>(chunk);
+}
+
+void AudioBuffer::Clear() {
+ chunks_.erase(chunks_.begin(), chunks_.end());
+}
+
+bool AudioBuffer::IsEmpty() const {
+ return chunks_.empty();
+}
+
+} // namespace speech