The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

143 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. A subclass of SynthesiserSound that represents a sampled audio clip.
  21. This is a pretty basic sampler, and just attempts to load the whole audio stream
  22. into memory.
  23. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  24. give it some SampledSound objects to play.
  25. @see SamplerVoice, Synthesiser, SynthesiserSound
  26. */
  27. class JUCE_API SamplerSound : public SynthesiserSound
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a sampled sound from an audio reader.
  32. This will attempt to load the audio from the source into memory and store
  33. it in this object.
  34. @param name a name for the sample
  35. @param source the audio to load. This object can be safely deleted by the
  36. caller after this constructor returns
  37. @param midiNotes the set of midi keys that this sound should be played on. This
  38. is used by the SynthesiserSound::appliesToNote() method
  39. @param midiNoteForNormalPitch the midi note at which the sample should be played
  40. with its natural rate. All other notes will be pitched
  41. up or down relative to this one
  42. @param attackTimeSecs the attack (fade-in) time, in seconds
  43. @param releaseTimeSecs the decay (fade-out) time, in seconds
  44. @param maxSampleLengthSeconds a maximum length of audio to read from the audio
  45. source, in seconds
  46. */
  47. SamplerSound (const String& name,
  48. AudioFormatReader& source,
  49. const BigInteger& midiNotes,
  50. int midiNoteForNormalPitch,
  51. double attackTimeSecs,
  52. double releaseTimeSecs,
  53. double maxSampleLengthSeconds);
  54. /** Destructor. */
  55. ~SamplerSound();
  56. //==============================================================================
  57. /** Returns the sample's name */
  58. const String& getName() const noexcept { return name; }
  59. /** Returns the audio sample data.
  60. This could return nullptr if there was a problem loading the data.
  61. */
  62. AudioSampleBuffer* getAudioData() const noexcept { return data; }
  63. //==============================================================================
  64. bool appliesToNote (int midiNoteNumber) override;
  65. bool appliesToChannel (int midiChannel) override;
  66. private:
  67. //==============================================================================
  68. friend class SamplerVoice;
  69. String name;
  70. ScopedPointer<AudioSampleBuffer> data;
  71. double sourceSampleRate;
  72. BigInteger midiNotes;
  73. int length, attackSamples, releaseSamples;
  74. int midiRootNote;
  75. JUCE_LEAK_DETECTOR (SamplerSound)
  76. };
  77. //==============================================================================
  78. /**
  79. A subclass of SynthesiserVoice that can play a SamplerSound.
  80. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  81. give it some SampledSound objects to play.
  82. @see SamplerSound, Synthesiser, SynthesiserVoice
  83. */
  84. class JUCE_API SamplerVoice : public SynthesiserVoice
  85. {
  86. public:
  87. //==============================================================================
  88. /** Creates a SamplerVoice. */
  89. SamplerVoice();
  90. /** Destructor. */
  91. ~SamplerVoice();
  92. //==============================================================================
  93. bool canPlaySound (SynthesiserSound*) override;
  94. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int pitchWheel) override;
  95. void stopNote (float velocity, bool allowTailOff) override;
  96. void pitchWheelMoved (int newValue) override;
  97. void controllerMoved (int controllerNumber, int newValue) override;
  98. void renderNextBlock (AudioSampleBuffer&, int startSample, int numSamples) override;
  99. private:
  100. //==============================================================================
  101. double pitchRatio;
  102. double sourceSamplePosition;
  103. float lgain, rgain, attackReleaseLevel, attackDelta, releaseDelta;
  104. bool isInAttack, isInRelease;
  105. JUCE_LEAK_DETECTOR (SamplerVoice)
  106. };