Jamoma API  0.6.0.a19
SynthNoteList.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 __SynthNoteList__
42 #define __SynthNoteList__
43 
44 #include "SynthNote.h"
45 
46 #if DEBUG
47 #ifndef DEBUG_PRINT
48  #define DEBUG_PRINT 0
49 #endif
50  #define SANITY_CHECK 0
51 #endif
52 
53 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
54 
55 struct SynthNoteList
56 {
57  SynthNoteList() : mState(0xFFFFFFFF), mHead(0), mTail(0) {}
58 
59  bool NotEmpty() const { return mHead != NULL; }
60  bool IsEmpty() const { return mHead == NULL; }
61  void Empty() {
62 #if SANITY_CHECK
63  SanityCheck();
64 #endif
65  mHead = mTail = NULL;
66  }
67 
68  UInt32 Length() const {
69 #if SANITY_CHECK
70  SanityCheck();
71 #endif
72  UInt32 length = 0;
73  for (SynthNote* note = mHead; note; note = note->mNext)
74  length++;
75  return length;
76  };
77 
78  void AddNote(SynthNote *inNote)
79  {
80 #if DEBUG_PRINT
81  printf("AddNote %d %08X\n", mState, inNote);
82 #endif
83 #if SANITY_CHECK
84  SanityCheck();
85 #endif
86  inNote->mState = mState;
87  inNote->mNext = mHead;
88  inNote->mPrev = NULL;
89 
90  if (mHead) { mHead->mPrev = inNote; mHead = inNote; }
91  else mHead = mTail = inNote;
92 #if SANITY_CHECK
93  SanityCheck();
94 #endif
95  }
96 
97  void RemoveNote(SynthNote *inNote)
98  {
99 #if DEBUG_PRINT
100  printf("RemoveNote\n");
101 #endif
102 #if SANITY_CHECK
103  SanityCheck();
104 #endif
105  if (inNote->mPrev) inNote->mPrev->mNext = inNote->mNext;
106  else mHead = inNote->mNext;
107 
108  if (inNote->mNext) inNote->mNext->mPrev = inNote->mPrev;
109  else mTail = inNote->mPrev;
110 
111  inNote->mPrev = 0;
112  inNote->mNext = 0;
113 #if SANITY_CHECK
114  SanityCheck();
115 #endif
116  }
117 
118  void TransferAllFrom(SynthNoteList *inNoteList, UInt32 inFrame)
119  {
120 #if DEBUG_PRINT
121  printf("TransferAllFrom\n");
122 #endif
123 #if SANITY_CHECK
124  SanityCheck();
125  inNoteList->SanityCheck();
126 #endif
127  if (!inNoteList->mTail) return;
128 
129  if (mState == kNoteState_Released)
130  {
131  for (SynthNote* note = inNoteList->mHead; note; note = note->mNext)
132  {
133 #if DEBUG_PRINT
134  printf("TransferAllFrom release %08X\n", note);
135 #endif
136  note->mState = mState;
137  note->Release(inFrame);
138  }
139  }
140  else
141  {
142  for (SynthNote* note = inNoteList->mHead; note; note = note->mNext)
143  {
144  note->mState = mState;
145  }
146  }
147 
148  inNoteList->mTail->mNext = mHead;
149 
150  if (mHead) mHead->mPrev = inNoteList->mTail;
151  else mTail = inNoteList->mTail;
152 
153  mHead = inNoteList->mHead;
154 
155  inNoteList->mHead = NULL;
156  inNoteList->mTail = NULL;
157 #if SANITY_CHECK
158  SanityCheck();
159  inNoteList->SanityCheck();
160 #endif
161  }
162 
163  SynthNote* FindOldestNote()
164  {
165 #if DEBUG_PRINT
166  printf("FindOldestNote\n");
167 #endif
168 #if SANITY_CHECK
169  SanityCheck();
170 #endif
171  SInt64 minStartFrame = -1;
172  SynthNote* oldestNote = NULL;
173  for (SynthNote* note = mHead; note; note = note->mNext)
174  {
175  if (note->mAbsoluteStartFrame < minStartFrame)
176  {
177  oldestNote = note;
178  minStartFrame = note->mAbsoluteStartFrame;
179  }
180  }
181  return oldestNote;
182  }
183 
184  SynthNote* FindMostQuietNote()
185  {
186 #if DEBUG_PRINT
187  printf("FindMostQuietNote\n");
188 #endif
189  Float32 minAmplitude = 1e9;
190  SInt64 minStartFrame = -1;
191  SynthNote* mostQuietNote = NULL;
192  for (SynthNote* note = mHead; note; note = note->mNext)
193  {
194  Float32 amp = note->Amplitude();
195 #if DEBUG_PRINT
196  printf(" amp %g minAmplitude %g\n", amp, minAmplitude);
197 #endif
198  if (amp < minAmplitude)
199  {
200  mostQuietNote = note;
201  minAmplitude = amp;
202  minStartFrame = note->mAbsoluteStartFrame;
203  }
204  else if (amp == minAmplitude && note->mAbsoluteStartFrame < minStartFrame)
205  {
206  // use earliest start time as a tie breaker
207  mostQuietNote = note;
208  minStartFrame = note->mAbsoluteStartFrame;
209  }
210  }
211 #if SANITY_CHECK
212  SanityCheck();
213 #endif
214  return mostQuietNote;
215  }
216 
217  void SanityCheck() const;
218 
219  UInt32 mState;
220  SynthNote *mHead;
221  SynthNote *mTail;
222 };
223 
224 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225 
226 #endif