summaryrefslogtreecommitdiff
path: root/libs/appleutility/CoreAudio/AudioFile/AFPublic/AudioFileObject.h
blob: 6d7c8689762b0a1c12ca4462ad322003b555d3a5 (plain)
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
/*
     File: AudioFileObject.h
 Abstract: Part of CoreAudio Utility Classes
  Version: 1.1
 
 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
 Inc. ("Apple") in consideration of your agreement to the following
 terms, and your use, installation, modification or redistribution of
 this Apple software constitutes acceptance of these terms.  If you do
 not agree with these terms, please do not use, install, modify or
 redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and
 subject to these terms, Apple grants you a personal, non-exclusive
 license, under Apple's copyrights in this original Apple software (the
 "Apple Software"), to use, reproduce, modify and redistribute the Apple
 Software, with or without modifications, in source and/or binary forms;
 provided that if you redistribute the Apple Software in its entirety and
 without modifications, you must retain this notice and the following
 text and disclaimers in all such redistributions of the Apple Software.
 Neither the name, trademarks, service marks or logos of Apple Inc. may
 be used to endorse or promote products derived from the Apple Software
 without specific prior written permission from Apple.  Except as
 expressly stated in this notice, no other rights or licenses, express or
 implied, are granted by Apple herein, including but not limited to any
 patent rights that may be infringed by your derivative works or by other
 works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 Copyright (C) 2014 Apple Inc. All Rights Reserved.
 
*/
#ifndef _AudioFileObject_H_
#define _AudioFileObject_H_

#include <TargetConditionals.h>

#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
	#include <CoreAudio/CoreAudioTypes.h>
	#include <AudioToolbox/AudioFile.h>
	#include <AudioToolbox/AudioFormat.h>
#else
	#include "CoreAudioTypes.h"
	#include "AudioFile.h"
	#include "AudioFormat.h"
#endif

#include "CompressedPacketTable.h"
#include "CACFDictionary.h"
#include "DataSource.h"
#include <vector>
#include <fcntl.h>

#if TARGET_OS_WIN32
#include <io.h>
#endif

/*
	These are structs defined in 10.5. They are included here for compatibility with sources
*/
#if COREAUDIOTYPES_VERSION < 1050

struct AudioFormatListItem
{
	AudioStreamBasicDescription		mASBD;
	AudioChannelLayoutTag			mChannelLayoutTag;
};
typedef struct AudioFormatListItem AudioFormatListItem;

struct AudioFormatInfo
{
	AudioStreamBasicDescription		mASBD;
	const void*						mMagicCookie;
	UInt32							mMagicCookieSize;
};
typedef struct AudioFormatInfo AudioFormatInfo;

enum {

	kAudioFormatProperty_FormatList						= 'flst',
		//	Returns a list of AudioFormatListItem structs describing the audio formats contained within the compressed bit stream
		//	as described by the magic cookie.
		//	The specifier is an AudioFormatInfo struct. At a minimum formatID member of the ASBD struct must filled in. Other fields
		//	may be filled in.
		
	kAudioFormatProperty_OutputFormatList				= 'ofls',
		//	Returns a list of AudioFormatListItem structs describing the audio formats which may be obtained by decoding the format
		//	described by the specifier.
		//	The specifier is an AudioFormatInfo struct. At a minimum formatID member of the ASBD struct must filled in. Other fields
		//	may be filled in. If there is no magic cookie, then the number of channels and sample rate should be filled in. 

};

enum {
	kAudioFilePropertyPacketSizeUpperBound  =	'pkub',
	kAudioFilePropertyFormatList			=	'flst',
	kAudioFilePropertyEstimatedDuration		=	'edur',
	kAudioFilePropertyBitRate				=	'brat'
};

enum {
	kAudioFileCreateURLSelect					= 0x0019,
	kAudioFileOpenURLSelect						= 0x001A,
	kAudioFileFileDataIsThisFormatSelect		= 0x001B
};
#endif

