The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

196 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "IAAEffectProcessor.h"
  20. #include "IAAEffectEditor.h"
  21. IAAEffectProcessor::IAAEffectProcessor()
  22. : AudioProcessor (BusesProperties()
  23. .withInput ("Input", AudioChannelSet::stereo(), true)
  24. .withOutput ("Output", AudioChannelSet::stereo(), true)),
  25. parameters (*this, nullptr)
  26. {
  27. parameters.createAndAddParameter ("gain",
  28. "Gain",
  29. String(),
  30. NormalisableRange<float> (0.0f, 1.0f),
  31. (float) (1.0 / 3.14),
  32. nullptr,
  33. nullptr);
  34. parameters.state = ValueTree (Identifier ("InterAppAudioEffect"));
  35. }
  36. IAAEffectProcessor::~IAAEffectProcessor()
  37. {
  38. }
  39. //==============================================================================
  40. const String IAAEffectProcessor::getName() const
  41. {
  42. return JucePlugin_Name;
  43. }
  44. bool IAAEffectProcessor::acceptsMidi() const
  45. {
  46. return false;
  47. }
  48. bool IAAEffectProcessor::producesMidi() const
  49. {
  50. return false;
  51. }
  52. double IAAEffectProcessor::getTailLengthSeconds() const
  53. {
  54. return 0.0;
  55. }
  56. int IAAEffectProcessor::getNumPrograms()
  57. {
  58. return 1;
  59. }
  60. int IAAEffectProcessor::getCurrentProgram()
  61. {
  62. return 0;
  63. }
  64. void IAAEffectProcessor::setCurrentProgram (int)
  65. {
  66. }
  67. const String IAAEffectProcessor::getProgramName (int)
  68. {
  69. return String();
  70. }
  71. void IAAEffectProcessor::changeProgramName (int, const String&)
  72. {
  73. }
  74. //==============================================================================
  75. void IAAEffectProcessor::prepareToPlay (double, int)
  76. {
  77. previousGain = *parameters.getRawParameterValue ("gain");
  78. }
  79. void IAAEffectProcessor::releaseResources()
  80. {
  81. }
  82. bool IAAEffectProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
  83. {
  84. if (layouts.getMainInputChannelSet() != AudioChannelSet::stereo())
  85. return false;
  86. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  87. return false;
  88. return true;
  89. }
  90. void IAAEffectProcessor::processBlock (AudioBuffer<float>& buffer, MidiBuffer&)
  91. {
  92. const float gain = *parameters.getRawParameterValue ("gain");
  93. auto totalNumInputChannels = getTotalNumInputChannels();
  94. auto totalNumOutputChannels = getTotalNumOutputChannels();
  95. auto numSamples = buffer.getNumSamples();
  96. for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  97. buffer.clear (i, 0, buffer.getNumSamples());
  98. // Apply the gain to the samples using a ramp to avoid discontinuities in
  99. // the audio between processed buffers.
  100. for (int channel = 0; channel < totalNumInputChannels; ++channel)
  101. {
  102. buffer.applyGainRamp (channel, 0, numSamples, previousGain, gain);
  103. auto newLevel = buffer.getMagnitude (channel, 0, numSamples);
  104. meterListeners.call ([=] (MeterListener& l) { l.handleNewMeterValue (channel, newLevel); });
  105. }
  106. previousGain = gain;
  107. // Now ask the host for the current time so we can store it to be displayed later.
  108. updateCurrentTimeInfoFromHost (lastPosInfo);
  109. }
  110. //==============================================================================
  111. bool IAAEffectProcessor::hasEditor() const
  112. {
  113. return true;
  114. }
  115. AudioProcessorEditor* IAAEffectProcessor::createEditor()
  116. {
  117. return new IAAEffectEditor (*this, parameters);
  118. }
  119. //==============================================================================
  120. void IAAEffectProcessor::getStateInformation (MemoryBlock& destData)
  121. {
  122. auto xml = std::unique_ptr<XmlElement> (parameters.state.createXml());
  123. copyXmlToBinary (*xml, destData);
  124. }
  125. void IAAEffectProcessor::setStateInformation (const void* data, int sizeInBytes)
  126. {
  127. auto xmlState = std::unique_ptr<XmlElement> (getXmlFromBinary (data, sizeInBytes));
  128. if (xmlState.get() != nullptr)
  129. if (xmlState->hasTagName (parameters.state.getType()))
  130. parameters.state = ValueTree::fromXml (*xmlState);
  131. }
  132. bool IAAEffectProcessor::updateCurrentTimeInfoFromHost (AudioPlayHead::CurrentPositionInfo &posInfo)
  133. {
  134. if (AudioPlayHead* ph = getPlayHead())
  135. {
  136. AudioPlayHead::CurrentPositionInfo newTime;
  137. if (ph->getCurrentPosition (newTime))
  138. {
  139. posInfo = newTime; // Successfully got the current time from the host.
  140. return true;
  141. }
  142. }
  143. // If the host fails to provide the current time, we'll just reset our copy to a default.
  144. lastPosInfo.resetToDefault();
  145. return false;
  146. }
  147. //==============================================================================
  148. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  149. {
  150. return new IAAEffectProcessor();
  151. }