Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: content/browser/speech/audio_buffer.cc

Issue 9861019: Speech refactoring: Turned AudioChunk into a refcounted class (CL1.4) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed according to Hans review. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/speech/audio_buffer.h ('k') | content/browser/speech/audio_encoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/logging.h" 5 #include "base/logging.h"
6 #include "base/stl_util.h" 6 #include "base/stl_util.h"
7 #include "content/browser/speech/audio_buffer.h" 7 #include "content/browser/speech/audio_buffer.h"
8 8
9 namespace speech { 9 namespace speech {
10 10
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 DCHECK(bytes_per_sample == 1 || 45 DCHECK(bytes_per_sample == 1 ||
46 bytes_per_sample == 2 || 46 bytes_per_sample == 2 ||
47 bytes_per_sample == 4); 47 bytes_per_sample == 4);
48 } 48 }
49 49
50 AudioBuffer::~AudioBuffer() { 50 AudioBuffer::~AudioBuffer() {
51 Clear(); 51 Clear();
52 } 52 }
53 53
54 void AudioBuffer::Enqueue(const uint8* data, size_t length) { 54 void AudioBuffer::Enqueue(const uint8* data, size_t length) {
55 AudioChunk* chunk = new AudioChunk(data, length, bytes_per_sample_); 55 chunks_.push_back(new AudioChunk(data, length, bytes_per_sample_));
56 chunks_.push_back(chunk);
57 } 56 }
58 57
59 scoped_ptr<AudioChunk> AudioBuffer::DequeueSingleChunk() { 58 scoped_refptr<AudioChunk> AudioBuffer::DequeueSingleChunk() {
60 DCHECK(!chunks_.empty()); 59 DCHECK(!chunks_.empty());
61 AudioChunk* chunk = *chunks_.begin(); 60 scoped_refptr<AudioChunk> chunk(chunks_.front());
62 chunks_.weak_erase(chunks_.begin()); 61 chunks_.pop_front();
63 return scoped_ptr<AudioChunk>(chunk); 62 return chunk;
64 } 63 }
65 64
66 scoped_ptr<AudioChunk> AudioBuffer::DequeueAll() { 65 scoped_refptr<AudioChunk> AudioBuffer::DequeueAll() {
67 AudioChunk* chunk = new AudioChunk(bytes_per_sample_); 66 scoped_refptr<AudioChunk> chunk(new AudioChunk(bytes_per_sample_));
68 size_t resulting_length = 0; 67 size_t resulting_length = 0;
69 ChunksContainer::const_iterator it; 68 ChunksContainer::const_iterator it;
70 // In order to improve performance, calulate in advance the total length 69 // In order to improve performance, calulate in advance the total length
71 // and then copy the chunks. 70 // and then copy the chunks.
72 for (it = chunks_.begin(); it != chunks_.end(); ++it) { 71 for (it = chunks_.begin(); it != chunks_.end(); ++it) {
73 resulting_length += (*it)->data_string_.length(); 72 resulting_length += (*it)->data_string_.length();
74 } 73 }
75 chunk->data_string_.reserve(resulting_length); 74 chunk->data_string_.reserve(resulting_length);
76 for (it = chunks_.begin(); it != chunks_.end(); ++it) { 75 for (it = chunks_.begin(); it != chunks_.end(); ++it) {
77 chunk->data_string_.append((*it)->data_string_); 76 chunk->data_string_.append((*it)->data_string_);
78 } 77 }
79 Clear(); 78 Clear();
80 return scoped_ptr<AudioChunk>(chunk); 79 return chunk;
81 } 80 }
82 81
83 void AudioBuffer::Clear() { 82 void AudioBuffer::Clear() {
84 chunks_.erase(chunks_.begin(), chunks_.end()); 83 chunks_.clear();
85 } 84 }
86 85
87 bool AudioBuffer::IsEmpty() const { 86 bool AudioBuffer::IsEmpty() const {
88 return chunks_.empty(); 87 return chunks_.empty();
89 } 88 }
90 89
91 } // namespace speech 90 } // namespace speech
OLDNEW
« no previous file with comments | « content/browser/speech/audio_buffer.h ('k') | content/browser/speech/audio_encoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698