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

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: 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
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 scoped_refptr<AudioChunk> chunk(
56 new AudioChunk(data, length, bytes_per_sample_));
56 chunks_.push_back(chunk); 57 chunks_.push_back(chunk);
hans 2012/03/27 15:56:31 could we just do this on a single line? chunks_.p
Primiano Tucci (use gerrit) 2012/03/27 16:51:24 Uh, right. It was a remainder of the old code :)
57 } 58 }
58 59
59 scoped_ptr<AudioChunk> AudioBuffer::DequeueSingleChunk() { 60 scoped_refptr<AudioChunk> AudioBuffer::DequeueSingleChunk() {
60 DCHECK(!chunks_.empty()); 61 DCHECK(!chunks_.empty());
61 AudioChunk* chunk = *chunks_.begin(); 62 scoped_refptr<AudioChunk> chunk(chunks_.front());
62 chunks_.weak_erase(chunks_.begin()); 63 chunks_.pop_front();
63 return scoped_ptr<AudioChunk>(chunk); 64 return chunk;
64 } 65 }
65 66
66 scoped_ptr<AudioChunk> AudioBuffer::DequeueAll() { 67 scoped_refptr<AudioChunk> AudioBuffer::DequeueAll() {
67 AudioChunk* chunk = new AudioChunk(bytes_per_sample_); 68 scoped_refptr<AudioChunk> chunk(new AudioChunk(bytes_per_sample_));
68 size_t resulting_length = 0; 69 size_t resulting_length = 0;
69 ChunksContainer::const_iterator it; 70 ChunksContainer::iterator it;
hans 2012/03/27 15:56:31 hmm, why isn't it a const_iterator anymore?
Primiano Tucci (use gerrit) 2012/03/27 16:51:24 Ah right. I tried to use a std::queue at beginning
70 // In order to improve performance, calulate in advance the total length 71 // In order to improve performance, calulate in advance the total length
71 // and then copy the chunks. 72 // and then copy the chunks.
72 for (it = chunks_.begin(); it != chunks_.end(); ++it) { 73 for (it = chunks_.begin(); it != chunks_.end(); ++it) {
73 resulting_length += (*it)->data_string_.length(); 74 resulting_length += (*it)->data_string_.length();
74 } 75 }
75 chunk->data_string_.reserve(resulting_length); 76 chunk->data_string_.reserve(resulting_length);
76 for (it = chunks_.begin(); it != chunks_.end(); ++it) { 77 for (it = chunks_.begin(); it != chunks_.end(); ++it) {
77 chunk->data_string_.append((*it)->data_string_); 78 chunk->data_string_.append((*it)->data_string_);
78 } 79 }
79 Clear(); 80 Clear();
80 return scoped_ptr<AudioChunk>(chunk); 81 return chunk;
81 } 82 }
82 83
83 void AudioBuffer::Clear() { 84 void AudioBuffer::Clear() {
84 chunks_.erase(chunks_.begin(), chunks_.end()); 85 chunks_.clear();
85 } 86 }
86 87
87 bool AudioBuffer::IsEmpty() const { 88 bool AudioBuffer::IsEmpty() const {
88 return chunks_.empty(); 89 return chunks_.empty();
89 } 90 }
90 91
91 } // namespace speech 92 } // namespace speech
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698