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.

680 lines
23KB

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