Audio plugin host https://kx.studio/carla
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.

juce_Sampler.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A subclass of SynthesiserSound that represents a sampled audio clip.
  23. This is a pretty basic sampler, and just attempts to load the whole audio stream
  24. into memory.
  25. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  26. give it some SampledSound objects to play.
  27. @see SamplerVoice, Synthesiser, SynthesiserSound
  28. @tags{Audio}
  29. */
  30. class JUCE_API SamplerSound : public SynthesiserSound
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a sampled sound from an audio reader.
  35. This will attempt to load the audio from the source into memory and store
  36. it in this object.
  37. @param name a name for the sample
  38. @param source the audio to load. This object can be safely deleted by the
  39. caller after this constructor returns
  40. @param midiNotes the set of midi keys that this sound should be played on. This
  41. is used by the SynthesiserSound::appliesToNote() method
  42. @param midiNoteForNormalPitch the midi note at which the sample should be played
  43. with its natural rate. All other notes will be pitched
  44. up or down relative to this one
  45. @param attackTimeSecs the attack (fade-in) time, in seconds
  46. @param releaseTimeSecs the decay (fade-out) time, in seconds
  47. @param maxSampleLengthSeconds a maximum length of audio to read from the audio
  48. source, in seconds
  49. */
  50. SamplerSound (const String& name,
  51. AudioFormatReader& source,
  52. const BigInteger& midiNotes,
  53. int midiNoteForNormalPitch,
  54. double attackTimeSecs,
  55. double releaseTimeSecs,
  56. double maxSampleLengthSeconds);
  57. /** Destructor. */
  58. ~SamplerSound() override;
  59. //==============================================================================
  60. /** Returns the sample's name */
  61. const String& getName() const noexcept { return name; }
  62. /** Returns the audio sample data.
  63. This could return nullptr if there was a problem loading the data.
  64. */
  65. AudioBuffer<float>* getAudioData() const noexcept { return data.get(); }
  66. //==============================================================================
  67. /** Changes the parameters of the ADSR envelope which will be applied to the sample. */
  68. void setEnvelopeParameters (ADSR::Parameters parametersToUse) { params = parametersToUse; }
  69. //==============================================================================
  70. bool appliesToNote (int midiNoteNumber) override;
  71. bool appliesToChannel (int midiChannel) override;
  72. private:
  73. //==============================================================================
  74. friend class SamplerVoice;
  75. String name;
  76. std::unique_ptr<AudioBuffer<float>> data;
  77. double sourceSampleRate;
  78. BigInteger midiNotes;
  79. int length = 0, midiRootNote = 0;
  80. ADSR::Parameters params;
  81. JUCE_LEAK_DETECTOR (SamplerSound)
  82. };
  83. //==============================================================================
  84. /**
  85. A subclass of SynthesiserVoice that can play a SamplerSound.
  86. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  87. give it some SampledSound objects to play.
  88. @see SamplerSound, Synthesiser, SynthesiserVoice
  89. @tags{Audio}
  90. */
  91. class JUCE_API SamplerVoice : public SynthesiserVoice
  92. {
  93. public:
  94. //==============================================================================
  95. /** Creates a SamplerVoice. */
  96. SamplerVoice();
  97. /** Destructor. */
  98. ~SamplerVoice() override;
  99. //==============================================================================
  100. bool canPlaySound (SynthesiserSound*) override;
  101. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int pitchWheel) override;
  102. void stopNote (float velocity, bool allowTailOff) override;
  103. void pitchWheelMoved (int newValue) override;
  104. void controllerMoved (int controllerNumber, int newValue) override;
  105. void renderNextBlock (AudioBuffer<float>&, int startSample, int numSamples) override;
  106. using SynthesiserVoice::renderNextBlock;
  107. private:
  108. //==============================================================================
  109. double pitchRatio = 0;
  110. double sourceSamplePosition = 0;
  111. float lgain = 0, rgain = 0;
  112. ADSR adsr;
  113. JUCE_LEAK_DETECTOR (SamplerVoice)
  114. };
  115. } // namespace juce