File size: 2,014 Bytes
90cb461
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import os
import wave
import struct
import math

# Função para garantir que os arquivos existam (mesma lógica do jogo)
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")
    
    # Player visível para diagnóstico
    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()