| import gradio as gr |
| import os |
| import wave |
| import struct |
| import math |
|
|
| |
| def ensure_sounds(): |
| if not os.path.exists("sounds"): |
| os.makedirs("sounds") |
| |
| sounds = { |
| "sounds/click.wav": 440, |
| "sounds/correct.wav": 880, |
| "sounds/wrong.wav": 220 |
| } |
| |
| for filename, freq in sounds.items(): |
| if not os.path.exists(filename): |
| sample_rate = 44100 |
| duration = 0.2 |
| volume = 0.3 |
| n_samples = int(sample_rate * duration) |
| with wave.open(filename, 'w') as f: |
| f.setnchannels(1) |
| f.setsampwidth(2) |
| f.setframerate(sample_rate) |
| for i in range(n_samples): |
| val = int(volume * 32767.0 * math.sin(2.0 * math.pi * freq * i / sample_rate)) |
| f.writeframesraw(struct.pack('<h', val)) |
| print(f"Created: {filename}") |
|
|
| ensure_sounds() |
|
|
| def play_sound(file_type): |
| path = f"sounds/{file_type}.wav" |
| if os.path.exists(path): |
| return path, f"Playing: {path}" |
| return None, f"Error: {path} not found!" |
|
|
| with gr.Blocks(title="Autuori Sound Test") as demo: |
| gr.Markdown("# 馃攰 Sound Diagnostic Tool") |
| gr.Markdown("Click the buttons below to test if the browser can play the generated files.") |
| |
| with gr.Row(): |
| btn_click = gr.Button("Test Click") |
| btn_correct = gr.Button("Test Correct") |
| btn_wrong = gr.Button("Test Wrong") |
| |
| |
| audio_player = gr.Audio(label="Audio Output", interactive=False, autoplay=True) |
| status = gr.Textbox(label="File Status") |
|
|
| btn_click.click(fn=lambda: play_sound("click"), outputs=[audio_player, status]) |
| btn_correct.click(fn=lambda: play_sound("correct"), outputs=[audio_player, status]) |
| btn_wrong.click(fn=lambda: play_sound("wrong"), outputs=[audio_player, status]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |