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.

175 lines
6.2KB

  1. /* Copyright 2013-2019 Matt Tytel
  2. *
  3. * vital is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * vital 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 for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with vital. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include "JuceHeader.h"
  18. #include "pitch_detector.h"
  19. #include "wavetable_component.h"
  20. #include "wave_frame.h"
  21. #include "wave_source.h"
  22. #include "utils.h"
  23. class FileSource : public WavetableComponent {
  24. public:
  25. static constexpr float kMaxFileSourceSamples = 176400;
  26. static constexpr int kExtraSaveSamples = 4;
  27. static constexpr int kExtraBufferSamples = 4;
  28. static constexpr int kPitchDetectMaxPeriod = 8096;
  29. enum FadeStyle {
  30. kWaveBlend,
  31. kNoInterpolate,
  32. kTimeInterpolate,
  33. kFreqInterpolate,
  34. kNumFadeStyles
  35. };
  36. enum PhaseStyle {
  37. kNone,
  38. kClear,
  39. kVocode,
  40. kNumPhaseStyles
  41. };
  42. struct SampleBuffer {
  43. SampleBuffer() : size(0), sample_rate(0) { }
  44. std::unique_ptr<float[]> data;
  45. int size;
  46. int sample_rate;
  47. };
  48. class FileSourceKeyframe : public WavetableKeyframe {
  49. public:
  50. FileSourceKeyframe(SampleBuffer* sample_buffer);
  51. virtual ~FileSourceKeyframe() { }
  52. void copy(const WavetableKeyframe* keyframe) override;
  53. void interpolate(const WavetableKeyframe* from_keyframe,
  54. const WavetableKeyframe* to_keyframe, float t) override;
  55. float getNormalizationScale();
  56. void render(vital::WaveFrame* wave_frame) override;
  57. void renderWaveBlend(vital::WaveFrame* wave_frame);
  58. void renderNoInterpolate(vital::WaveFrame* wave_frame);
  59. void renderTimeInterpolate(vital::WaveFrame* wave_frame);
  60. void renderFreqInterpolate(vital::WaveFrame* wave_frame);
  61. json stateToJson() override;
  62. void jsonToState(json data) override;
  63. double getStartPosition() { return start_position_; }
  64. double getWindowSize() { return window_size_; }
  65. double getWindowFade() { return window_fade_; }
  66. double getWindowFadeSamples() { return window_fade_ * window_size_; }
  67. int getSamplesNeeded() { return getWindowSize() + getWindowFadeSamples(); }
  68. force_inline void setStartPosition(double start_position) { start_position_ = start_position; }
  69. force_inline void setWindowFade(double window_fade) { window_fade_ = window_fade; }
  70. force_inline void setWindowSize(double window_size) { window_size_ = window_size; }
  71. force_inline void setFadeStyle(FadeStyle fade_style) { fade_style_ = fade_style; }
  72. force_inline void setPhaseStyle(PhaseStyle phase_style) { phase_style_ = phase_style; }
  73. force_inline void setOverriddenPhaseBuffer(const float* buffer) { overridden_phase_ = buffer; }
  74. force_inline const float* getDataBuffer() {
  75. if (sample_buffer_ == nullptr || sample_buffer_->data == nullptr)
  76. return nullptr;
  77. return sample_buffer_->data.get() + 1;
  78. }
  79. force_inline const float* getCubicInterpolationBuffer() {
  80. if (sample_buffer_ == nullptr)
  81. return nullptr;
  82. return sample_buffer_->data.get();
  83. }
  84. float getScaledInterpolatedSample(float time);
  85. void setInterpolateFromFrame(WaveSourceKeyframe* frame) {
  86. interpolate_from_frame_ = frame;
  87. }
  88. void setInterpolateToFrame(WaveSourceKeyframe* frame) {
  89. interpolate_to_frame_ = frame;
  90. }
  91. protected:
  92. SampleBuffer* sample_buffer_;
  93. const float* overridden_phase_;
  94. WaveSourceKeyframe* interpolate_from_frame_;
  95. WaveSourceKeyframe* interpolate_to_frame_;
  96. double start_position_;
  97. double window_fade_;
  98. double window_size_;
  99. FadeStyle fade_style_;
  100. PhaseStyle phase_style_;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileSourceKeyframe)
  102. };
  103. FileSource();
  104. virtual ~FileSource() { }
  105. WavetableKeyframe* createKeyframe(int position) override;
  106. void render(vital::WaveFrame* wave_frame, float position) override;
  107. WavetableComponentFactory::ComponentType getType() override;
  108. json stateToJson() override;
  109. void jsonToState(json data) override;
  110. FileSourceKeyframe* getKeyframe(int index);
  111. const SampleBuffer* buffer() const { return &sample_buffer_; }
  112. FadeStyle getFadeStyle() { return fade_style_; }
  113. PhaseStyle getPhaseStyle() { return phase_style_; }
  114. bool getNormalizeGain() { return normalize_gain_; }
  115. void setNormalizeGain(bool normalize_gain) { normalize_gain_ = normalize_gain; }
  116. void setWindowSize(double window_size) { window_size_ = window_size; }
  117. void setFadeStyle(FadeStyle fade_style) { fade_style_ = fade_style; }
  118. void setPhaseStyle(PhaseStyle phase_style);
  119. void writePhaseOverrideBuffer();
  120. double getWindowSize() { return window_size_; }
  121. void loadBuffer(const float* buffer, int size, int sample_rate);
  122. void detectPitch(int max_period = vital::WaveFrame::kWaveformSize);
  123. void detectWaveEditTable();
  124. force_inline const float* getDataBuffer() {
  125. if (sample_buffer_.data == nullptr)
  126. return nullptr;
  127. return sample_buffer_.data.get() + 1;
  128. }
  129. force_inline const float* getCubicInterpolationBuffer() { return sample_buffer_.data.get(); }
  130. protected:
  131. FileSourceKeyframe compute_frame_;
  132. WaveSourceKeyframe interpolate_from_frame_;
  133. WaveSourceKeyframe interpolate_to_frame_;
  134. SampleBuffer sample_buffer_;
  135. float overridden_phase_[vital::WaveFrame::kWaveformSize];
  136. FadeStyle fade_style_;
  137. PhaseStyle phase_style_;
  138. bool normalize_gain_;
  139. bool normalize_mult_;
  140. double window_size_;
  141. int random_seed_;
  142. vital::utils::RandomGenerator random_generator_;
  143. PitchDetector pitch_detector_;
  144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileSource)
  145. };