Mercurial > hg > audiodb
comparison examples/iAudioDB/AppController.m @ 680:15e71890b584
* Switched over to using sonic-annotator extraction
* Uses populate for import
author | mas01mj |
---|---|
date | Fri, 05 Mar 2010 16:17:09 +0000 |
parents | 780ebab29268 |
children | df9639a5cad2 |
comparison
equal
deleted
inserted
replaced
679:047415d1805e | 680:15e71890b584 |
---|---|
51 } | 51 } |
52 | 52 |
53 // Create new db, and set flags. | 53 // Create new db, and set flags. |
54 db = audiodb_create([[panel filename] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, 0); | 54 db = audiodb_create([[panel filename] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, 0); |
55 audiodb_l2norm(db); | 55 audiodb_l2norm(db); |
56 audiodb_power(db); | 56 // audiodb_power(db); |
57 | 57 |
58 // Store useful paths. | 58 // Store useful paths. |
59 dbName = [[[panel URL] relativePath] retain]; | 59 dbName = [[[panel URL] relativePath] retain]; |
60 dbFilename = [[panel filename] retain]; | 60 dbFilename = [[panel filename] retain]; |
61 plistFilename = [[NSString stringWithFormat:@"%@.plist", [dbFilename stringByDeletingPathExtension]] retain]; | 61 plistFilename = [[NSString stringWithFormat:@"%@.plist", [dbFilename stringByDeletingPathExtension]] retain]; |
80 if(response == NSFileHandlingPanelOKButton) | 80 if(response == NSFileHandlingPanelOKButton) |
81 { | 81 { |
82 // Tidy any existing references up. | 82 // Tidy any existing references up. |
83 if(db) | 83 if(db) |
84 { | 84 { |
85 NSLog(@"Close db"); | |
85 audiodb_close(db); | 86 audiodb_close(db); |
86 } | 87 } |
87 | 88 |
88 if(dbFilename) | 89 if(dbFilename) |
89 { | 90 { |
91 NSLog(@"Tidy up filenames"); | |
90 [dbFilename release]; | 92 [dbFilename release]; |
91 [dbName release]; | 93 [dbName release]; |
92 [plistFilename release]; | 94 [plistFilename release]; |
93 } | 95 } |
94 | 96 |
95 // Store useful paths. | 97 // Store useful paths. |
96 db = audiodb_open([[panel filename] cStringUsingEncoding:NSUTF8StringEncoding], O_RDWR); | 98 NSLog(@"Open"); |
99 db = audiodb_open([[panel filename] cStringUsingEncoding:NSUTF8StringEncoding], O_RDONLY); | |
97 dbName = [[[panel URL] relativePath] retain]; | 100 dbName = [[[panel URL] relativePath] retain]; |
98 dbFilename = [[panel filename] retain]; | 101 dbFilename = [[panel filename] retain]; |
99 | 102 |
100 // TODO: Verify this exists! | 103 // TODO: Verify this exists! |
101 plistFilename = [[NSString stringWithFormat:@"%@.plist", [dbFilename stringByDeletingPathExtension]] retain]; | 104 plistFilename = [[NSString stringWithFormat:@"%@.plist", [dbFilename stringByDeletingPathExtension]] retain]; |
124 /** | 127 /** |
125 * Update button states and status field based on current state. | 128 * Update button states and status field based on current state. |
126 */ | 129 */ |
127 -(void)updateStatus | 130 -(void)updateStatus |
128 { | 131 { |
132 NSLog(@"Update status"); | |
129 if(db) | 133 if(db) |
130 { | 134 { |
135 NSLog(@"Got a db"); | |
131 adb_status_ptr status = (adb_status_ptr)malloc(sizeof(struct adbstatus)); | 136 adb_status_ptr status = (adb_status_ptr)malloc(sizeof(struct adbstatus)); |
132 int flags; | 137 int flags; |
133 flags = audiodb_status(db, status); | 138 flags = audiodb_status(db, status); |
134 [statusField setStringValue: [NSString stringWithFormat:@"Database: %@ Dimensions: %d Files: %d", dbName, status->dim, status->numFiles]]; | 139 [statusField setStringValue: [NSString stringWithFormat:@"Database: %@ Dimensions: %d Files: %d", dbName, status->dim, status->numFiles]]; |
135 [chooseButton setEnabled:YES]; | 140 [chooseButton setEnabled:YES]; |
136 } | 141 } |
137 else | 142 else |
138 { | 143 { |
144 NSLog(@"No db"); | |
139 [chooseButton setEnabled:NO]; | 145 [chooseButton setEnabled:NO]; |
140 [playBothButton setEnabled:FALSE]; | 146 [playBothButton setEnabled:FALSE]; |
141 [playResultButton setEnabled:FALSE]; | 147 [playResultButton setEnabled:FALSE]; |
142 } | 148 } |
143 } | 149 } |
189 | 195 |
190 NSArray *filesToOpen = [panel filenames]; | 196 NSArray *filesToOpen = [panel filenames]; |
191 | 197 |
192 NSLog(@"Begin import"); | 198 NSLog(@"Begin import"); |
193 | 199 |
200 /* | |
201 vamp:vamp-audiodb-plugins:cq:cq | |
202 vamp:vamp-audiodb-plugins:chromagram:chroma | |
203 vamp:qm-vamp-plugins:qm-mfcc:coefficients | |
204 vamp:qm-vamp-plugins:qm-chromagram:chromagram | |
205 */ | |
206 | |
207 | |
208 // adb_chroma | |
209 // adb_cq | |
210 // qm_chroma | |
211 // qm_mfcc | |
212 | |
194 // Work out which extractor to use | 213 // Work out which extractor to use |
195 NSString* extractor = @"chromagram"; | 214 NSString* extractor = @"chromagram"; |
196 switch([extractorOptions selectedTag]) | 215 switch([extractorOptions selectedTag]) |
197 { | 216 { |
198 case 0: | 217 case 0: |
199 extractor = @"mfcc"; | 218 extractor = @"adb_chroma"; |
200 break; | 219 break; |
201 case 1: | 220 case 1: |
202 extractor = @"chromagram"; | 221 extractor = @"adb_cq"; |
203 break; | 222 break; |
204 } | 223 case 2: |
224 extractor = @"qm_chroma"; | |
225 break; | |
226 case 3: | |
227 extractor = @"qm_mfcc"; | |
228 break; | |
229 } | |
230 | |
205 | 231 |
206 for(int i=0; i<[filesToOpen count]; i++) | 232 for(int i=0; i<[filesToOpen count]; i++) |
207 { | 233 { |
208 // First extract powers | 234 audiodb_close(db); |
209 | 235 NSString* tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"features.XXXXXX"]; |
210 NSString *tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"powers.XXXXXX"]; | 236 const char* tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; |
211 const char *tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; | 237 char* tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); |
212 char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); | |
213 strcpy(tempFileNameCString, tempFileTemplateCString); | 238 strcpy(tempFileNameCString, tempFileTemplateCString); |
214 mktemp(tempFileNameCString); | 239 mktemp(tempFileNameCString); |
215 | 240 |
216 NSString* powersFileName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)]; | 241 NSString* featuresFileName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)]; |
217 free(tempFileNameCString); | 242 free(tempFileNameCString); |
218 | 243 |
219 NSTask *task = [[NSTask alloc] init]; | 244 NSTask* task = [[NSTask alloc] init]; |
220 [task setLaunchPath:@"/usr/local/bin/fftExtract2"]; | 245 |
221 NSArray *args = [NSArray arrayWithObjects:@"-P", @"-h", @"11025", @"-w", @"16384", @"-n", @"32768", @"-i", @"1000", [filesToOpen objectAtIndex:i], powersFileName, nil]; | 246 [task setLaunchPath:@"/usr/local/bin/sonic-annotator"]; |
247 | |
248 NSString* extractorPath = [NSString stringWithFormat:@"/Applications/iAudioDB.app/rdf/%@.n3", extractor]; | |
249 NSLog(@"Extractor path: %@", extractorPath); | |
250 NSArray* args; | |
251 args = [NSArray arrayWithObjects:@"-t", extractorPath, @"-w", @"rdf", @"-r", @"--rdf-network", @"--rdf-one-file", featuresFileName, @"--rdf-force", [filesToOpen objectAtIndex:i], nil]; | |
222 [task setArguments:args]; | 252 [task setArguments:args]; |
223 [task launch]; | 253 [task launch]; |
224 [task waitUntilExit]; | 254 [task waitUntilExit]; |
225 [task release]; | 255 [task release]; |
226 | 256 |
227 // Then features | 257 NSTask* importTask = [[NSTask alloc] init]; |
228 | 258 [importTask setLaunchPath:@"/usr/local/bin/populate"]; |
229 tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:@"features.XXXXXX"]; | 259 args = [NSArray arrayWithObjects:featuresFileName, dbFilename, nil]; |
230 tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation]; | 260 [importTask setArguments:args]; |
231 tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1); | 261 [importTask launch]; |
232 strcpy(tempFileNameCString, tempFileTemplateCString); | 262 [importTask waitUntilExit]; |
233 mktemp(tempFileNameCString); | 263 [importTask release]; |
234 | |
235 NSString* featuresFileName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)]; | |
236 free(tempFileNameCString); | |
237 | |
238 task = [[NSTask alloc] init]; | |
239 | |
240 [task setLaunchPath:@"/usr/local/bin/fftExtract2"]; | |
241 | |
242 NSArray *args2; | |
243 | |
244 // Choose the args (TODO: This should use sonic annotator eventually) | |
245 if([extractor isEqualToString:@"chromagram"]) | |
246 { | |
247 args2 = [NSArray arrayWithObjects:@"-p",@"/Users/moj/planfile",@"-c", @"36", @"-h", @"11025", @"-w", @"16384", @"-n", @"32768", @"-i", @"1000", [filesToOpen objectAtIndex:i], featuresFileName, nil]; | |
248 } | |
249 else | |
250 { | |
251 args2 = [NSArray arrayWithObjects:@"-p",@"/Users/moj/planfile",@"-m", @"13", @"-h", @"11025", @"-w", @"16384", @"-n ", @"32768", @"-i", @"1000", [filesToOpen objectAtIndex:i], featuresFileName, nil]; | |
252 } | |
253 [task setArguments:args2]; | |
254 [task launch]; | |
255 [task waitUntilExit]; | |
256 [task release]; | |
257 | 264 |
258 NSString* val = [[filesToOpen objectAtIndex:i] retain]; | 265 NSString* val = [[filesToOpen objectAtIndex:i] retain]; |
259 NSString* key = [[[filesToOpen objectAtIndex:i] lastPathComponent] retain]; | 266 NSString* key = [[[filesToOpen objectAtIndex:i] lastPathComponent] retain]; |
260 | 267 /* |
261 adb_insert_t insert; | 268 adb_insert_t insert; |
262 insert.features = [featuresFileName cStringUsingEncoding:NSUTF8StringEncoding]; | 269 insert.features = [featuresFileName cStringUsingEncoding:NSUTF8StringEncoding]; |
263 insert.power = [powersFileName cStringUsingEncoding:NSUTF8StringEncoding]; | 270 // insert.power = [powersFileName cStringUsingEncoding:NSUTF8StringEncoding]; |
264 insert.times = NULL; | 271 insert.times = NULL; |
265 insert.key = [key cStringUsingEncoding:NSUTF8StringEncoding]; | 272 insert.key = [key cStringUsingEncoding:NSUTF8StringEncoding]; |
266 | 273 |
267 // Insert into db. | 274 // Insert into db. |
268 if(audiodb_insert(db, &insert)) | 275 if(audiodb_insert(db, &insert)) |
269 { | 276 { |
270 // TODO: Show an error message. | 277 // TODO: Show an error message. |
271 NSLog(@"Weep: %@ %@ %@", featuresFileName, powersFileName, key); | 278 NSLog(@"Weep: %@ %@", featuresFileName, key); |
272 continue; | 279 continue; |
273 } | 280 }*/ |
274 | 281 |
275 // Update the plist store. | 282 // Update the plist store. |
276 [trackMap setValue:val forKey:key]; | 283 [trackMap setValue:val forKey:key]; |
277 [trackMap writeToFile:plistFilename atomically: YES]; | 284 [trackMap writeToFile:plistFilename atomically: YES]; |
278 | 285 |
286 | |
287 db = audiodb_open([dbFilename cStringUsingEncoding:NSUTF8StringEncoding], O_RDONLY); | |
279 [self updateStatus]; | 288 [self updateStatus]; |
280 } | 289 } |
281 | 290 |
282 newFrame.origin.x = [importSheet frame].origin.x; | 291 newFrame.origin.x = [importSheet frame].origin.x; |
283 newFrame.origin.y = [importSheet frame].origin.y + [extractingBox frame].size.height; | 292 newFrame.origin.y = [importSheet frame].origin.y + [extractingBox frame].size.height; |
564 | 573 |
565 NSLog(@"No results"); | 574 NSLog(@"No results"); |
566 } | 575 } |
567 else | 576 else |
568 { | 577 { |
578 float divisor = (44100/2048); | |
569 for(int i=0; i<result->nresults; i++) | 579 for(int i=0; i<result->nresults; i++) |
570 { | 580 { |
571 NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithCapacity:4]; | 581 NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithCapacity:4]; |
572 [dict setValue:[NSString stringWithFormat:@"%s", result->results[i].key] forKey:@"key"]; | 582 [dict setValue:[NSString stringWithFormat:@"%s", result->results[i].key] forKey:@"key"]; |
573 [dict setValue:[NSNumber numberWithFloat:result->results[i].dist] forKey:@"distance"]; | 583 [dict setValue:[NSNumber numberWithFloat:result->results[i].dist] forKey:@"distance"]; |
574 [dict setValue:[NSNumber numberWithFloat:result->results[i].dist] forKey:@"meter"]; | 584 [dict setValue:[NSNumber numberWithFloat:result->results[i].dist] forKey:@"meter"]; |
575 [dict setValue:[NSNumber numberWithFloat:result->results[i].qpos/4] forKey:@"qpos"]; | 585 [dict setValue:[NSNumber numberWithFloat:result->results[i].qpos/divisor] forKey:@"qpos"]; |
576 [dict setValue:[NSNumber numberWithFloat:result->results[i].ipos/4] forKey:@"ipos"]; | 586 [dict setValue:[NSNumber numberWithFloat:result->results[i].ipos/divisor] forKey:@"ipos"]; |
577 NSLog(@"%s qpos %d ipos %d", result->results[i].key, result->results[i].qpos/4, result->results[i].ipos/4); | 587 NSLog(@"%s qpos %d ipos %d", result->results[i].key, result->results[i].qpos/divisor, result->results[i].ipos/divisor); |
578 [results addObject: dict]; | 588 [results addObject: dict]; |
579 } | 589 } |
580 } | 590 } |
581 | 591 |
582 NSSortDescriptor *distSort = [[NSSortDescriptor alloc]initWithKey:@"meter" ascending:YES]; | 592 NSSortDescriptor *distSort = [[NSSortDescriptor alloc]initWithKey:@"meter" ascending:YES]; |