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.

189 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. AudioSourcePlayer::AudioSourcePlayer()
  24. : source (nullptr),
  25. sampleRate (0),
  26. bufferSize (0),
  27. lastGain (1.0f),
  28. gain (1.0f)
  29. {
  30. }
  31. AudioSourcePlayer::~AudioSourcePlayer()
  32. {
  33. setSource (nullptr);
  34. }
  35. void AudioSourcePlayer::setSource (AudioSource* newSource)
  36. {
  37. if (source != newSource)
  38. {
  39. AudioSource* const oldSource = source;
  40. if (newSource != nullptr && bufferSize > 0 && sampleRate > 0)
  41. newSource->prepareToPlay (bufferSize, sampleRate);
  42. {
  43. const ScopedLock sl (readLock);
  44. source = newSource;
  45. }
  46. if (oldSource != nullptr)
  47. oldSource->releaseResources();
  48. }
  49. }
  50. void AudioSourcePlayer::setGain (const float newGain) noexcept
  51. {
  52. gain = newGain;
  53. }
  54. void AudioSourcePlayer::audioDeviceIOCallback (const float** inputChannelData,
  55. int totalNumInputChannels,
  56. float** outputChannelData,
  57. int totalNumOutputChannels,
  58. int numSamples)
  59. {
  60. // these should have been prepared by audioDeviceAboutToStart()...
  61. jassert (sampleRate > 0 && bufferSize > 0);
  62. const ScopedLock sl (readLock);
  63. if (source != nullptr)
  64. {
  65. int numActiveChans = 0, numInputs = 0, numOutputs = 0;
  66. // messy stuff needed to compact the channels down into an array
  67. // of non-zero pointers..
  68. for (int i = 0; i < totalNumInputChannels; ++i)
  69. {
  70. if (inputChannelData[i] != nullptr)
  71. {
  72. inputChans [numInputs++] = inputChannelData[i];
  73. if (numInputs >= numElementsInArray (inputChans))
  74. break;
  75. }
  76. }
  77. for (int i = 0; i < totalNumOutputChannels; ++i)
  78. {
  79. if (outputChannelData[i] != nullptr)
  80. {
  81. outputChans [numOutputs++] = outputChannelData[i];
  82. if (numOutputs >= numElementsInArray (outputChans))
  83. break;
  84. }
  85. }
  86. if (numInputs > numOutputs)
  87. {
  88. // if there aren't enough output channels for the number of
  89. // inputs, we need to create some temporary extra ones (can't
  90. // use the input data in case it gets written to)
  91. tempBuffer.setSize (numInputs - numOutputs, numSamples,
  92. false, false, true);
  93. for (int i = 0; i < numOutputs; ++i)
  94. {
  95. channels[numActiveChans] = outputChans[i];
  96. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  97. ++numActiveChans;
  98. }
  99. for (int i = numOutputs; i < numInputs; ++i)
  100. {
  101. channels[numActiveChans] = tempBuffer.getWritePointer (i - numOutputs);
  102. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  103. ++numActiveChans;
  104. }
  105. }
  106. else
  107. {
  108. for (int i = 0; i < numInputs; ++i)
  109. {
  110. channels[numActiveChans] = outputChans[i];
  111. memcpy (channels[numActiveChans], inputChans[i], sizeof (float) * (size_t) numSamples);
  112. ++numActiveChans;
  113. }
  114. for (int i = numInputs; i < numOutputs; ++i)
  115. {
  116. channels[numActiveChans] = outputChans[i];
  117. zeromem (channels[numActiveChans], sizeof (float) * (size_t) numSamples);
  118. ++numActiveChans;
  119. }
  120. }
  121. AudioSampleBuffer buffer (channels, numActiveChans, numSamples);
  122. AudioSourceChannelInfo info (&buffer, 0, numSamples);
  123. source->getNextAudioBlock (info);
  124. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  125. buffer.applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  126. lastGain = gain;
  127. }
  128. else
  129. {
  130. for (int i = 0; i < totalNumOutputChannels; ++i)
  131. if (outputChannelData[i] != nullptr)
  132. zeromem (outputChannelData[i], sizeof (float) * (size_t) numSamples);
  133. }
  134. }
  135. void AudioSourcePlayer::audioDeviceAboutToStart (AudioIODevice* device)
  136. {
  137. prepareToPlay (device->getCurrentSampleRate(),
  138. device->getCurrentBufferSizeSamples());
  139. }
  140. void AudioSourcePlayer::prepareToPlay (double newSampleRate, int newBufferSize)
  141. {
  142. sampleRate = newSampleRate;
  143. bufferSize = newBufferSize;
  144. zeromem (channels, sizeof (channels));
  145. if (source != nullptr)
  146. source->prepareToPlay (bufferSize, sampleRate);
  147. }
  148. void AudioSourcePlayer::audioDeviceStopped()
  149. {
  150. if (source != nullptr)
  151. source->releaseResources();
  152. sampleRate = 0.0;
  153. bufferSize = 0;
  154. tempBuffer.setSize (2, 8);
  155. }