writing lpcm to file
writing lpcm to file
- Subject: writing lpcm to file
- From: Dennis Christopher <email@hidden>
- Date: Wed, 4 Feb 2009 12:25:18 -0500
I'm rather new to CoreAudio and I'm having trouble playing out sound
data my app receives in a stream. As a check on the
validity of the data, I'm writing it out to an audio file. But the
resultant file is noise.
I'm receiving 2 byte sound samples in BigEndian format. The samples
are lpcm.
The first attempt (recordToFile below) is perhaps too simple--I am
using the AudioFile interface and simply writing down
a chunk of the samples using AudioFileWritePackets. Am I obliged to
specify that the data format is BigEndian?
Also the AudioStreamBasicDescription I am supplying (taken from the
aqrecord sample code) seems wrong, but
if I substitute the commented lines beginning
recordFormat.mBytesPerPacket = size; then
AudioFileCreateWithURL fails with a 'fmt?' error.
Any suggestions would be appreciated.
Dennis Christopher
-(void)recordToFile:(char *)recordFileName size:(int)size data:
(void*)data
{
// 1. set up the format for the output file
AudioStreamBasicDescription recordFormat;
CFURLRef url;
// fill structures with 0/NULL
memset(&recordFormat, 0, sizeof(recordFormat));
// default to PCM, 16 bit int
recordFormat.mSampleRate = 11025.0;
recordFormat.mChannelsPerFrame = 1;
recordFormat.mFormatID = kAudioFormatLinearPCM;
// determine file format
AudioFileTypeID audioFileType = kAudioFileCAFType;
recordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked;
recordFormat.mBitsPerChannel = 16;
recordFormat.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
recordFormat.mBytesPerPacket = recordFormat.mBytesPerFrame =
(recordFormat.mBitsPerChannel / 8) * recordFormat.mChannelsPerFrame;
recordFormat.mFramesPerPacket = 1;
/*recordFormat.mBytesPerPacket = size;
recordFormat.mBytesPerFrame = 2;
recordFormat.mFramesPerPacket = size/2;*/
recordFormat.mReserved = 0;
// 2. create the audio file
AudioFileID recordFile;
if (recordFileName != nil) {
// convert recordFileName from C string to CFURL
url = CFURLCreateFromFileSystemRepresentation(NULL, (Byte
*)recordFileName, strlen(recordFileName), FALSE);
// create the audio file
CheckError(AudioFileCreateWithURL(url, audioFileType, &recordFormat,
kAudioFileFlags_EraseFile,
&recordFile), "AudioFileCreateWithURL failed");
CFRelease(url);
// 3. write packets to it
UInt32 inNumPackets = size/2;// supply number of frames==samples?
CheckError(AudioFileWritePackets(recordFile, FALSE, size,
NULL, 0, &inNumPackets, data),"AudioFileWritePackets
failed");
// 4. close the file
AudioFileClose(recordFile);
}
}
_______________________________________________
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