Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Inconsistent metronome in WP7 app?

Back to Forum: Tech Off
  • User profile image
    dentaku

    So, my cousin and I spent Christmas eve writing our first real WP7 app. We started with the Silverlight Sound Sample from here http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx">http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx and made a simple pad controller type of drum machine. Each pad triggers a drum machine sample and that seems to work OK. I did the Expression Blend/interface work and he did the C# stuff.

    QUESTION?
    It also has a slider that lets you set the speed of a metronome from 60 to 160 BPM.
    There's where the problem is. The metronome isn't terribly accurate but more importantly it stops for no reason once in a while if you press any of the drum pads. The timer keeps going but the sound just doesn't play.

    Do any of you have any experience in making a stable metronome tick for WP7?

    Here's the project if you're interested...
    http://www.box.net/shared/4vp6gynp74

  • User profile image
    W3bbo

    I've taken a look at your code; I can see you're using System.Threading.Timer for your metronome. I think that's your problem: the Timer class cannot guarantee precise timing for events (it uses a reentrant thread pool as opposed to a private thread that spins rather than sleeps for greater accuracy).

    I think you're better off building your own system for precise and accurate timing. It's a hack, but you could create a dedicated thread that yields for 75% of the time (as you can never sleep for an exact amount of time) and then spins until the required time is reached.

    e.g.

     

    void MetronomeLoop(int period) {
        long lastAt = 0;
        while(true) {
            Thread.Sleep( 3 * period / 4 ); // inaccurate sleep
            while( DateTime.Now.Ticks < lastAt + period ); // accurate spin
            lastAt = DateTime.Now.Ticks;
            PlaySound(); // be sure to initiate the sound playback on the same thread
        }
    }

  • User profile image
    dentaku

    @W3bbo: Thanks for the info. I'll forward it to the guy who actually knows how to code.

    I re-uploaded the code my link points to last night with some updated code so I'm not sure which version you looked at. Yesterday we messed with it and the metronome doesn't stop anymore but one weird side effect is that now the metronome sound will eventually play twice every tick. It doesn't start that way but after hitting a few drum pads it will start messing up. The timer is firing correctly but the sound is weird.

    The slider isn't connected to anything either in this new code but that's not a big deal. The metronome isn't even needed in this app, we just wanted to figure out how to make one Smiley

  • User profile image
    figuerres

    One way may be as was posted to get better timing control.

    another idea.... what about a sound file / sample that has just the beats for long enough to make one loop @ a given timing and then tell the sound / media system to loop that forever ?

    i wonder if that might work better as it's asking the media system to handle the timing and the playback ?

    the files should be small as all you need is a very basic sound that is repeated just a few times.

     

  • User profile image
    dentaku

    In an audio program I would just make a metronome blip sound with a very long tail then adjust the end point of the loop to make the BeatPerMinute faster or slower. Is that what you're suggesting? I'm assuming it's possible to do something like that with software code.I'm note a coding kinda guy so I'm just guessing Smiley

    If the BPM didn't have to be adjusted it would be very simmple to just make a file that's the correct length and loop it. A 500 milisecond file gives you 120BPM for instance.

     

    , figuerres wrote

    another idea.... what about a sound file / sample that has just the beats for long enough to make one loop @ a given timing and then tell the sound / media system to loop that forever ?

    i wonder if that might work better as it's asking the media system to handle the timing and the playback ?

    the files should be small as all you need is a very basic sound that is repeated just a few times.

  • User profile image
    figuerres

    , dentaku wrote

    In an audio program I would just make a metronome blip sound with a very long tail then adjust the end point of the loop to make the BeatPerMinute faster or slower. Is that what you're suggesting? I'm assuming it's possible to do something like that with software code.I'm note a coding kinda guy so I'm just guessing Smiley

    If the BPM didn't have to be adjusted it would be very simmple to just make a file that's the correct length and loop it. A 500 milisecond file gives you 120BPM for instance.

     

    *snip*

     

    something like that....

    say a simple wave file has some sound and then a tail of "Blank time" append more of that to the end and then use it.

    that's at the concept level... the question is how hard it is to modify the data.

    it may be better to make a set of std. files for common timings that folks use.

    while you can have all kinds of time i think there are some common ones that say 90% of all music uses that may be all you really need.

    and i would make the file have say 10 or 20 or 30 seconds of "time" so that it has good solid timing in that span and then see if looping that "sounds right" and is free of problems.

    then go from there.... possibly have an "editor" option to make a "custom metronone" sample for the folks who need / want that odd timing that not many folks use.

  • User profile image
    dentaku

    Well, we have a decent app now. He's a Java programmer who's never used Visual Studio and I know much more about graphics and music software. I guess the next step is for me to make it look like my AKAI LPD8 with Expression Blend. I'll probably end up making new button templates in Illustrator.

    If anyone's curious, here's what we have so far.
    http://www.box.net/shared/4vp6gynp74

    There just isn't a simple way of making an extremely acurate timer but at least this one works now.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.