Re: Decoding compressed to LPCM
Re: Decoding compressed to LPCM
- Subject: Re: Decoding compressed to LPCM
- From: Aurélien Ammeloot <email@hidden>
- Date: Mon, 21 Apr 2008 20:22:19 +0100
Le 21 avr. 08 à 15:34, Jens Alfke wrote :
If that's not it, you might have to post your code, or a link to it,
here.
Hi again,
Well I'm posting my code now.
I'm quite unclear with client formats etc. Which one do I have to
define or not? Both input and output ?
This code works for converting big endian AIFF to little endian WAV.
But it doesn't work from a compressed audio file AAC or MP3 to little
endian WAV. I've tried step-by-step debugging and it seems
ExtAudioFileCreateNew(...) fails in the latter cases.
Here's the code:
-------------
- (BOOL)convert
{
ExtAudioFileRef inputFileRef, outputFileRef; // Input and PCM files
as Core audio references
FSRef inputFileFSRef, outputFileFSRef; // Input and temporary file
paths as FS references
OSStatus err; // Return codes will be collected here
AudioStreamBasicDescription inputFileFormat, outputFileFormat; //
Audio format descriptions
CFStringRef ofName = CFStringCreateWithCString(NULL, [tempName
UTF8String], kCFStringEncodingUTF8);
// Converting the file path from a NSString object to a FS reference
err = FSPathMakeRef((UInt8 *)[originPath UTF8String],
&inputFileFSRef,
NULL);
// Error handling
NSLog(@"FSPathMakeRef %@ %d",originPath,err);
if(err != noErr)
return NO;
// Creating the temporaryFile
err = FSPathMakeRef((UInt8 *)[tempPath UTF8String],
&outputFileFSRef,
NULL);
// Error handling
NSLog(@"FSPathMakeRef %@ %d",tempPath,err);
if(err != noErr)
return NO;
// Open the input file and store its reference in memory
err = ExtAudioFileOpen(&inputFileFSRef, &inputFileRef);
// Error handling
NSLog(@"ExtAudioFileOpen %@ %d",originPath,err);
if(err != noErr)
return NO;
// Get audio properties from the source file to the input format
UInt32 size = sizeof(AudioStreamBasicDescription);
err = ExtAudioFileGetProperty(inputFileRef,
kExtAudioFileProperty_FileDataFormat, &size, &inputFileFormat);
// Error handling
NSLog(@"ExtAudioFileGetProperty %@ %d",originPath,err);
if(err != noErr)
return NO;
// Get audio properties from the input file to the output format
size = sizeof(AudioStreamBasicDescription);
err = ExtAudioFileGetProperty(inputFileRef,
kExtAudioFileProperty_FileDataFormat, &size, &outputFileFormat);
// Error handling
NSLog(@"ExtAudioFileGetProperty %@ %d",originPath,err);
if(err != noErr)
return NO;
// Modifying the data structure for output file --> Wave format
outputFileFormat.mSampleRate = 44100.;
outputFileFormat.mFormatID = kAudioFormatLinearPCM; // Linear PCM
format
outputFileFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger |
kAudioFormatFlagIsPacked;
outputFileFormat.mBytesPerPacket = 4;
outputFileFormat.mFramesPerPacket = 1;
outputFileFormat.mChannelsPerFrame = 2;
outputFileFormat.mBitsPerChannel = 16;
err = ExtAudioFileCreateNew(&outputFileFSRef, ofName,
kAudioFileWAVEType, &outputFileFormat, NULL, &outputFileRef);
// Error handling
NSLog(@"ExtAudioFileCreateNew %@ %d",originPath,err);
if(err != noErr)
return NO;
// Set the client format
size = sizeof(outputFileFormat);
err = ExtAudioFileSetProperty(inputFileRef,
kExtAudioFileProperty_ClientDataFormat, size, &inputFileFormat);
// Error handling
NSLog(@"ExtAudioFileCreateNew %@ %d",originPath,err);
if(err != noErr)
return NO;
// Set the client format
size = sizeof(outputFileFormat);
err = ExtAudioFileSetProperty(inputFileRef,
kExtAudioFileProperty_ClientDataFormat, size, &outputFileFormat);
// Error handling
NSLog(@"ExtAudioFileCreateNew %@ %d",originPath,err);
if(err != noErr)
return NO;
// Creating a buffer
UInt32 kSrcBufferSize = 32768;
char srcBuffer[kSrcBufferSize];
while(1) {
// Creating a new buffer list
AudioBufferList fillBufList;
fillBufList.mNumberBuffers = 1;
fillBufList.mBuffers[0].mNumberChannels =
inputFileFormat.mChannelsPerFrame;
fillBufList.mBuffers[0].mDataByteSize = kSrcBufferSize;
fillBufList.mBuffers[0].mData = srcBuffer;
// Getting remaining frames from file
UInt32 numFrames = (kSrcBufferSize / inputFileFormat.mBytesPerFrame);
// Filling buffer with file 1
err = ExtAudioFileRead(inputFileRef, &numFrames, &fillBufList);
// If no frames left, exit the loop
if(!numFrames)
break;
// Writing buffer to output file
err = ExtAudioFileWrite(outputFileRef, numFrames, &fillBufList);
}
ExtAudioFileDispose(inputFileRef);
ExtAudioFileDispose(outputFileRef);
return YES;
}
---
Thanks for any advice,
Aurélien _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden