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.

238 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file was auto-generated!
  4. It contains the basic framework code for a JUCE plugin processor.
  5. ==============================================================================
  6. */
  7. #include "PluginProcessor.h"
  8. #include "PluginEditor.h"
  9. std::unique_ptr<PropertiesFile> g_propsfile;
  10. //==============================================================================
  11. PaulstretchpluginAudioProcessor::PaulstretchpluginAudioProcessor()
  12. #ifndef JucePlugin_PreferredChannelConfigurations
  13. : AudioProcessor (BusesProperties()
  14. #if ! JucePlugin_IsMidiEffect
  15. #if ! JucePlugin_IsSynth
  16. .withInput ("Input", AudioChannelSet::stereo(), true)
  17. #endif
  18. .withOutput ("Output", AudioChannelSet::stereo(), true)
  19. #endif
  20. )
  21. #endif
  22. {
  23. m_afm = std::make_unique<AudioFormatManager>();
  24. m_afm->registerBasicFormats();
  25. m_control = std::make_unique<Control>(m_afm.get());
  26. m_control->ppar.pitch_shift.enabled = true;
  27. m_control->ppar.freq_shift.enabled = true;
  28. m_control->getStretchAudioSource()->setLoopingEnabled(true);
  29. addParameter(new AudioParameterFloat("mainvolume0", "Main volume", -24.0f, 12.0f, -3.0f));
  30. addParameter(new AudioParameterFloat("stretchamount0", "Stretch amount", 0.1f, 128.0f, 1.0f));
  31. addParameter(new AudioParameterFloat("fftsize0", "FFT size", 0.0f, 1.0f, 0.6f));
  32. addParameter(new AudioParameterFloat("pitchshift0", "Pitch shift", -24.0f, 24.0f, 0.0f));
  33. addParameter(new AudioParameterFloat("freqshift0", "Frequency shift", -1000.0f, 1000.0f, 0.0f));
  34. addParameter(new AudioParameterFloat("playrange_start0", "Sound start", 0.0f, 1.0f, 0.0f));
  35. addParameter(new AudioParameterFloat("playrange_end0", "Sound end", 0.0f, 1.0f, 1.0f));
  36. }
  37. PaulstretchpluginAudioProcessor::~PaulstretchpluginAudioProcessor()
  38. {
  39. m_control->stopplay();
  40. }
  41. //==============================================================================
  42. const String PaulstretchpluginAudioProcessor::getName() const
  43. {
  44. return JucePlugin_Name;
  45. }
  46. bool PaulstretchpluginAudioProcessor::acceptsMidi() const
  47. {
  48. #if JucePlugin_WantsMidiInput
  49. return true;
  50. #else
  51. return false;
  52. #endif
  53. }
  54. bool PaulstretchpluginAudioProcessor::producesMidi() const
  55. {
  56. #if JucePlugin_ProducesMidiOutput
  57. return true;
  58. #else
  59. return false;
  60. #endif
  61. }
  62. bool PaulstretchpluginAudioProcessor::isMidiEffect() const
  63. {
  64. #if JucePlugin_IsMidiEffect
  65. return true;
  66. #else
  67. return false;
  68. #endif
  69. }
  70. double PaulstretchpluginAudioProcessor::getTailLengthSeconds() const
  71. {
  72. return 0.0;
  73. }
  74. int PaulstretchpluginAudioProcessor::getNumPrograms()
  75. {
  76. return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
  77. // so this should be at least 1, even if you're not really implementing programs.
  78. }
  79. int PaulstretchpluginAudioProcessor::getCurrentProgram()
  80. {
  81. return 0;
  82. }
  83. void PaulstretchpluginAudioProcessor::setCurrentProgram (int index)
  84. {
  85. }
  86. const String PaulstretchpluginAudioProcessor::getProgramName (int index)
  87. {
  88. return {};
  89. }
  90. void PaulstretchpluginAudioProcessor::changeProgramName (int index, const String& newName)
  91. {
  92. }
  93. //==============================================================================
  94. void PaulstretchpluginAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
  95. {
  96. m_ready_to_play = false;
  97. m_control->set_input_file(File("C:/MusicAudio/sourcesamples/sheila.wav"), [this](String cberr)
  98. {
  99. if (cberr.isEmpty())
  100. {
  101. m_ready_to_play = true;
  102. String err;
  103. m_control->update_player_stretch();
  104. m_control->update_process_parameters();
  105. m_control->startplay(false, true, { 0.0,1.0 }, 2, err);
  106. }
  107. else m_ready_to_play = false;
  108. });
  109. }
  110. void PaulstretchpluginAudioProcessor::releaseResources()
  111. {
  112. m_control->stopplay();
  113. }
  114. #ifndef JucePlugin_PreferredChannelConfigurations
  115. bool PaulstretchpluginAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
  116. {
  117. #if JucePlugin_IsMidiEffect
  118. ignoreUnused (layouts);
  119. return true;
  120. #else
  121. // This is the place where you check if the layout is supported.
  122. // In this template code we only support mono or stereo.
  123. if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono()
  124. && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
  125. return false;
  126. // This checks if the input layout matches the output layout
  127. #if ! JucePlugin_IsSynth
  128. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  129. return false;
  130. #endif
  131. return true;
  132. #endif
  133. }
  134. #endif
  135. void PaulstretchpluginAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
  136. {
  137. ScopedNoDenormals noDenormals;
  138. const int totalNumInputChannels = getTotalNumInputChannels();
  139. const int totalNumOutputChannels = getTotalNumOutputChannels();
  140. for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  141. buffer.clear (i, 0, buffer.getNumSamples());
  142. if (m_ready_to_play == false)
  143. return;
  144. if (m_is_recording == true)
  145. {
  146. m_rec_pos += buffer.getNumSamples();
  147. if (m_rec_pos >= m_max_reclen * getSampleRate())
  148. {
  149. m_is_recording = false;
  150. // Set record buffer as strech source...
  151. }
  152. return;
  153. }
  154. m_control->getStretchAudioSource()->setRate(*getFloatParameter(1));
  155. //m_control->setFFTSize(*getFloatParameter(2));
  156. m_control->ppar.pitch_shift.cents = *getFloatParameter(3) * 100.0;
  157. m_control->ppar.freq_shift.Hz = *getFloatParameter(4);
  158. double t0 = *getFloatParameter(5);
  159. double t1 = *getFloatParameter(6);
  160. if (t0 > t1)
  161. std::swap(t0, t1);
  162. if (t1 - t0 < 0.001)
  163. t1 = t0 + 0.001;
  164. m_control->getStretchAudioSource()->setPlayRange({ t0,t1 }, true);
  165. m_control->update_process_parameters();
  166. m_control->processAudio(buffer);
  167. }
  168. //==============================================================================
  169. bool PaulstretchpluginAudioProcessor::hasEditor() const
  170. {
  171. return true; // (change this to false if you choose to not supply an editor)
  172. }
  173. AudioProcessorEditor* PaulstretchpluginAudioProcessor::createEditor()
  174. {
  175. return new PaulstretchpluginAudioProcessorEditor (*this);
  176. }
  177. //==============================================================================
  178. void PaulstretchpluginAudioProcessor::getStateInformation (MemoryBlock& destData)
  179. {
  180. // You should use this method to store your parameters in the memory block.
  181. // You could do that either as raw data, or use the XML or ValueTree classes
  182. // as intermediaries to make it easy to save and load complex data.
  183. }
  184. void PaulstretchpluginAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
  185. {
  186. // You should use this method to restore your parameters from this memory block,
  187. // whose contents will have been created by the getStateInformation() call.
  188. }
  189. void PaulstretchpluginAudioProcessor::setRecordingEnabled(bool b)
  190. {
  191. if (b == true)
  192. {
  193. m_is_recording = true;
  194. m_recbuffer.setSize(2, m_max_reclen*getSampleRate());
  195. m_rec_pos = 0;
  196. }
  197. else
  198. {
  199. m_is_recording = false;
  200. }
  201. }
  202. //==============================================================================
  203. // This creates new instances of the plugin..
  204. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  205. {
  206. return new PaulstretchpluginAudioProcessor();
  207. }