Beispiel #1
0
void SourceFile::registerSelf()
{
	static const int32_t SOURCE_PRIORITY = 2;
	
	IoRegistrar::SourceCreationFunc sourceFunc = SourceFile::createRef;
	
	OSStatus err;
	UInt32 propertySize = 0;
	err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_ReadableTypes, 0, NULL, &propertySize);
	if( err ) {
		//registers no types on error
		return;
	}
	uint32_t * audioTypes = (uint32_t *)malloc(propertySize);
	err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ReadableTypes, 0, NULL, &propertySize, audioTypes );
	if( err ) {
		//registers no types on error
		free(audioTypes);
		return;
	}
	
	int nTypes = propertySize / sizeof(uint32_t);
	for( int i = 0; i < nTypes; i++ ) {
		CFArrayRef extensions = NULL;
		propertySize = sizeof(extensions);
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType, sizeof(uint32_t), (UInt32 *)&audioTypes[i], &propertySize, &extensions);
		if ( ! err ) {
			CFIndex nExtensions = CFArrayGetCount(extensions);
			for( int j = 0; j < nExtensions; j++ ) {
				CFStringRef cfext = (CFStringRef)CFArrayGetValueAtIndex(extensions, j);
				Boolean res = FALSE;
				char * ext = NULL;
				uint8_t extLen = 3;
				while( ! res && extLen < 5 ) {
					ext = new char[extLen];
					res = CFStringGetCString( cfext, ext, extLen * sizeof(char), kCFStringEncodingASCII );
					if( res ) {
						IoRegistrar::registerSourceType( ext, sourceFunc, SOURCE_PRIORITY );
					}
					delete ext;
					ext = NULL;
					extLen++;
				}
			}
		}		
	}
	free(audioTypes);
	
	IoRegistrar::registerSourceGeneric( sourceFunc, SOURCE_PRIORITY );
	
}
Beispiel #2
0
CAAudioFileFormats::CAAudioFileFormats(bool loadDataFormats) : 
	mNumFileFormats(0), mFileFormats(NULL)
{
	OSStatus err;
	UInt32 size;
	UInt32 *fileTypes = NULL;
	
	// get all file types
	err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_WritableTypes, 0, NULL, &size);
	if (err != noErr) goto bail;
	mNumFileFormats = size / sizeof(UInt32);
	mFileFormats = new FileFormatInfo[mNumFileFormats];
	fileTypes = new UInt32[mNumFileFormats];
	err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_WritableTypes, 0, NULL, &size, fileTypes);
	if (err != noErr) goto bail;
	
	// get info for each file type
	for (int i = 0; i < mNumFileFormats; ++i) {
		FileFormatInfo *ffi = &mFileFormats[i];
		OSType filetype = fileTypes[i];

		ffi->mFileTypeID = filetype;
		
		// file type name
		ffi->mFileTypeName = NULL;
		size = sizeof(CFStringRef);
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_FileTypeName, sizeof(UInt32), &filetype, &size, &ffi->mFileTypeName);
		if (err == noErr && ffi->mFileTypeName)
			CFRetain(ffi->mFileTypeName);
		
		// file extensions
		size = sizeof(CFArrayRef);
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType,
			sizeof(OSType), &filetype, &size, &ffi->mExtensions);
		if (err)
			ffi->mExtensions = NULL;
		
		// file data formats
		ffi->mNumDataFormats = 0;
		ffi->mDataFormats = NULL;
		
		if (loadDataFormats)
			ffi->LoadDataFormats();
	}

	// sort file formats by name
	qsort(mFileFormats, mNumFileFormats, sizeof(FileFormatInfo), CompareFileFormatNames);
