18 #define CONVERT_16_BIT(unsV, sinV, bigE) \
20 unsV = ((unsV << 8) | (unsV >> 8)); \
21 sinV = unsV & 0x7fff; \
25 #define CONVERT_24_BIT(res, ptr, bigE) \
27 res = (((int) ptr[2]) << 16) \
28 | (((uint32) (uint8) ptr[1]) << 8) \
29 | ((uint32) (uint8) ptr[0]); \
31 res = (((int) ptr[0]) << 16) \
32 | (((uint32) (uint8) ptr[1]) << 8) \
33 | ((uint32) (uint8) ptr[2])
46 mURL = CFURLCreateWithFileSystemPath (NULL,
47 (CFStringCreateWithCString (NULL, tpath.c_str(), kCFStringEncodingMacRoman)),
48 kCFURLPOSIXPathStyle,
false);
63 CFStringGetCString (CFURLGetString(path), theName,
CSL_NAME_LEN, kCFStringEncodingMacRoman);
64 mPath = string(theName);
96 UInt32 propertySize, fileSize;
99 mURL = CFURLCreateFromFileSystemRepresentation(NULL,
100 (
const UInt8*)
mPath.c_str(),
mPath.size(),
false);
102 AudioFileOpenURL(
mURL, 0x01, kAudioFileCAFType, &
mSoundID);
104 err = AudioFileGetPropertyInfo(
mSoundID,
105 kAudioFilePropertyAudioDataPacketCount,
109 printf(
"\nCASoundFile::openForRead: AudioFileGetPropertyInfo Error = %x\nFile: \"%s\"\n\n",
110 (
int) err,
mPath.c_str());
113 err = AudioFileGetProperty(
mSoundID, kAudioFilePropertyAudioDataPacketCount, &propertySize, &fileSize);
115 printf(
"\nCASoundFile::openForRead: AudioFileGetProperty Error = %x\nFile: \"%s\"\n\n",
116 (
int) err,
mPath.c_str());
119 AudioStreamBasicDescription fileFormat;
120 propertySize =
sizeof(AudioStreamBasicDescription);
121 err = AudioFileGetProperty(
mSoundID, kAudioFilePropertyDataFormat, &propertySize, &fileFormat);
123 printf(
"\nCASoundFile::openForRead: AudioFileGetProperty2 Error = %x\nFile: \"%s\"\n\n",
124 (
int) err,
mPath.c_str());
129 mFrameRate = (unsigned) fileFormat.mSampleRate;
132 mBigEndian = fileFormat.mFormatFlags & kAudioFormatFlagIsBigEndian;
155 unsigned rate,
unsigned bitDepth)
throw (
CException) {
156 logMsg(
"Error: CASoundFile::openForWrite unsupported");
167 OSStatus err = noErr;
171 UInt32 numBytes, fileSize;
178 memset(sampleBufferPtr, 0, numFrames *
sizeof(
sample));
182 fileSize = numFrames;
184 unsigned char * soundBuffer = (
unsigned char *) malloc(numBytes);
196 err = AudioFileReadPackets(
mSoundID,
false, &numBytes, NULL, currentFrame, &fileSize, soundBuffer);
198 printf(
"\nCASoundFile::openForRead: AudioFileReadPackets Error = %x\nFile: \"%s\"\n\n",
199 (
int) err,
mPath.c_str());
202 if (fileSize != numFrames) {
203 printf(
"\nCASoundFile::openForRead: read too few samples, %d instead of %d\n",
204 (
int) fileSize, numFrames);
207 unsigned short * tBfr = (
unsigned short *) soundBuffer;
208 unsigned char * cBfr = soundBuffer;
213 if (myChannels == 1) {
215 if (mBytesPerSample == 2) {
216 sample scale = 1.0f / 32867.0f;
217 for (
unsigned j = 0; j < fileSize; j++) {
220 *sampPtr++ = (
sample) sVal * scale;
222 }
else if (mBytesPerSample == 3) {
224 for (
unsigned j = 0; j < fileSize; j++) {
226 *sampPtr++ = (
sample) iVal * scale;
230 }
else if (myChannels == 2) {
232 sample * sampPtr = sampBuf;
233 if (mBytesPerSample == 2) {
234 sample scale = 1.0f / 32867.0f;
235 for (
unsigned j = 0; j < (fileSize * myChannels); j++) {
238 *sampPtr++ = (
sample) sVal * scale;
240 }
else if (mBytesPerSample == 3) {
242 for (
unsigned j = 0; j < (fileSize * myChannels); j++) {
244 *sampPtr++ = (
sample) iVal * scale;
253 currentFrame += numFrames;
256 if (currentFrame > (
unsigned) mStop) {
257 unsigned numFramesRemaining = currentFrame -
mStop;
258 unsigned numFramesRead = numFrames - numFramesRemaining;
261 while (numFramesRead < numFrames) {
265 currentFrame += numFramesRead;
268 unsigned bytesToClear = numFramesRemaining *
sizeof(
sample);
271 memset(sampleBufferPtr, 0, bytesToClear);
281 if (this->isCached())
282 return mCurrentFrame;
285 mCurrentFrame = position;
288 mCurrentFrame = mCurrentFrame - position;
291 mCurrentFrame = duration() - position;
294 mCurrentFrame = SEEK_CUR;
295 logMsg(
"Error: Invalid position seek flag. Used kPositionCurrent.");
298 return mCurrentFrame;
304 logMsg(
"Error: CASoundFile::writeBuffer unsupported");
311 const char * nam =
path().c_str();
312 if (strlen(nam) > 50)
313 logMsg(
"SndFile \"%s\"\n\t\t%d Hz, %d ch, %5.3f sec %s",
317 logMsg(
"SndFile \"%s\" - %d Hz, %d ch, %5.3f sec %s",