-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterleaveProcessController.m
executable file
·881 lines (735 loc) · 29.9 KB
/
InterleaveProcessController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/*
De-Interleaver
Copyright (c) 2006, 2017 Scott Wilson. All rights reserved.
http://scottwilson.ca
De-Interleaver's development was funded in part by a grant from the Arts
and Humanities Research Council of Britain, as part of the BEASTMulch project.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "InterleaveProcessController.h"
#define kFramesToReadWrite 1024
int gIndex = 0, gRunning = 0, numOutputFiles;
double gLongestRecipPct; // used for updating the progress bar
@implementation InterleaveProcessController
- (void)awakeFromNib {
// force multi-threaded mode
[NSThread detachNewThreadSelector:@selector(self) toTarget:@"Dummy Object" withObject:nil];
pthread_mutex_init(&mutex, NULL);
channelOrderArray = [[NSMutableArray alloc] init];
[channelOrderTable registerForDraggedTypes: [NSArray arrayWithObjects:NSStringPboardType, NSFilenamesPboardType, nil]];
// toolbar
toolBar = [[NSToolbar alloc] initWithIdentifier:@"toolbar"];
[toolBar setAllowsUserCustomization:NO];
[toolBar setShowsBaselineSeparator:NO];
[toolBar setDelegate: self];
[mainWindow setToolbar:toolBar];
[mainWindow setOpaque:NO];
[mainWindow setBackgroundColor:[NSColor colorWithCalibratedWhite:0.87 alpha:1]];
types = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: SF_FORMAT_WAV], @"wav",
[NSNumber numberWithInt: SF_FORMAT_CAF], @"caf",
[NSNumber numberWithInt: SF_FORMAT_AIFF], @"aiff", nil];
[types retain];
}
- (IBAction)buttonPressed:(id)sender
{
// need to check states
if([sender state] == NSOffState) {
NSLog(@"Cancel Pressed");
lock();
gRunning = 0;
unlock();
} else if([sender state] == NSOnState) {
lock();
gIndex = 0;
gRunning = 1; // set global runflag
unlock();
// start a timer to update the progress bar
NSTimer* timer;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgressBar:) userInfo:nil repeats:YES];
[self performSelector:operation];
}
}
- (IBAction)deinterleave:(id)sender
{
operation = @selector(spawnDeinterleaveThread);
NSInteger result;
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:NO];
result = [op runModal];
if (result == NSFileHandlingPanelOKButton) {
NSArray *filesToOpen = [op URLs];
NSLog(@"%@", [filesToOpen description]);
int i;
inputinfo.format = 0;
if(deIntFilePath != NULL) [deIntFilePath release];
deIntFilePath = [[[filesToOpen objectAtIndex:0] path] retain];
NSLog(@"deint path: %@", deIntFilePath);
inputfile = sf_open([deIntFilePath fileSystemRepresentation], SFM_READ, &inputinfo);
if(inputfile == NULL) {
NSLog(@"%@", [NSString stringWithFormat: @"File %@ failed to open", deIntFilePath]);
[self makeWarningWithText: [NSString stringWithFormat: @"File %@ failed to open!", deIntFilePath]];
return;
}
if(inputinfo.channels <= 1) {
NSLog(@"%@", [NSString stringWithFormat: @"Input File %@ is not multichannel!", deIntFilePath]);
[self makeWarningWithText: [NSString stringWithFormat: @"Input File %@ is not multichannel!", deIntFilePath]];
closeSNDFiles(&(inputfile), 1); // close the file already open
return;
}
// set array for TableView
[channelOrderArray removeAllObjects];
for(i=0; i < inputinfo.channels; i++) {
[channelOrderArray addObject:[[[deIntFilePath stringByDeletingPathExtension] stringByAppendingFormat: @"-%i", i + 1] lastPathComponent]];
}
[channelOrderTable reloadData];
numOutputFiles = inputinfo.channels;
[startCancelButton setEnabled: YES];
[startCancelButton setState: NSOffState];
[mainWindow makeFirstResponder: startCancelButton];
[startCancelButton display];
[fileColumn setEditable: YES];
[[fileColumn headerCell] setObjectValue:@"Output Filenames (Double-Click to Edit)"];
}
}
- (void)spawnDeinterleaveThread {
[self setViewEnabledStatusRunning:[NSNumber numberWithBool: YES]];
// saving stuff
// use an open panel here as we just want to get a directory
NSOpenPanel *sp;
sp = [NSOpenPanel openPanel];
/* set up new attributes */
[sp setTitle: @"Set Base Path for Deinterleaved Output Files"];
[sp setCanChooseDirectories: YES];
[sp setCanChooseFiles: NO];
[sp setAllowsMultipleSelection: NO];
//[sp setDelegate: self]; // to check for existing files
NSPopUpButton *typesMenu;
NSRect bRect = {0, 0, 130, 30};
typesMenu = [[NSPopUpButton alloc] initWithFrame:bRect pullsDown:NO];
[typesMenu addItemsWithTitles: [types allKeys]];
[typesMenu selectItemWithTitle:@"aiff"];
[typesMenu setTarget: self];
[typesMenu setAction: @selector(setExtension:)];
NSBox *box = [NSBox new];
[box setTitle: @"Output File Format"];
[box setTitlePosition: NSAtTop];
[box setBorderType: NSNoBorder];
[box addSubview: typesMenu];
[box sizeToFit];
[box setAutoresizingMask: NSViewNotSizable];
[sp setAccessoryView: box];
// default output format is input format
NSString *extension = [deIntFilePath pathExtension];
if([extension isEqualToString: @"aif"]) extension = @"aiff";
[typesMenu selectItemWithTitle: extension];
// display the NSSavePanel
NSInteger runResult;
runResult = [sp runModal];
if (runResult == NSFileHandlingPanelOKButton) {
NSString *filename = [[sp URL] path];
NSLog(@"outputdir: %@", filename);
switch([self noOverwritesInDir:filename usingPathExtension:[typesMenu titleOfSelectedItem]]) {
case 0:
[self spawnDeinterleaveThread];
return;
case 2:
[self setViewEnabledStatusRunning:[NSNumber numberWithBool: NO]];
return;
}
[textField setStringValue:[NSString stringWithFormat: @"Deinterleaving File: %@", [deIntFilePath lastPathComponent]]];
[textField display];
NSDictionary *params = @{ @"type" : [typesMenu titleOfSelectedItem], @"filename" : filename};
[NSThread detachNewThreadSelector: @selector(doDeinterleave:) toTarget: self withObject: params];
} else {
lock();
gRunning = 0;
unlock();
[startCancelButton setNextState];
[mainWindow display];
}
}
- (void)doDeinterleave:(NSDictionary*)params
{
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
NSString *type = [params objectForKey:@"type"];
NSString *filename = [params objectForKey:@"filename"];
int count = inputinfo.channels;
SF_INFO outfileinfos[count];
SNDFILE* outputfiles[count];
int inputSubtype, outMajorFormat, outformat;
SF_INFO testInfo;
inputSubtype = inputinfo.format & SF_FORMAT_SUBMASK;
outMajorFormat = [[types objectForKey: type] intValue];
outformat = outMajorFormat | inputSubtype; // take sample format of first input file
testInfo.format = outformat;
testInfo.samplerate = inputinfo.samplerate;
testInfo.channels = 1;
// test for valid format
if(!sf_format_check(&testInfo)) {
outformat = outMajorFormat | SF_FORMAT_FLOAT; // all output formats support this
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Input sample format not supported!\nUsing 32 bit float instead." waitUntilDone:YES];
}
NSMutableArray *outpaths = [NSMutableArray arrayWithCapacity: count];
for (int i=0; i<count; i++) {
SF_INFO thisInfo;
thisInfo.format = outformat;
thisInfo.channels = 1;
thisInfo.samplerate = inputinfo.samplerate;
NSString *outpath = [[filename stringByAppendingPathComponent:[channelOrderArray objectAtIndex:i]]
stringByAppendingPathExtension:type];
NSLog(@"outpath: %@", outpath);
outputfiles[i] = sf_open([outpath cStringUsingEncoding:NSUTF8StringEncoding], SFM_WRITE, &thisInfo);
if(outputfiles[i] == NULL) {
NSLog(@"%@", [NSString stringWithFormat: @"Failed to open file %@ for writing.", outpath]);
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: [NSString stringWithFormat: @"Failed to open file %@ for writing.", outpath] waitUntilDone:YES];
closeSNDFiles(outputfiles, i); // close the files already open
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
[self deletePaths: outpaths]; // delete created output files
[autoreleasepool release];
return;
}
[outpaths addObject: outpath];
outfileinfos[i] = thisInfo;
}
// do the deinterleave
int result = deinterleave(outputfiles, outfileinfos, count, inputfile, inputinfo, kFramesToReadWrite);
// check for errors; could post libsndfile errors
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Write failed!" waitUntilDone:YES];}
result = closeSNDFiles(outputfiles, count);
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Output files could not be closed!" waitUntilDone:YES];}
result = sf_close(inputfile);
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Input file could not be closed!" waitUntilDone:YES];}
lock();
if(gRunning) {
gRunning = 0; // immediately stop progress updates in main thread
unlock();
[textField performSelectorOnMainThread:@selector(setStringValue:)
withObject:@"Done."
waitUntilDone:YES];
[textField performSelectorOnMainThread:@selector(display)
withObject:nil
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(setProgessBarValue:)
withObject:[NSNumber numberWithDouble: 100.0]
waitUntilDone:YES];
} else {
unlock();
[textField performSelectorOnMainThread:@selector(setStringValue:)
withObject:@"Cancelled."
waitUntilDone:YES];
[textField performSelectorOnMainThread:@selector(display)
withObject:nil
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(setProgessBarValue:)
withObject:[NSNumber numberWithDouble: 0.0]
waitUntilDone:YES];
// delete cancelled output files
[self deletePaths: outpaths];
}
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(resetFileTable)
withObject:nil
waitUntilDone:YES];
[autoreleasepool release];
}
// since we're not specifying the actual path when deinterleaving, just the base
// we need this to warn users that files will be overwritten
- (int)noOverwritesInDir:(NSString *)path usingPathExtension:(NSString *)ext
{
NSLog(@"checking for overwrites");
int i;
NSMutableArray *outpaths = [NSMutableArray arrayWithCapacity: numOutputFiles];
NSMutableArray *existingFiles = [NSMutableArray arrayWithCapacity: numOutputFiles];
for (i=0; i<numOutputFiles; i++) {
NSString *outpath = [[path stringByAppendingPathComponent:[channelOrderArray objectAtIndex:i]]
stringByAppendingPathExtension: ext];
NSLog(@"outpath tested: %@", outpath);
if([[NSFileManager defaultManager] fileExistsAtPath: outpath]) {
NSLog(@"File Exists");
[outpaths addObject: outpath];
[existingFiles addObject: [outpath lastPathComponent]];
}
}
if([outpaths count] > 0) {
NSLog(@"Alert coming");
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Replace"];
[alert addButtonWithTitle:@"Select New Directory"];
[alert addButtonWithTitle:@"Change Output Filenames"];
NSString *filesString = [existingFiles description];
NSRange range = {1, [filesString length] - 2 };
[alert setMessageText:[NSString stringWithFormat: @"The files:\n%@\nalready exist. Do you wish to replace them?",
[filesString substringWithRange: range]]];
[alert setInformativeText: [NSString stringWithFormat:
@"Files with the same names already exist in %@. Replacing them will overwrite their current contents.",
[path lastPathComponent]]];
[alert setAlertStyle:NSAlertStyleWarning];
NSModalResponse clicked = [alert runModal];
switch(clicked) {
case NSAlertFirstButtonReturn:
[self deletePaths: outpaths];
[alert release];
return 1;
case NSAlertSecondButtonReturn:
[alert release];
return 0;
case NSAlertThirdButtonReturn:
[alert release];
return 2;
}
} else {
return 1;
}
return 1;
}
- (void)deletePaths:(NSArray *)outpaths {
NSString *aPath;
NSEnumerator *pathEnumerator = [outpaths objectEnumerator];
while (aPath = [pathEnumerator nextObject]) {
BOOL delete = [[NSFileManager defaultManager] removeItemAtPath: aPath error: nil];
if(!delete) [self makeWarningWithText: [NSString stringWithFormat: @"Cancelled output file %@ could not be deleted!", aPath]];
}
}
- (IBAction)interleave:(id)sender
{
operation = @selector(spawnInterleaveThread);
// opening stuff
NSInteger result;
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:YES];
result = [op runModal];
if (result == NSFileHandlingPanelOKButton) {
NSArray *filesToOpen = [op URLs];
NSLog(@"%@", [filesToOpen description]);
// set array for TableView
[channelOrderArray removeAllObjects];
[channelOrderArray addObjectsFromArray:filesToOpen];
[channelOrderTable reloadData];
[startCancelButton setEnabled: YES];
[startCancelButton setState: NSOffState];
[mainWindow makeFirstResponder: startCancelButton];
[startCancelButton display];
[fileColumn setEditable: NO];
[[fileColumn headerCell] setObjectValue:@"Files to Interleave (Drag to Change Channel Order)"];
}
}
- (void)spawnInterleaveThread {
NSUInteger count = [channelOrderArray count];
if(count <= 1) {
[self makeWarningWithText:@"Select at least two input files!"];
[self setViewEnabledStatusRunning:[NSNumber numberWithBool: NO]];
[mainWindow display];
return;
}
// saving stuff
NSSavePanel *sp;
/* create or get the shared instance of NSSavePanel */
sp = [NSSavePanel savePanel];
/* set up new attributes */
[sp setTitle: @"Set Path for Interleaved Output File"];
[sp setAllowedFileTypes:[NSArray arrayWithObject: @"aiff"] ];
[sp setCanSelectHiddenExtension: YES];
NSPopUpButton *typesMenu;
NSRect bRect = {0, 0, 130, 30};
typesMenu = [[NSPopUpButton alloc] initWithFrame:bRect pullsDown:NO];
[typesMenu addItemsWithTitles: [types allKeys]];
[typesMenu selectItemWithTitle:@"aiff"];
[typesMenu setTarget: self];
[typesMenu setAction: @selector(setExtension:)];
NSBox *box = [NSBox new];
[box setTitle: @"Output File Format"];
[box setTitlePosition: NSAtTop];
[box setBorderType: NSNoBorder];
[box addSubview: typesMenu];
[box sizeToFit];
[box setAutoresizingMask: NSViewNotSizable];
[sp setAccessoryView: box];
// make a plausible initial output file name
NSString *startingName = [[[[channelOrderArray objectAtIndex:0] path] lastPathComponent] stringByDeletingPathExtension];
NSRange lastCharRange = NSMakeRange([startingName length] - 1, 1);
if(([startingName compare:@"L" options:NSCaseInsensitiveSearch range:lastCharRange] == NSOrderedSame) ||
([startingName compare:@"0" options:NSLiteralSearch range:lastCharRange] == NSOrderedSame) ||
([startingName compare:@"1" options:NSLiteralSearch range:lastCharRange] == NSOrderedSame)
) {
startingName = [startingName substringToIndex: [startingName length] - 1];
}
startingName = [[startingName stringByAppendingString:@"-int"] stringByAppendingPathExtension:@"aiff"];
// display the NSSavePanel
NSInteger runResult;
runResult = [sp runModal];
// if successful, save file under designated name */
if (runResult == NSFileHandlingPanelOKButton) {
[self setViewEnabledStatusRunning:[NSNumber numberWithBool: YES]];
NSString *filename = [[sp URL] path];
[textField setStringValue:[NSString stringWithFormat: @"Interleaving File: %@", [filename lastPathComponent]]];
[textField display];
NSDictionary *params = @{ @"type" : [typesMenu titleOfSelectedItem], @"filename" : filename, @"count" : [NSNumber numberWithUnsignedLong:count]};
[NSThread detachNewThreadSelector: @selector(doInterleave:) toTarget: self withObject: params];
} else {
lock();
gRunning = 0;
unlock();
[startCancelButton setNextState];
[mainWindow display];
}
}
- (void)doInterleave:(NSDictionary *)params {
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
NSString *type = [params objectForKey:@"type"];
NSString *filename = [params objectForKey:@"filename"];
int i, count = [[params objectForKey:@"count"] intValue];
SNDFILE* inputfiles[count];
SF_INFO inputinfos[count];
for (i=0; i<count; i++) {
SF_INFO thisInfo;
thisInfo.format = 0;
NSString *aFile = [[channelOrderArray objectAtIndex:i] path];
inputfiles[i] = sf_open([aFile cStringUsingEncoding:NSUTF8StringEncoding], SFM_READ, &thisInfo);
if(inputfiles[i] == NULL) {
NSLog(@"%@", [NSString stringWithFormat: @"File %@ failed to open", aFile]);
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: [NSString stringWithFormat: @"File %@ failed to open!", aFile] waitUntilDone:YES];
closeSNDFiles(inputfiles, i); // close the files already open
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
return;
}
if(thisInfo.channels != 1) {
NSLog(@"%@", [NSString stringWithFormat: @"Input File %@ is multichannel!", aFile]);
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: [NSString stringWithFormat: @"Input File %@ is multichannel!", aFile] waitUntilDone:YES];
closeSNDFiles(inputfiles, i + 1); // close the files already open
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
return;
}
inputinfos[i] = thisInfo;
}
// set parameters for info
SF_INFO outfileinfo;
int inputSubtype, outMajorFormat, outformat;
inputSubtype = inputinfos[0].format & SF_FORMAT_SUBMASK;
outMajorFormat = [[types objectForKey: type] intValue];
outformat = outMajorFormat | inputSubtype; // take sample format of first input file
outfileinfo.format = outformat;
outfileinfo.samplerate = inputinfos[0].samplerate;
outfileinfo.channels = count;
if(!sf_format_check(&outfileinfo)) {
outfileinfo.format = outMajorFormat | SF_FORMAT_FLOAT; // all output formats support this
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Input sample format not supported!\nUsing 32 bit float instead." waitUntilDone:YES];
}
SNDFILE* outfile = sf_open([filename cStringUsingEncoding:NSUTF8StringEncoding], SFM_WRITE, &outfileinfo);
if(outfile == NULL) {
NSLog(@"%@", [NSString stringWithFormat: @"File %@ failed to open", filename]);
[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: [NSString stringWithFormat: @"File %@ failed to open!", filename] waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
[autoreleasepool release];
return;
}
// do the interleave
int result = interleave(inputfiles, inputinfos, count, outfile, outfileinfo, kFramesToReadWrite);
// check for errors; could post libsndfile errors
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Write failed!" waitUntilDone:YES];}
result = closeSNDFiles(inputfiles, count);
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Input files could not be closed!" waitUntilDone:YES];}
result = sf_close(outfile);
if(result) {[self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Output file could not be closed!" waitUntilDone:YES];}
lock();
if(gRunning) {
gRunning = 0; // immediately stop progress updates in main thread
unlock();
[textField performSelectorOnMainThread:@selector(setStringValue:)
withObject:@"Done."
waitUntilDone:YES];
[textField performSelectorOnMainThread:@selector(display)
withObject:nil
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(setProgessBarValue:)
withObject:[NSNumber numberWithDouble: 100.0]
waitUntilDone:YES];
} else {
NSLog(@"Set UI Cancelled");
unlock();
[textField performSelectorOnMainThread:@selector(setStringValue:)
withObject:@"Cancelled."
waitUntilDone:YES];
[textField performSelectorOnMainThread:@selector(display)
withObject:nil
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(setProgessBarValue:)
withObject:[NSNumber numberWithDouble: 0.0]
waitUntilDone:YES];
// delete cancelled output file
BOOL delete = [[NSFileManager defaultManager] removeItemAtPath: filename error: nil];
if(!delete) [self performSelectorOnMainThread:@selector(makeWarningWithText:) withObject: @"Cancelled output file could not be deleted!" waitUntilDone:YES];
}
[self performSelectorOnMainThread:@selector(setViewEnabledStatusRunning:)
withObject:[NSNumber numberWithBool: NO]
waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(resetFileTable)
withObject:nil
waitUntilDone:YES];
[autoreleasepool release];
}
- (void)setExtension:(id)fileTypeMenu {
NSSavePanel *panel = (NSSavePanel *)[fileTypeMenu window];
[panel setAllowedFileTypes:[NSArray arrayWithObject: [fileTypeMenu titleOfSelectedItem]] ];
}
- (void)makeWarningWithText:(NSString *)text {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:text];
[alert setAlertStyle:NSAlertStyleWarning];
[alert runModal];
[alert release];
}
- (void)updateProgressBar:(NSTimer*)timer {
// stop the timer if done
lock();
if(!gRunning) {
unlock();
NSLog(@"Timer to be invalidated");
[timer invalidate];
} else {
double newval = (double)gIndex * gLongestRecipPct;
unlock();
[progressBar setDoubleValue: newval];
[progressBar display];
}
}
// used for setting values when done or cancelled
// can't call on progressBar directly since performSelectorOnMainThread needs an id
-(void)setProgessBarValue:(NSNumber *)val {
[progressBar setDoubleValue: [val doubleValue]];
[progressBar display];
}
-(void)setViewEnabledStatusRunning:(NSNumber *)flag {
BOOL isRunning = [flag boolValue];
[interleaveMenuItem setEnabled: !isRunning];
[deinterleaveMenuItem setEnabled: !isRunning];
[startCancelButton setEnabled: isRunning];
[startCancelButton display];
[mainWindow display];
}
-(void)resetFileTable {
[fileColumn setEditable: NO];
[[fileColumn headerCell] setObjectValue:@"Filename"];
[channelOrderArray removeAllObjects];
[channelOrderTable reloadData];
}
// The data-source protocol methods
- (NSUInteger)numberOfRowsInTableView:(NSTableView *)aTable
{
return ([channelOrderArray count]);
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
NSParameterAssert(rowIndex >= 0 && rowIndex < [channelOrderArray count]);
if([[aTableColumn identifier] isEqualToString:@"Channel"]) { return [NSString stringWithFormat:@"%i", rowIndex + 1]; }
return [channelOrderArray objectAtIndex:rowIndex];
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
[channelOrderArray replaceObjectAtIndex:rowIndex withObject:anObject];
}
// drag and drop
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndices toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndices];
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pboard setData:data forType:NSStringPboardType];
return YES;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
row:(int)row dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard* pboard = [info draggingPasteboard];
// a file was dropped
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
// Perform operation using the list of files
NSLog(@"Got a file(s): %@", files);
return YES;
}
// Or a row was dragged
NSData* rowData = [pboard dataForType:NSStringPboardType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSUInteger dragRow = [rowIndexes firstIndex];
NSString* path = [channelOrderArray objectAtIndex:dragRow];
[channelOrderArray removeObjectAtIndex:dragRow];
int newIndex;
if(dragRow > row) {
newIndex = row;
} else {
newIndex = row - 1;
}
[channelOrderArray insertObject:path atIndex:newIndex];
[channelOrderTable reloadData];
[channelOrderTable selectRowIndexes:[NSIndexSet indexSetWithIndex:newIndex] byExtendingSelection:NO];
return YES;
}
// only allow drop between rows
- (NSDragOperation) tableView: (NSTableView*) tableView
validateDrop: (id ) info
proposedRow: (int) row
proposedDropOperation: (NSTableViewDropOperation) op
{
int result = NSDragOperationNone;
if (op == NSTableViewDropAbove) {
result = NSDragOperationMove;
}
return (result);
}
// toolbar
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObjects:NSToolbarFlexibleSpaceItemIdentifier, @"intTBItem", @"deintTBItem", nil];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObjects:@"intTBItem", NSToolbarFlexibleSpaceItemIdentifier, @"deintTBItem", nil];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSImage* tbImage;
NSToolbarItem* item;
if([itemIdentifier isEqualToString:@"deintTBItem"]) {
tbImage = [NSImage imageNamed: @"deinttb.pdf"];
if(tbImage) {
item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
[item setImage: tbImage];
[item setLabel: @"Deinterleave"];
[item setToolTip: @"Deinterleave a multichannel file to mono files"];
[item setAction:@selector(deinterleave:)];
[item setTarget:self];
return item;
}
} else if([itemIdentifier isEqualToString:@"intTBItem"]) {
tbImage = [NSImage imageNamed: @"inttb.pdf"];
if(tbImage) {
item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
[item setImage: tbImage];
[item setLabel: @"Interleave"];
[item setToolTip: @"Interleave a number of mono files into a multichannel file"];
[item setAction:@selector(interleave:)];
[item setTarget:self];
return item;
}
}
return nil;
}
// disable while running
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
if(gRunning) return NO;
return YES;
}
@end
// Might want to port this or a commandline version
// so keep these as separate functions for easy reuse
int interleave(SNDFILE* inputfiles[], SF_INFO inputinfos[], int numFiles, SNDFILE* outfile, SF_INFO outfileinfo, int framesToRead) {
int i, j, longest = 0;
sf_count_t k;
for(i = 0; i < numFiles; i++) {
sf_count_t size;
size = inputinfos[i].frames;
if((int)size > longest) longest = (int)size;
}
lock();
gLongestRecipPct = 100.0 / (double)longest;
unlock();
for(i=0; i < longest; i = i + framesToRead) {
lock();
gIndex = i; // update global index
if(!gRunning) {
unlock();
break; // exit the loop if cancelled
}
unlock();
// adjust for smaller than framesToRead if needed
int thisRead = (i + framesToRead) > longest ? longest - i : framesToRead;
float interleaved[numFiles * thisRead];
for(j=0; j < numFiles; j++) {
float channel[thisRead];
sf_count_t framesRead = sf_readf_float(inputfiles[j], channel, (sf_count_t)thisRead);
// check results and zeropad if needed
if(framesRead < thisRead) {
for(k=framesRead; k < thisRead; k++) channel[k] = 0.0;
}
for(k=0; k < thisRead; k++) {
interleaved[k * numFiles + j] = channel[k];
}
}
sf_count_t framesWritten = sf_writef_float(outfile, interleaved, (sf_count_t)(thisRead));
if((int)framesWritten < (thisRead)) {
printf("Warning! Write failed!\n");
return(1);
}
}
return(0);
// closing is the responsibility of the calling function
}
int deinterleave(SNDFILE* outputfiles[], SF_INFO outputinfos[], int numFiles, SNDFILE* infile, SF_INFO infileinfo, int framesToWrite) {
int i, j, k, length;
length = (int)infileinfo.frames; // each output file will be this long
lock();
gLongestRecipPct = 100.0 / (double)length;
unlock();
for(i=0; i < length; i = i + framesToWrite) {
lock();
gIndex = i; // update global index
if(!gRunning) {
unlock();
break; // exit the loop if cancelled
}
unlock();
// adjust for smaller than framesToWrite if needed
int thisRead = (i + framesToWrite) > length ? length - i : framesToWrite;
//int thisRead = (int)infileinfo.channels * thisWrite;
float interleaved[(int)infileinfo.channels * thisRead];
sf_count_t framesRead = sf_readf_float(infile, interleaved, (sf_count_t)thisRead);
if(framesRead < thisRead) {
printf("Warning! Read failed!\n");
return(1);
}
for(j=0; j < numFiles; j++) {
float channel[thisRead];
for(k=0; k < thisRead; k++) {
channel[k] = interleaved[k * numFiles + j];
}
sf_count_t framesWritten = sf_writef_float(outputfiles[j], channel, (sf_count_t)thisRead);
if((int)framesWritten < thisRead) {
printf("Warning! Write failed!\n");
return(1);
}
}
}
return(0);
// closing is the responsibility of the calling function
}
int closeSNDFiles(SNDFILE* files[], int numFiles) {
int i, result;
for(i=0; i < numFiles; i++) {
result = sf_close(files[i]);
if(result) return result;
}
return(0);
}