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.

712 lines
24KB

  1. /*
  2. Copyright (C) 2006-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. Copyright (C) 2017 Xenakios
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "PluginProcessor.h"
  17. #include "PluginEditor.h"
  18. #include <set>
  19. #ifdef WIN32
  20. #undef min
  21. #undef max
  22. #endif
  23. String g_plugintitle{ "PaulXStretch 1.0.0 preview 4" };
  24. std::set<PaulstretchpluginAudioProcessor*> g_activeprocessors;
  25. template<typename F>
  26. void callGUI(AudioProcessor* ap, F&& f, bool async)
  27. {
  28. auto ed = dynamic_cast<PaulstretchpluginAudioProcessorEditor*>(ap->getActiveEditor());
  29. if (ed)
  30. {
  31. if (async == false)
  32. f(ed);
  33. else
  34. MessageManager::callAsync([ed,f]() { f(ed); });
  35. }
  36. }
  37. int get_optimized_updown(int n, bool up) {
  38. int orig_n = n;
  39. while (true) {
  40. n = orig_n;
  41. while (!(n % 11)) n /= 11;
  42. while (!(n % 7)) n /= 7;
  43. while (!(n % 5)) n /= 5;
  44. while (!(n % 3)) n /= 3;
  45. while (!(n % 2)) n /= 2;
  46. if (n<2) break;
  47. if (up) orig_n++;
  48. else orig_n--;
  49. if (orig_n<4) return 4;
  50. };
  51. return orig_n;
  52. };
  53. int optimizebufsize(int n) {
  54. int n1 = get_optimized_updown(n, false);
  55. int n2 = get_optimized_updown(n, true);
  56. if ((n - n1)<(n2 - n)) return n1;
  57. else return n2;
  58. };
  59. //==============================================================================
  60. PaulstretchpluginAudioProcessor::PaulstretchpluginAudioProcessor()
  61. : m_bufferingthread("pspluginprebufferthread")
  62. #ifndef JucePlugin_PreferredChannelConfigurations
  63. : AudioProcessor (BusesProperties()
  64. #if ! JucePlugin_IsMidiEffect
  65. #if ! JucePlugin_IsSynth
  66. .withInput ("Input", AudioChannelSet::stereo(), true)
  67. #endif
  68. .withOutput ("Output", AudioChannelSet::stereo(), true)
  69. #endif
  70. )
  71. #endif
  72. {
  73. g_activeprocessors.insert(this);
  74. m_recbuffer.setSize(2, 44100);
  75. m_recbuffer.clear();
  76. if (m_afm->getNumKnownFormats()==0)
  77. m_afm->registerBasicFormats();
  78. m_stretch_source = std::make_unique<StretchAudioSource>(2, m_afm);
  79. m_ppar.pitch_shift.enabled = true;
  80. m_ppar.freq_shift.enabled = true;
  81. m_ppar.filter.enabled = true;
  82. m_ppar.compressor.enabled = true;
  83. m_stretch_source->setOnsetDetection(0.0);
  84. m_stretch_source->setLoopingEnabled(true);
  85. m_stretch_source->setFFTWindowingType(1);
  86. addParameter(new AudioParameterFloat("mainvolume0", "Main volume", -24.0f, 12.0f, -3.0f)); // 0
  87. addParameter(new AudioParameterFloat("stretchamount0", "Stretch amount",
  88. NormalisableRange<float>(0.1f, 1024.0f, 0.01f, 0.25),1.0f)); // 1
  89. addParameter(new AudioParameterFloat("fftsize0", "FFT size", 0.0f, 1.0f, 0.7f)); // 2
  90. addParameter(new AudioParameterFloat("pitchshift0", "Pitch shift", -24.0f, 24.0f, 0.0f)); // 3
  91. addParameter(new AudioParameterFloat("freqshift0", "Frequency shift", -1000.0f, 1000.0f, 0.0f)); // 4
  92. addParameter(new AudioParameterFloat("playrange_start0", "Sound start", 0.0f, 1.0f, 0.0f)); // 5
  93. addParameter(new AudioParameterFloat("playrange_end0", "Sound end", 0.0f, 1.0f, 1.0f)); // 6
  94. addParameter(new AudioParameterBool("freeze0", "Freeze", false)); // 7
  95. addParameter(new AudioParameterFloat("spread0", "Frequency spread", 0.0f, 1.0f, 0.0f)); // 8
  96. addParameter(new AudioParameterFloat("compress0", "Compress", 0.0f, 1.0f, 0.0f)); // 9
  97. addParameter(new AudioParameterFloat("loopxfadelen0", "Loop xfade length", 0.0f, 1.0f, 0.0f)); // 10
  98. auto numhar_convertFrom0To1Func = [](float rangemin, float rangemax, float value)
  99. {
  100. return jmap<float>(value, 0.0f, 1.0f, 101.0f, 1.0f);
  101. };
  102. auto numhar_convertTo0To1Func = [](float rangemin, float rangemax, float value)
  103. {
  104. return jmap<float>(value, 101.0f, 1.0f, 0.0f, 1.0f);
  105. };
  106. addParameter(new AudioParameterFloat("numharmonics0", "Num harmonics",
  107. NormalisableRange<float>(1.0f, 101.0f,
  108. numhar_convertFrom0To1Func, numhar_convertTo0To1Func), 101.0f)); // 11
  109. addParameter(new AudioParameterFloat("harmonicsfreq0", "Harmonics base freq",
  110. NormalisableRange<float>(1.0f, 5000.0f, 1.00f, 0.5), 128.0f)); // 12
  111. addParameter(new AudioParameterFloat("harmonicsbw0", "Harmonics bandwidth", 0.1f, 200.0f, 25.0f)); // 13
  112. addParameter(new AudioParameterBool("harmonicsgauss0", "Gaussian harmonics", false)); // 14
  113. addParameter(new AudioParameterFloat("octavemixm2_0", "2 octaves down level", 0.0f, 1.0f, 0.0f)); // 15
  114. addParameter(new AudioParameterFloat("octavemixm1_0", "Octave down level", 0.0f, 1.0f, 0.0f)); // 16
  115. addParameter(new AudioParameterFloat("octavemix0_0", "Normal pitch level", 0.0f, 1.0f, 1.0f)); // 17
  116. addParameter(new AudioParameterFloat("octavemix1_0", "1 octave up level", 0.0f, 1.0f, 0.0f)); // 18
  117. addParameter(new AudioParameterFloat("octavemix15_0", "1 octave and fifth up level", 0.0f, 1.0f, 0.0f)); // 19
  118. addParameter(new AudioParameterFloat("octavemix2_0", "2 octaves up level", 0.0f, 1.0f, 0.0f)); // 20
  119. addParameter(new AudioParameterFloat("tonalvsnoisebw_0", "Tonal vs Noise BW", 0.74f, 1.0f, 0.74f)); // 21
  120. addParameter(new AudioParameterFloat("tonalvsnoisepreserve_0", "Tonal vs Noise preserve", -1.0f, 1.0f, 0.5f)); // 22
  121. auto filt_convertFrom0To1Func = [](float rangemin, float rangemax, float value)
  122. {
  123. if (value < 0.5f)
  124. return jmap<float>(value, 0.0f, 0.5f, 20.0f, 1000.0f);
  125. return jmap<float>(value, 0.5f, 1.0f, 1000.0f, 20000.0f);
  126. };
  127. auto filt_convertTo0To1Func = [](float rangemin, float rangemax, float value)
  128. {
  129. if (value < 1000.0f)
  130. return jmap<float>(value, 20.0f, 1000.0f, 0.0f, 0.5f);
  131. return jmap<float>(value, 1000.0f, 20000.0f, 0.5f, 1.0f);
  132. };
  133. addParameter(new AudioParameterFloat("filter_low_0", "Filter low",
  134. NormalisableRange<float>(20.0f, 20000.0f,
  135. filt_convertFrom0To1Func, filt_convertTo0To1Func), 20.0f)); // 23
  136. addParameter(new AudioParameterFloat("filter_high_0", "Filter high",
  137. NormalisableRange<float>(20.0f, 20000.0f,
  138. filt_convertFrom0To1Func,filt_convertTo0To1Func), 20000.0f));; // 24
  139. addParameter(new AudioParameterFloat("onsetdetect_0", "Onset detection", 0.0f, 1.0f, 0.0f)); // 25
  140. addParameter(new AudioParameterBool("capture_enabled0", "Capture", false)); // 26
  141. m_outchansparam = new AudioParameterInt("numoutchans0", "Num output channels", 2, 8, 2); // 27
  142. addParameter(m_outchansparam); // 27
  143. addParameter(new AudioParameterBool("pause_enabled0", "Pause", false)); // 28
  144. addParameter(new AudioParameterFloat("maxcapturelen_0", "Max capture length", 1.0f, 120.0f, 10.0f)); // 29
  145. addParameter(new AudioParameterBool("passthrough0", "Pass input through", false)); // 30
  146. setPreBufferAmount(2);
  147. startTimer(1, 50);
  148. }
  149. PaulstretchpluginAudioProcessor::~PaulstretchpluginAudioProcessor()
  150. {
  151. g_activeprocessors.erase(this);
  152. m_bufferingthread.stopThread(1000);
  153. }
  154. void PaulstretchpluginAudioProcessor::setPreBufferAmount(int x)
  155. {
  156. int temp = jlimit(0, 5, x);
  157. if (temp != m_prebuffer_amount || m_use_backgroundbuffering == false)
  158. {
  159. m_use_backgroundbuffering = true;
  160. m_prebuffer_amount = temp;
  161. m_recreate_buffering_source = true;
  162. ScopedLock locker(m_cs);
  163. m_ready_to_play = false;
  164. m_cur_num_out_chans = *m_outchansparam;
  165. //Logger::writeToLog("Switching to use " + String(m_cur_num_out_chans) + " out channels");
  166. String err;
  167. startplay({ *getFloatParameter(cpi_soundstart),*getFloatParameter(cpi_soundend) },
  168. m_cur_num_out_chans, m_curmaxblocksize, err);
  169. m_ready_to_play = true;
  170. }
  171. }
  172. int PaulstretchpluginAudioProcessor::getPreBufferAmount()
  173. {
  174. if (m_use_backgroundbuffering == false)
  175. return -1;
  176. return m_prebuffer_amount;
  177. }
  178. //==============================================================================
  179. const String PaulstretchpluginAudioProcessor::getName() const
  180. {
  181. return JucePlugin_Name;
  182. }
  183. bool PaulstretchpluginAudioProcessor::acceptsMidi() const
  184. {
  185. #if JucePlugin_WantsMidiInput
  186. return true;
  187. #else
  188. return false;
  189. #endif
  190. }
  191. bool PaulstretchpluginAudioProcessor::producesMidi() const
  192. {
  193. #if JucePlugin_ProducesMidiOutput
  194. return true;
  195. #else
  196. return false;
  197. #endif
  198. }
  199. bool PaulstretchpluginAudioProcessor::isMidiEffect() const
  200. {
  201. #if JucePlugin_IsMidiEffect
  202. return true;
  203. #else
  204. return false;
  205. #endif
  206. }
  207. double PaulstretchpluginAudioProcessor::getTailLengthSeconds() const
  208. {
  209. return 0.0;
  210. //return (double)m_bufamounts[m_prebuffer_amount]/getSampleRate();
  211. }
  212. int PaulstretchpluginAudioProcessor::getNumPrograms()
  213. {
  214. return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
  215. // so this should be at least 1, even if you're not really implementing programs.
  216. }
  217. int PaulstretchpluginAudioProcessor::getCurrentProgram()
  218. {
  219. return 0;
  220. }
  221. void PaulstretchpluginAudioProcessor::setCurrentProgram (int index)
  222. {
  223. }
  224. const String PaulstretchpluginAudioProcessor::getProgramName (int index)
  225. {
  226. return {};
  227. }
  228. void PaulstretchpluginAudioProcessor::changeProgramName (int index, const String& newName)
  229. {
  230. }
  231. void PaulstretchpluginAudioProcessor::setFFTSize(double size)
  232. {
  233. if (m_prebuffer_amount == 5)
  234. m_fft_size_to_use = pow(2, 7.0 + size * 14.5);
  235. else m_fft_size_to_use = pow(2, 7.0 + size * 10.0); // chicken out from allowing huge FFT sizes if not enough prebuffering
  236. int optim = optimizebufsize(m_fft_size_to_use);
  237. m_fft_size_to_use = optim;
  238. m_stretch_source->setFFTSize(optim);
  239. //Logger::writeToLog(String(m_fft_size_to_use));
  240. }
  241. void PaulstretchpluginAudioProcessor::startplay(Range<double> playrange, int numoutchans, int maxBlockSize, String& err)
  242. {
  243. m_stretch_source->setPlayRange(playrange, true);
  244. int bufamt = m_bufamounts[m_prebuffer_amount];
  245. if (m_buffering_source != nullptr && numoutchans != m_buffering_source->getNumberOfChannels())
  246. m_recreate_buffering_source = true;
  247. if (m_recreate_buffering_source == true)
  248. {
  249. m_buffering_source = std::make_unique<MyBufferingAudioSource>(m_stretch_source.get(),
  250. m_bufferingthread, false, bufamt, numoutchans, false);
  251. m_recreate_buffering_source = false;
  252. }
  253. if (m_bufferingthread.isThreadRunning() == false)
  254. m_bufferingthread.startThread();
  255. m_stretch_source->setNumOutChannels(numoutchans);
  256. m_stretch_source->setFFTSize(m_fft_size_to_use);
  257. m_stretch_source->setProcessParameters(&m_ppar);
  258. m_last_outpos_pos = 0.0;
  259. m_last_in_pos = playrange.getStart()*m_stretch_source->getInfileLengthSeconds();
  260. m_buffering_source->prepareToPlay(maxBlockSize, getSampleRateChecked());
  261. }
  262. double PaulstretchpluginAudioProcessor::getSampleRateChecked()
  263. {
  264. if (m_cur_sr < 1.0)
  265. return 44100.0;
  266. return m_cur_sr;
  267. }
  268. void PaulstretchpluginAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
  269. {
  270. ScopedLock locker(m_cs);
  271. m_cur_sr = sampleRate;
  272. m_curmaxblocksize = samplesPerBlock;
  273. m_input_buffer.setSize(2, samplesPerBlock);
  274. int numoutchans = *m_outchansparam;
  275. if (numoutchans != m_cur_num_out_chans)
  276. m_ready_to_play = false;
  277. if (m_using_memory_buffer == true)
  278. {
  279. int len = jlimit(100,m_recbuffer.getNumSamples(),
  280. int(getSampleRateChecked()*(*getFloatParameter(cpi_max_capture_len))));
  281. m_stretch_source->setAudioBufferAsInputSource(&m_recbuffer,
  282. getSampleRateChecked(),
  283. len);
  284. callGUI(this,[this,len](auto ed) { ed->setAudioBuffer(&m_recbuffer, getSampleRateChecked(), len); },false);
  285. }
  286. if (m_ready_to_play == false)
  287. {
  288. setFFTSize(*getFloatParameter(cpi_fftsize));
  289. m_stretch_source->setProcessParameters(&m_ppar);
  290. m_stretch_source->setFFTWindowingType(1);
  291. String err;
  292. startplay({ *getFloatParameter(cpi_soundstart),*getFloatParameter(cpi_soundend) },
  293. numoutchans, samplesPerBlock, err);
  294. m_cur_num_out_chans = numoutchans;
  295. m_ready_to_play = true;
  296. }
  297. }
  298. void PaulstretchpluginAudioProcessor::releaseResources()
  299. {
  300. //m_control->stopplay();
  301. //m_ready_to_play = false;
  302. }
  303. #ifndef JucePlugin_PreferredChannelConfigurations
  304. bool PaulstretchpluginAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
  305. {
  306. #if JucePlugin_IsMidiEffect
  307. ignoreUnused (layouts);
  308. return true;
  309. #else
  310. // This is the place where you check if the layout is supported.
  311. // In this template code we only support mono or stereo.
  312. if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono()
  313. && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
  314. return false;
  315. // This checks if the input layout matches the output layout
  316. #if ! JucePlugin_IsSynth
  317. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  318. return false;
  319. #endif
  320. return true;
  321. #endif
  322. }
  323. #endif
  324. void copyAudioBufferWrappingPosition(const AudioBuffer<float>& src, AudioBuffer<float>& dest, int destbufpos, int maxdestpos)
  325. {
  326. for (int i = 0; i < dest.getNumChannels(); ++i)
  327. {
  328. int channel_to_copy = i % src.getNumChannels();
  329. if (destbufpos + src.getNumSamples() > maxdestpos)
  330. {
  331. int wrappos = (destbufpos + src.getNumSamples()) % maxdestpos;
  332. int partial_len = src.getNumSamples() - wrappos;
  333. dest.copyFrom(channel_to_copy, destbufpos, src, channel_to_copy, 0, partial_len);
  334. dest.copyFrom(channel_to_copy, partial_len, src, channel_to_copy, 0, wrappos);
  335. }
  336. else
  337. {
  338. dest.copyFrom(channel_to_copy, destbufpos, src, channel_to_copy, 0, src.getNumSamples());
  339. }
  340. }
  341. }
  342. void PaulstretchpluginAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
  343. {
  344. ScopedLock locker(m_cs);
  345. AudioPlayHead* phead = getPlayHead();
  346. if (phead != nullptr)
  347. {
  348. phead->getCurrentPosition(m_playposinfo);
  349. }
  350. ScopedNoDenormals noDenormals;
  351. double srtemp = getSampleRate();
  352. if (srtemp != m_cur_sr)
  353. m_cur_sr = srtemp;
  354. const int totalNumInputChannels = getTotalNumInputChannels();
  355. const int totalNumOutputChannels = getTotalNumOutputChannels();
  356. for (int i = 0; i < totalNumInputChannels; ++i)
  357. m_input_buffer.copyFrom(i, 0, buffer, i, 0, buffer.getNumSamples());
  358. for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  359. buffer.clear (i, 0, buffer.getNumSamples());
  360. if (m_ready_to_play == false)
  361. return;
  362. if (m_is_recording == true)
  363. {
  364. if (m_playposinfo.isPlaying == false && m_capture_when_host_plays == true)
  365. return;
  366. int recbuflenframes = m_max_reclen * getSampleRate();
  367. copyAudioBufferWrappingPosition(buffer, m_recbuffer, m_rec_pos, recbuflenframes);
  368. callGUI(this,[this, &buffer](PaulstretchpluginAudioProcessorEditor*ed)
  369. {
  370. ed->addAudioBlock(buffer, getSampleRate(), m_rec_pos);
  371. }, false);
  372. m_rec_pos = (m_rec_pos + buffer.getNumSamples()) % recbuflenframes;
  373. return;
  374. }
  375. jassert(m_buffering_source != nullptr);
  376. jassert(m_bufferingthread.isThreadRunning());
  377. if (m_last_host_playing == false && m_playposinfo.isPlaying)
  378. {
  379. m_stretch_source->seekPercent(*getFloatParameter(cpi_soundstart));
  380. m_last_host_playing = true;
  381. }
  382. else if (m_last_host_playing == true && m_playposinfo.isPlaying == false)
  383. {
  384. m_last_host_playing = false;
  385. }
  386. if (m_play_when_host_plays == true && m_playposinfo.isPlaying == false)
  387. return;
  388. m_stretch_source->setMainVolume(*getFloatParameter(cpi_main_volume));
  389. m_stretch_source->setRate(*getFloatParameter(cpi_stretchamount));
  390. setFFTSize(*getFloatParameter(cpi_fftsize));
  391. m_ppar.pitch_shift.cents = *getFloatParameter(cpi_pitchshift) * 100.0;
  392. m_ppar.freq_shift.Hz = *getFloatParameter(cpi_frequencyshift);
  393. m_ppar.spread.enabled = *getFloatParameter(cpi_spreadamount) > 0.0f;
  394. m_ppar.spread.bandwidth = *getFloatParameter(cpi_spreadamount);
  395. m_ppar.compressor.enabled = *getFloatParameter(cpi_compress)>0.0f;
  396. m_ppar.compressor.power = *getFloatParameter(cpi_compress);
  397. m_ppar.harmonics.enabled = *getFloatParameter(cpi_numharmonics)<101.0;
  398. m_ppar.harmonics.nharmonics = *getFloatParameter(cpi_numharmonics);
  399. m_ppar.harmonics.freq = *getFloatParameter(cpi_harmonicsfreq);
  400. m_ppar.harmonics.bandwidth = *getFloatParameter(cpi_harmonicsbw);
  401. m_ppar.harmonics.gauss = getParameter(cpi_harmonicsgauss);
  402. m_ppar.octave.om2 = *getFloatParameter(cpi_octavesm2);
  403. m_ppar.octave.om1 = *getFloatParameter(cpi_octavesm1);
  404. m_ppar.octave.o0 = *getFloatParameter(cpi_octaves0);
  405. m_ppar.octave.o1 = *getFloatParameter(cpi_octaves1);
  406. m_ppar.octave.o15 = *getFloatParameter(cpi_octaves15);
  407. m_ppar.octave.o2 = *getFloatParameter(cpi_octaves2);
  408. m_ppar.octave.enabled = true;
  409. m_ppar.filter.low = *getFloatParameter(cpi_filter_low);
  410. m_ppar.filter.high = *getFloatParameter(cpi_filter_high);
  411. m_ppar.tonal_vs_noise.enabled = (*getFloatParameter(cpi_tonalvsnoisebw)) > 0.75;
  412. m_ppar.tonal_vs_noise.bandwidth = *getFloatParameter(cpi_tonalvsnoisebw);
  413. m_ppar.tonal_vs_noise.preserve = *getFloatParameter(cpi_tonalvsnoisepreserve);
  414. m_stretch_source->setOnsetDetection(*getFloatParameter(cpi_onsetdetection));
  415. m_stretch_source->setLoopXFadeLength(*getFloatParameter(cpi_loopxfadelen));
  416. double t0 = *getFloatParameter(cpi_soundstart);
  417. double t1 = *getFloatParameter(cpi_soundend);
  418. if (t0 > t1)
  419. std::swap(t0, t1);
  420. if (t1 - t0 < 0.001)
  421. t1 = t0 + 0.001;
  422. m_stretch_source->setPlayRange({ t0,t1 }, true);
  423. m_stretch_source->setFreezing(getParameter(cpi_freeze));
  424. m_stretch_source->setPaused(getParameter(cpi_pause_enabled));
  425. m_stretch_source->setProcessParameters(&m_ppar);
  426. AudioSourceChannelInfo aif(buffer);
  427. if (isNonRealtime() || m_use_backgroundbuffering == false)
  428. {
  429. m_stretch_source->getNextAudioBlock(aif);
  430. }
  431. else
  432. {
  433. m_buffering_source->getNextAudioBlock(aif);
  434. }
  435. if (getParameter(cpi_passthrough) > 0.5f)
  436. {
  437. for (int i = 0; i < totalNumInputChannels; ++i)
  438. {
  439. buffer.addFrom(i, 0, m_input_buffer, i, 0, buffer.getNumSamples());
  440. }
  441. }
  442. for (int i = 0; i < buffer.getNumChannels(); ++i)
  443. {
  444. for (int j = 0; j < buffer.getNumSamples(); ++j)
  445. {
  446. float sample = buffer.getSample(i,j);
  447. if (std::isnan(sample) || std::isinf(sample))
  448. ++m_abnormal_output_samples;
  449. }
  450. }
  451. }
  452. //==============================================================================
  453. bool PaulstretchpluginAudioProcessor::hasEditor() const
  454. {
  455. return true; // (change this to false if you choose to not supply an editor)
  456. }
  457. AudioProcessorEditor* PaulstretchpluginAudioProcessor::createEditor()
  458. {
  459. return new PaulstretchpluginAudioProcessorEditor (*this);
  460. }
  461. //==============================================================================
  462. void PaulstretchpluginAudioProcessor::getStateInformation (MemoryBlock& destData)
  463. {
  464. ValueTree paramtree("paulstretch3pluginstate");
  465. for (int i=0;i<getNumParameters();++i)
  466. {
  467. auto par = getFloatParameter(i);
  468. if (par != nullptr)
  469. {
  470. paramtree.setProperty(par->paramID, (double)*par, nullptr);
  471. }
  472. }
  473. paramtree.setProperty(m_outchansparam->paramID, (int)*m_outchansparam, nullptr);
  474. if (m_current_file != File())
  475. {
  476. paramtree.setProperty("importedfile", m_current_file.getFullPathName(), nullptr);
  477. }
  478. auto specorder = m_stretch_source->getSpectrumProcessOrder();
  479. paramtree.setProperty("numspectralstages", (int)specorder.size(), nullptr);
  480. for (int i = 0; i < specorder.size(); ++i)
  481. {
  482. paramtree.setProperty("specorder" + String(i), specorder[i], nullptr);
  483. }
  484. if (m_use_backgroundbuffering)
  485. paramtree.setProperty("prebufamount", m_prebuffer_amount, nullptr);
  486. else
  487. paramtree.setProperty("prebufamount", -1, nullptr);
  488. MemoryOutputStream stream(destData,true);
  489. paramtree.writeToStream(stream);
  490. }
  491. void PaulstretchpluginAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
  492. {
  493. ValueTree tree = ValueTree::readFromData(data, sizeInBytes);
  494. if (tree.isValid())
  495. {
  496. {
  497. ScopedLock locker(m_cs);
  498. if (tree.hasProperty("numspectralstages"))
  499. {
  500. std::vector<int> order;
  501. int ordersize = tree.getProperty("numspectralstages");
  502. for (int i = 0; i < ordersize; ++i)
  503. {
  504. order.push_back((int)tree.getProperty("specorder" + String(i)));
  505. }
  506. m_stretch_source->setSpectrumProcessOrder(order);
  507. }
  508. for (int i = 0; i < getNumParameters(); ++i)
  509. {
  510. auto par = getFloatParameter(i);
  511. if (par != nullptr)
  512. {
  513. double parval = tree.getProperty(par->paramID, (double)*par);
  514. *par = parval;
  515. }
  516. }
  517. if (tree.hasProperty(m_outchansparam->paramID))
  518. *m_outchansparam = tree.getProperty(m_outchansparam->paramID, 2);
  519. }
  520. int prebufamt = tree.getProperty("prebufamount", 2);
  521. if (prebufamt==-1)
  522. m_use_backgroundbuffering = false;
  523. else
  524. setPreBufferAmount(prebufamt);
  525. String fn = tree.getProperty("importedfile");
  526. if (fn.isEmpty() == false)
  527. {
  528. File f(fn);
  529. setAudioFile(f);
  530. }
  531. }
  532. }
  533. void PaulstretchpluginAudioProcessor::setRecordingEnabled(bool b)
  534. {
  535. ScopedLock locker(m_cs);
  536. int lenbufframes = getSampleRateChecked()*m_max_reclen;
  537. if (b == true)
  538. {
  539. m_using_memory_buffer = true;
  540. m_current_file = File();
  541. m_recbuffer.setSize(2, m_max_reclen*getSampleRateChecked()+4096,false,false,true);
  542. m_recbuffer.clear();
  543. m_rec_pos = 0;
  544. callGUI(this,[this,lenbufframes](PaulstretchpluginAudioProcessorEditor* ed)
  545. {
  546. ed->beginAddingAudioBlocks(2, getSampleRateChecked(), lenbufframes);
  547. },false);
  548. m_is_recording = true;
  549. }
  550. else
  551. {
  552. if (m_is_recording == true)
  553. {
  554. finishRecording(lenbufframes);
  555. }
  556. }
  557. }
  558. double PaulstretchpluginAudioProcessor::getRecordingPositionPercent()
  559. {
  560. if (m_is_recording==false)
  561. return 0.0;
  562. return 1.0 / m_recbuffer.getNumSamples()*m_rec_pos;
  563. }
  564. String PaulstretchpluginAudioProcessor::setAudioFile(File f)
  565. {
  566. //if (f==File())
  567. // return String();
  568. //if (f==m_current_file && f.getLastModificationTime()==m_current_file_date)
  569. // return String();
  570. auto ai = unique_from_raw(m_afm->createReaderFor(f));
  571. if (ai != nullptr)
  572. {
  573. if (ai->numChannels > 32)
  574. {
  575. //MessageManager::callAsync([cb, file]() { cb("Too many channels in file " + file.getFullPathName()); });
  576. return "Too many channels in file "+f.getFullPathName();
  577. }
  578. if (ai->bitsPerSample>32)
  579. {
  580. //MessageManager::callAsync([cb, file]() { cb("Too high bit depth in file " + file.getFullPathName()); });
  581. return "Too high bit depth in file " + f.getFullPathName();
  582. }
  583. ScopedLock locker(m_cs);
  584. m_stretch_source->setAudioFile(f);
  585. m_current_file = f;
  586. m_current_file_date = m_current_file.getLastModificationTime();
  587. m_using_memory_buffer = false;
  588. return String();
  589. //MessageManager::callAsync([cb, file]() { cb(String()); });
  590. }
  591. return "Could not open file " + f.getFullPathName();
  592. }
  593. Range<double> PaulstretchpluginAudioProcessor::getTimeSelection()
  594. {
  595. return { *getFloatParameter(cpi_soundstart),*getFloatParameter(cpi_soundend) };
  596. }
  597. double PaulstretchpluginAudioProcessor::getPreBufferingPercent()
  598. {
  599. if (m_buffering_source==nullptr)
  600. return 0.0;
  601. return m_buffering_source->getPercentReady();
  602. }
  603. void PaulstretchpluginAudioProcessor::timerCallback(int id)
  604. {
  605. if (id == 1)
  606. {
  607. bool capture = getParameter(cpi_capture_enabled);
  608. if (capture == false && m_max_reclen != *getFloatParameter(cpi_max_capture_len))
  609. {
  610. m_max_reclen = *getFloatParameter(cpi_max_capture_len);
  611. //Logger::writeToLog("Changing max capture len to " + String(m_max_reclen));
  612. }
  613. if (capture == true && m_is_recording == false)
  614. {
  615. setRecordingEnabled(true);
  616. return;
  617. }
  618. if (capture == false && m_is_recording == true)
  619. {
  620. setRecordingEnabled(false);
  621. return;
  622. }
  623. if (m_cur_num_out_chans != *m_outchansparam)
  624. {
  625. jassert(m_curmaxblocksize > 0);
  626. ScopedLock locker(m_cs);
  627. m_ready_to_play = false;
  628. m_cur_num_out_chans = *m_outchansparam;
  629. //Logger::writeToLog("Switching to use " + String(m_cur_num_out_chans) + " out channels");
  630. String err;
  631. startplay({ *getFloatParameter(cpi_soundstart),*getFloatParameter(cpi_soundend) },
  632. m_cur_num_out_chans, m_curmaxblocksize, err);
  633. m_ready_to_play = true;
  634. }
  635. }
  636. }
  637. void PaulstretchpluginAudioProcessor::finishRecording(int lenrecording)
  638. {
  639. m_is_recording = false;
  640. m_stretch_source->setAudioBufferAsInputSource(&m_recbuffer, getSampleRateChecked(), lenrecording);
  641. m_stretch_source->setPlayRange({ *getFloatParameter(cpi_soundstart),*getFloatParameter(cpi_soundend) }, true);
  642. auto ed = dynamic_cast<PaulstretchpluginAudioProcessorEditor*>(getActiveEditor());
  643. if (ed)
  644. {
  645. //ed->setAudioBuffer(&m_recbuffer, getSampleRate(), lenrecording);
  646. }
  647. }
  648. //==============================================================================
  649. // This creates new instances of the plugin..
  650. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  651. {
  652. return new PaulstretchpluginAudioProcessor();
  653. }