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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef JUCE_SAMPLER_H_INCLUDED
  18. #define JUCE_SAMPLER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A subclass of SynthesiserSound that represents a sampled audio clip.
  22. This is a pretty basic sampler, and just attempts to load the whole audio stream
  23. into memory.
  24. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  25. give it some SampledSound objects to play.
  26. @see SamplerVoice, Synthesiser, SynthesiserSound
  27. */
  28. class JUCE_API SamplerSound : public SynthesiserSound
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a sampled sound from an audio reader.
  33. This will attempt to load the audio from the source into memory and store
  34. it in this object.
  35. @param name a name for the sample
  36. @param source the audio to load. This object can be safely deleted by the
  37. caller after this constructor returns
  38. @param midiNotes the set of midi keys that this sound should be played on. This
  39. is used by the SynthesiserSound::appliesToNote() method
  40. @param midiNoteForNormalPitch the midi note at which the sample should be played
  41. with its natural rate. All other notes will be pitched
  42. up or down relative to this one
  43. @param attackTimeSecs the attack (fade-in) time, in seconds
  44. @param releaseTimeSecs the decay (fade-out) time, in seconds
  45. @param maxSampleLengthSeconds a maximum length of audio to read from the audio
  46. source, in seconds
  47. */
  48. SamplerSound (const String& name,
  49. AudioFormatReader& source,
  50. const BigInteger& midiNotes,
  51. int midiNoteForNormalPitch,
  52. double attackTimeSecs,
  53. double releaseTimeSecs,
  54. double maxSampleLengthSeconds);
  55. /** Destructor. */
  56. ~SamplerSound();
  57. //==============================================================================
  58. /** Returns the sample's name */
  59. const String& getName() const noexcept { return name; }
  60. /** Returns the audio sample data.
  61. This could return nullptr if there was a problem loading the data.
  62. */
  63. AudioSampleBuffer* getAudioData() const noexcept { return data; }
  64. //==============================================================================
  65. bool appliesToNote (const int midiNoteNumber) override;
  66. bool appliesToChannel (const int midiChannel) override;
  67. private:
  68. //==============================================================================
  69. friend class SamplerVoice;
  70. String name;
  71. ScopedPointer<AudioSampleBuffer> data;
  72. double sourceSampleRate;
  73. BigInteger midiNotes;
  74. int length, attackSamples, releaseSamples;
  75. int midiRootNote;
  76. JUCE_LEAK_DETECTOR (SamplerSound)
  77. };
  78. //==============================================================================
  79. /**
  80. A subclass of SynthesiserVoice that can play a SamplerSound.
  81. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  82. give it some SampledSound objects to play.
  83. @see SamplerSound, Synthesiser, SynthesiserVoice
  84. */
  85. class JUCE_API SamplerVoice : public SynthesiserVoice
  86. {
  87. public:
  88. //==============================================================================
  89. /** Creates a SamplerVoice. */
  90. SamplerVoice();
  91. /** Destructor. */
  92. ~SamplerVoice();
  93. //==============================================================================
  94. bool canPlaySound (SynthesiserSound*) override;
  95. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int pitchWheel) override;
  96. void stopNote (bool allowTailOff) override;
  97. void pitchWheelMoved (int newValue);
  98. void controllerMoved (int controllerNumber, int newValue) override;
  99. void renderNextBlock (AudioSampleBuffer&, int startSample, int numSamples) override;
  100. private:
  101. //==============================================================================
  102. double pitchRatio;
  103. double sourceSamplePosition;
  104. float lgain, rgain, attackReleaseLevel, attackDelta, releaseDelta;
  105. bool isInAttack, isInRelease;
  106. JUCE_LEAK_DETECTOR (SamplerVoice)
  107. };
  108. #endif // JUCE_SAMPLER_H_INCLUDED