Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2011, 08:30 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
PeteX is on a distinguished road
Default Is NSTimer precise enough for BPM sounds ?

Hi,

I'm building a simple bpm / metronome app.
Most of it is working fine, but I'm having a hard time getting my click sounds to play in time.

I am using an NSTimer to trigger my sounds at the BPM interval.
I even put the NSTimer on a separate GCD queue, but still the sounds are sometimes (not always) out of sync.

I think I have it narrowed down to the interval value of my NSTimer.
Could it be that NSTimer is having a hard time rounding my interval value ?
Specifically, when I set my interval at 0.441 (which is 136 BPM), then playback is not consistent.

Code:


dispatch_async(myQueue, ^(void) {
		myTimer = [[NSTimer alloc] initWithFireDate:[NSDate date]
                                                  interval:frequency
                                                    target:self
                                                  selector:@selector(timerTask:)
                                                  userInfo:nil
                                                   repeats:YES];
		NSRunLoop *runloop = [NSRunLoop currentRunLoop];
		[runloop addTimer:myTimer forMode: NSDefaultRunLoopMode];
		[myTimer release];
        [runloop run];

Code:
- (void)timerTask:(NSTimer *)aTimer
{
    
    
    if(!soundIsMuted) {
        
        //play clickSound
 AudioServicesPlaySystemSound (self.oneFileObject);
        
        

    }
    
    // set button to highlight for blinking in tempo
    [tapButton setImage:[UIImage imageNamed:@"button-highlight.png"] forState:UIControlStateNormal];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeButtonBack:) userInfo:nil repeats:NO];
    
    

}
The above is what I'm using now, the interval frequency is set by buttontaps (but at the moment I'm hardcoding the value to test).
The NSTimer is calling my audio and button flashing code.

I have already gone from AVFoundation to AudioToolbox as I thought I didn't need the overhead to play very small sound fragments.

To be clear, everything works, but the sounds are sometimes not in tempo.

It is also not the case that the NSTimer might not keep up, if I set it's interval to 0.3 (= 200 BPM) the clicksounds are perfectly in tempo, and this is faster than 0.441, so speed seems not to be an issue.
This is why I believe it might be a rounding factor ?

I also need this level of precision (3 digits after comma) because :
134 BPM = 0.447 s
135 BPM = 0.444 s
136 BPM = 0.441 s

Is there a better way to tackle this ?

Last edited by PeteX; 04-08-2011 at 09:08 AM.
PeteX is offline   Reply With Quote
Old 04-08-2011, 09:19 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

0.441 cannot be represented as a binary number. Hence it is rounded/truncated and you lose precision. However 0.3 can't either and you say its timing is acceptable.
Maybe you could try with smaller fractions that are perfectly representable, lets say 0.125 , 0.0625, and work up from there until you reach the desired tempo.
nobre84 is offline   Reply With Quote
Old 04-08-2011, 09:36 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
PeteX is on a distinguished road
Default

Thanks for your reply.

I figured something like this would be the case.
I'll see where I get trying to round my intervals to acceptable values.

As you noticed, 0.3 does perform perfect.

What is strange about all this, is that I use the NSTimer to trigger a loop at a given interval.

Inside this loop, I play a sound, and I flash my button image in tempo.

At certain intervals (like 0.441 or anything in that area) the sounds are skewed out of tempo, but the button flash looks to be perfectly in tempo.

This is leading me to believe that some delays are occurring in PlaySystemSound... but why not at all tempo's ?

This is really giving me a headache.

Is there maybe a better way to trigger (loop) soundfiles at an exact tempo ?
PeteX is offline   Reply With Quote
Old 04-08-2011, 09:53 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
PeteX is on a distinguished road
Default

I'll have to be more precise.

The problem that I am hearing is that my sounds are not equally apart at certain interval times, where as the flashing button is.

so at interval 0.441 I hear two short sounds followed by a delay, instead of a steady rhythm.

The steady rhythm is working fine though in the button flashes.
PeteX is offline   Reply With Quote
Old 04-08-2011, 09:56 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

Hmmm... So it seems the timer is not really the culprit ? Have you tried other means of playing the sound to see if it helps ?
nobre84 is offline   Reply With Quote
Old 04-08-2011, 10:05 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
PeteX is on a distinguished road
Default

Initially I was using AVFoundation, but read in the documentation that playback was not instant.

I thought for my application I would be better with Sound System services so changed it.

In both instances I experienced the strange audio timing.


So I ended up putting the NSTimer on a separate queue with GCD, but it still behaved the same way.

I've now put my code full of breakpoints to see if something funky comes up...but I'm really scratching my head.
It certainly is something stupid I did, I just can't see it.
PeteX is offline   Reply With Quote
Old 04-09-2011, 06:18 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 7
PeteX is on a distinguished road
Default

just to follow up.

I have now gone back to AVAudioPlayer which is performing better than AudioToolbox, and have also switched my code from GCD to NSThread, just like the Apple metronome sample code, and this is working very well.

Last edited by PeteX; 04-09-2011 at 06:40 AM.
PeteX is offline   Reply With Quote
Reply

Bookmarks

Tags
bpm, gcd, nstimer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 358
13 members and 345 guests
dansparrow, dre, ilmman, LezB44, michelle, Nobbsy, Objective Zero, samdanielblr, Sami Gh, shagor012, sledzeppelin, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:42 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0