Professional Web Applications Themes

Cocoa question -- way to keep track of milliseconds? - Mac Programming

I'm a newbie Cocoa programmer and wanted to create a simple beats-per-minute calculator where the user could click the mouse on downbeats and the running beats-per-minute would be displayed. Looking at the doentation, there is a format string (%F) that can be used with setCalendarFormat: to print the current number of milliseconds for an NSCalendarDate, but I don't see any methods that return the time with milliseconds. If you need precision in milliseconds (which you do for a BPM calculator), is there a good way of handling it? I'm supposing I could print the %F strings of two times, convert ...

  1. #1

    Default Cocoa question -- way to keep track of milliseconds?

    I'm a newbie Cocoa programmer and wanted to create a simple
    beats-per-minute calculator where the user could click the mouse on
    downbeats and the running beats-per-minute would be displayed. Looking
    at the doentation, there is a format string (%F) that can be used
    with setCalendarFormat: to print the current number of milliseconds for
    an NSCalendarDate, but I don't see any methods that return the time with
    milliseconds. If you need precision in milliseconds (which you do for a
    BPM calculator), is there a good way of handling it? I'm supposing I
    could print the %F strings of two times, convert them both to int's and
    compare, but seems like there should be a better way.

    Similarly, if I wanted to create a metronome where the user would enter
    BPM, I would also need millisecond accurracy but NSTimer seems to only
    work at the granularity of seconds as well. Of course, I could just use
    a usleep() but I was wondering if there's any way to get better timer
    granularity than seconds.

    Sorry if I'm missing something obvious here -- most of my programming
    experience is at the device driver level in the kernel where you have
    access to system clock ticks and so on.

    Thanks in advance for any help...
    Jamal Bernhard Guest

  2. #2

    Default Re: Cocoa question -- way to keep track of milliseconds?

    Jamal Bernhard wrote:
    > I'm a newbie Cocoa programmer and wanted to create a simple
    > beats-per-minute calculator where the user could click the mouse on
    > downbeats and the running beats-per-minute would be displayed. Looking
    > at the doentation, there is a format string (%F) that can be used
    > with setCalendarFormat: to print the current number of milliseconds for
    > an NSCalendarDate, but I don't see any methods that return the time with
    > milliseconds. If you need precision in milliseconds (which you do for a
    > BPM calculator), is there a good way of handling it? I'm supposing I
    > could print the %F strings of two times, convert them both to int's and
    > compare, but seems like there should be a better way.
    You can use the C function gettimeofday() to get a result in
    microseconds, and then do with that what you will. Also, see the
    comment at the bottom.
    >
    > Similarly, if I wanted to create a metronome where the user would enter
    > BPM, I would also need millisecond accurracy but NSTimer seems to only
    > work at the granularity of seconds as well. Of course, I could just use
    > a usleep() but I was wondering if there's any way to get better timer
    > granularity than seconds.
    Unfortuantely, neither usleep() nor NSTimer can really give you hard
    guarantees. If you absolutely need millisecond accuracy, why not
    generate a sound with the beats at the right intervals ahead of time,
    and then send that to the speakers as one big sound?

    >
    > Sorry if I'm missing something obvious here -- most of my programming
    > experience is at the device driver level in the kernel where you have
    > access to system clock ticks and so on.
    You have access to the chip's timer, if you use a bit of assembly (mftb
    and mftbu). If you want a Cocoa wrapper around it, try the OmniTimer
    framework at [url]http://www.omnigroup.com/developer/gamedevelopment/[/url]

    -Peter

    Peter Ammon Guest

  3. #3

    Default Re: Cocoa question -- way to keep track of milliseconds?

    >> Similarly, if I wanted to create a metronome where the user would enter
    >> BPM, I would also need millisecond accurracy but NSTimer seems to only
    >> work at the granularity of seconds as well. Of course, I could just use
    >> a usleep() but I was wondering if there's any way to get better timer
    >> granularity than seconds.
    >
    > Unfortuantely, neither usleep() nor NSTimer can really give you hard
    > guarantees. If you absolutely need millisecond accuracy, why not
    > generate a sound with the beats at the right intervals ahead of time,
    > and then send that to the speakers as one big sound?
    Using CoreAudio, it is very easy to do that in real-time, there's no
    need to generate the whole sound in advance. And you do have millisecond
    accuracy there.

    M.

    Martin Guest

  4. #4

    Default Re: Cocoa question -- way to keep track of milliseconds?

    Peter Ammon wrote:
    >Unfortuantely, neither usleep() nor NSTimer can really give you hard
    >guarantees.
    Maybe the standard getitimer/setitimer functions would help here?


    Mike Guest

Similar Threads

  1. #39416 [NEW]: Milliseconds in date()
    By pascal at tweakers dot net in forum PHP Bugs
    Replies: 2
    Last Post: April 13th, 02:10 PM
  2. The milliseconds PC <> the milliseconds mac
    By j'patauge in forum Macromedia Director Lingo
    Replies: 2
    Last Post: October 21st, 08:08 AM
  3. convert milliseconds in minuts'seconds'milliseconds
    By Andrew Morton in forum Macromedia Director Lingo
    Replies: 2
    Last Post: September 17th, 12:03 AM
  4. Time compare using milliseconds
    By Paulers in forum PERL Miscellaneous
    Replies: 3
    Last Post: August 4th, 08:31 AM
  5. Absurdly Easy Cocoa Question
    By John C. Randolph in forum Mac Programming
    Replies: 0
    Last Post: July 8th, 10:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139