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.

643 lines
22KB

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