Precise timing

All your problems with Audio, Video and Images.

Precise timing

Postby johnmurdoch » Tue Aug 17, 2010 9:49 pm

Hi all,

I'm currently working on a small drumcomputer (sequencer) and have imprecise ticks.

I used thread.sleep(ms) between the ticks, knowing that this is not the precise way (according to api-docs).

Does anyone have experience having a better way of solving this (for instance via timer-class)
or is this impossible (due to having a GC etc).

Regards,
JohnM
johnmurdoch
Freshman
Freshman
 
Posts: 3
Joined: Sun Jun 14, 2009 1:56 pm

Top

Re: Precise timing

Postby blundell » Wed Aug 18, 2010 9:33 am

Code: Select all
import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;

public class Counter extends CountDownTimer {

        private TextView mTextField;
        Context theContext;

        public Counter(Activity ac, long millisFuture, TextView textView) {
                super(millisFuture, 1000);
                theContext = ac.getApplicationContext();
                mTextField = textView;
        }

        @Override
        public void onFinish() {
                mTextField.setText("FINISHED!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
                mTextField.setText(millisUntilFinished);
        }
}


I'm sure you could play with this to create a tick
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Precise timing

Postby johnmurdoch » Wed Aug 18, 2010 9:04 pm

Hi blundell,
thanks for the proposal, but the results were worse than before :(

API says about Timer:
The TimerTask does not offer any guarantees about the real-time nature of scheduling tasks as its underlying implementation relies on the Object.wait(long) method.

What I am looking for is a equal very precise tick (which is a must for a sequencer/drumcomputer)


Thanks anyways.
:-(
johnmurdoch
Freshman
Freshman
 
Posts: 3
Joined: Sun Jun 14, 2009 1:56 pm

Re: Precise timing

Postby blundell » Wed Aug 18, 2010 10:27 pm

Ah dahm sorry

Just shooting in the dark here, but could you use:
http://developer.android.com/reference/ ... #nanoTime()

And do
Code: Select all
// Class vars
static final long TICK = 1000;
Long time = System.nanoTime();
Long origTime = System.nanoTime();

// In a method
if(time >= origTime+TICK){
  // beep or w/e
   origTime = time;
} else {
   time = System.nanoTime();
}


Is that right, or am I running on empty? Do you have issues running this in a Thread .. hmm someone shoot me out my bubble
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 16 guests