Mercurial > hg > audiodb
comparison examples/iAudioDB/AppController.m @ 700:54974e8a6b87
Separated out import process - now generates VAMP config for each audio file as preparation for sample rate/hop size alterations.
author | mas01mj |
---|---|
date | Thu, 29 Apr 2010 16:36:07 +0000 |
parents | 9a7d829bc492 |
children | e21abbac820e |
comparison
equal
deleted
inserted
replaced
699:9a7d829bc492 | 700:54974e8a6b87 |
---|---|
112 dbState = [[NSMutableDictionary alloc] init]; | 112 dbState = [[NSMutableDictionary alloc] init]; |
113 trackMap = [[NSMutableDictionary alloc] init]; | 113 trackMap = [[NSMutableDictionary alloc] init]; |
114 [dbState setValue:trackMap forKey:@"tracks"]; | 114 [dbState setValue:trackMap forKey:@"tracks"]; |
115 [dbState setValue:extractor forKey:@"extractor"]; | 115 [dbState setValue:extractor forKey:@"extractor"]; |
116 [dbState setValue:[hopSizeField stringValue] forKey:@"hopsize"]; | 116 [dbState setValue:[hopSizeField stringValue] forKey:@"hopsize"]; |
117 [dbState setValue:[windowSizeField stringValue] forKey:@"windowsize"]; | |
118 [dbState writeToFile:plistFilename atomically:YES]; | 117 [dbState writeToFile:plistFilename atomically:YES]; |
119 | 118 |
120 [queryKey setStringValue:@"None Selected"]; | 119 [queryKey setStringValue:@"None Selected"]; |
121 [self updateStatus]; | 120 [self updateStatus]; |
122 } | 121 } |
141 [dbState release]; | 140 [dbState release]; |
142 } | 141 } |
143 | 142 |
144 if(selectedKey) | 143 if(selectedKey) |
145 { | 144 { |
146 NSLog(@"Released selected key: %@", selectedKey); | |
147 [selectedKey release]; | 145 [selectedKey release]; |
148 selectedKey = Nil; | 146 selectedKey = Nil; |
149 NSLog(@"Is now %@", selectedKey); | |
150 } | |
151 | |
152 if(selectedKey) | |
153 { | |
154 NSLog(@"Still evals"); | |
155 } | 147 } |
156 | 148 |
157 // Reset query flags | 149 // Reset query flags |
158 [queryPath setStringValue: @"No file selected"]; | 150 [queryPath setStringValue: @"No file selected"]; |
159 [queryLengthSeconds setDoubleValue:0]; | 151 [queryLengthSeconds setDoubleValue:0]; |
230 { | 222 { |
231 NSLog(@"Got a db"); | 223 NSLog(@"Got a db"); |
232 adb_status_t *status = (adb_status_t *)malloc(sizeof(adb_status_t)); | 224 adb_status_t *status = (adb_status_t *)malloc(sizeof(adb_status_t)); |
233 int flags; | 225 int flags; |
234 flags = audiodb_status(db, status); | 226 flags = audiodb_status(db, status); |
235 [statusField setStringValue: [NSString stringWithFormat:@"%@ Dim: %d Files: %d Hop: %@ Win: %@ Ext: %@", | 227 [statusField setStringValue: [NSString stringWithFormat:@"%@ Dim: %d Files: %d Hop: %@ Ext: %@", |
236 dbName, | 228 dbName, |
237 status->dim, | 229 status->dim, |
238 status->numFiles, | 230 status->numFiles, |
239 [dbState objectForKey:@"hopsize"], | 231 [dbState objectForKey:@"hopsize"], |
240 [dbState objectForKey:@"windowsize"], | |
241 [dbState objectForKey:@"extractor"]]]; | 232 [dbState objectForKey:@"extractor"]]]; |
242 [performQueryButton setEnabled:YES]; | 233 [performQueryButton setEnabled:YES]; |
243 [importAudioButton setEnabled:YES]; | 234 [importAudioButton setEnabled:YES]; |
244 } | 235 } |
245 else | 236 else |
251 [playResultButton setEnabled:NO]; | 242 [playResultButton setEnabled:NO]; |
252 [stopButton setEnabled:NO]; | 243 [stopButton setEnabled:NO]; |
253 } | 244 } |
254 } | 245 } |
255 | 246 |
247 -(void)importFile:(NSString *)filename withExtractorConfig:(NSString *)extractorPath | |
248 { | |
249 // Create the extractor configuration | |
250 | |
251 NSString* extractorContent = [NSString stringWithContentsOfFile:extractorPath]; | |
252 NSString* hopStr = [dbState objectForKey:@"hopsize"]; | |
253 NSString* newContent = [[extractorContent stringByReplacingOccurrencesOfString:@"HOP_SIZE" withString:hopStr] | |
254 stringByReplacingOccurrencesOfString:@"WINDOW_SIZE" withString:[NSString stringWithFormat:@"%d", [hopStr intValue] * 8]]; | |
255 NSString* n3FileName = [NSTemporaryDirectory() stringByAppendingPathComponent:@"extractor_config.n3"]; | |
256 NSLog(extractorContent); | |
257 NSLog(newContent); | |
258 | |
259 NSError* error; | |
260 [newContent writeToFile:n3FileName atomically:YES encoding:NSASCIIStringEncoding error:&error]; | |
261 | |
262 // Create the temp file for the extracted features | |
263 NSString* tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"features.XXXXXX"]; | |
264 const char* tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; | |
265 char* tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); | |
266 strcpy(tempFileNameCString, tempFileTemplateCString); | |
267 mktemp(tempFileNameCString); | |
268 | |
269 NSString* featuresFileName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)]; | |
270 free(tempFileNameCString); | |
271 | |
272 // Extract features with sonic-annotator | |
273 NSTask* task = [[NSTask alloc] init]; | |
274 [task setLaunchPath:@"/usr/local/bin/sonic-annotator"]; | |
275 NSArray* args; | |
276 args = [NSArray arrayWithObjects:@"-t", n3FileName, @"-w", @"rdf", @"-r", @"--rdf-network", @"--rdf-one-file", featuresFileName, @"--rdf-force", filename, nil]; | |
277 [task setArguments:args]; | |
278 [task launch]; | |
279 [task waitUntilExit]; | |
280 [task release]; | |
281 | |
282 // Populate the audioDB instance | |
283 NSTask* importTask = [[NSTask alloc] init]; | |
284 [importTask setLaunchPath:@"/usr/local/bin/populate"]; | |
285 args = [NSArray arrayWithObjects:featuresFileName, dbFilename, nil]; | |
286 [importTask setArguments:args]; | |
287 [importTask launch]; | |
288 [importTask waitUntilExit]; | |
289 [importTask release]; | |
290 | |
291 NSString* val = [filename retain]; | |
292 NSString* key = [[filename lastPathComponent] retain]; | |
293 | |
294 // Update the plist store. | |
295 [trackMap setValue:val forKey:key]; | |
296 [dbState writeToFile:plistFilename atomically: YES]; | |
297 | |
298 } | |
299 | |
256 /** | 300 /** |
257 * Choose the file(s) to be imported. | 301 * Choose the file(s) to be imported. |
258 * TODO: Currently handles the import process too - split this off. | 302 * TODO: Currently handles the import process too - split this off. |
259 */ | 303 */ |
260 -(IBAction)importAudio:(id)sender | 304 -(IBAction)importAudio:(id)sender |
278 NSString* extractor = [dbState objectForKey:@"extractor"]; | 322 NSString* extractor = [dbState objectForKey:@"extractor"]; |
279 NSString* extractorPath = [NSString stringWithFormat:@"/Applications/iAudioDB.app/rdf/%@.n3", extractor]; | 323 NSString* extractorPath = [NSString stringWithFormat:@"/Applications/iAudioDB.app/rdf/%@.n3", extractor]; |
280 | 324 |
281 // TODO Shift this process into a separate function. | 325 // TODO Shift this process into a separate function. |
282 // Create the customized extractor config | 326 // Create the customized extractor config |
283 NSString* extractorContent = [NSString stringWithContentsOfFile:extractorPath]; | 327 /* NSString* extractorContent = [NSString stringWithContentsOfFile:extractorPath]; |
284 NSString* hopStr = [dbState objectForKey:@"hopsize"]; | 328 NSString* hopStr = [dbState objectForKey:@"hopsize"]; |
285 NSString* winStr = [dbState objectForKey:@"windowsize"]; | 329 NSString* winStr = [dbState objectForKey:@"windowsize"]; |
286 NSString* newContent = [[extractorContent stringByReplacingOccurrencesOfString:@"HOP_SIZE" withString:hopStr] | 330 NSString* newContent = [[extractorContent stringByReplacingOccurrencesOfString:@"HOP_SIZE" withString:hopStr] |
287 stringByReplacingOccurrencesOfString:@"WINDOW_SIZE" withString:winStr]; | 331 stringByReplacingOccurrencesOfString:@"WINDOW_SIZE" withString:winStr]; |
288 NSString* n3FileName = [NSTemporaryDirectory() stringByAppendingPathComponent:@"extractor_config.n3"]; | 332 NSString* n3FileName = [NSTemporaryDirectory() stringByAppendingPathComponent:@"extractor_config.n3"]; |
289 | 333 |
290 NSError* error; | 334 NSError* error; |
291 [newContent writeToFile:n3FileName atomically:YES encoding:NSASCIIStringEncoding error:&error]; | 335 [newContent writeToFile:n3FileName atomically:YES encoding:NSASCIIStringEncoding error:&error]; |
292 | 336 */ |
293 for(int i=0; i<[filesToOpen count]; i++) | 337 for(int i=0; i<[filesToOpen count]; i++) |
294 { | 338 { |
295 audiodb_close(db); | 339 audiodb_close(db); |
296 NSString* tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"features.XXXXXX"]; | 340 |
341 // Get the sample rate for the audio file | |
342 | |
343 [self importFile:[filesToOpen objectAtIndex:i] withExtractorConfig:extractorPath]; | |
344 | |
345 /* NSString* tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"features.XXXXXX"]; | |
297 const char* tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; | 346 const char* tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; |
298 char* tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); | 347 char* tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); |
299 strcpy(tempFileNameCString, tempFileTemplateCString); | 348 strcpy(tempFileNameCString, tempFileTemplateCString); |
300 mktemp(tempFileNameCString); | 349 mktemp(tempFileNameCString); |
301 | 350 |
324 NSString* key = [[[filesToOpen objectAtIndex:i] lastPathComponent] retain]; | 373 NSString* key = [[[filesToOpen objectAtIndex:i] lastPathComponent] retain]; |
325 | 374 |
326 // Update the plist store. | 375 // Update the plist store. |
327 [trackMap setValue:val forKey:key]; | 376 [trackMap setValue:val forKey:key]; |
328 [dbState writeToFile:plistFilename atomically: YES]; | 377 [dbState writeToFile:plistFilename atomically: YES]; |
329 | 378 */ |
330 | 379 |
331 db = audiodb_open([dbFilename cStringUsingEncoding:NSUTF8StringEncoding], O_RDONLY); | 380 db = audiodb_open([dbFilename cStringUsingEncoding:NSUTF8StringEncoding], O_RDONLY); |
332 [self updateStatus]; | 381 [self updateStatus]; |
333 } | 382 } |
334 | 383 |