having to increment my stereo AudioFileReadPackets buffer by 4 rather than 2 to playback at regular speed
having to increment my stereo AudioFileReadPackets buffer by 4 rather than 2 to playback at regular speed
- Subject: having to increment my stereo AudioFileReadPackets buffer by 4 rather than 2 to playback at regular speed
- From: Mike Gao <email@hidden>
- Date: Fri, 7 May 2010 04:40:52 -0700
After AudioFileReadPackets to fill SInt32 buffer, stereo interleaved,
I end up having to increment my index by 4 rather than 2 in my audio
call back to play the sound file at regular speed.
I was using ReadBytes originally and thought the +4 was due to some
kind of 16bit vs 32bit and 2 bytes, but even after switching to
readPackets it ends up being +4 needed
To read the file:
//soundFileSize, etc. are arrays because of multiple sound files
OSStatus status =
AudioFileOpenURL(fileURL,kAudioFileReadPermission,0,&soundFileID[i]);
[self checkStatus:status];
status = AudioFileGetPropertyInfo(soundFileID[i],
kAudioFilePropertyAudioDataByteCount, &size, &propertyIsWritable);
status = AudioFileGetProperty(soundFileID[i],
kAudioFilePropertyAudioDataByteCount, &size, &fileSize64);
[self checkStatus:status];
size2=sizeof(soundFileSize[i]);
status = AudioFileGetProperty(soundFileID[i],
kAudioFilePropertyAudioDataPacketCount, &size, &soundFileSize[i]);
[self checkStatus:status];
status = AudioFileGetPropertyInfo(soundFileID[i],
kAudioFilePropertyDataFormat, &size, &propertyIsWritable);
if (DEBUG) NSLog(@"Property sizes: %d %d\n", size,
sizeof(AudioStreamBasicDescription));
theDescription = malloc(size);
status = AudioFileGetProperty(soundFileID[i],
kAudioFilePropertyDataFormat, &size, theDescription);
if (DEBUG)
{
NSLog(@"Sample rate: %f Bytes Per Frame: %d Bit: %d Channels: %d",
theDescription->mSampleRate,
theDescription->mBytesPerFrame,theDescription->mBitsPerChannel,theDescription->mChannelsPerFrame);
NSLog(@"Bytes per Packet: %d Frames Per Packet:
%d",theDescription->mBytesPerPacket,theDescription->mFramesPerPacket);
}
if (soundFileSize[i] < 1)
{
NSLog(@"File too smal!");
}
soundBuffer[i] = (SInt32*)malloc(sizeof(SInt32)*soundFileSize[i]*2);
UInt32 numBytesRead = -1;
status = AudioFileReadPackets (soundFileID[i],NO, &numBytesRead,
NULL, 0, &soundFileSize[i], soundBuffer[i]);
NSLog(@"Packets read from file * sizeof(SInt32) * 2 for stereo:
%d\n", soundFileSize[i]*sizeof(SInt32)*2);
NSLog(@"Bytes read from file: %d\n", numBytesRead);
NSLog(@"Sample count: %d\n", soundFileSize[i] / 2);
// OUTPUT:
Sample rate: 44100.000000 Bytes Per Frame: 8 Bit: 32 Channels: 2
2010-05-06 23:21:28.698 cocos2dTest[125:207] Bytes per Packet: 8
Frames Per Packet: 1
2010-05-06 23:21:28.758 cocos2dTest[125:207] Packets read from file *
sizeof(SInt32) * 2 for stereo: 2226928
2010-05-06 23:21:28.761 cocos2dTest[125:207] Bytes read from file: 2226928
2010-05-06 23:21:28.764 cocos2dTest[125:207] Sample count: 139183
/////////////////////////////////
To play it, I am using: (I use convert my audio data from SInt32 to
Float32 then back again so that I can do my call back in Float32 and
be compatible with my other DSP code)
buffer[2*i] = buffer[2*i]
+GAIN*(Float32)(soundBuffer[k][soundPlayIndex])/factor; // k is which
sound we play
buffer[2*i+1] = buffer[2*i+1]
+GAIN*(Float32)(soundBuffer[k][soundPlayIndex+1])/factor;
soundPlayIndex[k][j] += 4; // sizeof(SInt32) * 2?
if (soundPlayIndex[k][j] >= soundFileSize[k] * 2)
{
soundPlayIndex[k][j] = -1; // stop play
}
_______________________________________________
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