bail:
	delete[] fileTypes;
}
static Boolean MyFileFormatRequiresBigEndian(AudioFileTypeID audioFileType, int bitdepth)
{
	AudioFileTypeAndFormatID ftf;
	UInt32 propertySize;
	OSStatus err;
	Boolean requiresBigEndian;
	
	ftf.mFileType = audioFileType;
	ftf.mFormatID = kAudioFormatLinearPCM;
	
	err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat, sizeof(ftf), &ftf, &propertySize);
	if (err) return FALSE;

	AudioStreamBasicDescription *formats = (AudioStreamBasicDescription *)malloc(propertySize);
	err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat, sizeof(ftf), &ftf, &propertySize, formats);
	requiresBigEndian = TRUE;
    if (err == noErr) {
        int i, nFormats = propertySize / sizeof(AudioStreamBasicDescription);
        for (i = 0; i < nFormats; ++i) {
            if (formats[i].mBitsPerChannel == bitdepth
                && !(formats[i].mFormatFlags & kLinearPCMFormatFlagIsBigEndian)) {
                requiresBigEndian = FALSE;
                break;
            }
        }
    }
	free(formats);
	return requiresBigEndian;
}
// ____________________________________________________________________________________
// Infer an audio file type from a filename's extension.
static Boolean InferAudioFileFormatFromFilename(CFStringRef filename, AudioFileTypeID *outFiletype)
{
	OSStatus err;
	
	// find the extension in the filename.
	CFRange range = CFStringFind(filename, CFSTR("."), kCFCompareBackwards);
	if (range.location == kCFNotFound)
		return FALSE;
	range.location += 1;
	range.length = CFStringGetLength(filename) - range.location;
	CFStringRef extension = CFStringCreateWithSubstring(NULL, filename, range);
	
	UInt32 propertySize = sizeof(AudioFileTypeID);
	err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_TypesForExtension, sizeof(extension), &extension, &propertySize, outFiletype);
	CFRelease(extension);
	
	return (err == noErr && propertySize > 0);
}
CFArrayRef SFB::Audio::CoreAudioDecoder::CreateSupportedFileExtensions()
{
	CFArrayRef		supportedExtensions			= nullptr;
	UInt32			size						= sizeof(supportedExtensions);
	OSStatus		result						= AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllExtensions, 
																		 0, 
																		 nullptr, 
																		 &size, 
																		 &supportedExtensions);
	
	if(noErr != result) {
		LOGGER_ERR("org.sbooth.AudioEngine.Decoder.CoreAudio", "AudioFileGetGlobalInfo (kAudioFileGlobalInfo_AllExtensions) failed: " << result << "'" << SFB::StringForOSType((OSType)result) << "'");

		return nullptr;
	}
	
	return supportedExtensions;
}
CFArrayRef SFB::Audio::CoreAudioDecoder::CreateSupportedMIMETypes()
{
	CFArrayRef		supportedMIMETypes			= nullptr;
	UInt32			size						= sizeof(supportedMIMETypes);
	OSStatus		result						= AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllMIMETypes, 
																		 0, 
																		 nullptr, 
																		 &size, 
																		 &supportedMIMETypes);
	
	if(noErr != result) {
		LOGGER_ERR("org.sbooth.AudioEngine.Decoder.CoreAudio", "AudioFileGetGlobalInfo (kAudioFileGlobalInfo_AllMIMETypes) failed: " << result << "'" << SFB::StringForOSType((OSType)result) << "'");

		return nullptr;
	}
	
	return CFArrayCreateCopy(kCFAllocatorDefault, supportedMIMETypes);
}
Beispiel #7
0
CAAudioFileFormats::CAAudioFileFormats() : 
	mNumFileFormats(0), mFileFormats(NULL)
{
	OSStatus err;
	UInt32 size;
	UInt32 *fileTypes = NULL, *writableFormats = NULL, *readableFormats = NULL;
	int nWritableFormats, nReadableFormats;
	
	// get all file types
	err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_WritableTypes, 0, NULL, &size);
	if (err != noErr) goto bail;
	mNumFileFormats = size / sizeof(UInt32);
	mFileFormats = new FileFormatInfo[mNumFileFormats];
	fileTypes = new UInt32[mNumFileFormats];
	err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_WritableTypes, 0, NULL, &size, fileTypes);
	if (err != noErr) goto bail;
	
	// get all writable formats
	err = AudioFormatGetPropertyInfo(kAudioFormatProperty_EncodeFormatIDs, 0, NULL, &size);
	if (err != noErr) goto bail;
	nWritableFormats = size / sizeof(UInt32);
	writableFormats = new UInt32[nWritableFormats];
	err = AudioFormatGetProperty(kAudioFormatProperty_EncodeFormatIDs, 0, NULL, &size, writableFormats);
	if (err != noErr) goto bail;
	
	// get all readable formats
	err = AudioFormatGetPropertyInfo(kAudioFormatProperty_DecodeFormatIDs, 0, NULL, &size);
	if (err != noErr) goto bail;
	nReadableFormats = size / sizeof(UInt32);
	readableFormats = new UInt32[nReadableFormats];
	err = AudioFormatGetProperty(kAudioFormatProperty_DecodeFormatIDs, 0, NULL, &size, readableFormats);
	if (err != noErr) goto bail;
	
	// get info for each file type
	for (int i = 0; i < mNumFileFormats; ++i) {
		FileFormatInfo *ffi = &mFileFormats[i];
		OSType filetype = fileTypes[i];

		ffi->mFileTypeID = filetype;
		
		// file type name
		ffi->mFileTypeName = NULL;
		size = sizeof(CFStringRef);
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_FileTypeName, sizeof(UInt32), &filetype, &size, &ffi->mFileTypeName);
		if (ffi->mFileTypeName)
			CFRetain(ffi->mFileTypeName);
		
		// file extensions
		size = sizeof(CFArrayRef);
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType,
			sizeof(OSType), &filetype, &size, &ffi->mExtensions);
		if (err)
			ffi->mExtensions = NULL;
		
		// file data formats
		ffi->mNumDataFormats = 0;
		ffi->mDataFormats = NULL;

		err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AvailableFormatIDs, 
				sizeof(UInt32), &filetype, &size);
		if (err == noErr) {
			ffi->mNumDataFormats = size / sizeof(OSType);
			OSType *formatIDs = new OSType[ffi->mNumDataFormats];
			err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableFormatIDs,
				sizeof(UInt32), &filetype, &size, formatIDs);
			if (err == noErr) {
				ffi->mDataFormats = new DataFormatInfo[ffi->mNumDataFormats];
				for (int j = 0; j < ffi->mNumDataFormats; ++j) {
					int k;
					bool anyBigEndian = false, anyLittleEndian = false;
					DataFormatInfo *dfi = &ffi->mDataFormats[j];
					dfi->mFormatID = formatIDs[j];
					dfi->mReadable = (dfi->mFormatID == kAudioFormatLinearPCM);
					dfi->mWritable = (dfi->mFormatID == kAudioFormatLinearPCM);
					for (k = 0; k < nReadableFormats; ++k)
						if (readableFormats[k] == dfi->mFormatID) {
							dfi->mReadable = true;
							break;
						}
					for (k = 0; k < nWritableFormats; ++k)
						if (writableFormats[k] == dfi->mFormatID) {
							dfi->mWritable = true;
							break;
						}
					
					dfi->mNumVariants = 0;
					AudioFileTypeAndFormatID tf = { filetype, dfi->mFormatID };
					err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
						sizeof(AudioFileTypeAndFormatID), &tf, &size);
					if (err == noErr) {
						dfi->mNumVariants = size / sizeof(AudioStreamBasicDescription);
						dfi->mVariants = new AudioStreamBasicDescription[dfi->mNumVariants];
						err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
							sizeof(AudioFileTypeAndFormatID), &tf, &size, dfi->mVariants);
						if (err) {
							dfi->mNumVariants = 0;
							delete[] dfi->mVariants;
							dfi->mVariants = NULL;
						} else {
							for (k = 0; k < dfi->mNumVariants; ++k) {
								AudioStreamBasicDescription *desc = &dfi->mVariants[k];
								if (desc->mBitsPerChannel > 8) {
									if (desc->mFormatFlags & kAudioFormatFlagIsBigEndian)
										anyBigEndian = true;
									else
										anyLittleEndian = true;
								}
							}
						}
					}
					
					dfi->mEitherEndianPCM = (anyBigEndian && anyLittleEndian);
				}
			}
			delete[] formatIDs;
		}
	}

	// sort file formats by name
	qsort(mFileFormats, mNumFileFormats, sizeof(FileFormatInfo), CompareFileFormatNames);
