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

Side by Side Diff: media/base/audio_fifo.cc

Issue 10912079: Adds AudioFifo class to Chrome media. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "media/base/audio_fifo.h"
6
7 #include "media/audio/audio_parameters.h"
8 #include "base/logging.h"
9
10 namespace media {
11
12 static void GetSizes(int pos, int max_size, int src_size,
13 int* size, int* wrap_size) {
DaleCurtis 2012/09/05 10:34:18 Indent to start with (. (Or move the line above do
henrika (OOO until Aug 14) 2012/09/05 12:49:04 Done.
14 if (pos + src_size > max_size) {
DaleCurtis 2012/09/05 10:34:18 CHECK() size, wrap_size?
henrika (OOO until Aug 14) 2012/09/05 12:49:04 See comment above about using CHECK() in this clas
15 // Wrapping is required => derive size of each segment.
16 *size = max_size - pos;
17 *wrap_size = src_size - *size;
18 } else {
19 // Wrapping is not required.
20 *size = src_size;
21 *wrap_size = 0;
22 }
23 }
24
25 static int UpdatePos(int pos, int step, int max_size) {
26 return ((pos + step) % max_size);
27 }
28
29 AudioFifo::AudioFifo(int channels, int frames)
30 : audio_bus_(AudioBus::Create(channels, frames)),
31 size_(0),
32 read_pos_(0),
33 write_pos_(0) {
34 audio_bus_->Zero();
Chris Rogers 2012/09/04 20:07:20 I don't think Zero() is necessary since Create() s
henrika (OOO until Aug 14) 2012/09/05 10:14:36 removed.
DaleCurtis 2012/09/05 10:34:18 Create() doesn't Zero, so if it needs to be Zero'd
35 }
36
37 AudioFifo::~AudioFifo() {}
38
39 bool AudioFifo::Append(const AudioBus* source) {
40 DCHECK(source);
41 DCHECK_EQ(source->channels(), audio_bus_->channels());
42
43 // Ensure that there is space for the new data in the FIFO.
44 const int source_size = source->frames();
45 if (size_ + source_size > max_size()) {
46 DLOG(ERROR) << "FIFO overflow.";
47 return false;
48 }
49
50 // Figure out if wrapping is needed and if so what segment sizes we need
51 // when adding the new audio bus content to the FIFO.
52 int append_size = 0;
DaleCurtis 2012/09/05 10:34:18 Can these names include "frames" or something more
53 int wrap_size = 0;
54 GetSizes(write_pos_, max_size(), source_size, &append_size, &wrap_size);
55
56 // Copy all channels from the source to the FIFO. Wrap around if needed.
57 for (int ch = 0; ch < source->channels(); ++ch) {
58 float* dest = audio_bus_->channel(ch);
59 const float* src = source->channel(ch);
60
61 // Append part of (or the complete) source to the FIFO.
62 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0]));
Chris Rogers 2012/09/04 20:07:20 why "&dest[write_pos_]" instead of "dest + write_p
henrika (OOO until Aug 14) 2012/09/05 10:14:36 I prefer the "C++ way" and not the "C way" since &
63 if (wrap_size > 0) {
64 // Wrapping is needed: copy remaining part from the source to the FIFO.
65 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0]));
Chris Rogers 2012/09/04 20:07:20 why not "dest" instead of "&dest[0]"
henrika (OOO until Aug 14) 2012/09/05 10:14:36 ditto
66 }
67 }
68
69 size_ += source_size;
70 DCHECK_LE(size_, max_size());
71 write_pos_ = UpdatePos(write_pos_, source_size, max_size());
72 return true;
73 }
74
75 bool AudioFifo::Remove(AudioBus* destination) {
76 DCHECK(destination);
77 DCHECK_EQ(destination->channels(), audio_bus_->channels());
78
79 // It is not possible to ask for more data than what is available in the FIFO.
80 const int dest_size = destination->frames();
81 if (dest_size > size_) {
82 DLOG(ERROR) << "FIFO underrun.";
83 return false;
84 }
85
86 // Figure out if wrapping is needed and if so what segment sizes we need
87 // when removing audio bus content from the FIFO.
88 int remove_size = 0;
89 int wrap_size = 0;
90 GetSizes(read_pos_, max_size(), dest_size, &remove_size, &wrap_size);
91
92 // Remove all channels from the FIFO and copy the content to the
Chris Rogers 2012/09/04 20:07:20 "Remove all channels from the FIFO" sounds a littl
henrika (OOO until Aug 14) 2012/09/05 10:14:36 Done.
93 // destination. Wrap around if needed.
94 for (int ch = 0; ch < destination->channels(); ++ch) {
95 float* dest = destination->channel(ch);
96 const float* src = audio_bus_->channel(ch);
97
98 // Append part of (or the complete) source to the destination.
DaleCurtis 2012/09/05 10:34:18 Update comment.
99 memcpy(&dest[0], &src[read_pos_], remove_size * sizeof(src[0]));
Chris Rogers 2012/09/04 20:07:20 "dest" instead of "&dest[0]", etc. here and in mem
henrika (OOO until Aug 14) 2012/09/05 10:14:36 ditto
100 if (wrap_size > 0) {
101 // Wrapping is needed: copy remaining part to the destination.
102 memcpy(&dest[wrap_size], &src[0], wrap_size * sizeof(src[0]));
103 }
104 }
105
106 size_ -= dest_size;
107 read_pos_ = UpdatePos(read_pos_, dest_size, max_size());
108 return true;
109 }
110
111 void AudioFifo::Clear() {
112 size_ = 0;
113 read_pos_ = 0;
114 write_pos_ = 0;
115 }
116
117 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698