Possible accuracy issues in clock timer

As I suggested earlier, try this:

Hi Anke

Thanks a lot for your help. Looks like you modified my code to make it work. Can you please attach the aia file so I could download and try it out.
Also what is a TaifunPlayer? Where do I get it and its documentation?
Also how do I convert my .wav file to a .ogg file?

Thank you.

Search the forum ("TaifunPlayer ogg"), then you will find e.g. this:

WAV is also loop-able, but why not using a compressed audio format (OGG).

As you can see and hear it works and I showed you all blocks. So where is the problem to try it yourself?

Btw, these are the 4 audio files (1.ogg, ..., 4.ogg):

E.g. with Audacity.

Thanks for your help.
If I understand you right, for each metronome bpm setting, I need to source the corresponding ogg file and use Taifun player to loop which will operate independently of the timer interrupt? And the sounds in each ogg file should be spaced just right so that timing is correct?

This assumes that I have a prepopulated list of metronome settings and corresponding ogg files.
If I want any arbitrary integer value between 60 and 120, I have to generate and store 61 different ogg files?
I am not as familiar with timer interrupts and the player so I am learning by building a metronome which I can use for my music practice. Sure, I can download one among many out there but it is more satisfying to use one that I built (with help from the community, of course :slight_smile:

That's why I asked

See also here (point 2):

What sense does it make, e.g. to play a 63 or 111 bpm sound?
So again: what is the goal?

As I said before, the point is to create a metronome. You can google what a metronome is. If you want to keep a certain rhythm, a metronome can help achieve it and you can pretty much input any integer value (upto a reason) for beats per minute. There are also many free metronome apps on Google app store that take an integer value and work pretty well. The actual BPM depends on the music written indicated by the tempo.

I was testing it with a few values of BPM to see how the timer and the player works. While looping with a discrete set of ogg files is a good idea if you have a few values (and is a useful tool to have in my toolbox), for arbitrary integer inputs within a range this method would not be practical.

Based on all the discussion so far, looks like AppInventor has certain limitations which makes the creation of a general purpose metronome metronome not possible.

Thanks for all the suggestions and code snippets.

For this metronome app I used a

  • timer extension,
  • the Sound component and a
  • 25ms long metronome tone.

The bpm can be set between 60, 61, 62, ..., 119, 120 as well as 180 and 240 using a ListPicker.

Blocks

Contrary to appearances, the built-in clock can be accurate. You just have to approach it differently. Try this project witchout extensions and check its accuracy.
metronom.aia (6.5 KB)

Yes, but this approach is not really new (2016):
https://groups.google.com/g/app-inventor-developers-library/c/6eknWf2cJN0

and it works as long as the timer interval is high enough. But for many problems this is a possible solution to bypass the inaccuracy (which, however, is inherent in the system).

I would not call it a workaround. This type of programming is used in e.g. C language to create artificial multithreading. The clock error will be imperceptible here and certainly will not worsen with each tick.

1 Like

Hi Paryk_F:
Your solution is simple. I modified your code to also count the number of beats and display it when stop is paused. I also display total elapsed time from start to finish. I also increased the max number of beats to 240 from 160. For bpm of 60, the # of beats was exactly equal to the time elapsed over a 6 minute interval. When I increased the bpm to 240. and ran for 385 seconds total. I counted only 1517 beats instead of expected 1540. Is this expected? My modified code is attached if you want to try it.

Hi Anke:
You are using a precise timer extension. Is there an aia file for that? How is it different from the built in timer?

Thanks to both of you for awesome help.metronom (1).aia (7.4 KB)

Has the screen been turned off for a while during these 385 seconds? The screen must be on when the metronome is working.

No. The screen was always on. I had set it to turn off after 10 min. which is the maximum on my phone. I also made sure this was the only application that was running (as far as I could tell). I have a Samsung Galaxy S8.
Maybe you can try my code on your phone and see if you replicate similar results?

thanks.

In fact, that solution was not exact. Here is a solution that uses a counter to correct the time.
metronom2 (2).aia (7.5 KB)
Also test with a real stopwatch

I stopped the clock 1 second too fast, but you can see that it is exactly right now. So I still say the extension is not needed;).

This should not happen with my app.
Here is the result:

Blocks

Hi Patryk_F

Your modified code seems to work. I ran for 569s and I got a beat count of 2277 at 240 bpm.
It is off by 1 but that is good enough for my purposes. I understand the logic of your code but I recall reading somewhere in these forums that timer interval <10ms can be inaccurate. You have set it to 1ms.
Is that ok? Is the issue fixed.

Hi Anke:
Based on this code, the built-in timer seems to be accurate enough. You have created your extension of a precise timer. How is it different and how does it work?

As always, thanks to both of you for helping me debug the metronome.

An error by 1 measure may be due to something else. I don't know if you tested in the original code or modified it. In general, there are no delays in this solution, because it is corrected on an ongoing basis using a numerator. As for the clock speed, I don't think 1ms has any negative effects. You can try to set 10ms and see if this affects the accuracy. But in my opinion 1ms is ok.

It doesn't matter whether you set the TimerInterval 1, 10 or 25 (ms). The timer needs some time to start / react (I think at least 25 ms, depending on the device).