1 /*
2      File: SynthNote.cpp
3  Abstract: SynthNote.h
4   Version: 1.1
5 
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12 
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28 
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34 
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43 
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45 
46 */
47 #include "SynthNote.h"
48 #include "SynthElement.h"
49 #include "AUInstrumentBase.h"
50 
AttackNote(SynthPartElement * inPart,SynthGroupElement * inGroup,NoteInstanceID inNoteID,UInt64 inAbsoluteSampleFrame,UInt32 inOffsetSampleFrame,const MusicDeviceNoteParams & inParams)51 bool SynthNote::AttackNote(
52 			SynthPartElement *				inPart,
53 			SynthGroupElement *				inGroup,
54 			NoteInstanceID					inNoteID,
55 			UInt64							inAbsoluteSampleFrame,
56 			UInt32							inOffsetSampleFrame,
57 			const MusicDeviceNoteParams		&inParams)
58 {
59 #if DEBUG_PRINT
60 	printf("SynthNote::AttackNote %lu %lu abs frame %llu rel frame %lu\n", (UInt32)inGroup->GroupID(), (UInt32)inNoteID, inAbsoluteSampleFrame, inOffsetSampleFrame);
61 #endif
62 	mPart = inPart;
63 	mGroup = inGroup;
64 	mNoteID = inNoteID;
65 
66 	mAbsoluteStartFrame = inAbsoluteSampleFrame;
67 	mRelativeStartFrame = inOffsetSampleFrame;
68 	mRelativeReleaseFrame = -1;
69 	mRelativeKillFrame = -1;
70 
71 	mPitch = inParams.mPitch;
72 	mVelocity = inParams.mVelocity;
73 
74 
75 	return Attack(inParams);
76 }
77 
78 
Reset()79 void SynthNote::Reset()
80 {
81 	mPart = 0;
82 	mGroup = 0;
83 	mAbsoluteStartFrame = 0;
84 	mRelativeStartFrame = 0;
85 	mRelativeReleaseFrame = 0;
86 	mRelativeKillFrame = 0;
87 }
88 
Kill(UInt32 inFrame)89 void SynthNote::Kill(UInt32 inFrame)
90 {
91 	mRelativeKillFrame = inFrame;
92 }
93 
Release(UInt32 inFrame)94 void SynthNote::Release(UInt32 inFrame)
95 {
96 	mRelativeReleaseFrame = inFrame;
97 }
98 
FastRelease(UInt32 inFrame)99 void SynthNote::FastRelease(UInt32 inFrame)
100 {
101 	mRelativeReleaseFrame = inFrame;
102 }
103 
TuningA() const104 double SynthNote::TuningA() const
105 {
106 	return 440.0;
107 }
108 
Frequency()109 double SynthNote::Frequency()
110 {
111 	return TuningA() * pow(2., (mPitch - 69. + GetPitchBend()) / 12.);
112 }
113 
SampleRate()114 double SynthNote::SampleRate()
115 {
116 	return GetAudioUnit()->GetOutput(0)->GetStreamFormat().mSampleRate;
117 }
118 
GetAudioUnit() const119 AUInstrumentBase* SynthNote::GetAudioUnit() const
120 {
121 	return (AUInstrumentBase*)mGroup->GetAudioUnit();
122 }
123 
GetGlobalParameter(AudioUnitParameterID inParamID) const124 Float32 SynthNote::GetGlobalParameter(AudioUnitParameterID inParamID) const
125 {
126 	return mGroup->GetAudioUnit()->Globals()->GetParameter(inParamID);
127 }
128 
NoteEnded(UInt32 inFrame)129 void SynthNote::NoteEnded(UInt32 inFrame)
130 {
131 	mGroup->NoteEnded(this, inFrame);
132 	mNoteID = 0xFFFFFFFF;
133 }
134 
GetPitchBend() const135 float SynthNote::GetPitchBend() const
136 {
137 	return mGroup->GetPitchBend();
138 }
139 
140 
141