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.

182 lines
5.6KB

  1. /*
  2. Copyright (C) 2017 Xenakios
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of version 3 of the GNU General Public License
  5. as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License (version 3) for more details.
  10. www.gnu.org/licenses
  11. */
  12. #pragma once
  13. #include "../JuceLibraryCode/JuceHeader.h"
  14. #include "Input/AInputS.h"
  15. #include "ProcessedStretch.h"
  16. #include <mutex>
  17. #include <array>
  18. #include "../WDL/resample.h"
  19. class StretchAudioSource final : public PositionableAudioSource
  20. {
  21. public:
  22. StretchAudioSource() {}
  23. StretchAudioSource(int initialnumoutchans, AudioFormatManager* afm, std::array<AudioParameterBool*, 9>& enab_pars);
  24. ~StretchAudioSource();
  25. // Inherited via PositionableAudioSource
  26. void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override;
  27. void releaseResources() override;
  28. void getNextAudioBlock(const AudioSourceChannelInfo & bufferToFill) override;
  29. void setNextReadPosition(int64 newPosition) override;
  30. int64 getNextReadPosition() const override;
  31. int64 getTotalLength() const override;
  32. bool isLooping() const override;
  33. String setAudioFile(File file);
  34. File getAudioFile();
  35. AudioBuffer<float>* getSourceAudioBuffer();
  36. void setNumOutChannels(int chans);
  37. int getNumOutChannels() { return m_num_outchans; }
  38. double getInfilePositionPercent();
  39. double getInfilePositionSeconds();
  40. double getInfileLengthSeconds();
  41. double getInfileSamplerate();
  42. void setRate(double rate);
  43. double getRate()
  44. {
  45. return m_playrate;
  46. }
  47. double getOutputSamplerate() const { return m_outsr; }
  48. void setProcessParameters(ProcessParameters* pars);
  49. const ProcessParameters& getProcessParameters();
  50. void setFFTSize(int size);
  51. int getFFTSize() { return m_process_fftsize; }
  52. double getFreezePos() const { return m_freeze_pos; }
  53. void setFreezing(bool b) { m_freezing = b; }
  54. bool isFreezing() { return m_freezing; }
  55. void setPaused(bool b);
  56. bool isPaused() const;
  57. void seekPercent(double pos);
  58. double getOutputDurationSecondsForRange(Range<double> range, int fftsize);
  59. void setOnsetDetection(double x);
  60. void setPlayRange(Range<double> playrange);
  61. Range<double> getPlayRange() { return m_playrange; }
  62. bool isLoopEnabled();
  63. bool hasReachedEnd();
  64. bool isResampling();
  65. int64_t getDiskReadSampleCount() const;
  66. std::vector<SpectrumProcess> getSpectrumProcessOrder();
  67. void setSpectrumProcessOrder(std::vector<SpectrumProcess> order);
  68. void setFFTWindowingType(int windowtype);
  69. int getFFTWindowingType() { return m_fft_window_type; }
  70. std::pair<Range<double>,Range<double>> getFileCachedRangesNormalized();
  71. void setFreeFilterEnvelope(shared_envelope env);
  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 setSpectralModuleEnabled(int index, bool b);
  81. void setLoopXFadeLength(double lenseconds);
  82. double getLoopXFadeLengtj() const { return m_loopxfadelen; }
  83. void setPreviewDry(bool b);
  84. bool isPreviewingDry() const;
  85. void setDryPlayrate(double rate);
  86. double getDryPlayrate() const;
  87. int m_param_change_count = 0;
  88. double getLastSeekPos() const { return m_seekpos; }
  89. CriticalSection* getMutex() { return &m_cs; }
  90. int64_t getLastSourcePosition() const { return m_last_filepos; }
  91. int m_prebuffersize = 0;
  92. void setSpectralOrderPreset(int id);
  93. private:
  94. CircularBuffer<float> m_stretchoutringbuf{ 1024 * 1024 };
  95. AudioBuffer<float> m_file_inbuf;
  96. LinearSmoothedValue<double> m_vol_smoother;
  97. std::unique_ptr<AInputS> m_inputfile;
  98. std::vector<std::shared_ptr<ProcessedStretch>> m_stretchers;
  99. std::function<void(StretchAudioSource*)> SourceEndedCallback;
  100. bool m_firstbuffer = false;
  101. bool m_output_has_begun = false;
  102. int m_num_outchans = 0;
  103. double m_outsr = 44100.0;
  104. int m_process_fftsize = 0;
  105. int m_fft_window_type = -1;
  106. double m_main_volume = 0.0;
  107. double m_loopxfadelen = 0.0;
  108. ProcessParameters m_ppar;
  109. double m_playrate = 1.0;
  110. double m_lastplayrate = 0.0;
  111. double m_onsetdetection = 0.0;
  112. double m_seekpos = 0.0;
  113. bool m_freezing = false;
  114. int m_pause_state = 0;
  115. Range<double> m_playrange{ 0.0,1.0 };
  116. int64_t m_rand_count = 0;
  117. bool m_stream_end_reached = false;
  118. int64_t m_output_silence_counter = 0;
  119. File m_curfile;
  120. int64_t m_maxloops = 0;
  121. std::unique_ptr<WDL_Resampler> m_resampler;
  122. std::vector<double> m_resampler_outbuf;
  123. CriticalSection m_cs;
  124. std::vector<SpectrumProcess> m_specproc_order;
  125. bool m_stop_play_requested = false;
  126. double m_freeze_pos = 0.0;
  127. int64_t m_output_counter = 0;
  128. int64_t m_output_length = 0;
  129. bool m_clip_output = true;
  130. void initObjects();
  131. shared_envelope m_free_filter_envelope;
  132. AudioFormatManager* m_afm = nullptr;
  133. struct
  134. {
  135. AudioBuffer<float> buffer;
  136. int state = 0; // 0 not active, 1 fill xfade buffer, 2 play xfade buffer
  137. int xfade_len = 0;
  138. int counter = 0;
  139. int requested_fft_size = 0;
  140. File requested_file;
  141. } m_xfadetask;
  142. int m_pause_fade_counter = 0;
  143. bool m_preview_dry = false;
  144. double m_dryplayrate = 1.0;
  145. AudioBuffer<float> m_drypreviewbuf;
  146. int64_t m_last_filepos = 0;
  147. void playDrySound(const AudioSourceChannelInfo & bufferToFill);
  148. int m_current_spec_order_preset = -1;
  149. };