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.

799 lines
23KB

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