OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "platform/audio/PushPullFIFO.h" | |
6 | |
7 #include "wtf/PtrUtil.h" | |
8 #include <memory> | |
9 | |
10 namespace blink { | |
11 | |
12 PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t length) | |
13 : m_dataBus(AudioBus::create(numberOfChannels, length)), | |
14 m_length(length), | |
15 m_numberOfFrames(0), | |
16 m_indexRead(0), | |
17 m_indexWrite(0) {} | |
18 | |
19 PushPullFIFO::~PushPullFIFO() {} | |
20 | |
21 // Push the data from |inputBus| to FIFO. The size of push is determined by | |
22 // the length of |inputBus|. | |
23 void PushPullFIFO::push(const AudioBus* inputBus) { | |
24 if (!inputBus) { | |
25 NOTREACHED(); | |
o1ka
2017/01/09 14:09:03
Why not just DCHECK(inputBus)?
hongchan
2017/01/09 21:53:16
Done. The early return is for the release build.
o1ka
2017/01/10 10:07:19
See my other comment about early returns
| |
26 return; | |
27 } | |
28 | |
29 size_t remainder = m_length - m_indexWrite; | |
30 size_t inputBusLength = inputBus->length(); | |
31 | |
32 if (remainder > m_length || m_indexWrite > m_length || | |
33 inputBusLength > m_length || | |
34 inputBusLength + m_numberOfFrames > m_length) { | |
Raymond Toy
2017/01/06 22:22:03
Need a check if number of channels for the bus mat
o1ka
2017/01/09 14:09:03
Same comment about naming: m_length, inputBusLengt
hongchan
2017/01/09 21:53:16
@rtoy
Hmm. My code is based on the current FIFO d
o1ka
2017/01/10 10:07:19
Sorry for being unclear, my comment was about vari
hongchan
2017/01/10 21:15:58
Done.
| |
35 NOTREACHED(); | |
o1ka
2017/01/09 14:09:03
How will we debug silent failures like this?
Shoul
hongchan
2017/01/09 21:53:16
This is a more of WebAudio's practice from the old
o1ka
2017/01/10 10:07:19
See my other comment on this.
| |
36 return; | |
37 } | |
Raymond Toy
2017/01/06 22:22:03
Can we add DCHECKs for these too? While it's nice
hongchan
2017/01/09 21:53:16
I think these checks are being overly careful abou
Raymond Toy
2017/01/09 22:46:02
Ok. I'll leave it up to you to decide which checks
hongchan
2017/01/10 21:15:58
Done.
| |
38 | |
39 for (unsigned i = 0; i < m_dataBus->numberOfChannels(); ++i) { | |
o1ka
2017/01/09 14:09:03
Can we name it something like "m_fifoBus" to go al
hongchan
2017/01/09 21:53:16
Done. Changed it to fifoDataBus.
| |
40 float* fifoChannel = m_dataBus->channel(i)->mutableData(); | |
o1ka
2017/01/09 14:09:03
Can we name int fifoBusChannel to match inputBusCh
hongchan
2017/01/09 21:53:16
Done.
| |
41 const float* inputBusChannel = inputBus->channel(i)->data(); | |
42 if (remainder >= inputBusLength) { | |
43 // The remainder is big enough for the input data. | |
44 memcpy(fifoChannel + m_indexWrite, inputBusChannel, | |
45 inputBusLength * sizeof(*fifoChannel)); | |
46 } else { | |
47 // The input data overflows the remainder size. Wrap around the index. | |
48 memcpy(fifoChannel + m_indexWrite, inputBusChannel, | |
49 remainder * sizeof(*fifoChannel)); | |
50 memcpy(fifoChannel, inputBusChannel + remainder, | |
o1ka
2017/01/09 14:09:03
What if you overwrite the beginning of data writte
hongchan
2017/01/09 21:53:16
Yes, this is something I wanted to discuss in this
o1ka
2017/01/10 10:07:19
You added a check for that, right?
| |
51 (inputBusLength - remainder) * sizeof(*fifoChannel)); | |
52 } | |
53 } | |
54 | |
55 // Accumulate the valid number of frames in FIFO. | |
56 m_numberOfFrames += inputBusLength; | |
o1ka
2017/01/09 14:09:03
What does |m_numberOfFrames| represent? Can it be
hongchan
2017/01/09 21:53:16
It represents 'meaningful frames' that have not be
o1ka
2017/01/10 10:07:19
|framesAvailable| maybe?
hongchan
2017/01/10 21:15:58
Done.
| |
57 DCHECK_LE(m_numberOfFrames, m_length); | |
58 | |
59 // Update the write index; wrap it around if necessary. | |
60 m_indexWrite = (m_indexWrite + inputBusLength) % m_length; | |
Raymond Toy
2017/01/06 22:22:03
It would be nice if m_length were a power of two.
hongchan
2017/01/09 21:53:16
What's the benefit of the FIFO length being power
Raymond Toy
2017/01/09 22:46:02
Modulo would just be a mask instead of a division.
| |
61 } | |
62 | |
63 // Pull the data out of FIFO to |outputBus|. | |
Raymond Toy
2017/01/06 22:22:03
Specify what happens if outputBus is longer than f
hongchan
2017/01/09 21:53:16
Done.
| |
64 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesToPull) { | |
65 if (!outputBus) { | |
66 NOTREACHED(); | |
o1ka
2017/01/09 14:09:03
Same question as above for all NOTREACHED().
hongchan
2017/01/09 21:53:16
Done.
| |
67 return; | |
68 } | |
69 | |
70 size_t remainder = m_length - m_indexRead; | |
71 | |
o1ka
2017/01/09 14:09:03
DCHECK(remainder >= 0)?
hongchan
2017/01/09 21:53:15
Done.
Raymond Toy
2017/01/09 22:46:02
That seems wrong. remainder is a size_t so it's a
o1ka
2017/01/10 10:07:19
Right, we should have CHECKs before subtraction in
| |
72 if (remainder > m_length || m_indexRead > m_length || | |
73 framesToPull > m_length || framesToPull > m_numberOfFrames || | |
74 outputBus->length() > framesToPull) { | |
75 NOTREACHED(); | |
76 return; | |
77 } | |
78 | |
79 for (unsigned i = 0; i < m_dataBus->numberOfChannels(); ++i) { | |
80 const float* fifoChannel = m_dataBus->channel(i)->data(); | |
81 float* outputBusChannel = outputBus->channel(i)->mutableData(); | |
82 if (remainder >= framesToPull) { | |
83 // The remainder is big enough for the frames to pull. | |
84 memcpy(outputBusChannel, fifoChannel + m_indexRead, | |
85 framesToPull * sizeof(*fifoChannel)); | |
86 } else { | |
87 // The frames to pull is bigger than the remainder size. | |
88 // Wrap around the index. | |
89 memcpy(outputBusChannel, fifoChannel + m_indexWrite, | |
Raymond Toy
2017/01/06 22:22:03
You mean indexRead, not indexWrite? It would be g
hongchan
2017/01/09 21:53:16
Done. I am planning for a unit test for the FIFO.
| |
90 remainder * sizeof(*fifoChannel)); | |
91 memcpy(outputBusChannel + remainder, fifoChannel, | |
92 (framesToPull - remainder) * sizeof(*fifoChannel)); | |
93 } | |
94 } | |
95 | |
96 // Update the read index; wrap it around if necessary. | |
97 m_indexRead = (m_indexRead + framesToPull) % m_length; | |
98 | |
99 // Subtract the valid number of frames in FIFO. | |
100 DCHECK_GE(m_numberOfFrames, framesToPull); | |
101 m_numberOfFrames -= framesToPull; | |
102 } | |
103 | |
104 } // namespace blink | |
OLD | NEW |