Skip to content

Commit

Permalink
Issue #66: Added additional init/convenience methods to MIKMIDISequen…
Browse files Browse the repository at this point in the history
…ce to give the option to convert channels in the MIDI data to tracks.
  • Loading branch information
kris2point0 committed Mar 6, 2015
1 parent 2706b25 commit 20d5477
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
60 changes: 58 additions & 2 deletions Source/MIKMIDISequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ NS_INLINE MIKMIDITimeSignature MIKMIDITimeSignatureMake(UInt8 numerator, UInt8 d
*/
+ (instancetype)sequenceWithFileAtURL:(NSURL *)fileURL error:(NSError **)error;

/**
* Creates and initilazes a new instance of MIKMIDISequence from a MIDI file.
*
* @param fileURL The URL of the MIDI file.
* @param convertMIDIChannelsToTracks Determines whether or not the track structure should be altered. When YES, the resulting sequence will
* contain a tempo track, 1 track for each MIDI Channel that is found in the MIDI file, and 1 track for SysEx or MetaEvents as the last track in
* the sequence. When NO, the track structure of the original MIDI file is left unaltered.
* @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*
* @return A new instance of MIKMIDISequence containing the loaded file's MIDI sequence, or nil if an error occured.
*/
+ (instancetype)sequenceWithFileAtURL:(NSURL *)fileURL convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error;

/**
* Initilazes a new instance of MIKMIDISequence from a MIDI file.
*
Expand All @@ -64,28 +78,70 @@ NS_INLINE MIKMIDITimeSignature MIKMIDITimeSignatureMake(UInt8 numerator, UInt8 d
*/
- (instancetype)initWithFileAtURL:(NSURL *)fileURL error:(NSError **)error;

/**
* Initilazes a new instance of MIKMIDISequence from a MIDI file.
*
* @param fileURL The URL of the MIDI file.
* @param convertMIDIChannelsToTracks Determines whether or not the track structure should be altered. When YES, the resulting sequence will
* contain a tempo track, 1 track for each MIDI Channel that is found in the MIDI file, and 1 track for SysEx or MetaEvents as the last track in
* the sequence. When NO, the track structure of the original MIDI file is left unaltered.
* @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*
* @return A new instance of MIKMIDISequence containing the loaded file's MIDI sequence, or nil if an error occured.
*/
- (instancetype)initWithFileAtURL:(NSURL *)fileURL convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error;

/**
* Creates and initializes a new instance of MIKMIDISequence from MIDI data.
*
* @param data An NSData instance containing the data for the MIDI sequence/file.
* @param error If an
* @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
*
* @return If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*/
+ (instancetype)sequenceWithData:(NSData *)data error:(NSError **)error;

/**
* Creates and initializes a new instance of MIKMIDISequence from MIDI data.
*
* @param data An NSData instance containing the data for the MIDI sequence/file.
* @param convertMIDIChannelsToTracks Determines whether or not the track structure should be altered. When YES, the resulting sequence will
* contain a tempo track, 1 track for each MIDI Channel that is found in the MIDI file, and 1 track for SysEx or MetaEvents as the last track in
* the sequence. When NO, the track structure of the original MIDI file is left unaltered.
* @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
*
* @return If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*/
+ (instancetype)sequenceWithData:(NSData *)data convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error;

/**
* Initializes a new instance of MIKMIDISequence from MIDI data.
*
* @param data An NSData instance containing the data for the MIDI sequence/file.
* @param error If an
* @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
*
* @return If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*/
- (instancetype)initWithData:(NSData *)data error:(NSError **)error;

/**
* Initializes a new instance of MIKMIDISequence from MIDI data.
*
* @param data An NSData instance containing the data for the MIDI sequence/file.
* @param convertMIDIChannelsToTracks Determines whether or not the track structure should be altered. When YES, the resulting sequence will
* contain a tempo track, 1 track for each MIDI Channel that is found in the MIDI file, and 1 track for SysEx or MetaEvents as the last track in
* the sequence. When NO, the track structure of the original MIDI file is left unaltered.
* @param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
*
* @return If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors,
* you may pass in NULL.
*/
- (instancetype)initWithData:(NSData *)data convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error;

/**
* Writes the MIDI sequence in Standard MIDI File format to a file at the specified URL.
*
Expand Down
31 changes: 26 additions & 5 deletions Source/MIKMIDISequence.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,41 @@ - (instancetype)init

+ (instancetype)sequenceWithFileAtURL:(NSURL *)fileURL error:(NSError **)error;
{
return [[self alloc] initWithFileAtURL:fileURL error:error];
return [[self alloc] initWithFileAtURL:fileURL convertMIDIChannelsToTracks:NO error:error];
}

+ (instancetype)sequenceWithFileAtURL:(NSURL *)fileURL convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error
{
return [[self alloc] initWithFileAtURL:fileURL convertMIDIChannelsToTracks:convertMIDIChannelsToTracks error:error];
}

- (instancetype)initWithFileAtURL:(NSURL *)fileURL error:(NSError **)error;
{
NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:error];
return [self initWithData:data error:error];
return [self initWithFileAtURL:fileURL convertMIDIChannelsToTracks:NO error:error];
}

- (instancetype)initWithFileAtURL:(NSURL *)fileURL convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error
{
NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:error];
return [self initWithData:data convertMIDIChannelsToTracks:convertMIDIChannelsToTracks error:error];
}

+ (instancetype)sequenceWithData:(NSData *)data error:(NSError **)error
{
return [[self alloc] initWithData:data error:error];
return [[self alloc] initWithData:data convertMIDIChannelsToTracks:NO error:error];
}

+ (instancetype)sequenceWithData:(NSData *)data convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error
{
return [[self alloc] initWithData:data convertMIDIChannelsToTracks:convertMIDIChannelsToTracks error:error];
}

- (instancetype)initWithData:(NSData *)data error:(NSError **)error
{
return [self initWithData:data convertMIDIChannelsToTracks:NO error:error];
}

- (instancetype)initWithData:(NSData *)data convertMIDIChannelsToTracks:(BOOL)convertMIDIChannelsToTracks error:(NSError **)error
{
error = error ? error : &(NSError *__autoreleasing){ nil };

Expand All @@ -78,7 +98,8 @@ - (instancetype)initWithData:(NSData *)data error:(NSError **)error
return nil;
}

err = MusicSequenceFileLoadData(sequence, (__bridge CFDataRef)data, kMusicSequenceFile_MIDIType, 0);
MusicSequenceLoadFlags flags = convertMIDIChannelsToTracks ? kMusicSequenceLoadSMF_ChannelsToTracks : 0;
err = MusicSequenceFileLoadData(sequence, (__bridge CFDataRef)data, kMusicSequenceFile_MIDIType, flags);
if (err) {
NSLog(@"MusicSequenceFileLoadData() failed with error %d in %s.", err, __PRETTY_FUNCTION__);
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
Expand Down

0 comments on commit 20d5477

Please sign in to comment.