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.

700 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. if (m_rand_count % (int)m_free_filter_envelope->m_transform_y_random_rate == 0)
  228. {
  229. m_free_filter_envelope->updateRandomState();
  230. }
  231. ++m_rand_count;
  232. auto inbufptrs = m_file_inbuf.getArrayOfWritePointers();
  233. REALTYPE onset_max = std::numeric_limits<REALTYPE>::min();
  234. #ifdef USE_PPL_TO_PROCESS_STRETCHERS
  235. std::array<REALTYPE, 16> onset_values_arr;
  236. Concurrency::parallel_for(0, (int)m_stretchers.size(), [this, readed, &onset_values_arr](int i)
  237. {
  238. REALTYPE onset_val = m_stretchers[i]->process(m_inbufs[i].data(), readed);
  239. onset_values_arr[i] = onset_val;
  240. });
  241. for (int i = 0; i < m_stretchers.size(); ++i)
  242. onset_max = std::max(onset_max, onset_values_arr[i]);
  243. #else
  244. for (int i = 0; i < m_stretchers.size(); ++i)
  245. {
  246. REALTYPE onset_l = m_stretchers[i]->process(inbufptrs[i], readed);
  247. onset_max = std::max(onset_max, onset_l);
  248. }
  249. #endif
  250. for (int i = 0; i < m_stretchers.size(); ++i)
  251. m_stretchers[i]->here_is_onset(onset_max);
  252. int outbufsize = m_stretchers[0]->get_bufsize();
  253. int nskip = m_stretchers[0]->get_skip_nsamples();
  254. if (nskip > 0)
  255. m_inputfile->skip(nskip);
  256. for (int i = 0; i < outbufsize; i++)
  257. {
  258. for (int ch = 0; ch < m_num_outchans; ++ch)
  259. {
  260. REALTYPE outsa = m_stretchers[ch]->out_buf[i];
  261. m_stretchoutringbuf.push(outsa);
  262. }
  263. }
  264. }
  265. };
  266. int previousxfadestate = m_xfadetask.state;
  267. auto resamplertask = [this, &ringbuffilltask, &bufferToFill]()
  268. {
  269. double* rsinbuf = nullptr;
  270. int outsamplestoproduce = bufferToFill.numSamples;
  271. if (m_xfadetask.state == 1)
  272. outsamplestoproduce = m_xfadetask.xfade_len;
  273. int wanted = m_resampler->ResamplePrepare(outsamplestoproduce, m_num_outchans, &rsinbuf);
  274. ringbuffilltask(wanted);
  275. for (int i = 0; i < wanted*m_num_outchans; ++i)
  276. {
  277. double sample = m_stretchoutringbuf.get();
  278. rsinbuf[i] = sample;
  279. }
  280. if (outsamplestoproduce*m_num_outchans > m_resampler_outbuf.size())
  281. {
  282. m_resampler_outbuf.resize(outsamplestoproduce*m_num_outchans);
  283. }
  284. /*int produced =*/ m_resampler->ResampleOut(m_resampler_outbuf.data(), wanted, outsamplestoproduce, m_num_outchans);
  285. if (m_xfadetask.state == 1)
  286. {
  287. //Logger::writeToLog("Filling xfade buffer");
  288. for (int i = 0; i < outsamplestoproduce; ++i)
  289. {
  290. for (int j = 0; j < m_num_outchans; ++j)
  291. {
  292. m_xfadetask.buffer.setSample(j, i, m_resampler_outbuf[i*m_num_outchans + j]);
  293. }
  294. }
  295. if (m_process_fftsize != m_xfadetask.requested_fft_size)
  296. {
  297. m_process_fftsize = m_xfadetask.requested_fft_size;
  298. //Logger::writeToLog("Initing stretcher objects");
  299. initObjects();
  300. }
  301. m_xfadetask.state = 2;
  302. }
  303. };
  304. resamplertask();
  305. if (previousxfadestate == 1 && m_xfadetask.state == 2)
  306. {
  307. //Logger::writeToLog("Rerunning resampler task");
  308. resamplertask();
  309. }
  310. bool source_ended = m_inputfile->hasEnded();
  311. double samplelimit = 16384.0;
  312. if (m_clip_output == true)
  313. samplelimit = 1.0;
  314. for (int i = 0; i < bufferToFill.numSamples; ++i)
  315. {
  316. double smoothed_gain = m_vol_smoother.getNextValue();
  317. double mixed = 0.0;
  318. for (int j = 0; j < outbufchans; ++j)
  319. {
  320. double outsample = m_resampler_outbuf[i*m_num_outchans + j];
  321. if (m_xfadetask.state == 2)
  322. {
  323. double xfadegain = 1.0 / m_xfadetask.xfade_len*m_xfadetask.counter;
  324. jassert(xfadegain >= 0.0 && xfadegain <= 1.0);
  325. double outsample2 = m_xfadetask.buffer.getSample(j, m_xfadetask.counter);
  326. outsample = xfadegain * outsample + (1.0 - xfadegain)*outsample2;
  327. }
  328. outarrays[j][i + offset] = jlimit(-samplelimit,samplelimit , outsample * smoothed_gain);
  329. mixed += outsample;
  330. }
  331. if (m_xfadetask.state == 2)
  332. {
  333. ++m_xfadetask.counter;
  334. if (m_xfadetask.counter >= m_xfadetask.xfade_len)
  335. m_xfadetask.state = 0;
  336. }
  337. if (source_ended && m_output_counter>=2*m_process_fftsize)
  338. {
  339. if (fabs(mixed) < silencethreshold)
  340. ++m_output_silence_counter;
  341. else
  342. m_output_silence_counter = 0;
  343. }
  344. }
  345. if (m_pause_state == 1)
  346. {
  347. bufferToFill.buffer->applyGainRamp(bufferToFill.startSample, bufferToFill.numSamples, 1.0f, 0.0f);
  348. m_pause_state = 2;
  349. }
  350. if (m_pause_state == 3)
  351. {
  352. bufferToFill.buffer->applyGainRamp(bufferToFill.startSample, bufferToFill.numSamples, 0.0f, 1.0f);
  353. m_pause_state = 0;
  354. }
  355. m_output_counter += bufferToFill.numSamples;
  356. }
  357. void StretchAudioSource::setNextReadPosition(int64 /*newPosition*/)
  358. {
  359. }
  360. int64 StretchAudioSource::getNextReadPosition() const
  361. {
  362. return int64();
  363. }
  364. int64 StretchAudioSource::getTotalLength() const
  365. {
  366. if (m_inputfile == nullptr)
  367. return 0;
  368. return m_inputfile->info.nsamples;
  369. }
  370. bool StretchAudioSource::isLooping() const
  371. {
  372. return false;
  373. }
  374. String StretchAudioSource::setAudioFile(File file)
  375. {
  376. ScopedLock locker(m_cs);
  377. if (m_inputfile->openAudioFile(file))
  378. {
  379. m_curfile = file;
  380. return String();
  381. }
  382. return "Could not open file";
  383. }
  384. File StretchAudioSource::getAudioFile()
  385. {
  386. return m_curfile;
  387. }
  388. void StretchAudioSource::setNumOutChannels(int chans)
  389. {
  390. jassert(chans > 0 && chans < g_maxnumoutchans);
  391. m_num_outchans = chans;
  392. }
  393. void StretchAudioSource::initObjects()
  394. {
  395. ScopedLock locker(m_cs);
  396. m_inputfile->setActiveRange(m_playrange);
  397. if (m_inputfile->getActiveRange().contains(m_inputfile->getCurrentPositionPercent())==false)
  398. m_inputfile->seek(m_playrange.getStart());
  399. m_firstbuffer = true;
  400. if (m_stretchoutringbuf.getSize() < m_num_outchans*m_process_fftsize)
  401. {
  402. int newsize = m_num_outchans*m_process_fftsize*2;
  403. //Logger::writeToLog("Resizing circular buffer to " + String(newsize));
  404. m_stretchoutringbuf.resize(newsize);
  405. }
  406. m_stretchoutringbuf.clear();
  407. m_resampler->Reset();
  408. m_resampler->SetRates(m_inputfile->info.samplerate, m_outsr);
  409. REALTYPE stretchratio = m_playrate;
  410. FFTWindow windowtype = W_HAMMING;
  411. if (m_fft_window_type>=0)
  412. windowtype = (FFTWindow)m_fft_window_type;
  413. int inbufsize = m_process_fftsize;
  414. double onsetsens = m_onsetdetection;
  415. m_stretchers.resize(m_num_outchans);
  416. for (int i = 0; i < m_stretchers.size(); ++i)
  417. {
  418. if (m_stretchers[i] == nullptr)
  419. {
  420. //Logger::writeToLog("Creating stretch instance " + String(i));
  421. m_stretchers[i] = std::make_shared<ProcessedStretch>(stretchratio,
  422. m_process_fftsize, windowtype, false, (float)m_inputfile->info.samplerate, i + 1);
  423. }
  424. m_stretchers[i]->setBufferSize(m_process_fftsize);
  425. m_stretchers[i]->setSampleRate(m_inputfile->info.samplerate);
  426. m_stretchers[i]->set_onset_detection_sensitivity(onsetsens);
  427. m_stretchers[i]->set_parameters(&m_ppar);
  428. m_stretchers[i]->set_freezing(m_freezing);
  429. m_stretchers[i]->setFreeFilterEnvelope(m_free_filter_envelope);
  430. fill_container(m_stretchers[i]->out_buf, 0.0f);
  431. m_stretchers[i]->m_spectrum_processes = m_specproc_order;
  432. }
  433. m_file_inbuf.setSize(m_num_outchans, 3 * inbufsize);
  434. int poolsize = m_stretchers[0]->get_max_bufsize();
  435. }
  436. void StretchAudioSource::playDrySound(const AudioSourceChannelInfo & bufferToFill)
  437. {
  438. double maingain = Decibels::decibelsToGain(m_main_volume);
  439. m_inputfile->setXFadeLenSeconds(m_loopxfadelen);
  440. double* rsinbuf = nullptr;
  441. m_resampler->SetRates(m_inputfile->info.samplerate, m_outsr);
  442. int wanted = m_resampler->ResamplePrepare(bufferToFill.numSamples, m_num_outchans, &rsinbuf);
  443. m_inputfile->readNextBlock(m_drypreviewbuf, wanted, m_num_outchans);
  444. for (int i = 0; i < wanted; ++i)
  445. for (int j = 0; j < m_num_outchans; ++j)
  446. rsinbuf[i*m_num_outchans + j] = m_drypreviewbuf.getSample(j, i);
  447. m_resampler->ResampleOut(m_resampler_outbuf.data(), wanted, bufferToFill.numSamples, m_num_outchans);
  448. for (int i = 0; i < m_num_outchans; ++i)
  449. for (int j = 0; j < bufferToFill.numSamples; ++j)
  450. bufferToFill.buffer->setSample(i, j + bufferToFill.startSample, maingain * m_resampler_outbuf[j*m_num_outchans + i]);
  451. }
  452. double StretchAudioSource::getInfilePositionPercent()
  453. {
  454. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  455. return 0.0;
  456. return 1.0/m_inputfile->info.nsamples*m_inputfile->getCurrentPosition();
  457. }
  458. double StretchAudioSource::getInfilePositionSeconds()
  459. {
  460. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  461. return 0.0;
  462. //return m_lastinpos*m_inputfile->getLengthSeconds();
  463. return (double)m_inputfile->getCurrentPosition() / m_inputfile->info.samplerate;
  464. }
  465. double StretchAudioSource::getInfileLengthSeconds()
  466. {
  467. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  468. return 0.0;
  469. return (double)m_inputfile->info.nsamples / m_inputfile->info.samplerate;
  470. }
  471. void StretchAudioSource::setRate(double rate)
  472. {
  473. if (rate == m_playrate)
  474. return;
  475. if (m_cs.tryEnter())
  476. {
  477. m_playrate = rate;
  478. for (int i = 0; i < m_stretchers.size(); ++i)
  479. {
  480. m_stretchers[i]->set_rap((float)rate);
  481. }
  482. ++m_param_change_count;
  483. m_cs.exit();
  484. }
  485. }
  486. void StretchAudioSource::setProcessParameters(ProcessParameters * pars)
  487. {
  488. if (*pars == m_ppar)
  489. return;
  490. if (m_cs.tryEnter())
  491. {
  492. m_ppar = *pars;
  493. for (int i = 0; i < m_stretchers.size(); ++i)
  494. {
  495. m_stretchers[i]->set_parameters(pars);
  496. }
  497. ++m_param_change_count;
  498. m_cs.exit();
  499. }
  500. }
  501. const ProcessParameters& StretchAudioSource::getProcessParameters()
  502. {
  503. return m_ppar;
  504. }
  505. void StretchAudioSource::setFFTWindowingType(int windowtype)
  506. {
  507. if (windowtype==m_fft_window_type)
  508. return;
  509. if (m_cs.tryEnter())
  510. {
  511. m_fft_window_type = windowtype;
  512. for (int i = 0; i < m_stretchers.size(); ++i)
  513. {
  514. m_stretchers[i]->window_type = (FFTWindow)windowtype;
  515. }
  516. ++m_param_change_count;
  517. m_cs.exit();
  518. }
  519. }
  520. void StretchAudioSource::setFFTSize(int size)
  521. {
  522. jassert(size>0);
  523. if (m_xfadetask.state == 0 && (m_process_fftsize == 0 || size != m_process_fftsize))
  524. {
  525. ScopedLock locker(m_cs);
  526. if (m_xfadetask.buffer.getNumChannels() < m_num_outchans)
  527. {
  528. m_xfadetask.buffer.setSize(m_num_outchans, m_xfadetask.buffer.getNumSamples());
  529. }
  530. if (m_process_fftsize > 0)
  531. {
  532. m_xfadetask.state = 1;
  533. m_xfadetask.counter = 0;
  534. m_xfadetask.xfade_len = 16384;
  535. m_xfadetask.requested_fft_size = size;
  536. }
  537. else
  538. {
  539. m_process_fftsize = size;
  540. initObjects();
  541. }
  542. ++m_param_change_count;
  543. }
  544. }
  545. void StretchAudioSource::setPaused(bool b)
  546. {
  547. if (b == true && m_pause_state>0)
  548. return;
  549. if (b == false && m_pause_state == 0)
  550. return;
  551. ScopedLock locker(m_cs);
  552. if (b == true && m_pause_state == 0)
  553. {
  554. m_pause_state = 1;
  555. return;
  556. }
  557. if (b == false && m_pause_state == 2)
  558. {
  559. m_pause_state = 3;
  560. return;
  561. }
  562. }
  563. bool StretchAudioSource::isPaused() const
  564. {
  565. return m_pause_state > 0;
  566. }
  567. void StretchAudioSource::seekPercent(double pos)
  568. {
  569. ScopedLock locker(m_cs);
  570. m_seekpos = pos;
  571. //m_resampler->Reset();
  572. m_inputfile->seek(pos);
  573. ++m_param_change_count;
  574. }
  575. double StretchAudioSource::getOutputDurationSecondsForRange(Range<double> range, int fftsize)
  576. {
  577. if (m_inputfile == nullptr || m_inputfile->info.nsamples == 0)
  578. return 0.0;
  579. if (m_preview_dry==true)
  580. return (double)range.getLength()*m_inputfile->info.nsamples/m_inputfile->info.samplerate;
  581. int64_t play_end_pos = (fftsize * 2)+range.getLength()*m_playrate*m_inputfile->info.nsamples;
  582. return (double)play_end_pos / m_inputfile->info.samplerate;
  583. }
  584. void StretchAudioSource::setOnsetDetection(double x)
  585. {
  586. if (x == m_onsetdetection)
  587. return;
  588. if (m_cs.tryEnter())
  589. {
  590. m_onsetdetection = x;
  591. for (int i = 0; i < m_stretchers.size(); ++i)
  592. {
  593. m_stretchers[i]->set_onset_detection_sensitivity((float)x);
  594. }
  595. ++m_param_change_count;
  596. m_cs.exit();
  597. }
  598. }
  599. void StretchAudioSource::setPlayRange(Range<double> playrange, bool isloop)
  600. {
  601. if (m_playrange.isEmpty() == false && playrange == m_playrange)
  602. return;
  603. if (m_cs.tryEnter())
  604. {
  605. if (playrange.isEmpty())
  606. m_playrange = { 0.0,1.0 };
  607. else
  608. m_playrange = playrange;
  609. m_stream_end_reached = false;
  610. m_inputfile->setActiveRange(m_playrange);
  611. m_inputfile->setLoopEnabled(isloop);
  612. //if (m_playrange.contains(m_seekpos) == false)
  613. // m_inputfile->seek(m_playrange.getStart());
  614. m_seekpos = m_playrange.getStart();
  615. ++m_param_change_count;
  616. m_cs.exit();
  617. }
  618. }
  619. bool StretchAudioSource::isLoopEnabled()
  620. {
  621. if (m_inputfile == nullptr)
  622. return false;
  623. return m_inputfile->isLooping();
  624. }
  625. bool StretchAudioSource::hasReachedEnd()
  626. {
  627. if (m_inputfile == nullptr)
  628. return false;
  629. if (m_inputfile->isLooping() && m_maxloops == 0)
  630. return false;
  631. if (m_inputfile->isLooping() && m_inputfile->getLoopCount() > m_maxloops)
  632. return true;
  633. //return m_output_counter>=m_process_fftsize*2;
  634. return m_output_silence_counter>=65536;
  635. }