Hi this is my simple metronome app :
import numpy as np
import sys
import time
import pygame
from pygame import mixer
def generate_beep(freq, duration, samplerate=44100):
t = np.linspace(0, duration, int(samplerate * duration))
audio = np.sin(2 * np.pi * freq * t)
return audio.astype(np.int16)
if __name__ == "__main__":
if len(sys.argv) != 3 or sys.argv[1] != "-t":
print("Usage: python metronome.py -t <beats_per_minute>")
sys.exit(1)
bpm = int(sys.argv[2])
spb = 60 / bpm # seconds per beat
pygame.init()
mixer.init()
while True:
beep = generate_beep(440, 0.25, 44100)
# Fix reshape error
beep = beep.reshape(-1)
sound = pygame.sndarray.make_sound(beep)
sound.play()
time.sleep(spb - 0.25)
I have this bogue, don’t know how to repair it :
pygame 2.5.2 (SDL 2.28.3, Python 3.12.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "\OPENMETRONOME\main.py", line 28, in <module>
sound = pygame.sndarray.make_sound(beep)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\OPENMETRONOME\venv\Lib\site-packages\pygame\sndarray.py", line 92, in make_sound
return mixer.Sound(array=array)
^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Array must be 2-dimensional for stereo mixer