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.

678 lines
18KB

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