bail:
	delete[] fileTypes;
	delete[] readableFormats;
	delete[] writableFormats;
}
void	CAAudioFileConverter::GenerateOutputFileName(const char *inputFilePath, 
						const CAStreamBasicDescription &inputFormat,
						const CAStreamBasicDescription &outputFormat, OSType outputFileType, 
						char *outName)
{
	struct stat sb;
	char inputDir[256];
	char inputBasename[256];
	
	strcpy(inputDir, dirname(inputFilePath));
	const char *infname = basename(inputFilePath);
	const char *inext = strrchr(infname, '.');
	if (inext == NULL) strcpy(inputBasename, infname);
	else {
		int n;
		memcpy(inputBasename, infname, n = inext - infname);
		inputBasename[n] = '\0';
	}
	
	CFArrayRef exts;
	UInt32 propSize = sizeof(exts);
	XThrowIfError(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType,
		sizeof(OSType), &outputFileType, &propSize, &exts), "generate output file name");
	char outputExt[32];
	CFStringRef cfext = (CFStringRef)CFArrayGetValueAtIndex(exts, 0);
	CFStringGetCString(cfext, outputExt, sizeof(outputExt), kCFStringEncodingUTF8);
	CFRelease(exts);
	
	// 1. olddir + oldname + newext
	sprintf(outName, "%s/%s.%s", inputDir, inputBasename, outputExt);
