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.

230 lines
7.1KB

  1. /*
  2. Copyright (C) 2006-2011 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. Copyright (C) 2017 Xenakios
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License
  7. as published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #pragma once
  17. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "Input/AInputS.h"
  19. #include "ProcessedStretch.h"
  20. #include "BinauralBeats.h"
  21. #include <mutex>
  22. #include "../WDL/resample.h"
  23. class StretchAudioSource final : public PositionableAudioSource
  24. {
  25. public:
  26. StretchAudioSource() {}
  27. StretchAudioSource(int initialnumoutchans, AudioFormatManager* afm);
  28. ~StretchAudioSource();
  29. // Inherited via PositionableAudioSource
  30. void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override;
  31. void releaseResources() override;
  32. void getNextAudioBlock(const AudioSourceChannelInfo & bufferToFill) override;
  33. void setNextReadPosition(int64 newPosition) override;
  34. int64 getNextReadPosition() const override;
  35. int64 getTotalLength() const override;
  36. bool isLooping() const override;
  37. String setAudioFile(File file);
  38. File getAudioFile();
  39. AudioBuffer<float>* getSourceAudioBuffer();
  40. void setNumOutChannels(int chans);
  41. int getNumOutChannels() { return m_num_outchans; }
  42. double getInfilePositionPercent();
  43. double getInfilePositionSeconds();
  44. double getInfileLengthSeconds();
  45. void setRate(double rate);
  46. double getRate() { return m_playrate; }
  47. void setProcessParameters(ProcessParameters* pars);
  48. const ProcessParameters& getProcessParameters();
  49. void setFFTSize(int size);
  50. int getFFTSize() { return m_process_fftsize; }
  51. double getFreezePos() const { return m_freeze_pos; }
  52. void setFreezing(bool b) { m_freezing = b; }
  53. bool isFreezing() { return m_freezing; }
  54. void setPaused(bool b);
  55. bool isPaused() const;
  56. void seekPercent(double pos);
  57. double getOutputDurationSecondsForRange(Range<double> range, int fftsize);
  58. void setOnsetDetection(double x);
  59. void setPlayRange(Range<double> playrange, bool isloop);
  60. Range<double> getPlayRange() { return m_playrange; }
  61. bool isLoopEnabled();
  62. bool hasReachedEnd();
  63. bool isResampling();
  64. std::vector<int> getSpectrumProcessOrder();
  65. void setSpectrumProcessOrder(std::vector<int> order);
  66. void setFFTWindowingType(int windowtype);
  67. int getFFTWindowingType() { return m_fft_window_type; }
  68. std::pair<Range<double>,Range<double>> getFileCachedRangesNormalized();
  69. ValueTree getStateTree();
  70. void setStateTree(ValueTree state);
  71. void setClippingEnabled(bool b) { m_clip_output = b; }
  72. bool isLoopingEnabled();
  73. void setLoopingEnabled(bool b);
  74. void setMaxLoops(int64_t numloops) { m_maxloops = numloops; }
  75. void setAudioBufferAsInputSource(AudioBuffer<float>* buf, int sr, int len);
  76. void setMainVolume(double decibels);
  77. double getMainVolume() const { return m_main_volume; }
  78. void setLoopXFadeLength(double lenseconds);
  79. double getLoopXFadeLengtj() const { return m_loopxfadelen; }
  80. int m_param_change_count = 0;
  81. private:
  82. CircularBuffer<float> m_stretchoutringbuf{ 1024 * 1024 };
  83. AudioBuffer<float> m_file_inbuf;
  84. LinearSmoothedValue<double> m_vol_smoother;
  85. std::unique_ptr<AInputS> m_inputfile;
  86. std::vector<std::shared_ptr<ProcessedStretch>> m_stretchers;
  87. std::unique_ptr<BinauralBeats> m_binaubeats;
  88. std::function<void(StretchAudioSource*)> SourceEndedCallback;
  89. bool m_firstbuffer = false;
  90. bool m_output_has_begun = false;
  91. int m_num_outchans = 0;
  92. double m_outsr = 44100.0;
  93. int m_process_fftsize = 0;
  94. int m_fft_window_type = -1;
  95. double m_main_volume = 0.0;
  96. double m_loopxfadelen = 0.0;
  97. ProcessParameters m_ppar;
  98. BinauralBeatsParameters m_bbpar;
  99. double m_playrate = 1.0;
  100. double m_lastplayrate = 0.0;
  101. double m_onsetdetection = 0.0;
  102. double m_seekpos = 0.0;
  103. bool m_freezing = false;
  104. int m_pause_state = 0;
  105. Range<double> m_playrange{ 0.0,1.0 };
  106. bool m_stream_end_reached = false;
  107. int64_t m_output_silence_counter = 0;
  108. File m_curfile;
  109. int64_t m_maxloops = 0;
  110. std::unique_ptr<WDL_Resampler> m_resampler;
  111. std::vector<double> m_resampler_outbuf;
  112. CriticalSection m_cs;
  113. std::vector<int> m_specproc_order;
  114. bool m_stop_play_requested = false;
  115. double m_freeze_pos = 0.0;
  116. int64_t m_output_counter = 0;
  117. int64_t m_output_length = 0;
  118. bool m_clip_output = true;
  119. void initObjects();
  120. AudioFormatManager* m_afm = nullptr;
  121. struct
  122. {
  123. AudioBuffer<float> buffer;
  124. int state = 0; // 0 not active, 1 fill xfade buffer, 2 play xfade buffer
  125. int xfade_len = 0;
  126. int counter = 0;
  127. int requested_fft_size = 0;
  128. File requested_file;
  129. } m_xfadetask;
  130. int m_pause_fade_counter = 0;
  131. };
  132. class MultiStretchAudioSource final : public PositionableAudioSource
  133. {
  134. public:
  135. MultiStretchAudioSource() {}
  136. MultiStretchAudioSource(int initialnumoutchans, AudioFormatManager* afm);
  137. ~MultiStretchAudioSource();
  138. void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override;
  139. void releaseResources() override;
  140. void getNextAudioBlock(const AudioSourceChannelInfo & bufferToFill) override;
  141. void setNextReadPosition(int64 newPosition) override;
  142. int64 getNextReadPosition() const override;
  143. int64 getTotalLength() const override;
  144. bool isLooping() const override;
  145. String setAudioFile(File file);
  146. File getAudioFile();
  147. void setNumOutChannels(int chans);
  148. double getInfilePositionPercent();
  149. void setRate(double rate);
  150. double getRate();
  151. void setProcessParameters(ProcessParameters* pars);
  152. void setFFTSize(int size);
  153. int getFFTSize();
  154. void seekPercent(double pos);
  155. double getInfilePositionSeconds();
  156. double getInfileLengthSeconds();
  157. double getOutputDurationSecondsForRange(Range<double> range, int fftsize);
  158. void setOnsetDetection(double x);
  159. void setPlayRange(Range<double> playrange, bool isloop);
  160. bool isLoopingEnabled();
  161. void setLoopingEnabled(bool b);
  162. bool hasReachedEnd();
  163. bool isResampling();
  164. std::vector<int> getSpectrumProcessOrder();
  165. void setSpectrumProcessOrder(std::vector<int> order);
  166. void setFFTWindowingType(int windowtype);
  167. std::pair<Range<double>, Range<double>> getFileCachedRangesNormalized();
  168. void setFreezing(bool b) { m_freezing = b; }
  169. bool isFreezing() { return m_freezing; }
  170. Value val_MainVolume;
  171. Value val_XFadeLen;
  172. //ValueTree getStateTree();
  173. //void setStateTree(ValueTree state);
  174. void setAudioBufferAsInputSource(AudioBuffer<float>* buf, int sr, int len);
  175. private:
  176. std::vector<std::shared_ptr<StretchAudioSource>> m_stretchsources;
  177. bool m_is_in_switch{ false };
  178. bool m_is_playing = false;
  179. bool m_freezing = false;
  180. LinearSmoothedValue<double> m_xfadegain;
  181. StretchAudioSource* getActiveStretchSource() const;
  182. void switchActiveSource();
  183. int m_blocksize = 0;
  184. double m_samplerate = 44100.0;
  185. int m_numoutchans = 2;
  186. AudioBuffer<float> m_processbuffers[2];
  187. std::mutex m_mutex;
  188. double m_switchxfadelen = 1.0;
  189. AudioFormatManager* m_afm = nullptr;
  190. };