Re: Save PCM Stream to AIFF File (was How to play RAW PCM data using CA?)
Re: Save PCM Stream to AIFF File (was How to play RAW PCM data using CA?)
- Subject: Re: Save PCM Stream to AIFF File (was How to play RAW PCM data using CA?)
- From: Doug Wyatt <email@hidden>
- Date: Mon, 27 Apr 2009 07:45:27 -0700
If you call AudioFileWritePackets instead of AudioFileSetUserData, you
will be storing your audio data where other applications expect to
read it.
Doug
On Apr 26, 2009, at 7:46 AM, malcom wrote:
However, if that proves too difficult, you could change from saving
the raw NSData to using the CoreAudio
AudioFile API to save a CAF file instead of the raw data. You will
certainly be able to open an audio file created > with the
AudioFIle API (Or ExtAudioFile, if needed).
I've tried to save the file using AudioFileCreate() (to create the
file itself) and AudioFileSetUserData() to fill up with my samples.
Everything works fine... in fact file data is written correctly but
I'm unable to play it (every player I've used says simply "File Format
Error").
I've also tried to import it in Audacity as RAW file and it works fine
(but I need to set the "endianess" parameter on import. Due to this
fact probability I've set a wrong type in my
AudioStreamBasicDescription's mFormatFlags. Could be the problem?
This is my code. It's pretty simple and you can try it in a second.
Chirp function generate 16bit/mono/44100 sound samples (as an array
of shorts).
...
void WriteChirpAIFFFile(FSRef fsRef, CFStringRef newFile) {
// setup the format
Float64 sampleRate = 44100; // 44100 sample rate frequency
UInt32 bitsPerChannel = 16; // 16 bits of data
double start_freq = 1000; // starting frequency is 1000Hz
double end_freq = 10000; // end frequency is 10000Hz
int duration = 10; // duration of chirp is 10 seconds
// setup audio stream description
AudioStreamBasicDescription aiffFormat;
aiffFormat.mSampleRate = sampleRate; // sample rate data
aiffFormat.mFormatID = kAudioFormatLinearPCM; // linear pulse-code
modulation data
// importing my stream data in Audacity I need to set the endianess
parameter (not big endian or little endian)
// so... the error could be in here, but I don't know how to solve
it.
aiffFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked |
kLinearPCMFormatFlagIsBigEndian;
aiffFormat.mBytesPerPacket = 4; // The number of bytes in a
packet of data.
aiffFormat.mFramesPerPacket = 1; // In uncompressed audio, a
Packet is one frame, (mFramesPerPacket == 1)
aiffFormat.mBytesPerFrame = 4; // The number of bytes in a
packet of data.
aiffFormat.mChannelsPerFrame = 1;
aiffFormat.mBitsPerChannel = bitsPerChannel;
FSRef outRef;
AudioFileID audioFileID;
// create our AIFF audio file structure (headers only)
OSStatus status = AudioFileCreate(&fsRef, newFile,
kAudioFileAIFFType, &aiffFormat, 0, &outRef, &audioFileID);
...
int total_fileSize = 0;
// generate chirp signal from 1000Hz to 10000Hz of 10 secs at
16bit/44100 sample rate
short *arr = generateChirpSignalBuffer(start_freq, end_freq,
duration, (long)sampleRate, bitsPerChannel,&total_fileSize);
// copy buffer data to file
status = AudioFileSetUserData(audioFileID, kAudioFileAIFFType, 0,
total_fileSize, arr);
if (status == noErr)
NSLog(@"%d bytes of data, our chirp stream of shorts, was copied
successfully to aiff file",total_fileSize);.
...
AudioFileClose(audioFileID);
free(arr);
}
_______________________________________________
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