#if TARGET_OS_MAC	
	if (lstat(outName, &sb)) return;
#else
	if (stat(outName, &sb)) return;
#endif

	if (outputFormat.IsPCM()) {
		// If sample rate changed:
		//	2. olddir + oldname + "-SR" + newext
		if (inputFormat.mSampleRate != outputFormat.mSampleRate && outputFormat.mSampleRate != 0.) {
			sprintf(outName, "%s/%s-%.0fk.%s", inputDir, inputBasename, outputFormat.mSampleRate/1000., outputExt);
#if TARGET_OS_MAC	
			if (lstat(outName, &sb)) return;
#else
			if (stat(outName, &sb)) return;
#endif
		}
		// If bit depth changed:
		//	3. olddir + oldname + "-bit" + newext
		if (inputFormat.mBitsPerChannel != outputFormat.mBitsPerChannel) {
			sprintf(outName, "%s/%s-%ldbit.%s", inputDir, inputBasename, outputFormat.mBitsPerChannel, outputExt);
#if TARGET_OS_MAC	
			if (lstat(outName, &sb)) return;
#else
			if (stat(outName, &sb)) return;
#endif
		}
	}
	
	// maybe more with channels/layouts? $$$
	
	// now just append digits
	for (int i = 1; ; ++i) {
		sprintf(outName, "%s/%s-%d.%s", inputDir, inputBasename, i, outputExt);
#if TARGET_OS_MAC	
		if (lstat(outName, &sb)) return;
#else
		if (stat(outName, &sb)) return;
#endif
	}
}
Beispiel #9
0
void ParseArgs (int argc, char * const argv[],
                AudioFileTypeID	&	outFormat,
                Float64	&			outSampleRate,
                OSType	&			outFileType,
                CFURLRef&			outInputFileURL,
                CFURLRef&			outOutputFileURL,
                UInt32  &			outBitDepth,
                UInt32  &			outBitRate)
{
	if (argc < 2) {
		printf ("No Input File specified\n");
		UsageString(1);
	}
	
	// support "ConvertFile -h" usage
	if (argc == 2 && !strcmp("-h", argv[1])) {
		UsageString(0);
	}
	
	// first validate our initial condition
	const char* inputFileName = argv[1];
	
	outInputFileURL = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8 *)inputFileName, strlen(inputFileName), false);
	if (!outInputFileURL) {
		printf ("* * Bad input file path\n");
		UsageString(1);
    }
	
	outBitRate = 0;
	outBitDepth = 0;
	
	// look to see if a format or different file output has been specified
	for (int i = 2; i < argc; ++i) {
		if (!strcmp ("-d", argv[i])) {
			str2OSType (argv[++i], outFormat);
			outSampleRate = 0;
		}
		else if (!strcmp ("-r", argv[i])) {
			sscanf (argv[++i], "%lf", &outSampleRate);
			outFormat = 0;
		}
		else if (!strcmp("-bd", argv[i])) {
			int temp;
			sscanf (argv[++i], "%d", &temp);
			outBitDepth = temp;
		}
		else if (!strcmp ("-f", argv[i])) {
			str2OSType (argv[++i], outFileType);
		}
		else if (!strcmp ("-b", argv[i])) {
			int temp;
			sscanf (argv[++i], "%u", &temp);
			outBitRate = temp;
		}
		else if (!strcmp ("-h", argv[i])) {
			UsageString(0);
		}
		else {
			printf ("* * Unknown command: %s\n", argv[i]);
			UsageString(1);
		}
	}
	
    // output file
	UInt32 size = sizeof(CFArrayRef);
	CFArrayRef extensions;
	OSStatus err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType,
                                          sizeof(OSType), &outFileType,
                                          &size, &extensions);
	XThrowIfError (err, "Getting the file extensions for file type");
    
	// just take the first extension
	CFStringRef ext = (CFStringRef)CFArrayGetValueAtIndex(extensions, 0);
	char extstr[32];
	Boolean res = CFStringGetCString(ext, extstr, 32, kCFStringEncodingUTF8);
	XThrowIfError (!res, "CFStringGetCString");
	
	// release the array as we're done with this now
	CFRelease (extensions);
    
	char outFname[256];
