Beats per minute

Cor

Active Member
Licensed User
BPM calculator (beats perminute)

Calculate the the BPM when tapping on e.g. a label of the form

Is it possible to convert following javascript code to basci4ppc

Hints are welcome

grCor


function TapForBPM(e)
{
document.BEATSPERMINUTE.WAIT.blur();
timeSeconds = new Date;
secs = timeSeconds.getTime();
if ((secs - secsPrevious) > 1000 * document.BEATSPERMINUTE.WAIT.value)
{
count = 0;
}

if (count == 0)
{
document.BEATSPERMINUTE.AVG.value = "";
document.BEATSPERMINUTE.TAP.value = "First Beat";
secsFirst = secs;
count = 1;
}
else
{
bpmAvg = 60000 * count / (secs - secsFirst);
document.BEATSPERMINUTE.AVG.value = (Math.round(bpmAvg * 100)) / 100;
count++;
document.BEATSPERMINUTE.TAP.value = count;
}
secsPrevious = secs;
return true;
}
 

Rioven

Active Member
Licensed User
Tap on button per minute

Hi,
Hope this code helps a bit...

B4X:
Sub Globals
secSecond=0
secFirst=0
End Sub
Sub App_Start
   Form1.Show
End Sub

Sub button1_click
   secSecond=Now
      If secFirst=0 Then 
         secFirst = Now
         label1.text="start"
         label2.text="start"
      End If
   r=secSecond-secFirst
      If r>0 Then 
         label1.text=Round(r/ctickspersecond,2)
         bpmAvg = Round(60/(r/ctickspersecond),0)
         label2.text=bpmAvg
      End If
   secFirst=secSecond
End Sub

also
attached sbp file.

Regards,
Rioven
 

Attachments

Cor

Active Member
Licensed User
works great,

thanks

will improve my learning curve in basic4ppc ;)

grCor
 

Cor

Active Member
Licensed User
When running on rx 3715 pda

bpm is result is always 30 or 60

seems that it cannot handle this on a pda

strange

who can help

grCor
 

Cableguy

Expert
Licensed User
It may be because of the use of a button control...
remove the button and use the code in the form _click event...
Buttons seem to have some kind of latency time...perhaps to avoid double clikcs when only one was meant...
 

Rioven

Active Member
Licensed User
Now value on PPC

Hi Cor/Cableguy,
I have tried the code on my device and same result what cor is getting...
I've found out that the 'Now' ticks returns on device like 633375480580000000 means no milliseconds unlike on desktop. Why??

Regards,
Rioven
 

RandomCoder

Well-Known Member
Licensed User
I guess that the only solution is to use your own timer.
Then if it is improtant that it stays in sync with the system time you may need to compare your time with the system time and adjust as necessary.

Regards,
RandomCoder
 

Rioven

Active Member
Licensed User
Using timer

Hi Cor,
here is the code...

B4X:
Sub Globals
n=0
End Sub

Sub App_Start
 Form1.show
timer1.Enabled=true
timer1.Interval=1
End Sub

Sub Timer1_Tick
n=n+1
label1.Text=n/200
End Sub

Sub Button1_Click
label2.Text=Round(n/200,2)
label3.Text=Round(60/(n/200),0)
n=0
End Sub
EDIT: CODE is for device, use n/100 on desktop, also finetune as Randomcoder's advice.

also attached sbp
 

Attachments

Last edited:

Erel

Administrator
Staff member
Licensed User
You should set a larger interval.
1 ms may not be enough.
 

alfcen

Well-Known Member
Licensed User
Hello Cor
If precision is crucial please consider using dzt's dzHW.dll.
In there you will find a GetTickCount method.
Cheers
Robert
 

Rioven

Active Member
Licensed User
dzHW.dll is the answer then...

Hi Alfcen,
Thanks for pointing dzt's dzWH.dll. Problem solved!

Hi Cor, we could use the first code and change 'Now' with 'GetTickCount' and divide by 1000 for seconds lapses per beat.

B4X:
Sub Globals
secSecond=0
secFirst=0
End Sub
Sub App_Start
   Form1.Show
   dzH.New1 'dzHW.dll object
End Sub

Sub button1_click
   secSecond=dzh.GetTickCount
      If secFirst=0 Then 
         secFirst = dzh.GetTickCount
         label1.text="start"
         label2.text="start"
      End If
   rt=secSecond-secFirst
      If rt>0 Then 
         label1.text=Round(rt/1000,2)
         bpmAvg = Round(60/(rt/1000),0)
         label2.text=bpmAvg
      End If
   secFirst=secSecond
End Sub
Thanks guys for the refreshers!:)

Regards,
 

Cor

Active Member
Licensed User
Thanks for all the help
will try this evening on pda

Can Gettickcount be implemented directly as default in basic4ppc
because it will be used a lot i think.

grCor
 
Top