filmstro_ffmpeg
 All Classes Files Functions Friends Macros
filmstro_audiohelpers_AudioBufferFIFO.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3  Copyright (c) 2017, Filmstro Ltd. - Daniel Walz
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification,
7  are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13  3. Neither the name of the copyright holder nor the names of its contributors
14  may be used to endorse or promote products derived from this software without
15  specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26  OF THE POSSIBILITY OF SUCH DAMAGE.
27  ==============================================================================
28  \class AudioBufferFIFO
29  \file filmstro_audiobasics_AudioBufferFIFO.h
30  \brief A FIFO for audio samples
31 
32  \author Daniel Walz / Filmstro Ltd.
33  \date September 7th 2016
34 
35  \description A FIFO for AudioBuffer / samples
36 
37  ==============================================================================
38  */
39 
40 #ifndef FSPRO_AUDIOBASICS_AUDIOBUFFERFIFO_H_INCLUDED
41 #define FSPRO_AUDIOBASICS_AUDIOBUFFERFIFO_H_INCLUDED
42 
49 template<typename FloatType>
50 class AudioBufferFIFO : public juce::AbstractFifo
51 {
52 public:
53  /*< Creates a FIFO with a buffer of given number of channels and given number of samples */
54  AudioBufferFIFO (int channels, int buffersize) :
55  AbstractFifo (buffersize)
56  {
57  buffer.setSize (channels, buffersize);
58  }
59 
60  /*< Resize the buffer with new number of channels and new number of samples */
61  void setSize (const int channels, const int newBufferSize)
62  {
63  buffer.setSize (channels, newBufferSize);
64  setTotalSize (newBufferSize);
65  reset ();
66  }
67 
68  /*< Push samples into the FIFO from raw float arrays */
69  void addToFifo (const FloatType** samples, int numSamples)
70  {
71  jassert (getFreeSpace() >= numSamples);
72  int start1, size1, start2, size2;
73  prepareToWrite (numSamples, start1, size1, start2, size2);
74  if (size1 > 0)
75  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
76  buffer.copyFrom (channel, start1, samples[channel], size1);
77  if (size2 > 0)
78  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
79  buffer.copyFrom (channel, start2, samples[channel] + size1, size2);
80  finishedWrite (size1 + size2);
81  }
82 
83  /*< Push samples from an AudioBuffer into the FIFO */
84  void addToFifo (const juce::AudioBuffer<FloatType>& samples, int numSamples=-1)
85  {
86  const int addSamples = numSamples < 0 ? samples.getNumSamples() : numSamples;
87  jassert (getFreeSpace() >= addSamples);
88 
89  int start1, size1, start2, size2;
90  prepareToWrite (addSamples, start1, size1, start2, size2);
91  if (size1 > 0)
92  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
93  buffer.copyFrom (channel, start1, samples.getReadPointer (channel), size1);
94  if (size2 > 0)
95  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
96  buffer.copyFrom (channel, start2, samples.getReadPointer (channel, size1), size2);
97  finishedWrite (size1 + size2);
98 
99  }
100 
101  /*< Read samples from the FIFO into raw float arrays */
102  void readFromFifo (FloatType** samples, int numSamples)
103  {
104  jassert (getNumReady() >= numSamples);
105  int start1, size1, start2, size2;
106  prepareToRead (numSamples, start1, size1, start2, size2);
107  if (size1 > 0)
108  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
109  juce::FloatVectorOperations::copy (samples [channel],
110  buffer.getReadPointer (channel, start1),
111  size1);
112  if (size2 > 0)
113  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
114  juce::FloatVectorOperations::copy (samples [channel] + size1,
115  buffer.getReadPointer (channel, start2),
116  size2);
117  finishedRead (size1 + size2);
118  }
119 
120  /*< Read samples from the FIFO into AudioBuffers */
121  void readFromFifo (juce::AudioBuffer<FloatType>& samples, int numSamples=-1)
122  {
123  const int readSamples = numSamples > 0 ? numSamples : samples.getNumSamples();
124  jassert (getNumReady() >= readSamples);
125 
126  int start1, size1, start2, size2;
127  prepareToRead (readSamples, start1, size1, start2, size2);
128  if (size1 > 0)
129  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
130  samples.copyFrom (channel, 0, buffer.getReadPointer (channel, start1), size1);
131  if (size2 > 0)
132  for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
133  samples.copyFrom (channel, size1, buffer.getReadPointer (channel, start2), size2);
134  finishedRead (size1 + size2);
135  }
136 
137  /*< Read samples from the FIFO into AudioSourceChannelInfo buffers to be used in AudioSources getNextAudioBlock */
138  void readFromFifo (const juce::AudioSourceChannelInfo& info, int numSamples=-1)
139  {
140  const int readSamples = numSamples > 0 ? numSamples : info.numSamples;
141  jassert (getNumReady() >= readSamples);
142 
143  int start1, size1, start2, size2;
144  prepareToRead (readSamples, start1, size1, start2, size2);
145  if (size1 > 0)
146  for (int channel = 0; channel < info.buffer->getNumChannels(); ++channel)
147  info.buffer->copyFrom (channel, info.startSample, buffer.getReadPointer (channel, start1), size1);
148  if (size2 > 0)
149  for (int channel = 0; channel < info.buffer->getNumChannels(); ++channel)
150  info.buffer->copyFrom (channel, info.startSample + size1, buffer.getReadPointer (channel, start2), size2);
151  finishedRead (size1 + size2);
152  }
153 
154  /*< Returns the number of channels of the underlying buffer */
155  int getNumChannels () const {
156  return buffer.getNumChannels();
157  }
158 
159  /*< Clears all samples and sets the FIFO state to empty */
160  void clear () {
161  buffer.clear ();
162  reset();
163  }
164 
165 private:
166  /*< The actual audio buffer */
167  juce::AudioBuffer<FloatType> buffer;
168 };
169 
170 
171 
172 
173 #endif /* FSPRO_AUDIOBASICS_AUDIOBUFFERFIFO_H_INCLUDED */
174 
int getNumChannels() const
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:155
void readFromFifo(FloatType **samples, int numSamples)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:102
void readFromFifo(const juce::AudioSourceChannelInfo &info, int numSamples=-1)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:138
void readFromFifo(juce::AudioBuffer< FloatType > &samples, int numSamples=-1)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:121
The AudioBufferFIFO implements an actual sample buffer using JUCEs AbstractFIFO class.
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:50
AudioBufferFIFO(int channels, int buffersize)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:54
void clear()
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:160
void setSize(const int channels, const int newBufferSize)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:61
void addToFifo(const FloatType **samples, int numSamples)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:69
void addToFifo(const juce::AudioBuffer< FloatType > &samples, int numSamples=-1)
Definition: filmstro_audiohelpers_AudioBufferFIFO.h:84