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.

757 lines
26KB

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