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.

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