-
Notifications
You must be signed in to change notification settings - Fork 588
/
MIKMIDINoteEvent.m
213 lines (170 loc) · 6.14 KB
/
MIKMIDINoteEvent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// MIKMIDIEventMIDINoteMessage.m
// MIDI Files Testbed
//
// Created by Jake Gundersen on 5/21/14.
// Copyright (c) 2014 Mixed In Key. All rights reserved.
//
#import "MIKMIDINoteEvent.h"
#import "MIKMIDIEvent_SubclassMethods.h"
#import "MIKMIDIUtilities.h"
#import "MIKMIDIClock.h"
#if !__has_feature(objc_arc)
#error MIKMIDINoteEvent.m must be compiled with ARC. Either turn on ARC for the project or set the -fobjc-arc flag for MIKMIDINoteEvent.m in the Build Phases for this target
#endif
@implementation MIKMIDINoteEvent
+ (void)load { [MIKMIDIEvent registerSubclass:self]; }
+ (NSArray *)supportedMIDIEventTypes { return @[@(MIKMIDIEventTypeMIDINoteMessage)]; }
+ (Class)immutableCounterpartClass { return [MIKMIDINoteEvent class]; }
+ (Class)mutableCounterpartClass { return [MIKMutableMIDINoteEvent class]; }
+ (BOOL)isMutable { return NO; }
+ (NSData *)initialData { return [NSData dataWithBytes:&(MIDINoteMessage){0} length:sizeof(MIDINoteMessage)]; }
#pragma mark - Lifecycle
+ (instancetype)noteEventWithTimeStamp:(MusicTimeStamp)timeStamp
note:(UInt8)note
velocity:(UInt8)velocity
duration:(Float32)duration
channel:(UInt8)channel
{
MIDINoteMessage message = {
.note = note,
.velocity = velocity,
.channel = channel,
.duration = duration,
.releaseVelocity = 0,
};
return [self noteEventWithTimeStamp:timeStamp message:message];
}
+ (instancetype)noteEventWithTimeStamp:(MusicTimeStamp)timeStamp message:(MIDINoteMessage)message
{
NSData *data = [NSData dataWithBytes:&message length:sizeof(message)];
return [self midiEventWithTimeStamp:timeStamp eventType:kMusicEventType_MIDINoteMessage data:data];
}
#pragma mark - Properties
+ (NSSet *)keyPathsForValuesAffectingInternalData
{
return [NSSet setWithObjects:@"note", @"channel", @"velocity", @"releaseVelocity", @"duration", nil];
}
+ (NSSet *)keyPathsForValuesAffectingEndTimeStamp
{
return [NSSet setWithObjects:@"timeStamp", @"duration", nil];
}
- (UInt8)note
{
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
return noteMessage->note;
}
- (void)setNote:(UInt8)note
{
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
noteMessage->note = note;
}
- (UInt8)channel
{
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
return noteMessage->channel;
}
- (void)setChannel:(UInt8)channel
{
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
noteMessage->channel = channel;
}
- (UInt8)velocity
{
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
return noteMessage->velocity;
}
- (void)setVelocity:(UInt8)velocity
{
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
noteMessage->velocity = velocity;
}
- (UInt8)releaseVelocity
{
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
return noteMessage->releaseVelocity;
}
- (void)setReleaseVelocity:(UInt8)releaseVelocity
{
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
noteMessage->releaseVelocity = releaseVelocity;
}
- (Float32)duration
{
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
return noteMessage->duration;
}
- (void)setDuration:(Float32)duration
{
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;
MIDINoteMessage *noteMessage = (MIDINoteMessage*)[self.internalData bytes];
noteMessage->duration = duration;
}
- (MusicTimeStamp)endTimeStamp
{
return self.timeStamp + self.duration;
}
- (float)frequency
{
//tuning based on A4 = 440 hz
float A = 440.0f;
return (A / 32.0f) * powf(2.0f, (((float)self.note - 9.0f) / 12.0f));
}
- (NSString *)noteLetter
{
return MIKMIDINoteLetterForMIDINoteNumber(self.note);
}
- (NSString *)noteLetterAndOctave
{
return MIKMIDINoteLetterAndOctaveForMIDINote(self.note);
}
- (NSString *)additionalEventDescription
{
return [NSString stringWithFormat:@"MIDINote: %d, Note: %@, channel %d, duration %f, velocity %d, frequency %f", self.note, self.noteLetter, self.channel, self.duration, self.velocity, self.frequency];
}
@end
@implementation MIKMutableMIDINoteEvent
@dynamic timeStamp;
@dynamic data;
@dynamic note;
@dynamic velocity;
@dynamic channel;
@dynamic releaseVelocity;
@dynamic duration;
+ (BOOL)isMutable { return YES; }
@end
#pragma mark -
@implementation MIKMIDICommand (MIKMIDINoteEventToCommands)
+ (NSArray *)commandsFromNoteEvent:(MIKMIDINoteEvent *)noteEvent clock:(MIKMIDIClock *)clock
{
if (!clock) {
clock = [MIKMIDIClock clock];
[clock syncMusicTimeStamp:noteEvent.timeStamp withMIDITimeStamp:MIKMIDIGetCurrentTimeStamp() tempo:120];
}
return @[[self noteOnCommandFromNoteEvent:noteEvent clock:clock], [self noteOffCommandFromNoteEvent:noteEvent clock:clock]];
}
+ (MIKMIDINoteOnCommand *)noteOnCommandFromNoteEvent:(MIKMIDINoteEvent *)noteEvent clock:(MIKMIDIClock *)clock
{
MIKMutableMIDINoteOnCommand *noteOn = [[MIKMutableMIDINoteOnCommand alloc] init];
MIDITimeStamp timestamp = clock ? [clock midiTimeStampForMusicTimeStamp:noteEvent.timeStamp] : MIKMIDIGetCurrentTimeStamp();
noteOn.midiTimestamp = timestamp;
noteOn.channel = noteEvent.channel;
noteOn.note = noteEvent.note;
noteOn.velocity = noteEvent.velocity;
return [noteOn copy];
}
+ (MIKMIDINoteOffCommand *)noteOffCommandFromNoteEvent:(MIKMIDINoteEvent *)noteEvent clock:(MIKMIDIClock *)clock
{
MIKMutableMIDINoteOffCommand *noteOff = [[MIKMutableMIDINoteOffCommand alloc] init];
MIDITimeStamp timestamp = clock ? [clock midiTimeStampForMusicTimeStamp:noteEvent.endTimeStamp] : MIKMIDIGetCurrentTimeStamp();
noteOff.midiTimestamp = timestamp;
noteOff.channel = noteEvent.channel;
noteOff.note = noteEvent.note;
noteOff.velocity = noteEvent.releaseVelocity;
return [noteOff copy];
}
@end