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.

181 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. AudioSourcePlayer::AudioSourcePlayer()
  20. {
  21. }
  22. AudioSourcePlayer::~AudioSourcePlayer()
  23. {
  24. setSource (nullptr);
  25. }
  26. void AudioSourcePlayer::setSource (AudioSource* newSource)
  27. {
  28. if (source != newSource)
  29. {
  30. auto* oldSource = source;
  31. if (newSource != nullptr && bufferSize > 0 && sampleRate > 0)
  32. newSource->prepareToPlay (bufferSize, sampleRate);
  33. {
  34. const ScopedLock sl (readLock);
  35. source = newSource;
  36. }
  37. if (oldSource != nullptr)
  38. oldSource->releaseResources();
  39. }
  40. }
  41. void AudioSourcePlayer::setGain (const float newGain) noexcept
  42. {
  43. gain = newGain;
  44. }
  45. void AudioSourcePlayer::audioDeviceIOCallback (const float** inputChannelData,
  46. int totalNumInputChannels,
  47. float** outputChannelData,
  48. int totalNumOutputChannels,
  49. int numSamples)
  50. {
  51. // these should have been prepared by audioDeviceAboutToStart()...
  52. jassert (sampleRate > 0 && bufferSize > 0);
  53. const ScopedLock sl (readLock);
  54. if (source != nullptr)
  55. {
  56. int numActiveChans = 0, numInputs = 0, numOutputs = 0;
  57. // messy stuff needed to compact the channels down into an array
  58. // of non-zero pointers..
  59. for (int i = 0; i < totalNumInputChannels; ++i)
  60. {
  61. if (inputChannelData[i] != nullptr)
  62. {
  63. inputChans [numInputs++] = inputChannelData[i];
  64. if (numInputs >= numElementsInArray (inputChans))
  65. break;
  66. }
  67. }
  68. for (int i = 0; i < totalNumOutputChannels; ++i)
  69. {
  70. if (outputChannelData[i] != nullptr)
  71. {
  72. outputChans [numOutputs++] = outputChannelData[i];
  73. if (numOutputs >= numElementsInArray (outputChans))
  74. break;
  75. }
  76. }
  77. if (numInputs > numOutputs)
  78. {
  79. // if there aren't enough output channels for the number of
  80. // inputs, we need to create some temporary extra ones (can't
  81. // use the input data in case it gets written to)
  82. tempBuffer.setSize (numInputs - numOutputs, numSamples,
  83. false, false, true);
  84. for (int i = 0; i < numOutputs; ++i)
  85. {
  86. channels[numActiveChans] = outputChans[i];
  87. memcpy (channels[numActiveChans], inputChans[i], (size_t) numSamples * sizeof (float));
  88. ++numActiveChans;
  89. }
  90. for (int i = numOutputs; i < numInputs; ++i)
  91. {
  92. channels[numActiveChans] = tempBuffer.getWritePointer (i - numOutputs);
  93. memcpy (channels[numActiveChans], inputChans[i], (size_t) numSamples * sizeof (float));
  94. ++numActiveChans;
  95. }
  96. }
  97. else
  98. {
  99. for (int i = 0; i < numInputs; ++i)
  100. {
  101. channels[numActiveChans] = outputChans[i];
  102. memcpy (channels[numActiveChans], inputChans[i], (size_t) numSamples * sizeof (float));
  103. ++numActiveChans;
  104. }
  105. for (int i = numInputs; i < numOutputs; ++i)
  106. {
  107. channels[numActiveChans] = outputChans[i];
  108. zeromem (channels[numActiveChans], (size_t) numSamples * sizeof (float));
  109. ++numActiveChans;
  110. }
  111. }
  112. AudioBuffer<float> buffer (channels, numActiveChans, numSamples);
  113. AudioSourceChannelInfo info (&buffer, 0, numSamples);
  114. source->getNextAudioBlock (info);
  115. for (int i = info.buffer->getNumChannels(); --i >= 0;)
  116. buffer.applyGainRamp (i, info.startSample, info.numSamples, lastGain, gain);
  117. lastGain = gain;
  118. }
  119. else
  120. {
  121. for (int i = 0; i < totalNumOutputChannels; ++i)
  122. if (outputChannelData[i] != nullptr)
  123. zeromem (outputChannelData[i], (size_t) numSamples * sizeof (float));
  124. }
  125. }
  126. void AudioSourcePlayer::audioDeviceAboutToStart (AudioIODevice* device)
  127. {
  128. prepareToPlay (device->getCurrentSampleRate(),
  129. device->getCurrentBufferSizeSamples());
  130. }
  131. void AudioSourcePlayer::prepareToPlay (double newSampleRate, int newBufferSize)
  132. {
  133. sampleRate = newSampleRate;
  134. bufferSize = newBufferSize;
  135. zeromem (channels, sizeof (channels));
  136. if (source != nullptr)
  137. source->prepareToPlay (bufferSize, sampleRate);
  138. }
  139. void AudioSourcePlayer::audioDeviceStopped()
  140. {
  141. if (source != nullptr)
  142. source->releaseResources();
  143. sampleRate = 0.0;
  144. bufferSize = 0;
  145. tempBuffer.setSize (2, 8);
  146. }
  147. } // namespace juce