We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Performance on every event (make a ticking sound)
Page Index Toggle Pages: 1
Performance on every event (make a ticking sound) (Read 536 times)
Performance on every event (make a ticking sound)
Nov 14th, 2009, 3:59am
 
Hi all

Am trying to make a realistic 'ticking' sound accompanying a clockface.

The aim is to make a 'tick' sound per machine second.

Have been playing with snippet + for expressions but not realistically getting anywhere.

Code:
  int o = second();
int i = 0;
for(o = i; o>1; o=i+o)
{
snip.play();
snip.close();
minim.stop();
super.stop();
}


Any idea where I'm going wrong? Is this the right thing to be doing?

thanks
Calum
Re: Performance on every event (make a ticking sound)
Reply #1 - Nov 14th, 2009, 5:14am
 
first thing to do is move the stop code to its own function:

void stop(){
 snip.close();
 minim.stop();
 super.stop();
}

o is being set to the current number of seconds since the sketch started (o = second())...but then it's immediately being set to 0 by the first part of your for() loop.  The rest of your for() loop says: add 0 to 0 until the result is greater than 1.  That will take some time, you may want to grab a cup of coffee.  ;)

you need to track the change in current time since the last tick.  So if you have a variable "lastTick", you can set it to second() -- and when the number returned by second() increments, it will be greater than lastTick.  But you have to check every frame, so this will need to be in draw().

int lastTick; // declare outside of all functions, so this is a global var
lastTick = second(); // <-- last thing in setup() so it starts off on the right second...and in draw:
if (second() > lastTick){
 [play a sound and set lastTick to = the current second()]
}

for maximum accuracy I would also recommend adding "frameRate(9999);" to setup() to break the built-in limit of 60 fps.
Re: Performance on every event (make a ticking sound)
Reply #2 - Nov 14th, 2009, 5:49am
 
thank you very much. your advice worked.

thanks
Page Index Toggle Pages: 1