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.

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