#if TARGET_OS_WIN32
	char drive[3], dir[256];
	_splitpath_s(inputFileName, drive, 3, dir, 256, NULL, 0, NULL, 0);
	_makepath_s(outFname, 256, drive, dir, "outfile", extstr);
#else
    //	char outFname[64];
	sprintf (outFname, "/tmp/outfile.%s", extstr);
#endif
	outOutputFileURL = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8 *)outFname, strlen(outFname), false);
	if (!outOutputFileURL) {
		printf ("* * Bad output file path\n");
		UsageString(1);
    }
}
Beispiel #10
0
void	CAAudioFileFormats::FileFormatInfo::LoadDataFormats()
{
	if (mDataFormats != NULL) return;
	
	UInt32 *writableFormats = NULL, *readableFormats = NULL;
	int nWritableFormats, nReadableFormats;
	// get all writable formats
	UInt32 size;
	OSStatus err = AudioFormatGetPropertyInfo(kAudioFormatProperty_EncodeFormatIDs, 0, NULL, &size);
	if (err != noErr) goto bail;
	nWritableFormats = size / sizeof(UInt32);
	writableFormats = new UInt32[nWritableFormats];
	err = AudioFormatGetProperty(kAudioFormatProperty_EncodeFormatIDs, 0, NULL, &size, writableFormats);
	if (err != noErr) goto bail;
	
	// get all readable formats
	err = AudioFormatGetPropertyInfo(kAudioFormatProperty_DecodeFormatIDs, 0, NULL, &size);
	if (err != noErr) goto bail;
	nReadableFormats = size / sizeof(UInt32);
	readableFormats = new UInt32[nReadableFormats];
	err = AudioFormatGetProperty(kAudioFormatProperty_DecodeFormatIDs, 0, NULL, &size, readableFormats);
	if (err != noErr) goto bail;
	
	err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AvailableFormatIDs, sizeof(UInt32), &mFileTypeID, &size);
	if (err == noErr) {
		mNumDataFormats = size / sizeof(OSType);
		OSType *formatIDs = new OSType[mNumDataFormats];
		err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableFormatIDs,
			sizeof(UInt32), &mFileTypeID, &size, formatIDs);
		if (err == noErr) {
			mDataFormats = new DataFormatInfo[mNumDataFormats];
			for (int j = 0; j < mNumDataFormats; ++j) {
				int k;
				bool anyBigEndian = false, anyLittleEndian = false;
				DataFormatInfo *dfi = &mDataFormats[j];
				dfi->mFormatID = formatIDs[j];
				dfi->mReadable = (dfi->mFormatID == kAudioFormatLinearPCM);
				dfi->mWritable = (dfi->mFormatID == kAudioFormatLinearPCM);
				for (k = 0; k < nReadableFormats; ++k)
					if (readableFormats[k] == dfi->mFormatID) {
						dfi->mReadable = true;
						break;
					}
				for (k = 0; k < nWritableFormats; ++k)
					if (writableFormats[k] == dfi->mFormatID) {
						dfi->mWritable = true;
						break;
					}
				
				dfi->mNumVariants = 0;
				AudioFileTypeAndFormatID tf = { mFileTypeID, dfi->mFormatID };
				err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
					sizeof(AudioFileTypeAndFormatID), &tf, &size);
				if (err == noErr) {
					dfi->mNumVariants = size / sizeof(AudioStreamBasicDescription);
					dfi->mVariants = new AudioStreamBasicDescription[dfi->mNumVariants];
					err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableStreamDescriptionsForFormat,
						sizeof(AudioFileTypeAndFormatID), &tf, &size, dfi->mVariants);
					if (err) {
						dfi->mNumVariants = 0;
						delete[] dfi->mVariants;
						dfi->mVariants = NULL;
					} else {
						for (k = 0; k < dfi->mNumVariants; ++k) {
							AudioStreamBasicDescription *desc = &dfi->mVariants[k];
							if (desc->mBitsPerChannel > 8) {
								if (desc->mFormatFlags & kAudioFormatFlagIsBigEndian)
									anyBigEndian = true;
								else
									anyLittleEndian = true;
							}
						}
					}
				}
				
				dfi->mEitherEndianPCM = (anyBigEndian && anyLittleEndian);
			}
		}
		delete[] formatIDs;
	}
bail:
	delete[] readableFormats;
	delete[] writableFormats;
}