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.

309 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. It contains the basic startup code for a Juce application.
  5. ==============================================================================
  6. */
  7. #include "PluginProcessor.h"
  8. #include "PluginEditor.h"
  9. #include <functional>
  10. //==============================================================================
  11. AdmvAudioProcessor::AdmvAudioProcessor() : mSpectroSegments(NULL), mGonioSegments(NULL), mLastGonioScale(1.), mMaxStereoPairCount(0), mCurrentInputCount(0)
  12. {
  13. releaseResources();
  14. }
  15. AdmvAudioProcessor::~AdmvAudioProcessor()
  16. {
  17. }
  18. //==============================================================================
  19. const String AdmvAudioProcessor::getName() const
  20. {
  21. return JucePlugin_Name;
  22. }
  23. int AdmvAudioProcessor::getNumParameters()
  24. {
  25. return 0;
  26. }
  27. float AdmvAudioProcessor::getParameter (int index)
  28. {
  29. return 0.0f;
  30. }
  31. void AdmvAudioProcessor::setParameter (int index, float newValue)
  32. {
  33. }
  34. const String AdmvAudioProcessor::getParameterName (int index)
  35. {
  36. return String();
  37. }
  38. const String AdmvAudioProcessor::getParameterText (int index)
  39. {
  40. return String();
  41. }
  42. const String AdmvAudioProcessor::getInputChannelName (int channelIndex) const
  43. {
  44. return String (channelIndex + 1);
  45. }
  46. const String AdmvAudioProcessor::getOutputChannelName (int channelIndex) const
  47. {
  48. return String (channelIndex + 1);
  49. }
  50. bool AdmvAudioProcessor::isInputChannelStereoPair (int index) const
  51. {
  52. return true;
  53. }
  54. bool AdmvAudioProcessor::isOutputChannelStereoPair (int index) const
  55. {
  56. return true;
  57. }
  58. bool AdmvAudioProcessor::acceptsMidi() const
  59. {
  60. #if JucePlugin_WantsMidiInput
  61. return true;
  62. #else
  63. return false;
  64. #endif
  65. }
  66. bool AdmvAudioProcessor::producesMidi() const
  67. {
  68. #if JucePlugin_ProducesMidiOutput
  69. return true;
  70. #else
  71. return false;
  72. #endif
  73. }
  74. bool AdmvAudioProcessor::silenceInProducesSilenceOut() const
  75. {
  76. return false;
  77. }
  78. double AdmvAudioProcessor::getTailLengthSeconds() const
  79. {
  80. return 0.0;
  81. }
  82. int AdmvAudioProcessor::getNumPrograms()
  83. {
  84. return 0;
  85. }
  86. int AdmvAudioProcessor::getCurrentProgram()
  87. {
  88. return 0;
  89. }
  90. void AdmvAudioProcessor::setCurrentProgram (int index)
  91. {
  92. }
  93. const String AdmvAudioProcessor::getProgramName (int index)
  94. {
  95. return String();
  96. }
  97. void AdmvAudioProcessor::changeProgramName (int index, const String& newName)
  98. {
  99. }
  100. //==============================================================================
  101. void AdmvAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
  102. {
  103. size_t fftSize = 2048;
  104. mMaxStereoPairCount = JucePlugin_MaxNumInputChannels / 2;
  105. for (size_t i = 0; i < mMaxStereoPairCount; ++i)
  106. {
  107. mGonioCalcs.push_back(new tomatl::dsp::GonioCalculator<double>(1600, sampleRate));
  108. }
  109. for (size_t i = 0; i < mMaxStereoPairCount; ++i)
  110. {
  111. mSpectroCalcs.push_back(new tomatl::dsp::SpectroCalculator<double>(sampleRate, std::pair<double, double>(10, getState().mSpectrometerReleaseSpeed), i, fftSize));
  112. }
  113. mSpectroSegments = new tomatl::dsp::SpectrumBlock[mMaxStereoPairCount];
  114. mGonioSegments = new GonioPoints<double>[mMaxStereoPairCount];
  115. makeCurrentStateEffective();
  116. }
  117. void AdmvAudioProcessor::releaseResources()
  118. {
  119. for (size_t i = 0; i < mMaxStereoPairCount; ++i)
  120. {
  121. TOMATL_DELETE(mGonioCalcs[i]);
  122. }
  123. for (size_t i = 0; i < mMaxStereoPairCount; ++i)
  124. {
  125. TOMATL_DELETE(mSpectroCalcs[i]);
  126. }
  127. mGonioCalcs.clear();
  128. mSpectroCalcs.clear();
  129. TOMATL_BRACE_DELETE(mSpectroSegments);
  130. TOMATL_BRACE_DELETE(mGonioSegments);
  131. }
  132. void AdmvAudioProcessor::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
  133. {
  134. double cp[2];
  135. int channelCount = 0;
  136. size_t sampleRate = getSampleRate();
  137. for (int channel = 0; channel < (getTotalNumInputChannels() - 1); channel += 2)
  138. {
  139. // No need to process signal if editor is closed
  140. if (getActiveEditor() == NULL)
  141. {
  142. break;
  143. }
  144. // TODO: investigate how to get number of input channels really connected to the plugin ATM.
  145. // It seems that getTotalNumInputChannels() will always return max possible defined by JucePlugin_MaxNumInputChannels
  146. // This solution is bad, because it iterates through all input buffers.
  147. if (!isBlockInformative(buffer, channel / 2))
  148. {
  149. mGonioSegments[channel / 2] = GonioPoints<double>();
  150. mSpectroSegments[channel / 2] = tomatl::dsp::SpectrumBlock();
  151. continue;
  152. }
  153. channelCount += 2;
  154. float* l = buffer.getWritePointer(channel + 0);
  155. float* r = buffer.getWritePointer(channel + 1);
  156. for (int i = 0; i < buffer.getNumSamples(); ++i)
  157. {
  158. std::pair<double, double>* res = mGonioCalcs[channel / 2]->handlePoint(l[i], r[i], sampleRate);
  159. cp[0] = l[i];
  160. cp[1] = r[i];
  161. mSpectroCalcs[channel / 2]->checkSampleRate(getSampleRate());
  162. tomatl::dsp::SpectrumBlock spectroResult = mSpectroCalcs[channel / 2]->process((double*)&cp);
  163. if (res != NULL)
  164. {
  165. mGonioSegments[channel / 2] = GonioPoints<double>(res, mGonioCalcs[channel / 2]->getSegmentLength(), channel / 2, sampleRate);
  166. mLastGonioScale = mGonioCalcs[channel / 2]->getCurrentScaleValue();
  167. }
  168. if (spectroResult.mLength > 0)
  169. {
  170. mSpectroSegments[channel / 2] = spectroResult;
  171. }
  172. }
  173. }
  174. mCurrentInputCount = channelCount;
  175. if (getState().mOutputMode == AdmvPluginState::outputMute)
  176. {
  177. buffer.clear();
  178. }
  179. else
  180. {
  181. // In case we have more outputs than inputs, we'll clear any output
  182. // channels that didn't contain input data, (because these aren't
  183. // guaranteed to be empty - they may contain garbage).
  184. for (int i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
  185. {
  186. buffer.clear(i, 0, buffer.getNumSamples());
  187. }
  188. }
  189. }
  190. //==============================================================================
  191. bool AdmvAudioProcessor::hasEditor() const
  192. {
  193. return true; // (change this to false if you choose to not supply an editor)
  194. }
  195. void AdmvAudioProcessor::makeCurrentStateEffective()
  196. {
  197. for (size_t i = 0; i < mGonioCalcs.size(); ++i)
  198. {
  199. mGonioCalcs[i]->setCustomScaleEnabled(mState.mManualGoniometerScale);
  200. mGonioCalcs[i]->setCustomScale(mState.mManualGoniometerScaleValue);
  201. mGonioCalcs[i]->setReleaseSpeed(mState.mGoniometerScaleAttackRelease.second);
  202. }
  203. for (size_t i = 0; i < mSpectroCalcs.size(); ++i)
  204. {
  205. mSpectroCalcs[i]->setReleaseSpeed(mState.mSpectrometerReleaseSpeed);
  206. }
  207. if (getActiveEditor() != NULL)
  208. {
  209. ((AdmvAudioProcessorEditor*)getActiveEditor())->updateFromState(mState);
  210. }
  211. }
  212. void AdmvAudioProcessor::numChannelsChanged()
  213. {
  214. }
  215. AudioProcessorEditor* AdmvAudioProcessor::createEditor()
  216. {
  217. auto editor = new AdmvAudioProcessorEditor (this);
  218. editor->updateFromState(mState);
  219. return editor;
  220. }
  221. //==============================================================================
  222. void AdmvAudioProcessor::getStateInformation (MemoryBlock& destData)
  223. {
  224. uint8 version = getStateVersion();
  225. destData.ensureSize(sizeof(mState) + 1, false);
  226. destData.copyFrom(&version, 0, 1);
  227. destData.copyFrom(&mState, 1, sizeof(mState));
  228. }
  229. void AdmvAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
  230. {
  231. uint8 version;
  232. memcpy(&version, data, 1);
  233. if (version == getStateVersion())
  234. {
  235. memcpy(&mState, (uint8*)data + 1, sizeInBytes - 1);
  236. }
  237. }
  238. //==============================================================================
  239. // This creates new instances of the plugin..
  240. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  241. {
  242. return new AdmvAudioProcessor();
  243. }