Skip to content

Commit

Permalink
Issue #57: Fixed exception/crash when setting properties on an empty …
Browse files Browse the repository at this point in the history
…MIKMIDIMetaTimeSignatureEvent.
  • Loading branch information
armadsen committed Feb 25, 2015
1 parent c021588 commit 1d6c275
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Source/MIKMIDIMetaTimeSignatureEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ + (NSArray *)supportedMIDIEventTypes { return @[@(MIKMIDIEventTypeMetaTimeSignat
+ (Class)immutableCounterpartClass { return [MIKMIDIMetaTimeSignatureEvent class]; }
+ (Class)mutableCounterpartClass { return [MIKMutableMIDIMetaTimeSignatureEvent class]; }
+ (BOOL)isMutable { return NO; }
+ (size_t)minimumDataSize { return [super minimumDataSize] + 4; /* Account for numerator, denominator, metronome, and 32nd note bytes */ }

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
Expand All @@ -44,8 +45,9 @@ - (void)setNumerator:(UInt8)numerator
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;

NSMutableData *mutableMetaData = self.metaData.mutableCopy;
if ([mutableMetaData length] < 1) [mutableMetaData increaseLengthBy:1];
[mutableMetaData replaceBytesInRange:NSMakeRange(0, 1) withBytes:&numerator length:1];
[self setMetaData:[mutableMetaData copy]];
[self setMetaData:mutableMetaData];
}

- (UInt8)denominator
Expand All @@ -59,9 +61,10 @@ - (void)setDenominator:(UInt8)denominator
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;

NSMutableData *mutableMetaData = self.metaData.mutableCopy;
if ([mutableMetaData length] < 2) [mutableMetaData increaseLengthBy:2-[mutableMetaData length]];
UInt8 denominatorPower = log2(denominator);
[mutableMetaData replaceBytesInRange:NSMakeRange(1, 1) withBytes:&denominatorPower length:1];
[self setMetaData:[mutableMetaData copy]];
[self setMetaData:mutableMetaData];
}

- (UInt8)metronomePulse
Expand All @@ -74,8 +77,9 @@ - (void)setMetronomePulse:(UInt8)metronomePulse
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;

NSMutableData *mutableMetaData = self.metaData.mutableCopy;
if ([mutableMetaData length] < 3) [mutableMetaData increaseLengthBy:3-[mutableMetaData length]];
[mutableMetaData replaceBytesInRange:NSMakeRange(2, 1) withBytes:&metronomePulse length:1];
[self setMetaData:[mutableMetaData copy]];
[self setMetaData:mutableMetaData];
}

- (UInt8)thirtySecondsPerQuarterNote
Expand All @@ -88,8 +92,9 @@ - (void)setThirtySecondsPerQuarterNote:(UInt8)thirtySecondsPerQuarterNote
if (![[self class] isMutable]) return MIKMIDI_RAISE_MUTATION_ATTEMPT_EXCEPTION;

NSMutableData *mutableMetaData = self.metaData.mutableCopy;
if ([mutableMetaData length] < 4) [mutableMetaData increaseLengthBy:4-[mutableMetaData length]];
[mutableMetaData replaceBytesInRange:NSMakeRange(3, 1) withBytes:&thirtySecondsPerQuarterNote length:1];
[self setMetaData:[mutableMetaData copy]];
[self setMetaData:mutableMetaData];
}

- (NSString *)additionalEventDescription
Expand Down

0 comments on commit 1d6c275

Please sign in to comment.