Take a look at this snippets of metronome and image opening. ~~~ Python def playbeep():…
- User
Take a look at this snippets of metronome and image opening.
def play_beep(): sample_rate = 44100 frequency = 1000 duration = 0.1 # Generate mono audio data (1D array) t = np.linspace(0, duration, int(sample_rate * duration), False) audio_data = np.sin(2 * np.pi * frequency * t) audio_data *= 0.3 audio_data = (audio_data * 32767).astype(np.int16) # Convert 1D mono → 2D stereo (duplicate the channel) audio_data = np.column_stack((audio_data, audio_data)) # Now shape=(n_frames, 2) # Initialize mixer (stereo is default, so no need to set channels=2) if not pygame.mixer.get_init(): pygame.mixer.init(frequency=sample_rate, bits=16) # Create sound from 2D stereo array sound = pygame.sndarray.make_sound(audio_data) sound.play() while pygame.mixer.get_busy(): pygame.time.Clock().tick(10) def metronome(bpm, duration_seconds): pygame.mixer.init(frequency=44100, size=-16, channels=1) interval = 60.0 / bpm end_time = time.time() + duration_seconds while time.time() < end_time: play_beep() time.sleep(interval - 0.1) def display_image(image_path): try: img = pygame.image.load(image_path) except pygame.error as e: print(f"Error: Could not open or find the image. {e}") return False # Set up the display screen = pygame.display.set_mode(img.get_size()) pygame.display.set_caption("Image with Metronome") screen.blit(img, (0, 0)) pygame.display.flip() # Flag to track if the window was closed window_closed = False # Main loop to check for events while True: for event in pygame.event.get(): if event.type == pygame.QUIT: window_closed = True return window_closed # Return immediately on close elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: window_closed = True return window_closed # Return on 'q' key press # Optional: Add a small delay to reduce CPU usage time.sleep(0.01) def metronome_and_display(image_path, bpm, duration_seconds): # Start the metronome in a separate thread metronome_thread = threading.Thread(target=metronome, args=(bpm, duration_seconds)) metronome_thread.start() # Display the image display_image(image_path) # Wait for the metronome thread to finish metronome_thread.join()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.