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

7 years ago
10 years ago
10 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A subclass of SynthesiserSound that represents a sampled audio clip.
  24. This is a pretty basic sampler, and just attempts to load the whole audio stream
  25. into memory.
  26. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  27. give it some SampledSound objects to play.
  28. @see SamplerVoice, Synthesiser, SynthesiserSound
  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();
  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. AudioSampleBuffer* getAudioData() const noexcept { return data; }
  66. //==============================================================================
  67. bool appliesToNote (int midiNoteNumber) override;
  68. bool appliesToChannel (int midiChannel) override;
  69. private:
  70. //==============================================================================
  71. friend class SamplerVoice;
  72. String name;
  73. ScopedPointer<AudioSampleBuffer> data;
  74. double sourceSampleRate;
  75. BigInteger midiNotes;
  76. int length, attackSamples, releaseSamples;
  77. int midiRootNote;
  78. JUCE_LEAK_DETECTOR (SamplerSound)
  79. };
  80. //==============================================================================
  81. /**
  82. A subclass of SynthesiserVoice that can play a SamplerSound.
  83. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  84. give it some SampledSound objects to play.
  85. @see SamplerSound, Synthesiser, SynthesiserVoice
  86. */
  87. class JUCE_API SamplerVoice : public SynthesiserVoice
  88. {
  89. public:
  90. //==============================================================================
  91. /** Creates a SamplerVoice. */
  92. SamplerVoice();
  93. /** Destructor. */
  94. ~SamplerVoice();
  95. //==============================================================================
  96. bool canPlaySound (SynthesiserSound*) override;
  97. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int pitchWheel) override;
  98. void stopNote (float velocity, bool allowTailOff) override;
  99. void pitchWheelMoved (int newValue) override;
  100. void controllerMoved (int controllerNumber, int newValue) override;
  101. void renderNextBlock (AudioSampleBuffer&, int startSample, int numSamples) override;
  102. private:
  103. //==============================================================================
  104. double pitchRatio;
  105. double sourceSamplePosition;
  106. float lgain, rgain, attackReleaseLevel, attackDelta, releaseDelta;
  107. bool isInAttack, isInRelease;
  108. JUCE_LEAK_DETECTOR (SamplerVoice)
  109. };
  110. } // namespace juce