OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
14 * its contributors may be used to endorse or promote products derived | |
15 * from this software without specific prior written permission. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 */ | |
28 | |
29 #include "platform/audio/AudioFIFO.h" | |
30 | |
31 namespace blink { | |
32 | |
33 AudioFIFO::AudioFIFO(unsigned numberOfChannels, size_t fifoLength) | |
34 : m_fifoAudioBus(AudioBus::create(numberOfChannels, fifoLength)), | |
35 m_fifoLength(fifoLength), | |
36 m_framesInFifo(0), | |
37 m_readIndex(0), | |
38 m_writeIndex(0) {} | |
39 | |
40 void AudioFIFO::consume(AudioBus* destination, size_t framesToConsume) { | |
41 bool isGood = destination && (framesToConsume <= m_fifoLength) && | |
42 (framesToConsume <= m_framesInFifo) && | |
43 (destination->length() >= framesToConsume); | |
44 ASSERT(isGood); | |
45 if (!isGood) | |
46 return; | |
47 | |
48 // Copy the requested number of samples to the destination. | |
49 | |
50 size_t part1Length; | |
51 size_t part2Length; | |
52 findWrapLengths(m_readIndex, framesToConsume, part1Length, part2Length); | |
53 | |
54 size_t numberOfChannels = m_fifoAudioBus->numberOfChannels(); | |
55 | |
56 for (size_t channelIndex = 0; channelIndex < numberOfChannels; | |
57 ++channelIndex) { | |
58 float* destinationData = destination->channel(channelIndex)->mutableData(); | |
59 const float* sourceData = m_fifoAudioBus->channel(channelIndex)->data(); | |
60 | |
61 bool isCopyGood = ((m_readIndex < m_fifoLength) && | |
62 (m_readIndex + part1Length) <= m_fifoLength && | |
63 (part1Length <= destination->length()) && | |
64 (part1Length + part2Length) <= destination->length()); | |
65 ASSERT(isCopyGood); | |
66 if (!isCopyGood) | |
67 return; | |
68 | |
69 memcpy(destinationData, sourceData + m_readIndex, | |
70 part1Length * sizeof(*sourceData)); | |
71 // Handle wrap around of the FIFO, if needed. | |
72 if (part2Length) | |
73 memcpy(destinationData + part1Length, sourceData, | |
74 part2Length * sizeof(*sourceData)); | |
75 } | |
76 m_readIndex = updateIndex(m_readIndex, framesToConsume); | |
77 ASSERT(m_framesInFifo >= framesToConsume); | |
78 m_framesInFifo -= framesToConsume; | |
79 } | |
80 | |
81 void AudioFIFO::push(const AudioBus* sourceBus) { | |
82 // Copy the sourceBus into the FIFO buffer. | |
83 | |
84 bool isGood = | |
85 sourceBus && (m_framesInFifo + sourceBus->length() <= m_fifoLength); | |
86 if (!isGood) | |
87 return; | |
88 | |
89 size_t sourceLength = sourceBus->length(); | |
90 size_t part1Length; | |
91 size_t part2Length; | |
92 findWrapLengths(m_writeIndex, sourceLength, part1Length, part2Length); | |
93 | |
94 size_t numberOfChannels = m_fifoAudioBus->numberOfChannels(); | |
95 | |
96 for (size_t channelIndex = 0; channelIndex < numberOfChannels; | |
97 ++channelIndex) { | |
98 float* destination = m_fifoAudioBus->channel(channelIndex)->mutableData(); | |
99 const float* source = sourceBus->channel(channelIndex)->data(); | |
100 | |
101 bool isCopyGood = ((m_writeIndex < m_fifoLength) && | |
102 (m_writeIndex + part1Length) <= m_fifoLength && | |
103 part2Length < m_fifoLength && | |
104 part1Length + part2Length <= sourceLength); | |
105 ASSERT(isCopyGood); | |
106 if (!isCopyGood) | |
107 return; | |
108 | |
109 memcpy(destination + m_writeIndex, source, | |
110 part1Length * sizeof(*destination)); | |
111 | |
112 // Handle wrap around of the FIFO, if needed. | |
113 if (part2Length) | |
114 memcpy(destination, source + part1Length, | |
115 part2Length * sizeof(*destination)); | |
116 } | |
117 | |
118 m_framesInFifo += sourceLength; | |
119 ASSERT(m_framesInFifo <= m_fifoLength); | |
120 m_writeIndex = updateIndex(m_writeIndex, sourceLength); | |
121 } | |
122 | |
123 void AudioFIFO::findWrapLengths(size_t index, | |
124 size_t size, | |
125 size_t& part1Length, | |
126 size_t& part2Length) { | |
127 SECURITY_DCHECK(index < m_fifoLength && size <= m_fifoLength); | |
128 if (index < m_fifoLength && size <= m_fifoLength) { | |
129 if (index + size > m_fifoLength) { | |
130 // Need to wrap. Figure out the length of each piece. | |
131 part1Length = m_fifoLength - index; | |
132 part2Length = size - part1Length; | |
133 } else { | |
134 // No wrap needed. | |
135 part1Length = size; | |
136 part2Length = 0; | |
137 } | |
138 } else { | |
139 // Invalid values for index or size. Set the part lengths to zero so nothing | |
140 // is copied. | |
141 part1Length = 0; | |
142 part2Length = 0; | |
143 } | |
144 } | |
145 | |
146 } // namespace blink | |
OLD | NEW |