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