Jamoma API  0.6.0.a19
SynthNote.h
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #ifndef __SynthNote__
42 #define __SynthNote__
43 
44 #include <Carbon/Carbon.h>
45 #include <AudioUnit/AudioUnit.h>
46 #include <CoreAudio/CoreAudio.h>
47 #include "MusicDeviceBase.h"
48 
49 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50 
51 enum {
52  kNoteState_Attacked = 0,
53  kNoteState_Sostenutoed = 1,
54  kNoteState_ReleasedButSostenutoed = 2,
55  kNoteState_ReleasedButSustained = 3,
56  kNoteState_Released = 4,
57  kNoteState_FastReleased = 5,
58  kNoteState_Free = 6,
59  kNumberOfActiveNoteStates = 5,
60  kNumberOfSoundingNoteStates = 6,
61  kNumberOfNoteStates = 7
62 };
63 
64 /*
65  This table describes the state transitions for SynthNotes
66 
67  EVENT CURRENT STATE NEW STATE
68  note on free attacked
69  note off attacked (and sustain on) released but sustained
70  note off attacked released
71  note off sostenutoed released but sostenutoed
72  sustain on -- no changes --
73  sustain off released but sustained released
74  sostenuto on attacked sostenutoed
75  sostenuto off sostenutoed attacked
76  sostenuto off released but sostenutoed (and sustain on) released but sustained
77  sostenuto off released but sostenutoed released
78  end of note any state free
79  soft voice stealing any state fast released
80  hard voice stealing any state free
81 
82  soft voice stealing happens when there is a note on event and NumActiveNotes > MaxActiveNotes
83  hard voice stealing happens when there is a note on event and NumActiveNotes == NumNotes (no free notes)
84  voice stealing removes the quietest note in the highest numbered state that has sounding notes.
85 */
86 
87 class SynthGroupElement;
88 class SynthPartElement;
89 class AUInstrumentBase;
90 
91 struct SynthNote
92 {
93  SynthNote() :
94  mPrev(0), mNext(0), mState(kNoteState_Free),
95  mRelativeStartFrame(0),
96  mRelativeReleaseFrame(-1),
97  mRelativeKillFrame(-1)
98  {
99  }
100 
101  virtual ~SynthNote() {}
102 
103  virtual void Reset();
104  virtual void AttackNote(
105  SynthPartElement * inPart,
106  SynthGroupElement * inGroup,
107  NoteInstanceID inNoteID,
108  SInt64 inAbsoluteSampleFrame,
109  UInt32 inOffsetSampleFrame,
110  const MusicDeviceNoteParams &inParams
111  );
112 
113  virtual OSStatus Render(UInt32 inNumFrames, AudioBufferList& inBufferList)=0;
114  virtual void Attack(const MusicDeviceNoteParams &inParams) = 0;
115  virtual void Kill(UInt32 inFrame); // voice is being stolen.
116  virtual void Release(UInt32 inFrame);
117  virtual void FastRelease(UInt32 inFrame);
118  virtual Float32 Amplitude() = 0; // used for finding quietest note for voice stealing.
119 
120  virtual void NoteEnded(UInt32 inFrame);
121 
122  SynthGroupElement* GetGroup() const { return mGroup; }
123  SynthPartElement* GetPart() const { return mPart; }
124 
125  AUInstrumentBase* GetAudioUnit() const;
126 
127  Float32 GetGlobalParameter(AudioUnitParameterID inParamID) const;
128 
129  NoteInstanceID GetNoteID() const { return mNoteID; }
130  UInt32 GetState() const { return mState; }
131  Boolean IsSounding() const { return mState < kNumberOfSoundingNoteStates; }
132  Boolean IsActive() const { return mState < kNumberOfActiveNoteStates; }
133  SInt64 GetAbsoluteStartFrame() const { return mAbsoluteStartFrame; }
134  SInt32 GetRelativeStartFrame() const { return mRelativeStartFrame; }
135  SInt32 GetRelativeReleaseFrame() const { return mRelativeReleaseFrame; }
136  SInt32 GetRelativeKillFrame() const { return mRelativeKillFrame; }
137 
138  void ListRemove() { mPrev = mNext = 0; } // only use when lists will be reset.
139 
140  float PitchBend() const;
141  double TuningA() const;
142 
143  virtual double Frequency(); // returns the frequency of note + pitch bend.
144  double SampleRate();
145 
146 //private:
147 // friend class NoteList;
148 
149  // linked list pointers
150  SynthNote *mPrev;
151  SynthNote *mNext;
152 
153  SynthPartElement* mPart;
154  SynthGroupElement* mGroup;
155 
156  NoteInstanceID mNoteID;
157  UInt32 mState;
158  SInt64 mAbsoluteStartFrame;
159  SInt32 mRelativeStartFrame;
160  SInt32 mRelativeReleaseFrame;
161  SInt32 mRelativeKillFrame;
162 
163  Float32 mPitch;
164  Float32 mVelocity;
165 };
166 
167 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
168 
169 #endif
170