-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNote.m
executable file
·399 lines (376 loc) · 12.6 KB
/
Note.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
// Note.m
// Music Editor
//
// Created by Konstantine Prevas on 5/7/06.
// Copyright 2006 Konstantine Prevas. All rights reserved.
//
#import "Note.h"
#import "KeySignature.h"
//#import "DrumKit.h"
@class NoteDraw;
@implementation Note
- (id)initWithPitch:(int)_pitch octave:(int)_octave duration:(int)_duration dotted:(BOOL)_dotted accidental:(int)_accidental onStaff:(Staff *)_staff {
if(self = [super init]){
lastPitch = pitch = _pitch;
lastOctave = octave = _octave;
duration = _duration;
dotted = _dotted;
accidental = _accidental;
staff = _staff;
tieTo = nil;
tieFrom = nil;
}
return self;
}
//- (int)getPitch{
// return pitch;
//}
//- (int)getOctave{
// return octave;
//}
//- (int)getAccidental{
// return accidental;
//}
//- (int)getLastPitch{
// return lastPitch;
//}
//- (int)getLastOctave{
// return lastOctave;
//}
//
//- (void)setOctave:(int)_octave finished:(BOOL)finished{
// if(finished){
// [[[self undoManager] prepareWithInvocationTarget:self] setOctave:lastOctave finished:YES];
// if(lastOctave != _octave){
// [[self getTieFrom] tieTo:nil];
// [self tieFrom:nil];
// [[self getTieTo] tieFrom:nil];
// [self tieTo:nil];
// }
// lastOctave = _octave;
// }
// octave = _octave;
// [self sendChangeNotification];
//}
//- (void)setPitch:(int)_pitch finished:(BOOL)finished{
// if(finished){
// [[[self undoManager] prepareWithInvocationTarget:self] setPitch:lastPitch finished:YES];
// if(lastPitch != _pitch){
// [[self getTieFrom] tieTo:nil];
// [self tieFrom:nil];
// [[self getTieTo] tieFrom:nil];
// [self tieTo:nil];
// }
// lastPitch = _pitch;
// }
// pitch = _pitch;
// [self sendChangeNotification];
//}
//- (void)setPitch:(int)_pitch octave:(int)_octave finished:(BOOL)finished{
// [self setPitch:_pitch finished:finished];
// [self setOctave:_octave finished:finished];
//}
//
//- (void)setAccidental:(int)_accidental{
// [[[self undoManager] prepareWithInvocationTarget:self] setAccidental:accidental];
// accidental = _accidental;
// [self sendChangeNotification];
//}
- (id)copyWithZone:(NSZone *)zone {
return [[Note allocWithZone:zone] initWithPitch:pitch
octave:octave duration:duration dotted:dotted
accidental:accidental onStaff:staff];
}
//- (BOOL)isEqualTo:(id)obj{
// return [obj isKindOfClass:[Note class]] &&
// [obj getPitch] == pitch && [obj getOctave] == octave &&
// [obj getDuration] == duration && [obj getDotted] == dotted &&
// [obj getAccidental] == accidental;
//}
//
//- (int)getEffectiveAccidentalWithKeySignature:(KeySignature *)keySig priorAccidentals:(NSMutableDictionary *)accidentals{
// int effAccidental = accidental;
// if(accidentals != nil){
// if(effAccidental == NO_ACC){
// NSNumber *effAccGet = [accidentals objectForKey:[NSNumber numberWithInt:(octave * 7 + pitch)]];
// if(effAccGet != nil){
// effAccidental = [effAccGet intValue];
// }
// } else{
// [accidentals setObject:[NSNumber numberWithInt:accidental] forKey:[NSNumber numberWithInt:(octave * 7 + pitch)]];
// }
// }
// if(effAccidental == NO_ACC){
// return [keySig getAccidentalAtPosition:pitch];
// }
// return effAccidental;
//}
- (int)getEffectivePitchWithKeySignature:(KeySignature *)keySig priorAccidentals:(NSMutableDictionary *)accidentals {
int effPitch = octave * 12 + [keySig getPitchAtPosition:pitch];
int effAccidental = accidental;
if(accidentals != nil){
if(effAccidental == NO_ACC){
NSNumber *effAccGet = [accidentals objectForKey:[NSNumber numberWithInt:(octave * 7 + pitch)]];
if(effAccGet != nil){
effAccidental = [effAccGet intValue];
}
} else{
[accidentals setObject:[NSNumber numberWithInt:accidental] forKey:[NSNumber numberWithInt:(octave * 7 + pitch)]];
}
}
if(effAccidental != NO_ACC){
int keySigAcc = [keySig getAccidentalAtPosition:pitch];
if(keySigAcc != NO_ACC){
effAccidental -= keySigAcc;
}
effPitch += effAccidental;
}
return effPitch;
}
- (BOOL)pitchMatches:(Note *)note{
return [note getEffectivePitchWithKeySignature:[[[note getStaff] getMeasureContainingNote:note] getEffectiveKeySignature] priorAccidentals:nil] == [self getEffectivePitchWithKeySignature:[[[self getStaff] getMeasureContainingNote:self] getEffectiveKeySignature] priorAccidentals:nil];
}
//- (BOOL)isHigherThan:(Note *)note{
// return [note getEffectivePitchWithKeySignature:[[[note getStaff] getMeasureContainingNote:note] getEffectiveKeySignature] priorAccidentals:nil] < [self getEffectivePitchWithKeySignature:[[[self getStaff] getMeasureContainingNote:self] getEffectiveKeySignature] priorAccidentals:nil];
//}
//
//- (BOOL)isLowerThan:(Note *)note{
// return [note getEffectivePitchWithKeySignature:[[[note getStaff] getMeasureContainingNote:note] getEffectiveKeySignature] priorAccidentals:nil] > [self getEffectivePitchWithKeySignature:[[[self getStaff] getMeasureContainingNote:self] getEffectiveKeySignature] priorAccidentals:nil];
//}
//
//- (BOOL)isDrawBars{
// return [self getDuration] > 6;
//}
//
//- (float)addToMIDITrack:(MusicTrack *)musicTrack atPosition:(float)pos withKeySignature:(KeySignature *)keySig
// accidentals:(NSMutableDictionary *)accidentals transpose:(int)transposition onChannel:(int)channel{
// if(tieFrom != nil) return 4.0 * [self getEffectiveDuration] / 3;
// MIDINoteMessage note;
// note.channel = channel;
// note.velocity = 100.00;
// note.duration = 4.0 * [self getEffectiveDuration] / 3;
// Note *tie = tieTo;
// while(tie != nil){
// note.duration += 4.0 * [tie getEffectiveDuration] / 3;
// tie = [tie getTieTo];
// }
// note.note = [self getEffectivePitchWithKeySignature:keySig priorAccidentals:accidentals] + transposition;
// if (MusicTrackNewMIDINoteEvent(*musicTrack, pos, ¬e) != noErr) {
// NSLog(@"Cannot add note to track.");
// }
// return 4.0 * [self getEffectiveDuration] / 3;
//}
- (void)tieTo:(NoteBase *)note {
[[[self undoManager] prepareWithInvocationTarget:self] tieTo:tieTo];
if(![tieTo isEqual:note]){
[tieTo release];
tieTo = [note retain];
}
}
- (NoteBase *)getTieTo{
return tieTo;
}
- (void)tieFrom:(NoteBase *)note {
[[[self undoManager] prepareWithInvocationTarget:self] tieFrom:tieFrom];
if(![tieFrom isEqual:note]){
[tieFrom release];
tieFrom = [note retain];
}
}
//- (NoteBase *)getTieFrom{
// return tieFrom;
//}
//
//- (void)transposeBy:(int)numLines{
// int newPitch = pitch;
// int newOctave = octave;
// newPitch += numLines;
// while(newPitch >= 7){
// newPitch -= 7;
// newOctave++;
// }
// while(newPitch < 0){
// newPitch += 7;
// newOctave--;
// }
// [self setPitch:newPitch finished:YES];
// [self setOctave:newOctave finished:YES];
//}
//
//- (void)transposeBy:(int)numHalfSteps oldSignature:(KeySignature *)oldSig newSignature:(KeySignature *)newSig{
// int effectivePitch = [self getEffectivePitchWithKeySignature:oldSig priorAccidentals:nil];
// effectivePitch += numHalfSteps;
// int newOctave = effectivePitch / 12;
// effectivePitch -= newOctave * 12;
// int newPitch = [newSig positionForPitch:effectivePitch preferAccidental:accidental];
// if(effectivePitch == 11 && newPitch == 0) {
// newOctave++;
// }
// int newAccidental = [newSig accidentalForPitch:effectivePitch atPosition:newPitch];
// [self setOctave:newOctave finished:YES];
// [self setPitch:newPitch finished:YES];
// [self setAccidental:newAccidental];
//}
- (void)prepareForDelete{
[[self getTieTo] tieFrom:[self getTieFrom]];
[[self getTieFrom] tieTo:[self getTieTo]];
}
//- (void)collapseOnTo:(Note *)note{
// float effDuration = [self getEffectiveDuration];
// float targetDuration = [note getEffectiveDuration];
// float totalDuration = effDuration + targetDuration;
// //TODO: implement
//}
//
//+ (NSPoint)closestNoteTo:(NSPoint)base withRank:(int)rank{
// int pitch = (rank + 5) % 7;
// int octave = base.y;
// if(base.x > pitch && base.x - pitch > (pitch + 7) - base.x){
// octave++;
// }
// if(pitch > base.x && pitch - base.x > (base.x + 7) - pitch){
// octave--;
// }
// return NSMakePoint(pitch, octave);
//}
//
//- (NSPoint)closestNoteAtRank:(int)rank{
// return [Note closestNoteTo:NSMakePoint(pitch, octave) withRank:rank];
//}
//
//+ (NSPoint)noteAtRank:(int)rank onClef:(Clef *)clef{
// int pitch = [clef getPitchForPosition:4];
// int octave = [clef getOctaveForPosition:4];
// return [Note closestNoteTo:NSMakePoint(pitch, octave) withRank:rank];
//}
//
//- (int)getAbsoluteAccidentalWithPriorAccidentals:(NSMutableDictionary *)accidentals{
// if(accidental == NO_ACC){
// KeySignature *keySig = [[staff getMeasureContainingNote:self] getEffectiveKeySignature];
// return [self getEffectiveAccidentalWithKeySignature:keySig priorAccidentals:accidentals];
// } else {
// return accidental;
// }
//}
//
//- (char)getPitchLetter{
// return ('a' + ((pitch + 2) % 7));
//}
//
//- (void)addRegularPitchToLilypondString:(NSMutableString *)string accidentals:(NSMutableDictionary *)accidentals{
// NSString *accStr;
// int acc = [self getAbsoluteAccidentalWithPriorAccidentals:accidentals];
// if(acc == FLAT){
// accStr = @"es";
// } else if(acc == SHARP){
// accStr = @"is";
// } else {
// accStr = @"";
// }
// NSString *pitchStr = [NSString stringWithFormat:@"%c%@", [self getPitchLetter], accStr];
// NSMutableString *octaveStr = [NSMutableString string];
// int i;
// for(i = octave; i > 4; i--){
// [octaveStr appendString:@"'"];
// }
// for(i = octave; i < 4; i++){
// [octaveStr appendString:@","];
// }
// [string appendFormat:@"%@%@", pitchStr, octaveStr];
//}
//
//- (void)addDrumPitchToLilypondString:(NSMutableString *)string{
// Clef *clef = [[staff getMeasureContainingNote:self] getEffectiveClef];
// [string appendString:[clef lilypondStringForPitch:[self getPitch] octave:[self getOctave]]];
//}
//
//- (void)addPitchToLilypondString:(NSMutableString *)string accidentals:(NSMutableDictionary *)accidentals{
// if([staff isDrums]){
// [self addDrumPitchToLilypondString:string];
// } else{
// [self addRegularPitchToLilypondString:string accidentals:accidentals];
// }
//}
//
//- (void)addNoteToLilypondString:(NSMutableString *)string accidentals:(NSMutableDictionary *)accidentals{
// [self addPitchToLilypondString:string accidentals:accidentals];
// [self addDurationToLilypondString:string];
// [string appendString:@" "];
//}
//
//- (void)addToMusicXMLString:(NSMutableString *)string accidentals:(NSMutableDictionary *)accidentals{
// [self addToMusicXMLString:string accidentals:accidentals chord:NO];
//}
//
//- (void)addDrumPitchToMusicXMLString:(NSMutableString *)string{
// Clef *clef = [[staff getMeasureContainingNote:self] getEffectiveClef];
// [string appendString:[clef musicXMLStringForPitch:[self getPitch] octave:[self getOctave]]];
//}
//
//- (void)addToMusicXMLString:(NSMutableString *)string accidentals:(NSMutableDictionary *)accidentals chord:(BOOL)chord{
// [string appendString:@"<note>\n"];
// if(chord){
// [string appendString:@"<chord/>\n"];
// }
// if([staff isDrums]){
// [self addDrumPitchToMusicXMLString:string];
// } else {
// [string appendString:@"<pitch>\n"];
// [string appendFormat:@"<step>%c</step>\n", [self getPitchLetter]];
// int alter = [self getAbsoluteAccidentalWithPriorAccidentals:accidentals];
// if(alter != NO_ACC){
// [string appendFormat:@"<alter>%d</alter>\n", alter];
// }
// [string appendFormat:@"<octave>%d</octave>\n", [self getOctave]];
// [string appendString:@"</pitch>\n"];
// }
// [self addDurationToMusicXMLString:string];
// if([self getTieFrom] != nil){
// [string appendString:@"<tie type=\"stop\"/>\n"];
// }
// if([self getTieTo] != nil){
// [string appendString:@"<tie type=\"start\"/>\n"];
// }
// [string appendString:@"<notations>\n"];
// if([self getTieFrom] != nil){
// [string appendString:@"<tied type=\"stop\"/>\n"];
// }
// if([self getTieTo] != nil){
// [string appendString:@"<tied type=\"start\"/>\n"];
// }
// [string appendString:@"</notations>\n"];
// [string appendString:@"</note>\n"];
//}
//
//- (void)encodeWithCoder:(NSCoder *)coder{
// [coder encodeInt:duration forKey:@"duration"];
// [coder encodeBool:dotted forKey:@"dotted"];
// [coder encodeInt:octave forKey:@"octave"];
// [coder encodeInt:pitch forKey:@"pitch"];
// [coder encodeInt:accidental forKey:@"accidental"];
// [coder encodeObject:tieTo forKey:@"tieTo"];
// [coder encodeObject:tieFrom forKey:@"tieFrom"];
//}
//
//- (id)initWithCoder:(NSCoder *)coder{
// if(self = [super init]){
// duration = [coder decodeIntForKey:@"duration"];
// dotted = [coder decodeBoolForKey:@"dotted"];
// octave = [coder decodeIntForKey:@"octave"];
// pitch = [coder decodeIntForKey:@"pitch"];
// accidental = [coder decodeIntForKey:@"accidental"];
// [self tieTo:[coder decodeObjectForKey:@"tieTo"]];
// [self tieFrom:[coder decodeObjectForKey:@"tieFrom"]];
// }
// return self;
//}
- (void)dealloc {
tieTo = nil;
tieFrom = nil;
[super dealloc];
}
//- (Class)getViewClass{
// return [NoteDraw class];
//}
@end