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
20KB

  1. #include "StretchSource.h"
  2. #include <set>
  3. #ifdef WIN32
  4. #include <ppl.h>
  5. //#define USE_PPL_TO_PROCESS_STRETCHERS
  6. #undef min
  7. #undef max
  8. #endif
  9. StretchAudioSource::StretchAudioSource(int initialnumoutchans,
  10. AudioFormatManager* afm,
  11. std::array<AudioParameterBool*,9>& enab_pars) : m_afm(afm)
  12. {
  13. m_resampler = std::make_unique<WDL_Resampler>();
  14. m_resampler_outbuf.resize(1024*1024);
  15. m_inputfile = std::make_unique<AInputS>(m_afm);
  16. for (int i = 0; i < enab_pars.size(); ++i)
  17. {
  18. m_specproc_order.emplace_back(i, enab_pars[i]);
  19. }
  20. //m_specproc_order = { {0,false} , { 1, false} ,{2,true},{3,true},{4,true},{5,false},{6,true},{7,true},{8,false} };
  21. setNumOutChannels(initialnumoutchans);
  22. m_xfadetask.buffer.setSize(8, 65536);
  23. m_xfadetask.buffer.clear();
  24. }
  25. StretchAudioSource::~StretchAudioSource()
  26. {
  27. }
  28. void StretchAudioSource::prepareToPlay(int samplesPerBlockExpected, double sampleRate)
  29. {
  30. m_outsr = sampleRate;
  31. m_vol_smoother.reset(sampleRate, 0.5);
  32. m_lastplayrate = -1.0;
  33. m_stop_play_requested = false;
  34. m_output_counter = 0;
  35. m_output_silence_counter = 0;
  36. m_stream_end_reached = false;
  37. m_firstbuffer = true;
  38. m_output_has_begun = false;
  39. m_drypreviewbuf.setSize(m_num_outchans, 65536);
  40. initObjects();
  41. }
  42. void StretchAudioSource::releaseResources()
  43. {
  44. }
  45. AudioBuffer<float>* StretchAudioSource::getSourceAudioBuffer()
  46. {
  47. if (m_inputfile==nullptr)
  48. return nullptr;
  49. return m_inputfile->getAudioBuffer();
  50. }
  51. bool StretchAudioSource::isResampling()
  52. {
  53. if (m_inputfile==nullptr || m_inputfile->info.samplerate==0)
  54. return false;
  55. return (int)m_outsr!=m_inputfile->info.samplerate;
  56. }
  57. std::vector<SpectrumProcess> StretchAudioSource::getSpectrumProcessOrder()
  58. {
  59. return m_specproc_order;
  60. }
  61. void StretchAudioSource::setSpectrumProcessOrder(std::vector<SpectrumProcess> order)
  62. {
  63. ScopedLock locker(m_cs);
  64. m_specproc_order = order;
  65. Logger::writeToLog("<**");
  66. for (auto& e : m_specproc_order)
  67. Logger::writeToLog(e.m_enabled->name + " " + String(e.m_index));
  68. Logger::writeToLog("**>");
  69. for (int i = 0; i < m_stretchers.size(); ++i)
  70. {
  71. m_stretchers[i]->m_spectrum_processes = order;
  72. }
  73. }
  74. std::pair<Range<double>, Range<double>> StretchAudioSource::getFileCachedRangesNormalized()
  75. {
  76. if (m_inputfile == nullptr)
  77. return {};
  78. return m_inputfile->getCachedRangesNormalized();
  79. }
  80. void StretchAudioSource::setFreeFilterEnvelope(shared_envelope env)
  81. {
  82. ScopedLock locker(m_cs);
  83. m_free_filter_envelope = env;
  84. for (int i = 0; i < m_stretchers.size(); ++i)
  85. {
  86. m_stretchers[i]->setFreeFilterEnvelope(env);
  87. }
  88. }
  89. ValueTree StretchAudioSource::getStateTree()
  90. {
  91. ValueTree tree("stretchsourcestate");
  92. storeToTreeProperties(tree, nullptr, "pitch_shift", m_ppar.pitch_shift.cents,
  93. "octaves_minus2", m_ppar.octave.om2,
  94. "octaves_minus1",m_ppar.octave.om1,
  95. "octave0",m_ppar.octave.o0,
  96. "octave_plus1",m_ppar.octave.o1,
  97. "octaves_plus15",m_ppar.octave.o15,
  98. "octaves_plus2",m_ppar.octave.o2);
  99. return tree;
  100. }
  101. void StretchAudioSource::setStateTree(ValueTree state)
  102. {
  103. ScopedLock locker(m_cs);
  104. getFromTreeProperties(state, "pitch_shift", m_ppar.pitch_shift.cents,
  105. "octaves_minus2", m_ppar.octave.om2,
  106. "octaves_minus1", m_ppar.octave.om1,
  107. "octave0", m_ppar.octave.o0,
  108. "octave_plus1", m_ppar.octave.o1,
  109. "octaves_plus15", m_ppar.octave.o15,
  110. "octaves_plus2", m_ppar.octave.o2);
  111. for (int i = 0; i < m_stretchers.size(); ++i)
  112. {
  113. m_stretchers[i]->set_parameters(&m_ppar);
  114. }
  115. }
  116. bool StretchAudioSource::isLoopingEnabled()
  117. {
  118. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  119. return false;
  120. return m_inputfile->isLooping();
  121. }
  122. void StretchAudioSource::setLoopingEnabled(bool b)
  123. {
  124. ScopedLock locker(m_cs);
  125. if (m_inputfile != nullptr)
  126. {
  127. if (m_inputfile->isLooping() == false && b == true)
  128. seekPercent(m_inputfile->getActiveRange().getStart());
  129. m_inputfile->setLoopEnabled(b);
  130. }
  131. }
  132. void StretchAudioSource::setAudioBufferAsInputSource(AudioBuffer<float>* buf, int sr, int len)
  133. {
  134. ScopedLock locker(m_cs);
  135. m_inputfile->setAudioBuffer(buf, sr, len);
  136. m_seekpos = 0.0;
  137. m_curfile = File();
  138. if (m_playrange.isEmpty())
  139. setPlayRange({ 0.0,1.0 }, true);
  140. ++m_param_change_count;
  141. }
  142. void StretchAudioSource::setMainVolume(double decibels)
  143. {
  144. if (decibels == m_main_volume)
  145. return;
  146. if (m_cs.tryEnter())
  147. {
  148. m_main_volume = jlimit(-144.0, 12.0, decibels);
  149. ++m_param_change_count;
  150. m_cs.exit();
  151. }
  152. }
  153. #ifdef OLDMODULE_ENAB
  154. void StretchAudioSource::setSpectralModulesEnabled(const std::array<AudioParameterBool*, 9>& params)
  155. {
  156. jassert(m_specprocmap.size() > 0);
  157. /*
  158. bool changed = false;
  159. for (int i = 0; i < m_specproc_order.size(); ++i)
  160. {
  161. int index = m_specprocmap[i];
  162. if (*params[index] != *m_specproc_order[i].m_enabled)
  163. {
  164. changed = true;
  165. break;
  166. }
  167. }
  168. if (changed == false)
  169. return;
  170. */
  171. if (m_cs.tryEnter())
  172. {
  173. for (int i = 0; i < m_specproc_order.size(); ++i)
  174. {
  175. int index = m_specprocmap[i];
  176. *m_specproc_order[i].m_enabled = (bool)(*params[index]);
  177. }
  178. for (int i = 0; i < m_stretchers.size(); ++i)
  179. {
  180. m_stretchers[i]->m_spectrum_processes = m_specproc_order;
  181. }
  182. ++m_param_change_count;
  183. m_cs.exit();
  184. }
  185. }
  186. #endif
  187. void StretchAudioSource::setSpectralModuleEnabled(int index, bool b)
  188. {
  189. ++m_param_change_count;
  190. }
  191. void StretchAudioSource::setLoopXFadeLength(double lenseconds)
  192. {
  193. if (lenseconds == m_loopxfadelen)
  194. return;
  195. if (m_cs.tryEnter())
  196. {
  197. m_loopxfadelen = jlimit(0.0, 1.0, lenseconds);
  198. ++m_param_change_count;
  199. m_cs.exit();
  200. }
  201. }
  202. void StretchAudioSource::setPreviewDry(bool b)
  203. {
  204. if (b == m_preview_dry)
  205. return;
  206. if (m_cs.tryEnter())
  207. {
  208. m_resampler->Reset();
  209. m_preview_dry = b;
  210. ++m_param_change_count;
  211. m_cs.exit();
  212. }
  213. }
  214. bool StretchAudioSource::isPreviewingDry() const
  215. {
  216. return m_preview_dry;
  217. }
  218. void StretchAudioSource::getNextAudioBlock(const AudioSourceChannelInfo & bufferToFill)
  219. {
  220. ScopedLock locker(m_cs);
  221. if (m_preview_dry == true && m_inputfile!=nullptr && m_inputfile->info.nsamples>0)
  222. {
  223. playDrySound(bufferToFill);
  224. return;
  225. }
  226. double maingain = Decibels::decibelsToGain(m_main_volume);
  227. if (m_pause_state == 2)
  228. {
  229. bufferToFill.buffer->clear(bufferToFill.startSample,bufferToFill.numSamples);
  230. return;
  231. }
  232. if (m_stretchoutringbuf.available() > 0)
  233. m_output_has_begun = true;
  234. bool freezing = m_freezing;
  235. if (m_stretchers[0]->isFreezing() != freezing)
  236. {
  237. if (freezing == true && m_inputfile!=nullptr)
  238. m_freeze_pos = 1.0/m_inputfile->info.nsamples*m_inputfile->getCurrentPosition();
  239. for (auto& e : m_stretchers)
  240. {
  241. e->set_freezing(freezing);
  242. }
  243. }
  244. if (m_vol_smoother.getTargetValue() != maingain)
  245. m_vol_smoother.setValue(maingain);
  246. FloatVectorOperations::disableDenormalisedNumberSupport();
  247. float** outarrays = bufferToFill.buffer->getArrayOfWritePointers();
  248. int outbufchans = m_num_outchans; // bufferToFill.buffer->getNumChannels();
  249. int offset = bufferToFill.startSample;
  250. if (m_stretchers.size() == 0)
  251. return;
  252. if (m_inputfile == nullptr)
  253. return;
  254. if (m_inputfile->info.nsamples == 0)
  255. return;
  256. m_inputfile->setXFadeLenSeconds(m_loopxfadelen);
  257. double silencethreshold = Decibels::decibelsToGain(-70.0);
  258. bool tempfirst = true;
  259. auto foofilepos0 = m_inputfile->getCurrentPosition();
  260. auto ringbuffilltask = [this](int framestoproduce)
  261. {
  262. while (m_stretchoutringbuf.available() < framestoproduce*m_num_outchans)
  263. {
  264. int readsize = 0;
  265. double in_pos = (double)m_inputfile->getCurrentPosition() / (double)m_inputfile->info.nsamples;
  266. if (m_firstbuffer)
  267. {
  268. readsize = m_stretchers[0]->get_nsamples_for_fill();
  269. m_firstbuffer = false;
  270. }
  271. else
  272. {
  273. readsize = m_stretchers[0]->get_nsamples(in_pos*100.0);
  274. };
  275. int readed = 0;
  276. if (readsize != 0)
  277. {
  278. readed = m_inputfile->readNextBlock(m_file_inbuf, readsize, m_num_outchans);
  279. }
  280. if (m_rand_count % (int)m_free_filter_envelope->m_transform_y_random_rate == 0)
  281. {
  282. m_free_filter_envelope->updateRandomState();
  283. }
  284. ++m_rand_count;
  285. auto inbufptrs = m_file_inbuf.getArrayOfWritePointers();
  286. REALTYPE onset_max = std::numeric_limits<REALTYPE>::min();
  287. #ifdef USE_PPL_TO_PROCESS_STRETCHERS
  288. std::array<REALTYPE, 16> onset_values_arr;
  289. Concurrency::parallel_for(0, (int)m_stretchers.size(), [this, readed, &onset_values_arr](int i)
  290. {
  291. REALTYPE onset_val = m_stretchers[i]->process(m_inbufs[i].data(), readed);
  292. onset_values_arr[i] = onset_val;
  293. });
  294. for (int i = 0; i < m_stretchers.size(); ++i)
  295. onset_max = std::max(onset_max, onset_values_arr[i]);
  296. #else
  297. for (int i = 0; i < m_stretchers.size(); ++i)
  298. {
  299. REALTYPE onset_l = m_stretchers[i]->process(inbufptrs[i], readed);
  300. onset_max = std::max(onset_max, onset_l);
  301. }
  302. #endif
  303. for (int i = 0; i < m_stretchers.size(); ++i)
  304. m_stretchers[i]->here_is_onset(onset_max);
  305. int outbufsize = m_stretchers[0]->get_bufsize();
  306. int nskip = m_stretchers[0]->get_skip_nsamples();
  307. if (nskip > 0)
  308. m_inputfile->skip(nskip);
  309. for (int i = 0; i < outbufsize; i++)
  310. {
  311. for (int ch = 0; ch < m_num_outchans; ++ch)
  312. {
  313. REALTYPE outsa = m_stretchers[ch]->out_buf[i];
  314. m_stretchoutringbuf.push(outsa);
  315. }
  316. }
  317. }
  318. };
  319. int previousxfadestate = m_xfadetask.state;
  320. auto resamplertask = [this, &ringbuffilltask, &bufferToFill]()
  321. {
  322. double* rsinbuf = nullptr;
  323. int outsamplestoproduce = bufferToFill.numSamples;
  324. if (m_xfadetask.state == 1)
  325. outsamplestoproduce = m_xfadetask.xfade_len;
  326. int wanted = m_resampler->ResamplePrepare(outsamplestoproduce, m_num_outchans, &rsinbuf);
  327. ringbuffilltask(wanted);
  328. for (int i = 0; i < wanted*m_num_outchans; ++i)
  329. {
  330. double sample = m_stretchoutringbuf.get();
  331. rsinbuf[i] = sample;
  332. }
  333. if (outsamplestoproduce*m_num_outchans > m_resampler_outbuf.size())
  334. {
  335. m_resampler_outbuf.resize(outsamplestoproduce*m_num_outchans);
  336. }
  337. /*int produced =*/ m_resampler->ResampleOut(m_resampler_outbuf.data(), wanted, outsamplestoproduce, m_num_outchans);
  338. if (m_xfadetask.state == 1)
  339. {
  340. //Logger::writeToLog("Filling xfade buffer");
  341. for (int i = 0; i < outsamplestoproduce; ++i)
  342. {
  343. for (int j = 0; j < m_num_outchans; ++j)
  344. {
  345. m_xfadetask.buffer.setSample(j, i, m_resampler_outbuf[i*m_num_outchans + j]);
  346. }
  347. }
  348. if (m_process_fftsize != m_xfadetask.requested_fft_size)
  349. {
  350. m_process_fftsize = m_xfadetask.requested_fft_size;
  351. //Logger::writeToLog("Initing stretcher objects");
  352. initObjects();
  353. }
  354. m_xfadetask.state = 2;
  355. }
  356. };
  357. resamplertask();
  358. if (previousxfadestate == 1 && m_xfadetask.state == 2)
  359. {
  360. //Logger::writeToLog("Rerunning resampler task");
  361. resamplertask();
  362. }
  363. bool source_ended = m_inputfile->hasEnded();
  364. double samplelimit = 16384.0;
  365. if (m_clip_output == true)
  366. samplelimit = 1.0;
  367. for (int i = 0; i < bufferToFill.numSamples; ++i)
  368. {
  369. double smoothed_gain = m_vol_smoother.getNextValue();
  370. double mixed = 0.0;
  371. for (int j = 0; j < outbufchans; ++j)
  372. {
  373. double outsample = m_resampler_outbuf[i*m_num_outchans + j];
  374. if (m_xfadetask.state == 2)
  375. {
  376. double xfadegain = 1.0 / m_xfadetask.xfade_len*m_xfadetask.counter;
  377. jassert(xfadegain >= 0.0 && xfadegain <= 1.0);
  378. double outsample2 = m_xfadetask.buffer.getSample(j, m_xfadetask.counter);
  379. outsample = xfadegain * outsample + (1.0 - xfadegain)*outsample2;
  380. }
  381. outarrays[j][i + offset] = jlimit(-samplelimit,samplelimit , outsample * smoothed_gain);
  382. mixed += outsample;
  383. }
  384. if (m_xfadetask.state == 2)
  385. {
  386. ++m_xfadetask.counter;
  387. if (m_xfadetask.counter >= m_xfadetask.xfade_len)
  388. m_xfadetask.state = 0;
  389. }
  390. if (source_ended && m_output_counter>=2*m_process_fftsize)
  391. {
  392. if (fabs(mixed) < silencethreshold)
  393. ++m_output_silence_counter;
  394. else
  395. m_output_silence_counter = 0;
  396. }
  397. }
  398. if (m_pause_state == 1)
  399. {
  400. bufferToFill.buffer->applyGainRamp(bufferToFill.startSample, bufferToFill.numSamples, 1.0f, 0.0f);
  401. m_pause_state = 2;
  402. }
  403. if (m_pause_state == 3)
  404. {
  405. bufferToFill.buffer->applyGainRamp(bufferToFill.startSample, bufferToFill.numSamples, 0.0f, 1.0f);
  406. m_pause_state = 0;
  407. }
  408. m_output_counter += bufferToFill.numSamples;
  409. }
  410. void StretchAudioSource::setNextReadPosition(int64 /*newPosition*/)
  411. {
  412. }
  413. int64 StretchAudioSource::getNextReadPosition() const
  414. {
  415. return int64();
  416. }
  417. int64 StretchAudioSource::getTotalLength() const
  418. {
  419. if (m_inputfile == nullptr)
  420. return 0;
  421. return m_inputfile->info.nsamples;
  422. }
  423. bool StretchAudioSource::isLooping() const
  424. {
  425. return false;
  426. }
  427. String StretchAudioSource::setAudioFile(File file)
  428. {
  429. ScopedLock locker(m_cs);
  430. if (m_inputfile->openAudioFile(file))
  431. {
  432. m_curfile = file;
  433. m_firstbuffer = true;
  434. return String();
  435. }
  436. return "Could not open file";
  437. }
  438. File StretchAudioSource::getAudioFile()
  439. {
  440. return m_curfile;
  441. }
  442. void StretchAudioSource::setNumOutChannels(int chans)
  443. {
  444. jassert(chans > 0 && chans < g_maxnumoutchans);
  445. m_num_outchans = chans;
  446. }
  447. void StretchAudioSource::initObjects()
  448. {
  449. ScopedLock locker(m_cs);
  450. m_inputfile->setActiveRange(m_playrange);
  451. if (m_inputfile->getActiveRange().contains(m_inputfile->getCurrentPositionPercent())==false)
  452. m_inputfile->seek(m_playrange.getStart());
  453. m_firstbuffer = true;
  454. if (m_stretchoutringbuf.getSize() < m_num_outchans*m_process_fftsize)
  455. {
  456. int newsize = m_num_outchans*m_process_fftsize*2;
  457. //Logger::writeToLog("Resizing circular buffer to " + String(newsize));
  458. m_stretchoutringbuf.resize(newsize);
  459. }
  460. m_stretchoutringbuf.clear();
  461. m_resampler->Reset();
  462. m_resampler->SetRates(m_inputfile->info.samplerate, m_outsr);
  463. REALTYPE stretchratio = m_playrate;
  464. FFTWindow windowtype = W_HAMMING;
  465. if (m_fft_window_type>=0)
  466. windowtype = (FFTWindow)m_fft_window_type;
  467. int inbufsize = m_process_fftsize;
  468. double onsetsens = m_onsetdetection;
  469. m_stretchers.resize(m_num_outchans);
  470. for (int i = 0; i < m_stretchers.size(); ++i)
  471. {
  472. if (m_stretchers[i] == nullptr)
  473. {
  474. //Logger::writeToLog("Creating stretch instance " + String(i));
  475. m_stretchers[i] = std::make_shared<ProcessedStretch>(stretchratio,
  476. m_process_fftsize, windowtype, false, (float)m_inputfile->info.samplerate, i + 1);
  477. }
  478. m_stretchers[i]->setBufferSize(m_process_fftsize);
  479. m_stretchers[i]->setSampleRate(m_inputfile->info.samplerate);
  480. m_stretchers[i]->set_onset_detection_sensitivity(onsetsens);
  481. m_stretchers[i]->set_parameters(&m_ppar);
  482. m_stretchers[i]->set_freezing(m_freezing);
  483. m_stretchers[i]->setFreeFilterEnvelope(m_free_filter_envelope);
  484. fill_container(m_stretchers[i]->out_buf, 0.0f);
  485. m_stretchers[i]->m_spectrum_processes = m_specproc_order;
  486. }
  487. m_file_inbuf.setSize(m_num_outchans, 3 * inbufsize);
  488. int poolsize = m_stretchers[0]->get_max_bufsize();
  489. }
  490. void StretchAudioSource::playDrySound(const AudioSourceChannelInfo & bufferToFill)
  491. {
  492. double maingain = Decibels::decibelsToGain(m_main_volume);
  493. m_inputfile->setXFadeLenSeconds(m_loopxfadelen);
  494. double* rsinbuf = nullptr;
  495. m_resampler->SetRates(m_inputfile->info.samplerate, m_outsr);
  496. int wanted = m_resampler->ResamplePrepare(bufferToFill.numSamples, m_num_outchans, &rsinbuf);
  497. m_inputfile->readNextBlock(m_drypreviewbuf, wanted, m_num_outchans);
  498. for (int i = 0; i < wanted; ++i)
  499. for (int j = 0; j < m_num_outchans; ++j)
  500. rsinbuf[i*m_num_outchans + j] = m_drypreviewbuf.getSample(j, i);
  501. m_resampler->ResampleOut(m_resampler_outbuf.data(), wanted, bufferToFill.numSamples, m_num_outchans);
  502. for (int i = 0; i < m_num_outchans; ++i)
  503. for (int j = 0; j < bufferToFill.numSamples; ++j)
  504. bufferToFill.buffer->setSample(i, j + bufferToFill.startSample, maingain * m_resampler_outbuf[j*m_num_outchans + i]);
  505. }
  506. double StretchAudioSource::getInfilePositionPercent()
  507. {
  508. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  509. return 0.0;
  510. return 1.0/m_inputfile->info.nsamples*m_inputfile->getCurrentPosition();
  511. }
  512. double StretchAudioSource::getInfilePositionSeconds()
  513. {
  514. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  515. return 0.0;
  516. //return m_lastinpos*m_inputfile->getLengthSeconds();
  517. return (double)m_inputfile->getCurrentPosition() / m_inputfile->info.samplerate;
  518. }
  519. double StretchAudioSource::getInfileLengthSeconds()
  520. {
  521. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  522. return 0.0;
  523. return (double)m_inputfile->info.nsamples / m_inputfile->info.samplerate;
  524. }
  525. void StretchAudioSource::setRate(double rate)
  526. {
  527. if (rate == m_playrate)
  528. return;
  529. if (m_cs.tryEnter())
  530. {
  531. m_playrate = rate;
  532. for (int i = 0; i < m_stretchers.size(); ++i)
  533. {
  534. m_stretchers[i]->set_rap((float)rate);
  535. }
  536. ++m_param_change_count;
  537. m_cs.exit();
  538. }
  539. }
  540. void StretchAudioSource::setProcessParameters(ProcessParameters * pars)
  541. {
  542. if (*pars == m_ppar)
  543. return;
  544. if (m_cs.tryEnter())
  545. {
  546. m_ppar = *pars;
  547. for (int i = 0; i < m_stretchers.size(); ++i)
  548. {
  549. m_stretchers[i]->set_parameters(pars);
  550. }
  551. ++m_param_change_count;
  552. m_cs.exit();
  553. }
  554. }
  555. const ProcessParameters& StretchAudioSource::getProcessParameters()
  556. {
  557. return m_ppar;
  558. }
  559. void StretchAudioSource::setFFTWindowingType(int windowtype)
  560. {
  561. if (windowtype==m_fft_window_type)
  562. return;
  563. if (m_cs.tryEnter())
  564. {
  565. m_fft_window_type = windowtype;
  566. for (int i = 0; i < m_stretchers.size(); ++i)
  567. {
  568. m_stretchers[i]->window_type = (FFTWindow)windowtype;
  569. }
  570. ++m_param_change_count;
  571. m_cs.exit();
  572. }
  573. }
  574. void StretchAudioSource::setFFTSize(int size)
  575. {
  576. jassert(size>0);
  577. if (m_xfadetask.state == 0 && (m_process_fftsize == 0 || size != m_process_fftsize))
  578. {
  579. ScopedLock locker(m_cs);
  580. if (m_xfadetask.buffer.getNumChannels() < m_num_outchans)
  581. {
  582. m_xfadetask.buffer.setSize(m_num_outchans, m_xfadetask.buffer.getNumSamples());
  583. }
  584. if (m_process_fftsize > 0)
  585. {
  586. m_xfadetask.state = 1;
  587. m_xfadetask.counter = 0;
  588. m_xfadetask.xfade_len = 16384;
  589. m_xfadetask.requested_fft_size = size;
  590. }
  591. else
  592. {
  593. m_process_fftsize = size;
  594. initObjects();
  595. }
  596. ++m_param_change_count;
  597. }
  598. }
  599. void StretchAudioSource::setPaused(bool b)
  600. {
  601. if (b == true && m_pause_state>0)
  602. return;
  603. if (b == false && m_pause_state == 0)
  604. return;
  605. ScopedLock locker(m_cs);
  606. if (b == true && m_pause_state == 0)
  607. {
  608. m_pause_state = 1;
  609. return;
  610. }
  611. if (b == false && m_pause_state == 2)
  612. {
  613. m_pause_state = 3;
  614. return;
  615. }
  616. }
  617. bool StretchAudioSource::isPaused() const
  618. {
  619. return m_pause_state > 0;
  620. }
  621. void StretchAudioSource::seekPercent(double pos)
  622. {
  623. ScopedLock locker(m_cs);
  624. m_seekpos = pos;
  625. //m_resampler->Reset();
  626. m_inputfile->seek(pos);
  627. ++m_param_change_count;
  628. }
  629. double StretchAudioSource::getOutputDurationSecondsForRange(Range<double> range, int fftsize)
  630. {
  631. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  632. return 0.0;
  633. if (m_preview_dry==true)
  634. return (double)range.getLength()*m_inputfile->info.nsamples/m_inputfile->info.samplerate;
  635. int64_t play_end_pos = (fftsize * 2)+range.getLength()*m_playrate*m_inputfile->info.nsamples;
  636. return (double)play_end_pos / m_inputfile->info.samplerate;
  637. }
  638. void StretchAudioSource::setOnsetDetection(double x)
  639. {
  640. if (x == m_onsetdetection)
  641. return;
  642. if (m_cs.tryEnter())
  643. {
  644. m_onsetdetection = x;
  645. for (int i = 0; i < m_stretchers.size(); ++i)
  646. {
  647. m_stretchers[i]->set_onset_detection_sensitivity((float)x);
  648. }
  649. ++m_param_change_count;
  650. m_cs.exit();
  651. }
  652. }
  653. void StretchAudioSource::setPlayRange(Range<double> playrange, bool isloop)
  654. {
  655. if (m_playrange.isEmpty() == false && playrange == m_playrange)
  656. return;
  657. if (m_cs.tryEnter())
  658. {
  659. if (playrange.isEmpty())
  660. m_playrange = { 0.0,1.0 };
  661. else
  662. m_playrange = playrange;
  663. m_stream_end_reached = false;
  664. m_inputfile->setActiveRange(m_playrange);
  665. m_inputfile->setLoopEnabled(isloop);
  666. //if (m_playrange.contains(m_seekpos) == false)
  667. // m_inputfile->seek(m_playrange.getStart());
  668. m_seekpos = m_playrange.getStart();
  669. ++m_param_change_count;
  670. m_cs.exit();
  671. }
  672. }
  673. bool StretchAudioSource::isLoopEnabled()
  674. {
  675. if (m_inputfile == nullptr)
  676. return false;
  677. return m_inputfile->isLooping();
  678. }
  679. bool StretchAudioSource::hasReachedEnd()
  680. {
  681. if (m_inputfile == nullptr)
  682. return false;
  683. if (m_inputfile->isLooping() && m_maxloops == 0)
  684. return false;
  685. if (m_inputfile->isLooping() && m_inputfile->getLoopCount() > m_maxloops)
  686. return true;
  687. //return m_output_counter>=m_process_fftsize*2;
  688. return m_output_silence_counter>=65536;
  689. }