16 bit short array to AIFF
16 bit short array to AIFF
- Subject: 16 bit short array to AIFF
- From: Marco Bambini <email@hidden>
- Date: Sat, 13 Jun 2009 10:47:02 +0200
Hello,
I have an array of short int (16 bit) filled with values in the
-32768, 32767 range and I need to write this array into an AIFF file.
So far the function I am using is:
void generareAIFF (short *chirp, int samples, double durationParam,
int sampleRateParam)
{
NSString *realPath = [NSString stringWithFormat: @"file://
%@",AIFF_PATH];
// setup audio stream description
AudioStreamBasicDescription aiffFormat;
aiffFormat.mSampleRate = sampleRateParam; // sample rate data
aiffFormat.mFormatID = kAudioFormatLinearPCM; // linear pulse-code
modulation data
// importing my stream data in Audacity I need to set the endianess
parameter
// so you need to convert between big->little endian before saving data
aiffFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsBigEndian;
aiffFormat.mFramesPerPacket = 1;
aiffFormat.mChannelsPerFrame = 1;
aiffFormat.mBitsPerChannel = 16;
aiffFormat.mBytesPerPacket = aiffFormat.mBytesPerFrame =
(aiffFormat.mBitsPerChannel / 8) * aiffFormat.mChannelsPerFrame;
AudioFileID audioFileID;
CFURLRef url = CFURLCreateWithString(NULL, (CFStringRef)realPath,
NULL);
// create AIFF audio file structure (headers only)
OSStatus status = AudioFileCreateWithURL(url, kAudioFileAIFFType,
&aiffFormat, 0, &audioFileID);
//
UInt32 total_fileSize = (UInt32)(durationParam * sampleRateParam);
AudioStreamPacketDescription asbd;
asbd.mDataByteSize = total_fileSize;
asbd.mStartOffset = 0;
asbd.mVariableFramesInPacket = 0;
UInt32 pullPackets = total_fileSize;
// AIFF data needs to be in BIG ENDIAN format
if (CFByteOrderGetCurrent() != CFByteOrderBigEndian) {
for (int i = 0; i < samples; i++) chirp[i] =
CFSwapInt16HostToBig(chirp[i]);
}
status = AudioFileWritePackets(audioFileID, NO, total_fileSize,
&asbd, 0, &pullPackets, chirp);
AudioFileClose(audioFileID);
}
my questions are:
1) does that code seem correct?
2) why when I open that AIFF file in audacity it says 32bit float
instead of the correct 16bit PCM?
Thanks a lot,
Marco Bambini
_______________________________________________
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