Jamoma API  0.6.0.a19
SynthEvent.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 /* You can either fill in code here or remove this and create or add new files. */
42 
43 #ifndef __SynthEvent__
44 #define __SynthEvent__
45 
46 #include <Carbon/Carbon.h>
47 #include <AudioUnit/AudioUnit.h>
48 #include <CoreAudio/CoreAudio.h>
49 #include "MusicDeviceBase.h"
50 #include <stdexcept>
51 
52 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 
54 
55 class SynthEvent
56 {
57 public:
58  enum {
59  kEventType_NoteOn = 1,
60  kEventType_NoteOff = 2,
61  kEventType_SustainOn = 3,
62  kEventType_SustainOff = 4,
63  kEventType_SostenutoOn = 5,
64  kEventType_SostenutoOff = 6,
65  kEventType_AllNotesOff = 7,
66  kEventType_AllSoundOff = 8,
67  kEventType_ResetAllControllers = 9
68  };
69 
70 
71  SynthEvent() {}
72  ~SynthEvent() {}
73 
74  void Set(
75  UInt32 inEventType,
76  MusicDeviceGroupID inGroupID,
77  NoteInstanceID inNoteID,
78  UInt32 inOffsetSampleFrame,
79  const MusicDeviceNoteParams* inNoteParams
80  )
81  {
82  mEventType = inEventType;
83  mGroupID = inGroupID;
84  mNoteID = inNoteID;
85  mOffsetSampleFrame = inOffsetSampleFrame;
86 
87  if (inNoteParams)
88  {
89  UInt32 paramSize = offsetof(MusicDeviceNoteParams, mControls) + (inNoteParams->argCount-2)*sizeof(NoteParamsControlValue);
90  mNoteParams = inNoteParams->argCount > 3
91  ? (MusicDeviceNoteParams*)malloc(paramSize)
92  : &mSmallNoteParams;
93  memcpy(mNoteParams, inNoteParams, paramSize);
94  }
95  else
96  mNoteParams = NULL;
97  }
98 
99 
100  void Free()
101  {
102  if (mNoteParams)
103  {
104  if (mNoteParams->argCount > 3)
105  free(mNoteParams);
106  mNoteParams = NULL;
107  }
108  }
109 
110  UInt32 GetEventType() const { return mEventType; }
111  MusicDeviceGroupID GetGroupID() const { return mGroupID; }
112  NoteInstanceID GetNoteID() const { return mNoteID; }
113  UInt32 GetOffsetSampleFrame() const { return mOffsetSampleFrame; }
114 
115  MusicDeviceNoteParams* GetParams() const { return mNoteParams; }
116 
117  UInt32 GetArgCount() const { return mNoteParams->argCount; }
118  UInt32 NumberParameters() const { return mNoteParams->argCount - 2; }
119 
120  Float32 GetNote() const { return mNoteParams->mPitch; }
121  Float32 GetVelocity() const { return mNoteParams->mVelocity; }
122 
123  NoteParamsControlValue GetParameter(UInt32 inIndex) const
124  {
125  if (inIndex >= NumberParameters())
126  throw std::runtime_error("index out of range");
127  return mNoteParams->mControls[inIndex];
128  }
129 
130 private:
131  UInt32 mEventType;
132  MusicDeviceGroupID mGroupID;
133  NoteInstanceID mNoteID;
134  UInt32 mOffsetSampleFrame;
135  MusicDeviceNoteParams* mNoteParams;
136  MusicDeviceNoteParams mSmallNoteParams; // inline a small one to eliminate malloc for the simple case.
137 };
138 
139 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 #endif