Search code examples
pythonaudiopygamerecording

Record sound without blocking Pygame UI


I am making a simple Python utility that shows the tempo of a song (BPM) that is playing. I record short fragments of a few seconds to calculate the tempo over. The problem is that now I want to show this on a display using a Pygame UI, but when I'm recording sound, the UI does not respond. I want to make it so that the UI will stay responsive during the recording of the sound, and then update the value on the screen once the tempo over a new fragment has been calculated. How can I implement this?

I have looked at threading but I'm not sure this is the appropriate solution for this.


Solution

  • I'd use the python threading library.

    Use the pygame module in the main thread (just the normal python shell, effectively) an create a separate thread for the function that determines BPM.

    This BPM can then be saved to a global variable that can be accessed by PyGame for displaying.