how to get the AudioToolbox MusicTrack to loop in iOS 9.0? - ios

this code works fine for looping a MusicTrack in iOS 8.4, but will halt the app under iOS 9.0 when setting the sequence with MusicPlayerSetSequence
var loopInfo = MusicTrackLoopInfo(loopDuration: 1.0,numberOfLoops: 0)
MusicTrackSetProperty(track, UInt32(kSequenceTrackProperty_LoopInfo), &loopInfo, UInt32(sizeofValue(loopInfo)))
is there another way to get the track to loop in iOS 9?

Others are having a similar problem: https://forums.developer.apple.com/thread/9940 the general idea for a current work around is to set the playback marker back to 0 once it reaches the end of the track.
For example use this in the method you're using to start your music player:
MusicTrack track = NULL;
MusicTimeStamp trackLen = 0;
UInt32 trackLenLen = sizeof(trackLen);
//Get main track
MusicSequenceGetIndTrack(musicSequence, 0, &track);
//Get length of track
MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &trackLen, &trackLenLen);
//Create UserData for User Event with any data
static MusicEventUserData userData = {1, 0x09};
//Put new user event at the end of the track
MusicTrackNewUserEvent(track, trackLen, &userData);
//Set a callback for when User Events occur
MusicSequenceSetUserCallback(musicSequence, sequenceUserCallback, musicPlayer);
And then you can have a callback function:
static void sequenceUserCallback(void *inClientData,
MusicSequence inSequence,
MusicTrack inTrack,
MusicTimeStamp inEventTime,
const MusicEventUserData *inEventData,
MusicTimeStamp inStartSliceBeat,
MusicTimeStamp inEndSliceBeat)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
MusicPlayerSetTime((MusicPlayer) inClientData, 0.0);
}];
}
Which will set the player back to zero.

Related

Precise time of audio queue playback finish

