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.

376 lines
11KB

  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. #include <set>
  10. #undef min
  11. #undef max
  12. std::set<PaulstretchpluginAudioProcessor*> g_activeprocessors;
  13. template<typename F>
  14. void callGUI(AudioProcessor* ap, F&& f, bool async)
  15. {
  16. auto ed = dynamic_cast<PaulstretchpluginAudioProcessorEditor*>(ap->getActiveEditor());
  17. if (ed)
  18. {
  19. if (async == false)
  20. f(ed);
  21. else
  22. MessageManager::callAsync([ed,f]() { f(ed); });
  23. }
  24. }
  25. //==============================================================================
  26. PaulstretchpluginAudioProcessor::PaulstretchpluginAudioProcessor()
  27. #ifndef JucePlugin_PreferredChannelConfigurations
  28. : AudioProcessor (BusesProperties()
  29. #if ! JucePlugin_IsMidiEffect
  30. #if ! JucePlugin_IsSynth
  31. .withInput ("Input", AudioChannelSet::stereo(), true)
  32. #endif
  33. .withOutput ("Output", AudioChannelSet::stereo(), true)
  34. #endif
  35. )
  36. #endif
  37. {
  38. g_activeprocessors.insert(this);
  39. m_recbuffer.setSize(2, 44100);
  40. m_recbuffer.clear();
  41. m_afm = std::make_unique<AudioFormatManager>();
  42. m_afm->registerBasicFormats();
  43. m_control = std::make_unique<Control>(m_afm.get());
  44. m_control->setPreBufferAmount(2);
  45. m_control->ppar.pitch_shift.enabled = true;
  46. m_control->ppar.freq_shift.enabled = true;
  47. m_control->setOnsetDetection(0.0);
  48. m_control->getStretchAudioSource()->setLoopingEnabled(true);
  49. addParameter(new AudioParameterFloat("mainvolume0", "Main volume", -24.0f, 12.0f, -3.0f)); // 0
  50. addParameter(new AudioParameterFloat("stretchamount0", "Stretch amount", 0.1f, 128.0f, 1.0f)); // 1
  51. addParameter(new AudioParameterFloat("fftsize0", "FFT size", 0.0f, 1.0f, 0.7f)); // 2
  52. addParameter(new AudioParameterFloat("pitchshift0", "Pitch shift", -24.0f, 24.0f, 0.0f)); // 3
  53. addParameter(new AudioParameterFloat("freqshift0", "Frequency shift", -1000.0f, 1000.0f, 0.0f)); // 4
  54. addParameter(new AudioParameterFloat("playrange_start0", "Sound start", 0.0f, 1.0f, 0.0f)); // 5
  55. addParameter(new AudioParameterFloat("playrange_end0", "Sound end", 0.0f, 1.0f, 1.0f)); // 6
  56. addParameter(new AudioParameterBool("freeze0", "Freeze", false)); // 7
  57. }
  58. PaulstretchpluginAudioProcessor::~PaulstretchpluginAudioProcessor()
  59. {
  60. g_activeprocessors.erase(this);
  61. m_control->stopplay();
  62. }
  63. //==============================================================================
  64. const String PaulstretchpluginAudioProcessor::getName() const
  65. {
  66. return JucePlugin_Name;
  67. }
  68. bool PaulstretchpluginAudioProcessor::acceptsMidi() const
  69. {
  70. #if JucePlugin_WantsMidiInput
  71. return true;
  72. #else
  73. return false;
  74. #endif
  75. }
  76. bool PaulstretchpluginAudioProcessor::producesMidi() const
  77. {
  78. #if JucePlugin_ProducesMidiOutput
  79. return true;
  80. #else
  81. return false;
  82. #endif
  83. }
  84. bool PaulstretchpluginAudioProcessor::isMidiEffect() const
  85. {
  86. #if JucePlugin_IsMidiEffect
  87. return true;
  88. #else
  89. return false;
  90. #endif
  91. }
  92. double PaulstretchpluginAudioProcessor::getTailLengthSeconds() const
  93. {
  94. return 0.0;
  95. }
  96. int PaulstretchpluginAudioProcessor::getNumPrograms()
  97. {
  98. return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
  99. // so this should be at least 1, even if you're not really implementing programs.
  100. }
  101. int PaulstretchpluginAudioProcessor::getCurrentProgram()
  102. {
  103. return 0;
  104. }
  105. void PaulstretchpluginAudioProcessor::setCurrentProgram (int index)
  106. {
  107. }
  108. const String PaulstretchpluginAudioProcessor::getProgramName (int index)
  109. {
  110. return {};
  111. }
  112. void PaulstretchpluginAudioProcessor::changeProgramName (int index, const String& newName)
  113. {
  114. }
  115. //==============================================================================
  116. void PaulstretchpluginAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
  117. {
  118. if (getNumOutputChannels() != m_cur_num_out_chans)
  119. m_ready_to_play = false;
  120. if (m_using_memory_buffer == true)
  121. {
  122. int len = jlimit(100,m_recbuffer.getNumSamples(), m_rec_pos);
  123. m_control->getStretchAudioSource()->setAudioBufferAsInputSource(&m_recbuffer,
  124. getSampleRate(),
  125. len);
  126. callGUI(this,[this,len](auto ed) { ed->setAudioBuffer(&m_recbuffer, getSampleRate(), len); },false);
  127. }
  128. if (m_ready_to_play == false)
  129. {
  130. m_control->setFFTSize(0.5);
  131. m_control->update_player_stretch();
  132. m_control->update_process_parameters();
  133. String err;
  134. m_control->startplay(false, true,
  135. { *getFloatParameter(5),*getFloatParameter(6) },
  136. 2, err);
  137. m_cur_num_out_chans = getNumOutputChannels();
  138. m_ready_to_play = true;
  139. }
  140. }
  141. void PaulstretchpluginAudioProcessor::releaseResources()
  142. {
  143. //m_control->stopplay();
  144. //m_ready_to_play = false;
  145. }
  146. #ifndef JucePlugin_PreferredChannelConfigurations
  147. bool PaulstretchpluginAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
  148. {
  149. #if JucePlugin_IsMidiEffect
  150. ignoreUnused (layouts);
  151. return true;
  152. #else
  153. // This is the place where you check if the layout is supported.
  154. // In this template code we only support mono or stereo.
  155. if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono()
  156. && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
  157. return false;
  158. // This checks if the input layout matches the output layout
  159. #if ! JucePlugin_IsSynth
  160. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  161. return false;
  162. #endif
  163. return true;
  164. #endif
  165. }
  166. #endif
  167. void copyAudioBufferWrappingPosition(const AudioBuffer<float>& src, AudioBuffer<float>& dest, int destbufpos, int maxdestpos)
  168. {
  169. for (int i = 0; i < dest.getNumChannels(); ++i)
  170. {
  171. int channel_to_copy = i % src.getNumChannels();
  172. if (destbufpos + src.getNumSamples() > maxdestpos)
  173. {
  174. int wrappos = (destbufpos + src.getNumSamples()) % maxdestpos;
  175. int partial_len = src.getNumSamples() - wrappos;
  176. dest.copyFrom(channel_to_copy, destbufpos, src, channel_to_copy, 0, partial_len);
  177. dest.copyFrom(channel_to_copy, partial_len, src, channel_to_copy, 0, wrappos);
  178. }
  179. else
  180. {
  181. dest.copyFrom(channel_to_copy, destbufpos, src, channel_to_copy, 0, src.getNumSamples());
  182. }
  183. }
  184. }
  185. void PaulstretchpluginAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
  186. {
  187. std::lock_guard<std::mutex> locker(m_mutex);
  188. ScopedNoDenormals noDenormals;
  189. const int totalNumInputChannels = getTotalNumInputChannels();
  190. const int totalNumOutputChannels = getTotalNumOutputChannels();
  191. for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  192. buffer.clear (i, 0, buffer.getNumSamples());
  193. if (m_ready_to_play == false)
  194. return;
  195. if (m_is_recording == true)
  196. {
  197. int recbuflenframes = m_max_reclen * getSampleRate();
  198. copyAudioBufferWrappingPosition(buffer, m_recbuffer, m_rec_pos, recbuflenframes);
  199. callGUI(this,[this, &buffer](PaulstretchpluginAudioProcessorEditor*ed)
  200. {
  201. ed->addAudioBlock(buffer, getSampleRate(), m_rec_pos);
  202. }, false);
  203. m_rec_pos = (m_rec_pos + buffer.getNumSamples()) % recbuflenframes;
  204. return;
  205. }
  206. m_control->getStretchAudioSource()->val_MainVolume = (float)*getFloatParameter(0);
  207. m_control->getStretchAudioSource()->setRate(*getFloatParameter(1));
  208. m_control->getStretchAudioSource()->val_XFadeLen = 0.1;
  209. m_control->setFFTSize(*getFloatParameter(2));
  210. m_control->ppar.pitch_shift.cents = *getFloatParameter(3) * 100.0;
  211. m_control->ppar.freq_shift.Hz = *getFloatParameter(4);
  212. double t0 = *getFloatParameter(5);
  213. double t1 = *getFloatParameter(6);
  214. if (t0 > t1)
  215. std::swap(t0, t1);
  216. if (t1 - t0 < 0.001)
  217. t1 = t0 + 0.001;
  218. m_control->getStretchAudioSource()->setPlayRange({ t0,t1 }, true);
  219. m_control->getStretchAudioSource()->setFreezing(getParameter(7));
  220. m_control->update_process_parameters();
  221. m_control->processAudio(buffer);
  222. }
  223. //==============================================================================
  224. bool PaulstretchpluginAudioProcessor::hasEditor() const
  225. {
  226. return true; // (change this to false if you choose to not supply an editor)
  227. }
  228. AudioProcessorEditor* PaulstretchpluginAudioProcessor::createEditor()
  229. {
  230. return new PaulstretchpluginAudioProcessorEditor (*this);
  231. }
  232. //==============================================================================
  233. void PaulstretchpluginAudioProcessor::getStateInformation (MemoryBlock& destData)
  234. {
  235. ValueTree paramtree("paulstretch3pluginstate");
  236. for (int i=0;i<getNumParameters();++i)
  237. {
  238. auto par = getFloatParameter(i);
  239. if (par != nullptr)
  240. {
  241. paramtree.setProperty(par->paramID, (double)*par, nullptr);
  242. }
  243. }
  244. if (m_current_file != File())
  245. {
  246. paramtree.setProperty("importedfile", m_current_file.getFullPathName(), nullptr);
  247. }
  248. MemoryOutputStream stream(destData,true);
  249. paramtree.writeToStream(stream);
  250. }
  251. void PaulstretchpluginAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
  252. {
  253. ValueTree tree = ValueTree::readFromData(data, sizeInBytes);
  254. if (tree.isValid())
  255. {
  256. for (int i = 0; i<getNumParameters(); ++i)
  257. {
  258. auto par = getFloatParameter(i);
  259. if (par != nullptr)
  260. {
  261. double parval = tree.getProperty(par->paramID, (double)*par);
  262. *par = parval;
  263. }
  264. }
  265. String fn = tree.getProperty("importedfile");
  266. if (fn.isEmpty() == false)
  267. {
  268. m_using_memory_buffer = false;
  269. File f(fn);
  270. setAudioFile(f);
  271. }
  272. }
  273. }
  274. void PaulstretchpluginAudioProcessor::setRecordingEnabled(bool b)
  275. {
  276. std::lock_guard<std::mutex> locker(m_mutex);
  277. int lenbufframes = getSampleRate()*m_max_reclen;
  278. if (b == true)
  279. {
  280. m_using_memory_buffer = true;
  281. m_current_file = File();
  282. m_recbuffer.setSize(2, m_max_reclen*getSampleRate()+4096);
  283. m_recbuffer.clear();
  284. m_rec_pos = 0;
  285. callGUI(this,[this,lenbufframes](PaulstretchpluginAudioProcessorEditor* ed)
  286. {
  287. ed->beginAddingAudioBlocks(2, getSampleRate(), lenbufframes);
  288. },false);
  289. m_is_recording = true;
  290. }
  291. else
  292. {
  293. if (m_is_recording == true)
  294. {
  295. finishRecording(lenbufframes);
  296. }
  297. }
  298. }
  299. double PaulstretchpluginAudioProcessor::getRecordingPositionPercent()
  300. {
  301. if (m_is_recording==false)
  302. return 0.0;
  303. return 1.0 / m_recbuffer.getNumSamples()*m_rec_pos;
  304. }
  305. String PaulstretchpluginAudioProcessor::setAudioFile(File f)
  306. {
  307. std::lock_guard<std::mutex> locker(m_mutex);
  308. m_control->set_input_file(f, [this,f](String)
  309. {
  310. });
  311. m_current_file = f;
  312. m_using_memory_buffer = false;
  313. return String();
  314. }
  315. Range<double> PaulstretchpluginAudioProcessor::getTimeSelection()
  316. {
  317. return { *getFloatParameter(5),*getFloatParameter(6) };
  318. }
  319. void PaulstretchpluginAudioProcessor::finishRecording(int lenrecording)
  320. {
  321. m_is_recording = false;
  322. m_control->getStretchAudioSource()->setAudioBufferAsInputSource(&m_recbuffer, getSampleRate(), lenrecording);
  323. m_control->getStretchAudioSource()->setPlayRange({ *getFloatParameter(5),*getFloatParameter(6) }, true);
  324. auto ed = dynamic_cast<PaulstretchpluginAudioProcessorEditor*>(getActiveEditor());
  325. if (ed)
  326. {
  327. //ed->setAudioBuffer(&m_recbuffer, getSampleRate(), lenrecording);
  328. }
  329. }
  330. //==============================================================================
  331. // This creates new instances of the plugin..
  332. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  333. {
  334. return new PaulstretchpluginAudioProcessor();
  335. }