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.

167 lines
5.3KB

  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 <mutex>
  21. #include <array>
  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<SpectrumProcess> getSpectrumProcessOrder();
  65. void setSpectrumProcessOrder(std::vector<SpectrumProcess> order);
  66. void setFFTWindowingType(int windowtype);
  67. int getFFTWindowingType() { return m_fft_window_type; }
  68. std::pair<Range<double>,Range<double>> getFileCachedRangesNormalized();
  69. void setFreeFilterEnvelope(shared_envelope env);
  70. ValueTree getStateTree();
  71. void setStateTree(ValueTree state);
  72. void setClippingEnabled(bool b) { m_clip_output = b; }
  73. bool isLoopingEnabled();
  74. void setLoopingEnabled(bool b);
  75. void setMaxLoops(int64_t numloops) { m_maxloops = numloops; }
  76. void setAudioBufferAsInputSource(AudioBuffer<float>* buf, int sr, int len);
  77. void setMainVolume(double decibels);
  78. double getMainVolume() const { return m_main_volume; }
  79. void setSpectralModulesEnabled(const std::array<AudioParameterBool*, 9>& params);
  80. void setLoopXFadeLength(double lenseconds);
  81. double getLoopXFadeLengtj() const { return m_loopxfadelen; }
  82. void setPreviewDry(bool b);
  83. bool isPreviewingDry() const;
  84. int m_param_change_count = 0;
  85. CriticalSection* getMutex() { return &m_cs; }
  86. private:
  87. CircularBuffer<float> m_stretchoutringbuf{ 1024 * 1024 };
  88. AudioBuffer<float> m_file_inbuf;
  89. LinearSmoothedValue<double> m_vol_smoother;
  90. std::unique_ptr<AInputS> m_inputfile;
  91. std::vector<std::shared_ptr<ProcessedStretch>> m_stretchers;
  92. std::function<void(StretchAudioSource*)> SourceEndedCallback;
  93. bool m_firstbuffer = false;
  94. bool m_output_has_begun = false;
  95. int m_num_outchans = 0;
  96. double m_outsr = 44100.0;
  97. int m_process_fftsize = 0;
  98. int m_fft_window_type = -1;
  99. double m_main_volume = 0.0;
  100. double m_loopxfadelen = 0.0;
  101. ProcessParameters m_ppar;
  102. double m_playrate = 1.0;
  103. double m_lastplayrate = 0.0;
  104. double m_onsetdetection = 0.0;
  105. double m_seekpos = 0.0;
  106. bool m_freezing = false;
  107. int m_pause_state = 0;
  108. Range<double> m_playrange{ 0.0,1.0 };
  109. int64_t m_rand_count = 0;
  110. bool m_stream_end_reached = false;
  111. int64_t m_output_silence_counter = 0;
  112. File m_curfile;
  113. int64_t m_maxloops = 0;
  114. std::unique_ptr<WDL_Resampler> m_resampler;
  115. std::vector<double> m_resampler_outbuf;
  116. CriticalSection m_cs;
  117. std::vector<SpectrumProcess> m_specproc_order;
  118. bool m_stop_play_requested = false;
  119. double m_freeze_pos = 0.0;
  120. int64_t m_output_counter = 0;
  121. int64_t m_output_length = 0;
  122. bool m_clip_output = true;
  123. void initObjects();
  124. shared_envelope m_free_filter_envelope;
  125. AudioFormatManager* m_afm = nullptr;
  126. struct
  127. {
  128. AudioBuffer<float> buffer;
  129. int state = 0; // 0 not active, 1 fill xfade buffer, 2 play xfade buffer
  130. int xfade_len = 0;
  131. int counter = 0;
  132. int requested_fft_size = 0;
  133. File requested_file;
  134. } m_xfadetask;
  135. int m_pause_fade_counter = 0;
  136. bool m_preview_dry = false;
  137. AudioBuffer<float> m_drypreviewbuf;
  138. void playDrySound(const AudioSourceChannelInfo & bufferToFill);
  139. };