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.

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