Recently I've been trying to figure out how to write a metronome app, but I've come across many difficulties with OS time control (calling functions e.g.) or significantly inaccurate onset detection algorhithms (I've used librosa) and it seems to be a pretty complicated topic. Yet for some reason I wasn't successfull in my research upon professional metronome apps source code/tutorials/books touching the topic etc... The web seems to be filled with amateur examples and solutions that don't really meet the modern metronome app (such as Frozenapes iOS Tempo app) standards. e.g. a metronome which uses pre-recorded .wav samples when a certain tempo is called (so there are all the tempos between 30 and 300 bpm stored in the app). That kind of solutions seem to be a bit of dead end if user wants to change the tempo while metronome is working. To sum it up - I'm looking for any professional resources with code/text/whatev which isn't 2000 pages signal processing book for math graduates and could help me with designing a decent metronome with some recording features.
Asked
Active
Viewed 242 times
0
-
@PhilFreihofner 's answer is the correct approach. You want to write directly to the audio thread rather than trigger events. I can sense your frustration but sadly SO isn't the platform for this type of question. What I can recommend is to search for similar projects on GitHub https://github.com/search?l=Python&q=metronome&type=Repositories which should yield enough examples. – fdcpp Apr 18 '21 at 08:14
1 Answers
0
Maybe my experience with writing a metronome using Java will be helpful. The key was not relying on the system clock, but instead, on counting the exact number of PCM frames and placing the click audio PCM at that point. At a sample rate of 44100 fps, that is basically accuracy to 1/44100th of a second.
I cannot advise you as to how to stream PCM and to count the frames as they pass. Does Python even give you access to the individual frames?

Phil Freihofner
- 7,645
- 1
- 20
- 41
-
Python Librosa package comes with features that allow you to manipulate sample rates and frames, but how you would implement that kind of idea? If you use any kind of loop to call a click sample then you'd need to deal with controlling the time it takes the loop to complete its lifecycle but it isn't always the same exact amount of time. – user14864717 Apr 18 '21 at 08:24
-
Java has a class, `javax.sound.sampled.SourceDataLine`, that allows one to stream data. If Python allows you to stream bytes to the sound system, the solution is to stream continuously, counting frames, and either use zeroes as your source (when silent) or the click (has a fixed number of bytes, also to be counted). The software that plays back sound is very accurate, much more so than system OS clock messages. The underlying playback code (in Java) employs 'blocking queues' which keep the timing tight. – Phil Freihofner Apr 18 '21 at 16:42