Hi All, I am looking for this for weeks now: My app needs a metronome like function and I did it with a thread (copied and adapted from Apples Metronom example) but the timing is miserable. I know that the timer ressources are not accurate but I cannot find another feaseable technique. And I need just short "beep" sounds so tackling the full Audio playback stuff is not required. I divide the BPM (beats per minute) by 12 to have the possibillity to play also the eigth, sixteenth and so on notes. So the problem is not the audio play, the problem is the accurate time distance between two "ticks".
Here the Code for calculation the tick distance:
- (double)Be_get12TickDistance:(NSInteger)bpm{
double result = 60.0 / (bpm*12);
return result;
} Here the thread:
- (void)startDriverTimer:(id)info {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Give sound thread high priority to keep the timing steady
[NSThread setThreadPriority:1.0];
BOOL continuePlaying = YES;
while (continuePlaying) { // loop until cancelled
// Use an autorelease pool to prevent the build-up of temporary date objects
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
SoundEffect *currentSoundEffect= [[self beepAndLEDKeeper] Be_GetBeepSound4CurrentTick:beatNumber theCurrentGroove:grooveNumber];
[currentSoundEffect play];
if ([[self beepAndLEDKeeper] Be_isLEDFlashPoint:beatNumber]){
[self performSelectorOnMainThread:@ selector(setLEDtoHidden:) withObject:[NSNumber numberWithInt:LEDoldBeatNumber+oldGrooveNumber*9] waitUntilDone:NO];
[self performSelectorOnMainThread:@ selector(setLEDtoUnHidden:) withObject:[NSNumber numberWithInt:LEDbeatNumber+grooveNumber*9] waitUntilDone:NO];
LEDoldBeatNumber = LEDbeatNumber++;
}
oldBeatNumber = beatNumber++;
oldGrooveNumber = grooveNumber;
if (beatNumber >= grooveEnd[grooveNumber])
{
beatNumber=0;
LEDbeatNumber=0;
if (++grooveNumber > 1)
grooveNumber=0;
if (grooveEnd[grooveNumber] == 0)
grooveNumber=0;
}
if ([soundPlayerThread isCancelled] == YES)
continuePlaying = NO;
else
[NSThread sleepForTimeInterval:self.duration];
[loopPool release];
}
[pool release];
}
Question: Does anybody has a hint how to do that in an easy way? I just need a better timing possibillity, the playing with SoundEffect is good enough.
Thanks for all help, Andreas