enum {
	kTEMPAudioFilePropertySoundCheckDictionary = 'scdc',
	kTEMPAudioFilePropertyLoudnessInfo = 'loud',
	kTEMPAudioFilePropertyGenerateLoudnessInfo = 'glou'
};

const UInt32 kCopySoundDataBufferSize = 1024 * 1024;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// some files encode these as upper case
enum {
  kUpperCase_IMACompression               = 'IMA4', /*IMA 4:1*/
  kUpperCase_ULawCompression              = 'ULAW', /*µLaw 2:1*/
  kUpperCase_ALawCompression              = 'ALAW', /*aLaw 2:1*/
  
  kUpperCase_Float32					  = 'FL32',
  kUpperCase_Float64					  = 'FL64'
};

enum
{
	// in what header is this defined? what is it?
	kGSM = 'agsm', 
	kUpperCase_GSM = 'GSM '
};

#define	kPackedBESInt	(kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsPacked)
#define	kPackedLESInt	(kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked)
#define kPackedBEFloat	(kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsPacked)
#define kPackedLEFloat	(kLinearPCMFormatFlagIsFloat | kLinearPCMFormatFlagIsPacked)

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

inline int TransformPerm_FS_O (SInt8 inPerm)
{
	switch (inPerm) {
		case kAudioFileReadPermission: return O_RDONLY;
		case kAudioFileWritePermission: return O_WRONLY;
		case kAudioFileReadWritePermission: return O_RDWR;
	}
	return O_RDONLY;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#pragma mark "File Error Handling"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <errno.h>
#include <CoreAudio/CoreAudioTypes.h>

inline OSErr AudioFileTranslateErrno(int err)
{
    switch (err) {
#if !TARGET_OS_WIN32
    case ENFILE:
#endif
    case EMFILE:
        return -42 /* kAudio_TooManyFilesOpenError */;
#if !TARGET_OS_WIN32
    case EPERM:
    case EROFS:
#endif
    case EACCES:
    case EEXIST:
        return -54 /* kAudio_FilePermissionError */;
#if !TARGET_OS_WIN32
    case EMLINK:
        return (OSErr)'!pth' /* kAudio_BadFilePathError */;
    case ENOTDIR:
    case ELOOP:
#endif
    case ENOENT:
    default:
			return (OSErr)kAudioFileUnspecifiedError;
        }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class AudioFileObject
{
protected:

private:
	SInt64							mNumBytes;				// total bytes of audio data in the audio file
	SInt64							mNumPackets;			// total frames of audio data in the audio file
	AudioStreamBasicDescription		mDataFormat;		// format of the audio data
	SInt64							mDataOffset;		// position if the file where audio data begins
	UInt32							mIsOptimized;		// 1 if there is nothing in the file following the audio data, 0 if there is
	UInt32							mFileType;			// file type of the audio file (i.e. AIFF, WAVE, etc.)			
	CFURLRef						mFileRef;				// URL of the file passed to AudioFileOpen or AudioFileCreate
	int								mFileD;			// Ref num of the file after opening within Audio File 
	SInt8							mPermissions;		// file permissions indicated by the caller, passed by AudioFileOpen or set with SetProperty function
	Boolean							mIsInitialized;		// has the AudioFileObject for this file been intialized?
	DataSource						*mDataSource;
    UInt32							mMaximumPacketSize;
    CompressedPacketTable			*mPacketTable;
	UInt32							mDeferSizeUpdates;
	Boolean							mNeedsSizeUpdate;
	Boolean							mFirstSetFormat;
	Boolean							mAlignDataWithFillerChunks;
	
public:    
  
	AudioFileObject (UInt32 inFileType)
		: mNumBytes(0),
		  mNumPackets(0),
		  mDataOffset(0),
		  mIsOptimized(1),
		  mFileType(inFileType),
		  mFileRef(NULL),
		  mFileD(-1),
		  mPermissions(0),
		  mIsInitialized (false),
		  mDataSource(0),
          mMaximumPacketSize(0),
          mPacketTable(NULL),
		  mDeferSizeUpdates(1),
		  mNeedsSizeUpdate(false),
		  mFirstSetFormat(true),
		  mAlignDataWithFillerChunks(true)
		  {
			memset(&mDataFormat, 0, sizeof(mDataFormat));
		  }
	
	virtual ~AudioFileObject();

	
/* Public API Function Implementation */
	// The DoSomething() versions of these functions are wrappers that perform a standard prologue.
	// The Something() methods are those which should be overridden in the subclasses.
	
	OSStatus DoCreate(				CFURLRef							inFileRef,
									const AudioStreamBasicDescription	*inFormat,
									UInt32								inFlags);
                                
	virtual OSStatus Create(		CFURLRef							inFileRef,
									const AudioStreamBasicDescription	*inFormat);
                                
	OSStatus DoOpen(				CFURLRef		inFileRef, 
									SInt8  			inPermissions,
									int				inFD);
									
	virtual OSStatus Open(			CFURLRef		inFileRef, 
									SInt8  			inPermissions,
									int				inFD);

	OSStatus DoOpenWithCallbacks(
				void *								inRefCon, 
				AudioFile_ReadProc					inReadFunc, 
				AudioFile_WriteProc					inWriteFunc, 
				AudioFile_GetSizeProc				inGetSizeFunc,
				AudioFile_SetSizeProc				inSetSizeFunc);
				
									
	OSStatus DoInitialize(			CFURLRef							inFileRef,
									const AudioStreamBasicDescription	*inFormat,
									UInt32								inFlags);
	
	virtual OSStatus Initialize(	CFURLRef							inFileRef,
									const AudioStreamBasicDescription	*inFormat,
									UInt32								inFlags);
										
	OSStatus DoInitializeWithCallbacks(
				void *								inRefCon, 
				AudioFile_ReadProc					inReadFunc, 
				AudioFile_WriteProc					inWriteFunc, 
				AudioFile_GetSizeProc				inGetSizeFunc,
				AudioFile_SetSizeProc				inSetSizeFunc,
				UInt32								inFileType,
				const AudioStreamBasicDescription	*inFormat,
				UInt32								inFlags);
				
	virtual OSStatus OpenFromDataSource(void);
						
	virtual OSStatus InitializeDataSource(const AudioStreamBasicDescription	*inFormat, UInt32 inFlags);
	
	OSStatus DoClose();
	
	virtual OSStatus Close();
	
	OSStatus DoOptimize();
	
	virtual OSStatus Optimize();
	
	virtual OSStatus ReadBytes(		Boolean			inUseCache,
									SInt64			inStartingByte, 
									UInt32			*ioNumBytes, 
									void			*outBuffer);
	
	virtual OSStatus WriteBytes(	Boolean			inUseCache,
									SInt64			inStartingByte, 
									UInt32			*ioNumBytes, 
									const void		*inBuffer);
	
	virtual OSStatus ReadPackets(	Boolean							inUseCache,
									UInt32							*outNumBytes,
									AudioStreamPacketDescription	*outPacketDescriptions,
									SInt64							inStartingPacket, 
									UInt32  						*ioNumPackets, 
									void							*outBuffer);

	virtual OSStatus ReadPacketData(	
									Boolean							inUseCache,
									UInt32							*ioNumBytes,
									AudioStreamPacketDescription	*outPacketDescriptions,
									SInt64							inStartingPacket, 
									UInt32  						*ioNumPackets, 
									void							*outBuffer);
	
	virtual OSStatus ReadPacketDataVBR(	
									Boolean							inUseCache,
									UInt32							*ioNumBytes,
									AudioStreamPacketDescription	*outPacketDescriptions,
									SInt64							inStartingPacket, 
									UInt32  						*ioNumPackets, 
									void							*outBuffer);

    virtual OSStatus	HowManyPacketsCanBeReadIntoBuffer(UInt32* ioNumBytes, SInt64 inStartingPacket, UInt32 *ioNumPackets);

    virtual OSStatus	ReadPacketDataVBR_InTable(	
                                    Boolean							inUseCache,
                                    UInt32							*ioNumBytes,
                                    AudioStreamPacketDescription	*outPacketDescriptions,
                                    SInt64							inStartingPacket, 
                                    UInt32  						*ioNumPackets, 
                                    void							*outBuffer);
	
	virtual OSStatus WritePackets(	Boolean								inUseCache,
                                    UInt32								inNumBytes,
                                    const AudioStreamPacketDescription	*inPacketDescriptions,
                                    SInt64								inStartingPacket, 
                                    UInt32								*ioNumPackets, 
                                    const void							*inBuffer);
/* Property Support */

	virtual OSStatus GetPropertyInfo	(	
                                        AudioFilePropertyID		inPropertyID,
                                        UInt32					*outDataSize,
                                        UInt32					*isWritable);
										
	virtual OSStatus GetProperty	(	AudioFilePropertyID		inPropertyID,
										UInt32					*ioDataSize,
										void					*ioPropertyData);
										
	virtual OSStatus SetProperty	(	AudioFilePropertyID		inPropertyID,
										UInt32					inDataSize,
										const void				*inPropertyData);
										
	UInt32 GetFileType() const { return mFileType; }
	void SetFileType(UInt32 inFileType) { mFileType = inFileType; }
	
	// this will set the format in memory only.
	virtual OSStatus SetDataFormat(const AudioStreamBasicDescription* inStreamFormat);
	
	// this will update the format info on disk and in memory.
	virtual OSStatus UpdateDataFormat(const AudioStreamBasicDescription* inStreamFormat);
	
	const UInt32 GetBytesPerPacket() const { return mDataFormat.mBytesPerPacket; }
	
	const AudioStreamBasicDescription &GetDataFormat() const { return mDataFormat; }
	
	virtual OSStatus GetFormatListInfo(	UInt32				&outDataSize,
											UInt32				&outWritable);
										
	virtual OSStatus GetFormatList(	UInt32									&ioDataSize,
									AudioFormatListItem				*ioPropertyData);
										
	virtual OSStatus SetFormatList(	UInt32									inDataSize,
									const AudioFormatListItem		*inPropertyData);
	
	virtual OSStatus UpdateSize() { return noErr; }
	UInt32	DeferSizeUpdates() { return mDeferSizeUpdates; }
	void SetDeferSizeUpdates(UInt32 inFlag) { mDeferSizeUpdates = inFlag; }
	OSStatus UpdateSizeIfNeeded();
	OSStatus SizeChanged();
	
	Boolean IsOptimized() const { return mIsOptimized != 0; }
	void SetIsOptimized(Boolean inIsOptimized) { mIsOptimized = inIsOptimized ? 1 : 0; }

	Boolean AlignDataWithFillerChunks() const { return mAlignDataWithFillerChunks; }

	virtual SInt64 GetNumBytes() { return mNumBytes; }
	virtual void SetNumBytes(SInt64 inNumBytes) { mNumBytes = inNumBytes; }

	// this will update the header size info on disk
	OSStatus UpdateNumBytes(SInt64 inNumBytes);
	
	virtual SInt64 GetNumPackets(){ return mNumPackets; }
	virtual void SetNumPackets(SInt64 inNumPackets) { mNumPackets = inNumPackets; }
	
	// this will update the header size info on disk
	OSStatus UpdateNumPackets(SInt64 inNumPackets);

	virtual OSStatus PacketToFrame(SInt64 inPacket, SInt64& outFirstFrameInPacket);
	virtual OSStatus FrameToPacket(SInt64 inFrame, SInt64& outPacket, UInt32& outFrameOffsetInPacket);
	
	virtual OSStatus PacketToByte(AudioBytePacketTranslation* abpt);
	
	virtual OSStatus ByteToPacket(AudioBytePacketTranslation* abpt);
	
	virtual OSStatus GetBitRate(				UInt32					*outBitRate);
		
	virtual OSStatus GetMagicCookieDataSize(	UInt32					*outDataSize,
												UInt32					*isWritable);
	                      
	virtual OSStatus GetMagicCookieData(		UInt32					*ioDataSize,
												void					*ioPropertyData);
	                      
	virtual OSStatus SetMagicCookieData(		UInt32					inDataSize,
												const void				*inPropertyData);

	virtual OSStatus GetMarkerListSize(			UInt32						*outDataSize,
												UInt32						*isWritable);
	                      
	virtual OSStatus GetMarkerList(				UInt32						*ioDataSize,
												AudioFileMarkerList			*ioMarkerList);
	                      
	virtual OSStatus SetMarkerList(				UInt32						inDataSize,
												const AudioFileMarkerList	*inMarkerList);


	virtual OSStatus GetRegionListSize(			UInt32						*outDataSize,
												UInt32						*isWritable);
	                      
	virtual OSStatus GetRegionList(				UInt32						*ioDataSize,
												AudioFileRegionList			*ioRegionList);
	                      
	virtual OSStatus SetRegionList(				UInt32						inDataSize,
												const AudioFileRegionList	*inRegionList);

	virtual OSStatus GetChannelLayoutSize(		UInt32						*outDataSize,
												UInt32						*isWritable);
	                      
	virtual OSStatus GetChannelLayout(			UInt32						*ioDataSize,
												AudioChannelLayout			*ioChannelLayout);
	                      
	virtual OSStatus SetChannelLayout(			UInt32						inDataSize,
												const AudioChannelLayout	*inChannelLayout);


	virtual OSStatus GetInfoDictionarySize(		UInt32						*outDataSize,
												UInt32						*isWritable);
												
	virtual OSStatus GetInfoDictionary(			CACFDictionary  *infoDict);
												
	virtual OSStatus SetInfoDictionary(			CACFDictionary  *infoDict);

	virtual OSStatus GetSoundCheckDictionarySize(		UInt32						*outDataSize,
														UInt32						*isWritable) { return kAudioFileUnsupportedPropertyError; }
														
	virtual OSStatus GetSoundCheckDictionary(	CACFDictionary  *infoDict) { return kAudioFileUnsupportedPropertyError; }
												
	virtual OSStatus SetSoundCheckDictionary(	CACFDictionary  *infoDict) { return kAudioFileUnsupportedPropertyError; }
														
	virtual OSStatus GetLoudnessInfo(	CACFDictionary  *infoDict) { return kAudioFileUnsupportedPropertyError; }
	
	virtual OSStatus GetSoundCheckDictionaryFromLoudnessInfo(CACFDictionary* outInfoDict) { return kAudioFileUnsupportedPropertyError; }
	virtual OSStatus GetLoudnessInfoFromSoundCheckDictionary(CACFDictionary* outInfoDict) { return kAudioFileUnsupportedPropertyError; }
	
	virtual OSStatus SetLoudnessInfo(	CACFDictionary  *infoDict) { return kAudioFileUnsupportedPropertyError; }

	virtual OSStatus GetLoudnessInfoSize(				UInt32			*outDataSize,
														UInt32			*isWritable) { return kAudioFileUnsupportedPropertyError; }

	virtual OSStatus GenerateLoudnessInfo(	CACFDictionary  *infoDict) { return kAudioFileUnsupportedPropertyError; }

	virtual OSStatus GetEstimatedDuration(		Float64*		duration);

	virtual OSStatus CountUserData(			UInt32					inUserDataID,
											UInt32					*outNumberItems);
											
	virtual OSStatus GetUserDataSize(		UInt32					inUserDataID,
											UInt32					inIndex,
											UInt32					*outDataSize);
											
	virtual OSStatus GetUserData(			UInt32					inUserDataID,
											UInt32					inIndex,
											UInt32					*ioDataSize,
											void					*ioUserData);
											
	virtual OSStatus SetUserData(			UInt32					inUserDataID,
											UInt32					inIndex,
											UInt32					inDataSize,
											const void				*inUserData);

	virtual OSStatus RemoveUserData(		UInt32					inUserDataID,
											UInt32					inIndex);

	virtual OSStatus GetLyrics(CFStringRef  *outLyrics) { return kAudioFileUnsupportedPropertyError; }


	Boolean CanRead() const { return mPermissions & kAudioFileReadPermission; }
	Boolean CanWrite() const { return mPermissions & kAudioFileWritePermission; }

	void SetPermissions(SInt8 inPermissions) { mPermissions = inPermissions; }
		
/* Other Helper Methods: (some may not be necessary depending on how things are refactored) */
	
	OSStatus OpenFile(SInt8 inPermissions, int inFD);

	OSStatus CreateDataFile (CFURLRef	inFileRef, int	&outFileD);

	OSStatus AddDurationToInfoDictionary(CACFDictionary *infoDict, Float64 &inDuration);	

	virtual Boolean IsDataFormatSupported(const AudioStreamBasicDescription	*inFormat) = 0;
	virtual Boolean IsDataFormatValid(const AudioStreamBasicDescription	*inFormat);

	virtual OSStatus IsValidFilePosition(SInt64 /*position*/) { return noErr; }

	OSStatus MoveData(SInt64 fromPos, SInt64 toPos, SInt64 size);

/* Accessors: */
	Boolean IsInitialized() const { return mIsInitialized; }
	void SetInitialized(Boolean inFlag) { mIsInitialized = inFlag; }
			
	DataSource* GetDataSource() const { return mDataSource; }
	void SetDataSource(DataSource *inDataSource);
    
	void	SetURL (CFURLRef inURL);
	
    virtual UInt32	GetMaximumPacketSize() { return mMaximumPacketSize; }
    virtual UInt32	FindMaximumPacketSize() { return mMaximumPacketSize; }
    virtual void	SetMaximumPacketSize(const UInt32 inPacketSize) { mMaximumPacketSize = inPacketSize; }
    	
    virtual UInt32	GetPacketSizeUpperBound() { return GetMaximumPacketSize (); }
    	
	SInt64 GetDataOffset() const { return mDataOffset; }	
	void SetDataOffset(SInt64 inOffset) { mDataOffset = inOffset; }	
    
	// I like this idiom better than DoesPacketTableExist+NewPacketTable:
	CompressedPacketTable* GetPacketTable(Boolean inCreateIt = false)
	{
		if (!mPacketTable && inCreateIt)
			mPacketTable = new CompressedPacketTable(mDataFormat.mFramesPerPacket);
		return mPacketTable;
	}
	void ClearPacketTable() 
	{
		DeletePacketTable();
		GetPacketTable(true);
	}
	
	virtual OSStatus ScanForPackets(SInt64  inToPacketCount, DataSource* inDataSrc = NULL, bool fullyParsedIfEndOfDataReached = true) 
	{
		// In formats that read packets lazily, this will be overridden to scan for packets up to the index.
		if (inToPacketCount > GetNumPackets())
			return kAudioFileEndOfFileError;
		
		return noErr; 
	}
		
    void AppendPacket(const AudioStreamPacketDescription &inPacket) 
		{
			CompressedPacketTable* packetTable = GetPacketTable(true);
			UInt32 numFramesInPacket = mDataFormat.mFramesPerPacket ? mDataFormat.mFramesPerPacket : inPacket.mVariableFramesInPacket;
			
			AudioStreamPacketDescriptionExtended pext;
			memset(&pext, 0, sizeof(pext));
			pext.mStartOffset = inPacket.mStartOffset;
			pext.mDataByteSize = inPacket.mDataByteSize;
			pext.mVariableFramesInPacket = inPacket.mVariableFramesInPacket;
			pext.mFrameOffset = numFramesInPacket + (packetTable->size() ? packetTable->back().mFrameOffset : 0);
			
			packetTable->push_back(pext); 
			if (inPacket.mDataByteSize > mMaximumPacketSize) 
				mMaximumPacketSize = inPacket.mDataByteSize;
		}
    void DeletePacketTable() { delete mPacketTable; mPacketTable = NULL;}
    SInt64	GetPacketTableSize() { return mPacketTable ? mPacketTable->size() : 0; }
    OSStatus GetPacketDescriptions(UInt32   inStartingPacket, UInt32   *ioDataSize, AudioStreamPacketDescription    *outPacketDescriptions)
        {
			if (outPacketDescriptions == NULL) return kAudioFileUnspecifiedError;
            if (mPacketTable)
			{
				// only get as many packet descriptions as can fit in outPacketDescriptions
				UInt32	count = *ioDataSize / sizeof(AudioStreamPacketDescription);
				if (count + inStartingPacket  > GetPacketTableSize()) 
					count = (UInt32)(GetPacketTableSize() - inStartingPacket);
					
				*ioDataSize = 0;
				for (UInt32 i = inStartingPacket; i < (count + inStartingPacket); i++)
				{
					AudioStreamPacketDescription    curPacket =  (*mPacketTable)[i];
					outPacketDescriptions[i].mStartOffset = curPacket.mStartOffset - GetDataOffset();
					outPacketDescriptions[i].mVariableFramesInPacket = curPacket.mVariableFramesInPacket;
					outPacketDescriptions[i].mDataByteSize = curPacket.mDataByteSize;
					*ioDataSize += sizeof(AudioStreamPacketDescription);
				}
			}
			else
				*ioDataSize = 0;

            return noErr;
        }

#if DEBUG
	void DumpPacketTable()
		{
            if (mPacketTable) {
				SInt64 size = mPacketTable->size();
				printf("PacketTable size %d\n", (int)size);
				for (SInt64 i = 0; i < size; i++) {
					AudioStreamPacketDescription    curPacket =  (*mPacketTable)[i];
					printf("dpkt %5qd %8qd %5d %5d\n", i, curPacket.mStartOffset, (int)curPacket.mDataByteSize, (int)curPacket.mVariableFramesInPacket);
				}
			}
		}
#endif

	Boolean GetNeedsSizeUpdate() const { return mNeedsSizeUpdate; }
	void SetNeedsSizeUpdate(Boolean inNeedsSizeUpdate) { mNeedsSizeUpdate = inNeedsSizeUpdate; }
	
	CFURLRef GetURL () const { return mFileRef; }

	virtual OSStatus			GetSourceBitDepth(SInt32& outValue) { outValue = 0; return kAudioFileUnsupportedPropertyError; }
	virtual OSStatus			SetSourceBitDepth(SInt32 inValue) { return kAudioFileUnsupportedPropertyError; }

	virtual OSStatus	GetAlbumArtwork(CFDataRef& outValue) { outValue = NULL; return kAudioFileUnsupportedPropertyError; }
	virtual OSStatus	SetAlbumArtwork(CFDataRef inValue){ return kAudioFileUnsupportedPropertyError; }

private:

	void SetAlignDataWithFillerChunks(Boolean inFlag) { mAlignDataWithFillerChunks = inFlag; }

	OSStatus ValidateFormatAndData();
	
/* debug */
//	virtual void PrintFile (FILE* inFile, const char *indent) = 0;
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

inline Boolean cfstrcmpi(CFStringRef a, CFStringRef b)
{
	// case insensitive CFString compare
	return CFStringCompare(a, b, kCFCompareCaseInsensitive) == kCFCompareEqualTo;
}

CFBundleRef GetAudioToolboxBundle();


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#endif