Re: AudioQueue newbie : Handling Forward , Rewind , etc..,
Re: AudioQueue newbie : Handling Forward , Rewind , etc..,
- Subject: Re: AudioQueue newbie : Handling Forward , Rewind , etc..,
- From: Rajeswar Rao <email@hidden>
- Date: Fri, 1 Aug 2008 17:29:20 +0530
Devon and William , thanks for the valuable and timely input.
your suggestions lead to different solution , thought all the
solutions are working like a charm :D ( for forward and backward
features )
but I have come across some issue .......
the Structure I employed is
typedef struct {
AudioStreamBasicDescription mDataFormat;
AudioQueueRef mQueue;
AudioQueueBufferRef mBuffers[kNumberBuffers];
AudioFileID mAudioFile;
UInt32 bufferByteSize;
SInt64 mCurrentPacket;
UInt32 mNumPacketsToRead;
AudioStreamPacketDescription *mPacketDescs;
bool mIsRunning;
}AQPlayer
-(id)init
{
aqData = malloc(sizeof(AQPlayer));
// other allocation and initialization routines for AudioQueue
AudioQueueNewOutput (&(aqData-
>mDataFormat),HandleOutputBuffer,aqData,CFRunLoopGetCurrent
(),kCFRunLoopCommonModes,0,&(aqData->mQueue));
//etc.,,,,
}
- (void)startPlayback
{
AudioQueueStart (aqData->mQueue,NULL);
}
- (void)pausePlayback
{
AudioQueuePause(aqData->mQueue);
}
- (void)playFrom:(float )seekPoint
{
UInt32 propertySize;
UInt64 totPackets;
propertySize = sizeof(totPackets);
OSStatus err = AudioFileGetProperty(aqData->mAudioFile,
kAudioFilePropertyAudioDataPacketCount, &propertySize, &totPackets);
totPackets= totPackets* seekPoint ;
aqData->mCurrentPacket = totPackets;
}
static void HandleOutputBuffer (void *aqData,AudioQueueRef
inAQ,AudioQueueBufferRef inBuffer)
{
AQPlayer *pAqData = (AQPlayer *) aqData;
if (pAqData->mIsRunning == 0) return;
UInt32 numBytesReadFromFile;
UInt32 numPackets = pAqData->mNumPacketsToRead;
AudioFileReadPackets (pAqData->mAudioFile,
false,&numBytesReadFromFile,pAqData->mPacketDescs,pAqData-
>mCurrentPacket,&numPackets,inBuffer->mAudioData);
if (numPackets > 0) {
inBuffer->mAudioDataByteSize = numBytesReadFromFile;
AudioQueueEnqueueBuffer (pAqData->mQueue,inBuffer, (pAqData-
>mPacketDescs ? numPackets : 0), pAqData->mPacketDescs );
pAqData->mCurrentPacket += numPackets;
} else {
AudioQueueStop (
pAqData->mQueue,
false
);
pAqData->mIsRunning = false;
}
UInt32 propertySize = sizeof(pAqData->mDataFormat);
UInt64 numBytes;
propertySize = sizeof(numBytes);
UInt64 totPackets;
propertySize = sizeof(totPackets);
OSStatus err = AudioFileGetProperty(pAqData->mAudioFile,
kAudioFilePropertyAudioDataPacketCount, &propertySize, &totPackets);
printf("numPackets ------- %d mCurrentPacket %d percentage is %f
mAudioDataByteSize %d \n", totPackets,pAqData->mCurrentPacket ,
(pAqData->mCurrentPacket)/totPackets ,inBuffer->mAudioDataByteSize );
NSTimeInterval seconds;
propertySize = sizeof(seconds);
err = AudioFileGetProperty(pAqData->mAudioFile,
kAudioFilePropertyEstimatedDuration, &propertySize, &seconds);
printf("seconds %f \n",seconds);
}
The issue is :
o/p is numPackets ------- 7444 mCurrentPacket 0 percentage is
0.000000 mAudioDataByteSize 0 seconds 194.455510
1) currentPacket is always "zero" no matter what I do...... hence I
cant get percentage played. Hence showing the percentage played has to
be calculated in reverse way.... ( but its working so far ) I think I
have to maintain separate index for maintaining the packet Index ( as
suggested by Devon )
2) and also observe that , the Estimated Time differs to the actual
time by a 'mile' to be precise by 7 sec.
Is there a way to get the actual duration of file ??
am I missing any obvious part in it ? :)
Once again thank you guys.
On Jul 30, 2008, at 1:27 AM, William Stewart wrote:
On Jul 29, 2008, at 12:22 PM, Devon Tucker wrote:
Raj,
I'm currently grappling with the exact same issues. I think I know how
to solve these problems conceptually but I'm not sure about the
implementation. Keep in mind that I know very little about digital
audio so I hope someone will correct me if I'm wrong (Which I very
well could be).
For forward and rewind, seeking, you need to determine the packet
index given the time in seconds you wish to seek to. For example given
that you want to seek to time "pos" you would calculate the necessary
packet index like this:
packetIndex = (pos * sampleRate) / framesPerPacket
Implementation-wise, I guess, depends on how you want to do it. You
could stop playback, clear your buffers, start buffering from then new
packet index and start playback again. You could also simply reset
your packetIndex to the desired index and keep playing normally.
Yes - that's pretty much it. You can implement a skip-type fast
forward by just feeding in bits of data as you skip through the file.
As for tracking the position through the song, this can be
accomplished much the same way. Given the currently playing packet we
can determine the position by:
AudioQueueGetCurrentTime
An audio queue always starts at sample time == 0.
position = (packetIndex * framesPerPacket) / sampleRate
In order to keep track of the position in the audio file, with respect
to implementation, we need to keep track of our current packet index.
This packet index could be different from the packet index needed for
your buffer callback however, due to buffer priming. I recommend
keeping a separate packet index for playback, which starts at zero
when playback starts and gets incremented by the number of packets
read in your buffer callback.
Hope this helps,
Devon
On 28-Jul-08, at 5:56 AM, Rajeswar Rao wrote:
Hi ,
Im trying to paly audio file, using AudioQueue ,
I could Play , Pause using AudioQueueStart and AudioQueuePause ;
Now im trying to implement 'Forward/Rewind' functionality also the
duration played so far .....
How can I achieve this ? Do I have to handle these things in "Callback
function" ? if so , how ?
any suggestions are appreciated.
Thanks in Adavance
~Raj
_______________________________________________
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
Cheers,
Devon
_______________________________________________
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
_______________________________________________
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