Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_AudioSourcePlayer.cpp 5.9KB

9 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. AudioSourcePlayer::AudioSourcePlayer()
  18. : source (nullptr),
  19. sampleRate (0),
  20. bufferSize (0),
  21. lastGain (1.0f),
  22. gain (1.0f)
  23. {
  24. }
  25. AudioSourcePlayer::~AudioSourcePlayer()
  26. {
  27. setSource (nullptr);
  28. }
  29. void AudioSourcePlayer::setSource (AudioSource* newSource)
  30. {
  31. if (source != newSource)
  32. {
  33. AudioSource* const oldSource = source;
  34. if (newSource != nullptr && bufferSize > 0 && sampleRate > 0)
  35. newSource->prepareToPlay (bufferSize, sampleRate);
  36. {
  37. const ScopedLock sl (readLock);
  38. source = newSource;
  39. }
  40. if (oldSource != nullptr)
  41. oldSource->releaseResources();
  42. }
  43. }
  44. void AudioSourcePlayer::setGain (const float newGain) noexcept
  45. {
  46. gain = newGain;
  47. }
  48. void AudioSourcePlayer::audioDeviceIOCallback (const float** inputChannelData,
  49. int totalNumInputChannels,
  50. float** outputChannelData,
  51. int totalNumOutputChannels,
  52. int numSamples)
  53. {
  54. // these should have been prepared by audioDeviceAboutToStart()...
  55. jassert (sampleRate > 0 && bufferSize > 0);
  56. const ScopedLock sl (readLock);
  57. if (source != nullptr)
  58. {
  59. int numActiveChans = 0, numInputs = 0, numOutputs = 0;
  60. // messy stuff needed to compact the channels down into an array
  61. // of non-zero pointers..
  62. for (int i = 0; i < totalNumInputChannels; ++i)
  63. {
  64. if (inputChannelData[i] != nullptr)
  65. {
  66. inputChans [numInputs++] = inputChannelData[i];
  67. if (numInputs >= numElementsInArray (inputChans))
  68. break;
  69. }
  70. }
  71. for (int i = 0; i < totalNumOutputChannels; ++i)
  72. {
  73. if (outputChannelData[i] != nullptr)
  74. {
  75. outputChans [numOutputs++] = outputChannelData[i];
  76. if (numOutputs >= numElementsInArray (outputChans))
  77. break;
  78. }
  79. }
  80. if (numInputs > numOutputs)
  81. {
  82. // if there aren't enough output channels for the number of
  83. // inputs, we need to create some temporary extra ones (can't
  84. // use the input data in case it gets written to)
  85. tempBuffer.setSize (numInputs - numOutputs, numSamples,
  86. false, false, true);
  87. for (int i = 0; i < numOutputs; ++i)
  88. {
  89. channels[numActiveChans] = outputChans[i];
  90. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  91. ++numActiveChans;
  92. }
  93. for (int i = numOutputs; i < numInputs; ++i)
  94. {
  95. channels[numActiveChans] = tempBuffer.getWritePointer (i - numOutputs);
  96. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  97. ++numActiveChans;
  98. }
  99. }
  100. else
  101. {
  102. for (int i = 0; i < numInputs; ++i)
  103. {
  104. channels[numActiveChans] = outputChans[i];
  105. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  106. ++numActiveChans;
  107. }
  108. for (int i = numInputs; i < numOutputs; ++i)
  109. {
  110. channels[numActiveChans] = outputChans[i];
  111. zeromem (channels[numActiveChans], sizeof (float) * (size_t) numSamples);
  112. ++numActiveChans;
  113. }
  114. }
  115. AudioSampleBuffer buffer (channels, numActiveChans, numSamples);
  116. AudioSourceChannelInfo info (&buffer, 0, numSamples);
  117. source->getNextAudioBlock (info);
  118. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  119. buffer.applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  120. lastGain = gain;
  121. }
  122. else
  123. {
  124. for (int i = 0; i < totalNumOutputChannels; ++i)
  125. if (outputChannelData[i] != nullptr)
  126. zeromem (outputChannelData[i], sizeof (float) * (size_t) numSamples);
  127. }
  128. }
  129. void AudioSourcePlayer::audioDeviceAboutToStart (AudioIODevice* device)
  130. {
  131. prepareToPlay (device->getCurrentSampleRate(),
  132. device->getCurrentBufferSizeSamples());
  133. }
  134. void AudioSourcePlayer::prepareToPlay (double newSampleRate, int newBufferSize)
  135. {
  136. sampleRate = newSampleRate;
  137. bufferSize = newBufferSize;
  138. zeromem (channels, sizeof (channels));
  139. if (source != nullptr)
  140. source->prepareToPlay (bufferSize, sampleRate);
  141. }
  142. void AudioSourcePlayer::audioDeviceStopped()
  143. {
  144. if (source != nullptr)
  145. source->releaseResources();
  146. sampleRate = 0.0;
  147. bufferSize = 0;
  148. tempBuffer.setSize (2, 8);
  149. }