-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting7.html
executable file
·870 lines (695 loc) · 28.5 KB
/
listing7.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>audioburntest - /main.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Carbon/index.html">Carbon</a> > <a href="../../samplecode/Carbon/idxFileManagement-date.html">File Management</a> > <A HREF="javascript:location.replace('index.html');">audioburntest</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">audioburntest</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/main.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/discrecutils/dru_burning.c</option>
<option value="listing2.html">/discrecutils/dru_burning.h</option>
<option value="listing3.html">/discrecutils/dru_devices.c</option>
<option value="listing4.html">/discrecutils/dru_devices.h</option>
<option value="listing5.html">/discrecutils/dru_progress.c</option>
<option value="listing6.html">/discrecutils/dru_progress.h</option>
<option value="listing7.html">/main.c</option></select>
</p>
</form>
<p><strong><a href="audioburntest.zip">Download Sample</a></strong> (“audioburntest.zip”, 21.0K)<BR>
<strong><a href="audioburntest.dmg">Download Sample</a></strong> (“audioburntest.dmg”, 79.3K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">#include <glob.h>
#include "dru_devices.h"
#include "dru_burning.h"
#import <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
typedef struct AIFFFileInfo
{
int fd; // The file descriptor of the open file
unsigned numChannels; // The number of channels of audio
unsigned sampleFreq; // the sampling frequency. We're only going to support 44.1KHz.
unsigned sampleBits; // bits/sample
unsigned sampleBytes; // number of bytes for each sample. Calculated value
unsigned long dataStart; // Where the sound data starts in the file
unsigned long dataEnd; // Where the sound data ends in the file.
unsigned long cursor; // where in the file we're at
int mask; // channel mixing mask
} AIFFFileInfo;
int main(int argc, char *argv[]);
/* Creating the audio tracks */
CFArrayRef createTracks();
CFMutableDictionaryRef
createAudioTrackProperties(uint32_t trackLength);
/* track production callback functions */
OSStatus sampleTrackProducerCallback(DRTrackRef track, DRTrackMessage message, void* ioParam);
OSStatus preBurnCallback(DRTrackRef track);
OSStatus productionCallback(DRTrackRef track, DRTrackProductionInfo* prodInfo);
OSStatus postBurnCallback(DRTrackRef track);
/* utility routines */
void expandPathname(char *path);
AIFFFileInfo* parseFile(char* filepath);
char* promptForFolder(const char *prompt, const char *defaultPath, char* folderPath, int folderPathLen);
void trackRefConDestructor(const void* refCon);
#pragma mark -
int main(int argc, char *argv[])
{
#pragma unused(argc, argv)
DRDeviceRef device = NULL;
DRBurnRef burn = NULL;
CFArrayRef trackLayout = NULL;
/* Hello world! */
printf("Welcome to the audio burn sample code!\n\n");
/* First, let's use DRU to prompt the user to
pick a device. If there's only one device to
choose, the selection is automatic. */
device = druPromptForDevice(NULL,druFilter_AnyBurner);
/* We know we're going to burn to this device - acquire blank media
reservations just in case the user inserts something.
Just because we ask for a shot at blank media doesn't mean that
we'll get it. Some other app might not be willing to give
up the blank media for our use. */
DRDeviceAcquireMediaReservation(device);
/* create the AIFF tracks we'll be burning to CD. */
trackLayout = createTracks();
/* Next, use DRU to prompt the user to insert blank media. */
druPromptForBlankMediaInDevice(device);
/* Check to see if we have the media reservation. If we don't
have it that means that some other application has it and
won't give it up. We might not get it if, for instance, if
the Finder has claimed the media for it's CD proxy and the user
has placed files onto that proxy image. In this case the
Finder wouldn't want to unmount the proxy image since the
user's intention is to burn that information to the inserted
disc. */
if (druMediaIsReserved(device))
{
/* We've got blank media, and a folder to burn ...
now set up the burn objects. */
burn = DRBurnCreate(device);
/* Use DRU to run the burn and report progress. */
druBurn(burn,trackLayout);
}
else
{
printf("The media in the selected device is reserved for use by another application.\n");
}
/* Clean up after ourselves. */
if (burn != NULL)
CFRelease(burn);
if (trackLayout != NULL)
CFRelease(trackLayout);
if (device != NULL)
{
DRDeviceReleaseMediaReservation(device);
CFRelease(device);
}
return 0;
}
CFArrayRef createTracks()
{
CFMutableArrayRef trackArray = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
char path[PATH_MAX];
int pathLen;
/* Prompt the user to pick a folder of AIFF's to burn. */
promptForFolder(NULL,"~/Music", path, PATH_MAX);
pathLen = strlen(path);
// Remove trailing slashes, if any.
if (pathLen>0 && path[pathLen-1] == '/') pathLen--;
// Open the directory.
DIR *dirfd = opendir((const char*)path);
if (dirfd == NULL) return 0;
while (1)
{
CFMutableStringRef fileStr;
CFStringRef aStr;
AIFFFileInfo* info;
char filepath[PATH_MAX];
DRRefConCallbacks callbacks;
CFMutableDictionaryRef properties;
/* Get the next child. This is threadsafe, because the struct dirent* is
not global; it's actually pointing to a buffer that was malloc'ed
inside the DIR structure. */
struct dirent *childPtr = readdir(dirfd);
if (childPtr == NULL) break;
int nameLength = childPtr->d_namlen;
char *name = childPtr->d_name;
if (childPtr->d_type == DT_DIR)
continue;
/* Skip "." and ".." entries, and AppleDouble "._" entries which
we treat as one file. */
if (name[0] == '.' && (nameLength == 1 || name[1] == '_' || (name[1] == '.' && nameLength == 2)))
continue;
/* skip all non-AIFF files by looking at the extension. */
if (strncmp(".aiff", &name[nameLength-5], 5) != 0 && strncmp(".aif", &name[nameLength-4], 4) != 0)
continue;
aStr = CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingUTF8);
fileStr = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, aStr);
CFRelease(aStr);
CFStringAppendCString(fileStr, "/", kCFStringEncodingUTF8);
CFStringAppendCString(fileStr, name, kCFStringEncodingUTF8);
/* parse out the file information and construct an information structure
to hold this info for use when we start the burn and start producing data. */
CFStringGetCString(fileStr, filepath, PATH_MAX, kCFStringEncodingUTF8);
info = parseFile(filepath);
/* Create the properties dictionary describing this track. */
properties = createAudioTrackProperties(((info->dataEnd - info->dataStart) / (info->sampleBytes * info->numChannels) * 4) / 2352);
/* Add the file path for this file to the track properties. This will
be used by the producer callback later. */
CFDictionarySetValue(properties, CFSTR("kTrackSourceFilePath"), fileStr);
CFRelease(fileStr);
/* Create a track that is described by properties and whose data is
produced by sampleTrackProducerCallback */
DRTrackRef track = DRTrackCreate(properties, sampleTrackProducerCallback);
/* stuff the info structure pointer into the refCon of the track object. Each object in
DiscRecording has a refCon associated with it. */
callbacks.version = 0;
callbacks.retain = NULL;
callbacks.release = trackRefConDestructor; /* we just need to destroy it when the track is released */
DRSetRefCon(track, info, &callbacks);
/* Put the track into the array for burning */
CFArrayAppendValue(trackArray, track);
CFRelease(properties);
CFRelease(track);
}
closedir(dirfd);
if (CFArrayGetCount(trackArray) == 0)
{
printf("No AIFF files were located! Aborting.\n");
exit(1);
}
return trackArray;
}
CFMutableDictionaryRef createAudioTrackProperties(uint32_t trackLength)
{
CFMutableDictionaryRef properties = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFNumberRef value;
uint32_t temp;
/* Create a properties dictionary for all of the tracks. This dictionary
is common to each since each will be an audio track and other than the size
will be identical. */
temp = kDRBlockSizeAudio;
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &temp);
CFDictionaryAddValue(properties, kDRBlockSizeKey, value);
CFRelease(value);
temp = kDRBlockTypeAudio;
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &temp);
CFDictionaryAddValue(properties, kDRBlockTypeKey, value);
CFRelease(value);
temp = kDRDataFormAudio;
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &temp);
CFDictionaryAddValue(properties, kDRDataFormKey, value);
CFRelease(value);
temp = kDRSessionFormatAudio;
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &temp);
CFDictionaryAddValue(properties, kDRSessionFormatKey, value);
CFRelease(value);
temp = kDRTrackModeAudio;
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &temp);
CFDictionaryAddValue(properties, kDRTrackModeKey, value);
CFRelease(value);
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &trackLength);
CFDictionarySetValue(properties, kDRTrackLengthKey, value);
CFRelease(value);
return properties;
}
/* This is a dispatcher to take the message and callout to different
functions to handle the messages we handle. */
OSStatus sampleTrackProducerCallback(DRTrackRef track, DRTrackMessage message, void* ioParam)
{
switch (message)
{
case kDRTrackMessagePreBurn:
return preBurnCallback(track);
break;
case kDRTrackMessageProduceData:
return productionCallback(track, (DRTrackProductionInfo*)ioParam);
break;
case kDRTrackMessagePostBurn:
return postBurnCallback(track);
break;
}
return noErr;
}
/* This callback is called before the burn is to begin. It it we open up the AIFF file to read
from and make sure it's valid (this could have been done before we started also). We also
set up the final length of the track */
OSStatus preBurnCallback(DRTrackRef track)
{
CFDictionaryRef properties = DRTrackGetProperties(track);
CFStringRef fileStr = CFDictionaryGetValue(properties, CFSTR("kTrackSourceFilePath"));
char filepath[PATH_MAX];
AIFFFileInfo* info = (AIFFFileInfo*)DRGetRefCon(track);
/* Get the file string from the properties dictionary and get the c string from that
to open the file using open */
CFStringGetCString(fileStr, filepath, PATH_MAX, kCFStringEncodingUTF8);
info->fd = open(filepath, O_RDONLY, 0);
if (info->fd == -1) return kDRDataProductionErr;
fcntl(info->fd, F_NOCACHE, 1); /* Don't cache the file in the UBC */
lseek(info->fd, info->dataStart, SEEK_SET); /* put the file pointer at the start of the sound data */
info->mask = ((int)-0x10000) >> info->sampleBits; /* set up the mask to do channel mixing */
info->cursor = info->dataStart;
return noErr;
}
/* The meat of the producer code. This callback is called repeatedly during a burn.
It's this function's job to write data into the passed in buffer each time it's
called. It's best if you can fill up the buffer completely. The buffer is
a multiple of the track's block size in length. */
OSStatus productionCallback(DRTrackRef track, DRTrackProductionInfo* prodInfo)
{
unsigned long expectedFrames = (prodInfo->reqCount / 4);
unsigned long readSize, readLen;
char* tempBuffer;
char* outBuffer = prodInfo->buffer;
unsigned long outLength;
unsigned long i;
unsigned long step;
AIFFFileInfo* info = (AIFFFileInfo*)DRGetRefCon(track);
readSize = (info->sampleBytes * info->numChannels) * expectedFrames;
step = info->sampleBytes*info->numChannels;
if (readSize + info->cursor > info->dataEnd)
readSize = info->dataEnd - info->cursor;
tempBuffer = (char*)malloc(readSize);
readLen = read(info->fd, tempBuffer, readSize);
if (readSize != readLen) return kDRDataProductionErr;
outLength = 0;
for (i=0; i<readSize; i += step)
{
unsigned short leftSample = 0, rightSample = 0;
unsigned const char* sampleFrame = tempBuffer + i;
#define READ_SAMPLE_POINT(frame,index) \
((*(uint16_t*)(frame + info->sampleBytes*index)) & info->mask)
#define MIX(a,b) \
(((a * 2) / 3) + ((b * 2) / 3))
/* Handle our wonderful multichannel logic. Yeah, it's almost certainly unnecessary,
but it took an extra 2 minutes to write it this way since I was already thinking
about how to parse the AIFF properly. Maybe it'll make someone's world a better place. */
if (info->numChannels == 2)
{
leftSample = READ_SAMPLE_POINT(sampleFrame,0);
rightSample = READ_SAMPLE_POINT(sampleFrame,1);
}
else if (info->numChannels == 1)
{
leftSample = READ_SAMPLE_POINT(sampleFrame,0);
rightSample = leftSample;
}
else if (info->numChannels == 3)
{
unsigned short centerSample = READ_SAMPLE_POINT(sampleFrame,2);
leftSample = MIX(READ_SAMPLE_POINT(sampleFrame,0),centerSample);
rightSample = MIX(READ_SAMPLE_POINT(sampleFrame,1),centerSample);
}
else if (info->numChannels == 4)
{
/* The spec is unclear on how to distinguish quadrophonic vs 4 ch surround AIFFs, as
they both have four channels. Since surround seems to be the winner of these
ancient audio wars, for now I'm going to assume 4 channels means surround.
unsigned short that it matters. */
UInt16 centerSample = MIX(READ_SAMPLE_POINT(sampleFrame,1),READ_SAMPLE_POINT(sampleFrame,3));
leftSample = MIX(READ_SAMPLE_POINT(sampleFrame,0),centerSample);
rightSample = MIX(READ_SAMPLE_POINT(sampleFrame,2),centerSample);
}
else if (info->numChannels == 6)
{
unsigned short centerSample = MIX(READ_SAMPLE_POINT(sampleFrame,2),READ_SAMPLE_POINT(sampleFrame,5));
leftSample = MIX(MIX(READ_SAMPLE_POINT(sampleFrame,0),READ_SAMPLE_POINT(sampleFrame,1)),centerSample);
rightSample = MIX(MIX(READ_SAMPLE_POINT(sampleFrame,3),READ_SAMPLE_POINT(sampleFrame,4)),centerSample);
}
else
return kDRDataProductionErr;
#undef READ_SAMPLE_POINT
#undef MIX
// Dump the samples into the output.
((unsigned short*)outBuffer)[0] = ((leftSample & 0xFF00) >> 8) + ((leftSample & 0x00FF) << 8);
((unsigned short*)outBuffer)[1] = ((rightSample & 0xFF00) >> 8) + ((rightSample & 0x00FF) << 8);
outBuffer += 4;
outLength += 4;
}
free(tempBuffer);
info->cursor += outLength;
prodInfo->actCount = outLength;
return noErr;
}
/* This callback comes in after the burn is finished and all data has been written
to disc and optionally verified. All we need to do is to close the file opened in
preBurn. */
OSStatus postBurnCallback(DRTrackRef track)
{
AIFFFileInfo* info = (AIFFFileInfo*)DRGetRefCon(track);
close(info->fd);
info->fd = 0;
return noErr;
}
void trackRefConDestructor(const void* refCon)
{
AIFFFileInfo* info = (AIFFFileInfo*)refCon;
if (info->fd != 0)
{
close(info->fd);
info->fd = 0;
}
free(info);
}
char* promptForFolder(const char *prompt, const char *defaultPath, char* folderPath, int folderPathLen)
{
char path[PATH_MAX];
OSStatus err;
Boolean isDirectory;
int len;
FSRef ref;
/* Display the prompt. */
if (prompt == NULL)
prompt = "Please enter the path to a folder:";
printf("%s ", prompt);
if (defaultPath != NULL)
printf("[%s] ",defaultPath);
fflush(stdout);
/* Get user input, and trim trailing newlines. */
fgets(path,sizeof(path),stdin);
for (len = strlen(path); len > 0 && path[len-1] == '\n';)
path[--len] = 0;
if (path[0] == 0)
strcpy(path,defaultPath);
/* Expand magic characters just like a shell (mostly so ~ will work) */
expandPathname(path);
/* Convert the path into an FSRef, which is what the burn engine needs. */
err = FSPathMakeRef((const UInt8*)path,&ref,&isDirectory);
if (err != noErr)
{
printf("Bad path. Aborting. (%d)\n", (int)err);
exit(1);
}
if (!isDirectory)
{
printf("That's a file, not a directory! Aborting.\n");
exit(1);
}
return strncpy(folderPath, path, folderPathLen);
}
void expandPathname(char *path)
{
char original[PATH_MAX];
int i;
glob_t g = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* Preserve the incoming path */
strcpy(original,path);
/* Call glob to expand it. */
glob(path,GLOB_NOSORT | GLOB_TILDE | GLOB_QUOTE,NULL,&g);
if (g.gl_matchc == 1)
strcpy(path,g.gl_pathv[0]);
else
{
printf("Bad path! ");
if (g.gl_matchc == 0)
printf("Not found. ");
else
{
printf("%d matches found:\n",g.gl_matchc);
for (i=0; i<g.gl_matchc; ++i)
printf(" %s\n", g.gl_pathv[i]);
}
printf("Aborted.\n");
exit(1);
}
globfree(&g);
}
#if PRAGMA_STRUCT_ALIGN
#pragma options align=mac68k
#elif PRAGMA_STRUCT_PACKPUSH
#pragma pack(push, 2)
#elif PRAGMA_STRUCT_PACK
#pragma pack(2)
#endif
typedef struct IFFChunkHeader IFFChunkHeader;
typedef struct AIFFHeader AIFFHeader;
typedef struct AIFFChunk AIFFChunk;
typedef struct AIFFCommonChunk AIFFCommonChunk;
typedef struct AIFFSoundChunk AIFFSoundChunk;
struct IFFChunkHeader
{
UInt32 chunkID; // id for this chunk
UInt32 chunkSize; // size of this chunk not including header
};
struct AIFFChunk
{
UInt32 chunkID; // id for this chunk
UInt32 chunkSize; // size of this chunk not including header
UInt32 data[1]; // streamed data
};
struct AIFFHeader
{
UInt32 form; // 'FORM'
UInt32 formChunkSize; // largely ignored, but it should be (filesize - 8)
UInt32 aiff; // 'AIFF'
AIFFChunk chunk[1]; // packed array of chunks
};
struct AIFFCommonChunk
{
UInt32 comm; // 'COMM'
UInt32 commChunkSize; // size of this chunk not including header (18 bytes)
UInt16 numChannels; // number of sound channels
UInt32 numSampleFrames; // number of sample frames - probably incorrect!
UInt16 sampleSize; // sample size in bits (eg, 16)
UInt32 sampleFreq; // sample frequency
UInt8 zeroes[6]; // AIFF defines the preceding as an 80-bit IEEE 754 fp number... most folks seem to use only 4 bytes
};
struct AIFFSoundChunk
{
UInt32 ssnd; // 'SSND'
UInt32 ssndChunkSize; // size of this chunk not including header (8 bytes + samples)
UInt32 offset; // used for aligning, usually zero
UInt32 blockSize; // used for aligning, usually zero
};
const UInt32 AIFF_SAMPLE_RATE_44_1 = 0x400EAC44;
const UInt32 AIFF_SAMPLE_RATE_44_0 = 0x400DAC44;
#if PRAGMA_STRUCT_ALIGN
#pragma options align=reset
#elif PRAGMA_STRUCT_PACKPUSH
#pragma pack(pop)
#elif PRAGMA_STRUCT_PACK
#pragma pack()
#endif
AIFFFileInfo* parseFile(char* filepath)
{
AIFFFileInfo* info = (AIFFFileInfo*)malloc(sizeof(AIFFFileInfo));
AIFFHeader aiffHeader;
AIFFCommonChunk commonChunk;
AIFFSoundChunk soundChunk;
IFFChunkHeader chunkHeader;
ssize_t readLen;
off_t filepos;
/* Initialize our file info to reasonable values. */
info->numChannels = 0;
info->sampleFreq = 0;
info->sampleBits = 0;
info->sampleBytes = 0;
info->dataStart = 0;
info->dataEnd = 0;
info->cursor = 0;
info->mask = 0;
/* Get a c string of the file path so we can open up the file */
info->fd = open(filepath, O_RDONLY, 0);
if (info->fd == -1)
{
printf("file '%s' could not be opened. Aborting\n", filepath);
exit(1);
}
/* Read in what should be the aiff file FORM header. Check for valid values. */
readLen = read(info->fd, &aiffHeader, sizeof(aiffHeader));
if (sizeof(aiffHeader) != readLen)
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
if (aiffHeader.form != 'FORM' || aiffHeader.aiff != 'AIFF')
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
/* OK, it's a valid AIFF file, search through for the common chunk. We do this
by reading in the "header" (my terminology) of a chunk. This will give us the
chunkID and the size of the chunk. If it's not the chunk we want, we'll skip over
it to the next one. If the file's corrupted, we'll eventually hit EOF and
fall out. */
filepos = lseek(info->fd, 12, SEEK_SET);
chunkHeader.chunkSize = 0;
do
{
filepos = lseek(info->fd, 0, SEEK_CUR);
filepos = lseek(info->fd, filepos + chunkHeader.chunkSize, SEEK_SET);
readLen = read(info->fd, &chunkHeader, sizeof(chunkHeader));
if (sizeof(chunkHeader) != readLen)
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
}
while (chunkHeader.chunkID != 'COMM');
filepos = lseek(info->fd, 0, SEEK_CUR);
filepos = lseek(info->fd, filepos-sizeof(chunkHeader), SEEK_SET);
readLen = read(info->fd, &commonChunk, sizeof(commonChunk));
if (sizeof(commonChunk) != readLen)
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
if (commonChunk.numSampleFrames == 0) {
return noErr; /* It's valid, just nothing we can really read from. We'll get a zero length track if we try. */
}
info->numChannels = commonChunk.numChannels;
info->sampleFreq = commonChunk.sampleFreq;
info->sampleBits = commonChunk.sampleSize;
info->sampleBytes = (info->sampleBits + 7) / 8;
if ((info->numChannels == 0) || (info->numChannels == 5) || (info->numChannels > 6))
{
printf("file '%s' is an invalid AIFF - wrong number of channels. Aborting\n", filepath);
exit(1);
}
if (info->sampleFreq != AIFF_SAMPLE_RATE_44_1 && info->sampleFreq != AIFF_SAMPLE_RATE_44_0)
{
printf("file '%s' is an invalid AIFF - wrong sample rate. Aborting\n", filepath);
exit(1);
}
if ((info->sampleBits < 1) || (info->sampleBits > 128))
{
printf("file '%s' is an invalid AIFF - sample bits are wrong. Aborting\n", filepath);
exit(1);
}
/* go back the start of the file (remember chunks in an IFF file don't have to be in any order)
and look for the Sound chunk the same way we did the Common chunk. */
filepos = lseek(info->fd, 12, SEEK_SET);
chunkHeader.chunkSize = 0;
do
{
filepos = lseek(info->fd, 0, SEEK_CUR);
filepos = lseek(info->fd, filepos + chunkHeader.chunkSize, SEEK_SET);
readLen = read(info->fd, &chunkHeader, sizeof(chunkHeader));
if (sizeof(chunkHeader) != readLen)
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
}
while (chunkHeader.chunkID != 'SSND');
filepos = lseek(info->fd, 0, SEEK_CUR);
filepos = lseek(info->fd, filepos-sizeof(chunkHeader), SEEK_SET);
readLen = read(info->fd, &soundChunk, sizeof(soundChunk));
if (sizeof(soundChunk) != readLen)
{
printf("file '%s' is an invalid AIFF. Aborting\n", filepath);
exit(1);
}
info->dataStart = lseek(info->fd, 0, SEEK_CUR) + soundChunk.offset;
info->dataEnd = lseek(info->fd, 0, SEEK_CUR) + soundChunk.ssndChunkSize - 8;
if ((soundChunk.ssndChunkSize < 8) || (info->dataEnd > lseek(info->fd, 0, SEEK_END)))
info->dataEnd = lseek(info->fd, 0, SEEK_END);
close(info->fd);
return info;
}
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/audioburntest/listing7.html%3Fid%3DDTS10000462-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/audioburntest/listing7.html%3Fid%3DDTS10000462-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/audioburntest/listing7.html%3Fid%3DDTS10000462-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>