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.

740 lines
19KB

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