OpenShot Library | libopenshot-audio  0.2.0
juce_MixerAudioSource.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
27  : currentSampleRate (0.0), bufferSizeExpected (0)
28 {
29 }
30 
32 {
34 }
35 
36 //==============================================================================
37 void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
38 {
39  if (input != nullptr && ! inputs.contains (input))
40  {
41  double localRate;
42  int localBufferSize;
43 
44  {
45  const ScopedLock sl (lock);
46  localRate = currentSampleRate;
47  localBufferSize = bufferSizeExpected;
48  }
49 
50  if (localRate > 0.0)
51  input->prepareToPlay (localBufferSize, localRate);
52 
53  const ScopedLock sl (lock);
54 
55  inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
56  inputs.add (input);
57  }
58 }
59 
61 {
62  if (input != nullptr)
63  {
64  std::unique_ptr<AudioSource> toDelete;
65 
66  {
67  const ScopedLock sl (lock);
68  const int index = inputs.indexOf (input);
69 
70  if (index < 0)
71  return;
72 
73  if (inputsToDelete [index])
74  toDelete.reset (input);
75 
76  inputsToDelete.shiftBits (-1, index);
77  inputs.remove (index);
78  }
79 
80  input->releaseResources();
81  }
82 }
83 
85 {
86  OwnedArray<AudioSource> toDelete;
87 
88  {
89  const ScopedLock sl (lock);
90 
91  for (int i = inputs.size(); --i >= 0;)
92  if (inputsToDelete[i])
93  toDelete.add (inputs.getUnchecked(i));
94 
95  inputs.clear();
96  }
97 
98  for (int i = toDelete.size(); --i >= 0;)
99  toDelete.getUnchecked(i)->releaseResources();
100 }
101 
102 void MixerAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
103 {
104  tempBuffer.setSize (2, samplesPerBlockExpected);
105 
106  const ScopedLock sl (lock);
107 
108  currentSampleRate = sampleRate;
109  bufferSizeExpected = samplesPerBlockExpected;
110 
111  for (int i = inputs.size(); --i >= 0;)
112  inputs.getUnchecked(i)->prepareToPlay (samplesPerBlockExpected, sampleRate);
113 }
114 
116 {
117  const ScopedLock sl (lock);
118 
119  for (int i = inputs.size(); --i >= 0;)
120  inputs.getUnchecked(i)->releaseResources();
121 
122  tempBuffer.setSize (2, 0);
123 
124  currentSampleRate = 0;
125  bufferSizeExpected = 0;
126 }
127 
129 {
130  const ScopedLock sl (lock);
131 
132  if (inputs.size() > 0)
133  {
134  inputs.getUnchecked(0)->getNextAudioBlock (info);
135 
136  if (inputs.size() > 1)
137  {
138  tempBuffer.setSize (jmax (1, info.buffer->getNumChannels()),
139  info.buffer->getNumSamples());
140 
141  AudioSourceChannelInfo info2 (&tempBuffer, 0, info.numSamples);
142 
143  for (int i = 1; i < inputs.size(); ++i)
144  {
145  inputs.getUnchecked(i)->getNextAudioBlock (info2);
146 
147  for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
148  info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
149  }
150  }
151  }
152  else
153  {
155  }
156 }
157 
158 } // namespace juce
juce::MixerAudioSource::removeInputSource
void removeInputSource(AudioSource *input)
Removes an input source.
Definition: juce_MixerAudioSource.cpp:60
juce::AudioSourceChannelInfo::clearActiveBufferRegion
void clearActiveBufferRegion() const
Convenient method to clear the buffer if the source is not producing any data.
Definition: juce_AudioSource.h:88
juce::MixerAudioSource::prepareToPlay
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
Definition: juce_MixerAudioSource.cpp:102
juce::AudioSource
Base class for objects that can produce a continuous stream of audio.
Definition: juce_AudioSource.h:113
juce::OwnedArray::size
int size() const noexcept
Returns the number of items currently in the array.
Definition: juce_OwnedArray.h:134
juce::OwnedArray
An array designed for holding objects.
Definition: juce_OwnedArray.h:54
juce::AudioBuffer::setSize
void setSize(int newNumChannels, int newNumSamples, bool keepExistingContent=false, bool clearExtraSpace=false, bool avoidReallocating=false)
Changes the buffer's size or number of channels.
Definition: juce_AudioSampleBuffer.h:335
juce::MixerAudioSource::addInputSource
void addInputSource(AudioSource *newInput, bool deleteWhenRemoved)
Adds an input source to the mixer.
Definition: juce_MixerAudioSource.cpp:37
juce::MixerAudioSource::getNextAudioBlock
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Implementation of the AudioSource method.
Definition: juce_MixerAudioSource.cpp:128
juce::AudioBuffer::addFrom
void addFrom(int destChannel, int destStartSample, const AudioBuffer &source, int sourceChannel, int sourceStartSample, int numSamples, Type gainToApplyToSource=Type(1)) noexcept
Adds samples from another buffer to this one.
Definition: juce_AudioSampleBuffer.h:703
juce::AudioSource::prepareToPlay
virtual void prepareToPlay(int samplesPerBlockExpected, double sampleRate)=0
Tells the source to prepare for playing.
juce::MixerAudioSource::~MixerAudioSource
~MixerAudioSource() override
Destructor.
Definition: juce_MixerAudioSource.cpp:31
juce::AudioSourceChannelInfo::startSample
int startSample
The first sample in the buffer from which the callback is expected to write data.
Definition: juce_AudioSource.h:81
juce::AudioSourceChannelInfo::numSamples
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
Definition: juce_AudioSource.h:85
juce::AudioBuffer::getNumChannels
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
Definition: juce_AudioSampleBuffer.h:237
juce::AudioSourceChannelInfo::buffer
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
Definition: juce_AudioSource.h:77
juce::AudioSourceChannelInfo
Used by AudioSource::getNextAudioBlock().
Definition: juce_AudioSource.h:36
juce::OwnedArray::getUnchecked
ObjectClass * getUnchecked(const int index) const noexcept
Returns a pointer to the object at this index in the array, without checking whether the index is in-...
Definition: juce_OwnedArray.h:164
juce::GenericScopedLock
Automatically locks and unlocks a mutex object.
Definition: juce_ScopedLock.h:58
juce::BigInteger::setBit
void setBit(int bitNumber)
Sets a specified bit to 1.
Definition: juce_BigInteger.cpp:292
juce::OwnedArray::add
ObjectClass * add(ObjectClass *newObject) noexcept
Appends a new object to the end of the array.
Definition: juce_OwnedArray.h:274
juce::AudioSource::releaseResources
virtual void releaseResources()=0
Allows the source to release anything it no longer needs after playback has stopped.
juce::MixerAudioSource::releaseResources
void releaseResources() override
Implementation of the AudioSource method.
Definition: juce_MixerAudioSource.cpp:115
juce::AudioBuffer::getNumSamples
int getNumSamples() const noexcept
Returns the number of samples allocated in each of the buffer's channels.
Definition: juce_AudioSampleBuffer.h:242
juce::BigInteger::shiftBits
void shiftBits(int howManyBitsLeft, int startBit)
Shifts a section of bits left or right.
Definition: juce_BigInteger.cpp:862
juce::MixerAudioSource::MixerAudioSource
MixerAudioSource()
Creates a MixerAudioSource.
Definition: juce_MixerAudioSource.cpp:26
juce::MixerAudioSource::removeAllInputs
void removeAllInputs()
Removes all the input sources.
Definition: juce_MixerAudioSource.cpp:84