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.

145 lines
5.3KB

  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. #pragma once
  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 noexcept { return name; }
  61. /** Returns the audio sample data.
  62. This could return nullptr if there was a problem loading the data.
  63. */
  64. AudioSampleBuffer* getAudioData() const noexcept { return data; }
  65. //==============================================================================
  66. bool appliesToNote (int midiNoteNumber) override;
  67. bool appliesToChannel (int midiChannel) override;
  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. SamplerVoice();
  92. /** Destructor. */
  93. ~SamplerVoice();
  94. //==============================================================================
  95. bool canPlaySound (SynthesiserSound*) override;
  96. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int pitchWheel) override;
  97. void stopNote (float velocity, bool allowTailOff) override;
  98. void pitchWheelMoved (int newValue) override;
  99. void controllerMoved (int controllerNumber, int newValue) override;
  100. void renderNextBlock (AudioSampleBuffer&, int startSample, int numSamples) override;
  101. private:
  102. //==============================================================================
  103. double pitchRatio;
  104. double sourceSamplePosition;
  105. float lgain, rgain, attackReleaseLevel, attackDelta, releaseDelta;
  106. bool isInAttack, isInRelease;
  107. JUCE_LEAK_DETECTOR (SamplerVoice)
  108. };