1 /* 2 Copyright (C) 2016 Apple Inc. All Rights Reserved. 3 See LICENSE.txt for this sample’s licensing information 4 5 Abstract: 6 Part of Core Audio AUInstrument Base Classes 7 */ 8 9 /* You can either fill in code here or remove this and create or add new files. */ 10 11 #ifndef __SynthEvent__ 12 #define __SynthEvent__ 13 14 #include <AudioUnit/AudioUnit.h> 15 #include <CoreAudio/CoreAudio.h> 16 #include "MusicDeviceBase.h" 17 #include <stdexcept> 18 19 //////////////////////////////////////////////////////////////////////////////////////////////////////////// 20 21 22 class SynthEvent 23 { 24 public: 25 enum { 26 kEventType_NoteOn = 1, 27 kEventType_NoteOff = 2, 28 kEventType_SustainOn = 3, 29 kEventType_SustainOff = 4, 30 kEventType_SostenutoOn = 5, 31 kEventType_SostenutoOff = 6, 32 kEventType_AllNotesOff = 7, 33 kEventType_AllSoundOff = 8, 34 kEventType_ResetAllControllers = 9 35 }; 36 37 SynthEvent()38 SynthEvent() {} ~SynthEvent()39 ~SynthEvent() {} 40 Set(UInt32 inEventType,MusicDeviceGroupID inGroupID,NoteInstanceID inNoteID,UInt32 inOffsetSampleFrame,const MusicDeviceNoteParams * inNoteParams)41 void Set( 42 UInt32 inEventType, 43 MusicDeviceGroupID inGroupID, 44 NoteInstanceID inNoteID, 45 UInt32 inOffsetSampleFrame, 46 const MusicDeviceNoteParams* inNoteParams 47 ) 48 { 49 mEventType = inEventType; 50 mGroupID = inGroupID; 51 mNoteID = inNoteID; 52 mOffsetSampleFrame = inOffsetSampleFrame; 53 54 if (inNoteParams) 55 { 56 UInt32 paramSize = offsetof(MusicDeviceNoteParams, mControls) + (inNoteParams->argCount-2)*sizeof(NoteParamsControlValue); 57 mNoteParams = inNoteParams->argCount > 3 58 ? (MusicDeviceNoteParams*)malloc(paramSize) 59 : &mSmallNoteParams; 60 memcpy(mNoteParams, inNoteParams, paramSize); 61 } 62 else 63 mNoteParams = NULL; 64 } 65 66 Free()67 void Free() 68 { 69 if (mNoteParams) 70 { 71 if (mNoteParams->argCount > 3) 72 free(mNoteParams); 73 mNoteParams = NULL; 74 } 75 } 76 GetEventType()77 UInt32 GetEventType() const { return mEventType; } GetGroupID()78 MusicDeviceGroupID GetGroupID() const { return mGroupID; } GetNoteID()79 NoteInstanceID GetNoteID() const { return mNoteID; } GetOffsetSampleFrame()80 UInt32 GetOffsetSampleFrame() const { return mOffsetSampleFrame; } 81 GetParams()82 MusicDeviceNoteParams* GetParams() const { return mNoteParams; } 83 GetArgCount()84 UInt32 GetArgCount() const { return mNoteParams->argCount; } NumberParameters()85 UInt32 NumberParameters() const { return mNoteParams->argCount - 2; } 86 GetNote()87 Float32 GetNote() const { return mNoteParams->mPitch; } GetVelocity()88 Float32 GetVelocity() const { return mNoteParams->mVelocity; } 89 GetParameter(UInt32 inIndex)90 NoteParamsControlValue GetParameter(UInt32 inIndex) const 91 { 92 if (inIndex >= NumberParameters()) 93 throw std::runtime_error("index out of range"); 94 return mNoteParams->mControls[inIndex]; 95 } 96 97 private: 98 UInt32 mEventType; 99 MusicDeviceGroupID mGroupID; 100 NoteInstanceID mNoteID; 101 UInt32 mOffsetSampleFrame; 102 MusicDeviceNoteParams* mNoteParams; 103 MusicDeviceNoteParams mSmallNoteParams; // inline a small one to eliminate malloc for the simple case. 104 }; 105 106 //////////////////////////////////////////////////////////////////////////////////////////////////////////// 107 #endif 108