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.

155 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SAMPLER_JUCEHEADER__
  19. #define __JUCE_SAMPLER_JUCEHEADER__
  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. */
  29. class JUCE_API SamplerSound : public SynthesiserSound
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a sampled sound from an audio reader.
  34. This will attempt to load the audio from the source into memory and store
  35. it in this object.
  36. @param name a name for the sample
  37. @param source the audio to load. This object can be safely deleted by the
  38. caller after this constructor returns
  39. @param midiNotes the set of midi keys that this sound should be played on. This
  40. is used by the SynthesiserSound::appliesToNote() method
  41. @param midiNoteForNormalPitch the midi note at which the sample should be played
  42. with its natural rate. All other notes will be pitched
  43. up or down relative to this one
  44. @param attackTimeSecs the attack (fade-in) time, in seconds
  45. @param releaseTimeSecs the decay (fade-out) time, in seconds
  46. @param maxSampleLengthSeconds a maximum length of audio to read from the audio
  47. source, in seconds
  48. */
  49. SamplerSound (const String& name,
  50. AudioFormatReader& source,
  51. const BigInteger& midiNotes,
  52. int midiNoteForNormalPitch,
  53. double attackTimeSecs,
  54. double releaseTimeSecs,
  55. double maxSampleLengthSeconds);
  56. /** Destructor. */
  57. ~SamplerSound();
  58. //==============================================================================
  59. /** Returns the sample's name */
  60. const String& getName() const { return name; }
  61. /** Returns the audio sample data.
  62. This could be 0 if there was a problem loading it.
  63. */
  64. AudioSampleBuffer* getAudioData() const { return data; }
  65. //==============================================================================
  66. bool appliesToNote (const int midiNoteNumber);
  67. bool appliesToChannel (const int midiChannel);
  68. private:
  69. //==============================================================================
  70. friend class SamplerVoice;
  71. String name;
  72. ScopedPointer <AudioSampleBuffer> data;
  73. double sourceSampleRate;
  74. BigInteger midiNotes;
  75. int length, attackSamples, releaseSamples;
  76. int midiRootNote;
  77. JUCE_LEAK_DETECTOR (SamplerSound);
  78. };
  79. //==============================================================================
  80. /**
  81. A subclass of SynthesiserVoice that can play a SamplerSound.
  82. To use it, create a Synthesiser, add some SamplerVoice objects to it, then
  83. give it some SampledSound objects to play.
  84. @see SamplerSound, Synthesiser, SynthesiserVoice
  85. */
  86. class JUCE_API SamplerVoice : public SynthesiserVoice
  87. {
  88. public:
  89. //==============================================================================
  90. /** Creates a SamplerVoice.
  91. */
  92. SamplerVoice();
  93. /** Destructor. */
  94. ~SamplerVoice();
  95. //==============================================================================
  96. bool canPlaySound (SynthesiserSound* sound);
  97. void startNote (const int midiNoteNumber,
  98. const float velocity,
  99. SynthesiserSound* sound,
  100. const int currentPitchWheelPosition);
  101. void stopNote (const bool allowTailOff);
  102. void pitchWheelMoved (const int newValue);
  103. void controllerMoved (const int controllerNumber,
  104. const int newValue);
  105. void renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples);
  106. private:
  107. //==============================================================================
  108. double pitchRatio;
  109. double sourceSamplePosition;
  110. float lgain, rgain, attackReleaseLevel, attackDelta, releaseDelta;
  111. bool isInAttack, isInRelease;
  112. JUCE_LEAK_DETECTOR (SamplerVoice);
  113. };
  114. #endif // __JUCE_SAMPLER_JUCEHEADER__