Cog/Plugins/MIDI/MIDI/MIDIContainer.mm
Christopher Snowhill cd95cd68f8 MIDI: Add exception handling
Both the midi_processing and the various players may
throw exceptions, so we should check for these too.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2025-01-30 01:17:44 -08:00

74 lines
1.5 KiB
Text

//
// MIDIContainer.mm
// MIDI
//
// Created by Christopher Snowhill on 10/16/13.
// Copyright 2013 __NoWork, Inc__. All rights reserved.
//
#import "MIDIContainer.h"
#import "MIDIDecoder.h"
#import <midi_processing/midi_processor.h>
#import "Logging.h"
@implementation MIDIContainer
+ (NSArray *)fileTypes {
return [MIDIDecoder fileTypes];
}
+ (NSArray *)mimeTypes {
return [MIDIDecoder mimeTypes];
}
+ (float)priority {
return 1.0f;
}
// This really should be source...
+ (NSArray *)urlsForContainerURL:(NSURL *)url {
if([url fragment]) {
// input url already has fragment defined - no need to expand further
return [NSMutableArray arrayWithObject:url];
}
id audioSourceClass = NSClassFromString(@"AudioSource");
id<CogSource> source = [audioSourceClass audioSourceForURL:url];
if(![source open:url])
return @[];
if(![source seekable])
return @[];
[source seek:0 whence:SEEK_END];
long size = [source tell];
[source seek:0 whence:SEEK_SET];
size_t track_count = 0;
try {
std::vector<uint8_t> data;
data.resize(size);
[source read:&data[0] amount:size];
if(!midi_processor::process_track_count(data, [[url pathExtension] UTF8String], track_count))
return @[];
} catch (std::exception &e) {
ALog(@"Exception caught processing MIDI track count: %s", e.what());
return @[];
}
NSMutableArray *tracks = [NSMutableArray array];
long i;
for(i = 0; i < track_count; i++) {
[tracks addObject:[NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:@"#%li", i]]];
}
return tracks;
}
@end