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.

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