I am using Audio Queues to playback audio files. I need precise timing on the finish of last buffer.
I need to notify a function no later than 150ms-200 ms after the last buffer is played...
Thru callback method I know how many buffers are enqueued
I know the buffer size, I know the how many bytes last buffer is filled with.
First I initialize a number of buffers end fill the buffers with audio data, then enqueue them. When Audio Queue needs a buffer to be filled it calls the callback and I fill the buffer with data.
When there is no more audio data available Audio Queue sends me the last empty buffer, so I fill it with whatever data I have:
if (sharedCache.numberOfToTalPackets>0)
{
if (currentlyReadingBufferIndex==[sharedCache.baseAudioCache count]-1) {
inBuffer->mAudioDataByteSize = (UInt32)bytesFilled;
lastEnqueudBufferSize=bytesFilled;
err=AudioQueueEnqueueBuffer(inAQ,inBuffer,(UInt32)packetsFilled,packetDescs);
if (err) {
[self failWithErrorCode:err customError:AP_AUDIO_QUEUE_ENQUEUE_FAILED];
}
printf("if that was the last free packet description, then enqueue the buffer\n");
//go to the next item on keepbuffer array
isBufferFilled=YES;
[self incrementBufferUsedCount];
return;
}
}
When Audio Queue asks for more data via callback and I have no more data , I start to countdown the buffers. If buffer count equals to zero, which means only one buffer left on the flight to be played, the moment playback is done I try to stop the audio queue.
-(void)decrementBufferUsedCount
{
if (buffersUsed>0) {
buffersUsed--;
printf("buffer on the queue %i\n",buffersUsed);
if (buffersUsed==0) {
NSLog(#"playback is finished\n");
// end playback
isPlayBackDone=YES;
double sampleRate = dataFormat.mSampleRate;
double bufferDuration = lastEnqueudBufferSize/ sampleRate;
double estimatedTimeNeded=bufferDuration*1;
[self performSelector:#selector(stopPlayer) withObject:nil afterDelay:estimatedTimeNeded];
}
}
}
-(void)stopPlayer
{
#synchronized(self)
{
state=AP_STOPPING;
}
err=AudioQueueStop(queue, TRUE);
if (err) {
[self failWithErrorCode:err customError:AP_AUDIO_QUEUE_STOP_FAILED];
}
else
{
#synchronized(self)
{
state=AP_STOPPED;
NSLog(#"Stopped\n");
}
However it seems I can't get precise timing here. Above code stops player early.
if I do following audio cuts early too
double bufferDuration = XMAQDefaultBufSize/ sampleRate;
double estimatedTimeNeded=bufferDuration*1;
if increase 1 to 2 since the buffer size is big I get some delay, seem 1.5 is the optimum value for now but I dont understand why lastEnqueudBufferSize/ sampleRate is not wotking
Details of the audio file, and buffers:
Audio file has 22050 sample rate
#define kNumberPlaybackBuffers 4
#define kAQDefaultBufSize 16384
it is a vbr file format with no bitrate information available
EDIT:
I found an easier way that gets the same results (+/-10ms). After you set up your output Queue with AudioQueueNewOutput() you initialize a AudioQueueTimelineRef to be used in your output callback. (ticksToSeconds function is included below in my first method) don't forget to import<mach/mach_time.h>
//After AudioQueueNewOutput()
AudioQueueTimelineRef timeLine; //ivar
AudioQueueCreateTimeline(queue, self.timeLine);
Then in your output callback you call AudioQueueGetCurrentTime(). Caveat: queue must be playing for valid timestamps. So for very short files you might need to use the AudioQueueProcessingTap method below.
AudioTimeStamp timestamp;
AudioQueueGetCurrentTime(queue, self->timeLine, &timestamp, NULL);
The timestamp ties together the current sample playing with the current machine time. With that info we can get an exact machine time in the future when our last sample will be played.
Float64 samplesLeft = self->frameCount - timestamp.mSampleTime;//samples in file - current sample
Float64 secondsLeft = samplesLeft / self->sampleRate; //seconds of audio to play
UInt64 ticksLeft = secondsLeft / ticksToSeconds(); //seconds converted to machine ticks
UInt64 machTimeFinish = timestamp.mHostTime + ticksLeft; //machine time of first sample + ticks left
Now that we have this future machine time we can use it to time whatever it is that you want to do with some accuracy.
UInt64 currentMachTime = mach_absolute_time();
Uint64 ticksFromNow = machTimeFinish - currentMachTime;
float secondsFromNow = ticksFromNow * ticksToSeconds();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(secondsFromNow * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//do the thing!!!
printf("Giggety");
});
If GCD dispatch_async isn't accurate enough there are ways to set up a precision timer
Using AudioQueueProcessingTap
You can get fairly low response time from an AudioQueueProcessingTap. First you make your callback that will essentially put itself in-between the audio stream. The MyObject type is just whatever self is in your code(this is ARC bridging here to get self inside the function). Inspecting ioFlags tells you when the stream starts and finishes. The ioTimeStamp of an output callback describes time that the first sample in the callback will hit the speaker in the future. So if you want to get exact here's how you do it. I added some convenience functions for converting machine time to seconds.
#import <mach/mach_time.h>
double getTimeConversion(){
double timecon;
mach_timebase_info_data_t tinfo;
kern_return_t kerror;
kerror = mach_timebase_info(&tinfo);
timecon = (double)tinfo.numer / (double)tinfo.denom;
return timecon;
}
double ticksToSeconds(){
static double ticksToSeconds = 0;
if (!ticksToSeconds) {
ticksToSeconds = getTimeConversion() * 0.000000001;
}
return ticksToSeconds;
}
void processingTapCallback(
void * inClientData,
AudioQueueProcessingTapRef inAQTap,
UInt32 inNumberFrames,
AudioTimeStamp * ioTimeStamp,
UInt32 * ioFlags,
UInt32 * outNumberFrames,
AudioBufferList * ioData){
MyObject *self = (__bridge Object *)inClientData;
AudioQueueProcessingTapGetSourceAudio(inAQTap, inNumberFrames, ioTimeStamp, ioFlags, outNumberFrames, ioData);
if (*ioFlags == kAudioQueueProcessingTap_EndOfStream) {
Float64 sampTime;
UInt32 frameCount;
AudioQueueProcessingTapGetQueueTime(inAQTap, &sampTime, &frameCount);
Float64 samplesInThisCallback = self->frameCount - sampleTime;//file sampleCount - queue current sample
//double secondsInCallback = outNumberFrames / (double)self->sampleRate; outNumberFrames was inaccurate
double secondsInCallback = * samplesInThisCallback / (double)self->sampleRate;
uint64_t timeOfLastSampleLeavingSpeaker = ioTimeStamp->mHostTime + (secondsInCallback / ticksToSeconds());
[self lastSampleDoneAt:timeOfLastSampleLeavingSpeaker];
}
}
-(void)lastSampleDoneAt:(uint64_t)lastSampTime{
uint64_t currentTime = mach_absolute_time();
if (lastSampTime > currentTime) {
double secondsFromNow = (lastSampTime - currentTime) * ticksToSeconds();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(secondsFromNow * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//do the thing!!!
});
}
else{
//do the thing!!!
}
}
You set it up like this after AudioQueueNewOutput and before AudioQueueStart. Notice the passing of bridged self to the inClientData argument. The queue actually holds self as void* to be used in callback where we bridge it back to an objective-C object within the callback.
AudioStreamBasicDescription format;
AudioQueueProcessingTapRef tapRef;
UInt32 maxFrames = 0;
AudioQueueProcessingTapNew(queue, processingTapCallback, (__bridge void *)self, kAudioQueueProcessingTap_PostEffects, &maxFrames, &format, &tapRef);
You could get the end machine time as soon as the file starts too. A little cleaner too.
void processingTapCallback(
void * inClientData,
AudioQueueProcessingTapRef inAQTap,
UInt32 inNumberFrames,
AudioTimeStamp * ioTimeStamp,
UInt32 * ioFlags,
UInt32 * outNumberFrames,
AudioBufferList * ioData){
MyObject *self = (__bridge Object *)inClientData;
AudioQueueProcessingTapGetSourceAudio(inAQTap, inNumberFrames, ioTimeStamp, ioFlags, outNumberFrames, ioData);
if (*ioFlags == kAudioQueueProcessingTap_StartOfStream) {
uint64_t timeOfLastSampleLeavingSpeaker = ioTimeStamp->mHostTime + (self->audioDurSeconds / ticksToSeconds());
[self lastSampleDoneAt:timeOfLastSampleLeavingSpeaker];
}
}
If you use AudioQueueStop in asynchronous mode, then stopping happens after all queued buffers have been played or recorded. See doc.
You're using it in a synchronous mode, where stopping happens ASAP, and playback cuts out immediately, without regard for previously buffered audio data. You want precise timing, but only because audio is cutting off. Right? So rather than go synchronous + add additional timing/callback code, I recommend going asynchronous:
err=AudioQueueStop(queue, FALSE);
From docs:
If you pass false, the function returns immediately, but the audio
queue does not stop until its queued buffers are played or recorded
(that is, the stop occurs asynchronously). Audio queue callbacks are
invoked as necessary until the queue actually stops.
For me this worked really well for what I heeded:
stopping the queue in callback when data is over using AudioQueueStop(queue, FALSE), while:
listening to actual stop using kAudioQueueProperty_IsRunning property (happens later than AudioQueueStop() is called, actually, when last buffer gets actually rendered)
after stopping the queue You can get prepared for action You need to execute on audio ending, and when listener fires - actually execute this action.
I am not sure about time precision of that event but for my task it behaved definitely better than using notification straight from callback. There is buffering inside AudioQueue and output device itself so definitely IsRunning listener gives better results as to when AudioQueue stops playing.

terminate excuation of block in objective c

- (void)beat{
__block UInt64 total_frames = 0;
// The next frame that the beat will play on
static UInt64 next_beat_frame = 0;
static UInt64 next_tick_frame = 0;
static BOOL making_beat = NO;
static BOOL tick=NO;
_bpm=self.tempo_slider.value;
// Oscillator specifics - instead you can easily load the samples from cowbell.aif or somesuch
float oscillatorRate = 440./44100.0;
__block float oscillatorPosition = 0; // this is outside the block since beats can span calls to the block
self.blockChannel.volume=0.5;
// The block that is our metronome
self.blockChannel = [AEBlockChannel channelWithBlock:^(const AudioTimeStamp *time, UInt32 frames, AudioBufferList *audio) {
UInt64 frames_between_beats = 44100/(_bpm/60.);
UInt64 end_frame=frames_between_beats*_midi_length;
// For each frame, count and if we reach the frame that should start a beat, start the beat
// frames=_length;
for (int i=0; i<frames; i++) { // frame...by frame...
if(end_frame==total_frames){
if (_recordStatus != 0){
total_frames=0;
next_beat_frame =0;
next_tick_frame=0;
oscillatorPosition = 0;
_recordStatus=0;
}
NSLog(#"forever here");
return;
}
if (next_beat_frame == total_frames) {
making_beat = YES;
oscillatorPosition = 0; // reset the osc position to make them all sound the same
next_tick_frame=next_beat_frame+frames_between_beats;
next_beat_frame += _upper_timesig*frames_between_beats;
}
total_frames++;
}
}];
// Add the block channel to the audio controller
[_audioController addChannels:[NSArray arrayWithObject:_blockChannel]];
}
The logic of above code is running the block and total_frames will increment. When it reaches end_frame, it will terminate the block and reset everything. When it reaches next_beats, it will beats.
Currently I am trying to use return to terminate the block. In fact, it works. But when I re-execute the block, it wont reach the return statement, instead it will execute
total_frames=0;
next_beat_frame =0;
next_tick_frame=0;
oscillatorPosition = 0;
_recordStatus=0; .
then continue run the block, when it reaches above code again, nothing
It , and execute the lane before return statement NSLog(#"forever here"); forever. Why return does not work second time?If I go to the previous view and go back to this view, the block will run ok.
Second question, if I am changing the view, and I wanna terminate the block. What should I do in the block or generally in objective c, how can I detect the view changes.
Third question, what is exactly difference between static and __block variable in the block?
I fixed this problem by resetting variables instead of terminating the block. I will let block run in the background. Whenever I want to use it, I will reset
total_frames=0;
next_beat_frame =0;
next_tick_frame=0;
oscillatorPosition
So, I change variables to __block.

play multiple SpeakHere audio files

By recording multiple snippets using filenames, I have attempted to record multiple separate short voice snippets in SpeakHere, I want to play them serially, separated by a set fixed interval of time between the starts of each snippet. I want the series of snippets to play in a loop forever, or until the user stops play.
My question is how do I alter SpeakHere to do so?
(I say "attempted" because I have not been able yet to run SpeakHere on my Mac Mini iPhone simulator. That is the subject of another question and because another question on the subject of multiple files has not been answered, either.)
In SpeakHereController.mm is the following method definition for playing a recorded file. Notice the final else clause calls player->StartQueue(false)
- (IBAction)play:(id)sender
{
if (player->IsRunning())
{ [snip]
}
else
{
OSStatus result = player->StartQueue(false);
if (result == noErr)
[[NSNotificationCenter defaultCenter] postNotificationName:#"playbackQueueResumed" object:self];
}
}
Below is an excerpt from SpeakHere AQPlayer.mm
OSStatus AQPlayer::StartQueue(BOOL inResume)
{
// if we have a file but no queue, create one now
if ((mQueue == NULL) && (mFilePath != NULL)) CreateQueueForFile(mFilePath);
mIsDone = false;
// if we are not resuming, we also should restart the file read index
if (!inResume) {
mCurrentPacket = 0;
// prime the queue with some data before starting
for (int i = 0; i < kNumberBuffers; ++i) {
AQBufferCallback (this, mQueue, mBuffers[i]);
}
}
return AudioQueueStart(mQueue, NULL);
}
So, can the method play and AQPlayer::StartQueue be used to play the multiple files, how can the intervals be enforced, and how can the loop be repeated?
My adaptation of the code for the method 'record` is as follows, so you can see how the multiple files are being created.
- (IBAction)record:(id)sender
{
if (recorder->IsRunning()) // If we are currently recording, stop and save the file.
{
[self stopRecord];
}
else // If we're not recording, start.
{
self.counter = self.counter + 1 ; //Added *****
btn_play.enabled = NO;
// Set the button's state to "stop"
btn_record.title = #"Stop";
// Start the recorder
NSString *filename = [[NSString alloc] initWithFormat:#"recordedFile%d.caf",self.counter];
// recorder->StartRecord(CFSTR("recordedFile.caf"));
recorder->StartRecord((CFStringRef)filename);
[self setFileDescriptionForFormat:recorder->DataFormat() withName:#"Recorded File"];
// Hook the level meter up to the Audio Queue for the recorder
[lvlMeter_in setAq: recorder->Queue()];
}
}
Having spoken with a local "meetup" group on iOS I have learned that the easy solution to my question is to avoid AudioQueues and to instead use the "higher level" AVAudioRecorder and AVAudioPlayer from AVFoundation.
I also found how to partially test my app on the simulator with my Mac Mini: by plugging in an Olympus audio recorder with USB to my Mini as an input "voice". This works as an alternative to the iSight which does not produce an input audio on the Mini.

ios audio: using audioqueue to change the playing progress

ios problem: using audioqueue to change the playing value
Recentlly, I make a music player with audioqueue (not using avplayer ), the player plays local audio files but not the internet files.Now I want to achieve a function that user could change the playing progress with slider.How can I do?
In short, how can I use audioqueue to achieve to slide playing progress when it play local file.
Wish your answer with some important code!
---------------------------Append-----------------------
I modify the codes in demo of audio streamer(URL:http://code4app.net/ios/Audio-Streamer/5019d9066803faa344000000), My thinking is as follows in rough:
1、open the stream with a local music file.
2、Create a AudioFileStreamID Obj (audioFileStream).
3、Create queue in the callback ASPacketsProc.
4、After createqueue, then filled the buffers of the queue in the callback ASPacketsProc.
5、At last, I will call the function to start to play.
Some codes is as followings:
- (void) openReadStream {
CFURLRef urlPath = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)filePath, kCFURLPOSIXPathStyle, false);
stream = CFReadStreamCreateWithFile(kCFAllocatorSystemDefault, urlPath);
if (CFReadStreamOpen(stream))
{
CFStreamClientContext context = {0, self, NULL, NULL, NULL};
CFReadStreamSetClient(
stream,
kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
ASReadStreamCallBack,
&context);
CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
}
static void ASReadStreamCallBack (CFReadStreamRef aStream, CFStreamEventType eventType,void* inClientInfo)
{
AudioStreamer* streamer = (AudioStreamer *)inClientInfo;
[streamer handleReadFromStream:aStream eventType:eventType];
}
AudioFileStreamOpen(self,ASPropertyListenerProc,ASPacketsProc,fileTypeHint,&audioFileStream);
- (void)createQueue {
sampleRate = asbd.mSampleRate;
packetDuration = asbd.mFramesPerPacket / sampleRate;
// create the audio queue
AudioQueueNewOutput(&asbd, ASAudioQueueOutputCallback, self, NULL, NULL, 0, &audioQueue);
// listen to the "isRunning" property
AudioQueueAddPropertyListener(audioQueue, kAudioQueueProperty_IsRunning, ASAudioQueueIsRunningCallback, self);
// get the packet size if it is available
UInt32 sizeOfUInt32 = sizeof(UInt32);
AudioFileStreamGetProperty(audioFileStream, kAudioFileStreamProperty_PacketSizeUpperBound, &sizeOfUInt32, &packetBufferSize);
for (unsigned int i = 0; i < kNumAQBufs; ++i)
{
AudioQueueAllocateBuffer(audioQueue, packetBufferSize, &audioQueueBuffer[i]);
}
}
AudioQueueEnqueueBuffer(audioQueue, fillBuf, packetsFilled, packetDescs);
AudioQueueStart(audioQueue, NULL);
I suppose, you used MPMusicPlayerController class. If you need some code, you must give some data or code too. I don't know, what class you are using...
Though, if you used this, then you can set currentPlaybackTime property, when slider changes.
Let's say, you have this instance:
#property (nonatomic, strong) MPMusicPlayerController *musicPlayer;
Then, probably, you can change play time like this:
- (IBAction)sliderChanged:(id)sender
{
self.musicPlayer.currentPlaybackTime = self.slider.value;
}

iOs Play Audio for VOIP App

This is my first post to stackoverflow. At present I am developing an voip app for ios. I want to do something like this.
//in a thread
while(callIsOnGoing){
data = getDataFromNetwork()
playData()
sleep(10ms)
}
But problem is that audio in ios works in a "Pull" model(uses callback to get data). But i need to push data to play it. I have tried AudioQueue, but in audioQueue the data i push in buffer outside of callback doesn't get played though callback is called.
Again, i have seen AVCaptureToAudioUnit example by apple(http://developer.apple.com/library/ios/#samplecode/AVCaptureToAudioUnit/Introduction/Intro.html) where they called AudioUnitRender synchronously in case of of a delay audio unit. I tried similar for RemoteI/O Audio unit. But every time it returns OSStatus -50.
The code is given below
//in a separate thread
do { // 5
int data_length = [NativeLibraryHelper GetData:(playBuff)];
if(data_length == 0){
}else{
double numberOfFrameCount = data_length / player->audioStreamDesc->mBytesPerFrame;
currentSampleTime += numberOfFrameCount;
//AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp timeStamp;
memset(&timeStamp, 0, sizeof(AudioTimeStamp));
timeStamp.mSampleTime = currentSampleTime;
timeStamp.mFlags |= kAudioTimeStampSampleTimeValid;
AudioUnitRenderActionFlags flags = 0;
AudioBuffer buffer;
buffer.mNumberChannels = player->audioStreamDesc->mChannelsPerFrame;
buffer.mDataByteSize = data_length;
buffer.mData = malloc(data_length);
memcpy(buffer.mData, playBuff, data_length);
AudioBufferList audBuffList;
audBuffList.mBuffers[0] = buffer;
audBuffList.mNumberBuffers = 1;
printf("Audio REnder call back funciotn called with data size %d\n", data_length);
status = AudioUnitRender(audioUnitInstance, &flags, &timeStamp, 0, numberOfFrameCount, &audBuffList);
printf("osstatus %d\n", status);
}//end if else
CFRunLoopRunInMode ( // 6
kCFRunLoopDefaultMode, // 7
0.25, // 8
false // 9
);
//} while (aqData.mIsRunning);
[NSThread sleepForTimeInterval:.05];
}while (player->isRunning == YES);
I am struggling with audio play part for more than one month. Please help. Thanks in advance.
One general solution is to have an async network getdata/read function push data to an intermediate buffer or queue, then have the audio callback read from that intermediate buffer (or read silence if the intermediate buffer/queue is empty).

Resources