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.

754 lines
20KB

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