Create sounds.py
Browse files
sounds.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# sounds.py
|
2 |
+
import base64
|
3 |
+
from pathlib import Path
|
4 |
+
from typing import Dict
|
5 |
+
|
6 |
+
# Audio file paths (ตั้งชื่อไฟล์แบบนี้)
|
7 |
+
AUDIO_PATHS = {
|
8 |
+
'bgm': 'assets/audio/gentle_story.mp3', # เพลงพื้นหลังแนวนิทาน
|
9 |
+
'submit': 'assets/audio/pop.mp3', # เสียงปุ่มกด
|
10 |
+
'achievement': 'assets/audio/chime.mp3', # เสียงได้รางวัล
|
11 |
+
'complete': 'assets/audio/success.mp3' # เสียงจบเรื่อง
|
12 |
+
}
|
13 |
+
|
14 |
+
class AudioManager:
|
15 |
+
def __init__(self):
|
16 |
+
self._audio_cache: Dict[str, str] = {}
|
17 |
+
self._initialize_cache()
|
18 |
+
|
19 |
+
def _initialize_cache(self):
|
20 |
+
"""Load and cache audio files"""
|
21 |
+
for name, path in AUDIO_PATHS.items():
|
22 |
+
try:
|
23 |
+
with open(path, "rb") as f:
|
24 |
+
audio_bytes = f.read()
|
25 |
+
self._audio_cache[name] = base64.b64encode(audio_bytes).decode()
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Error loading audio {name}: {e}")
|
28 |
+
|
29 |
+
def get_audio_html(self) -> str:
|
30 |
+
"""Generate HTML for all audio elements"""
|
31 |
+
audio_elements = []
|
32 |
+
|
33 |
+
# Background music (with loop)
|
34 |
+
if 'bgm' in self._audio_cache:
|
35 |
+
audio_elements.append(f"""
|
36 |
+
<audio id="bgm" loop>
|
37 |
+
<source src="data:audio/mp3;base64,{self._audio_cache['bgm']}" type="audio/mp3">
|
38 |
+
</audio>
|
39 |
+
""")
|
40 |
+
|
41 |
+
# Sound effects (without loop)
|
42 |
+
for name, base64_data in self._audio_cache.items():
|
43 |
+
if name != 'bgm':
|
44 |
+
audio_elements.append(f"""
|
45 |
+
<audio id="sound_{name}">
|
46 |
+
<source src="data:audio/mp3;base64,{base64_data}" type="audio/mp3">
|
47 |
+
</audio>
|
48 |
+
""")
|
49 |
+
|
50 |
+
# Add JavaScript controls
|
51 |
+
audio_elements.append("""
|
52 |
+
<script>
|
53 |
+
// Play specific sound
|
54 |
+
function playSound(id) {
|
55 |
+
const sound = document.getElementById(id);
|
56 |
+
if (sound) {
|
57 |
+
sound.currentTime = 0;
|
58 |
+
sound.play();
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
// Toggle background music
|
63 |
+
function toggleBGM(play) {
|
64 |
+
const bgm = document.getElementById('bgm');
|
65 |
+
if (bgm) {
|
66 |
+
if (play) {
|
67 |
+
bgm.play();
|
68 |
+
} else {
|
69 |
+
bgm.pause();
|
70 |
+
}
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
// Set volume for all audio
|
75 |
+
function setVolume(vol) {
|
76 |
+
const audios = document.getElementsByTagName('audio');
|
77 |
+
for (let audio of audios) {
|
78 |
+
audio.volume = vol;
|
79 |
+
}
|
80 |
+
}
|
81 |
+
</script>
|
82 |
+
""")
|
83 |
+
|
84 |
+
return "\n".join(audio_elements)
|
85 |
+
|
86 |
+
def get_sound_commands() -> Dict[str, str]:
|
87 |
+
"""Get JavaScript commands for playing sounds"""
|
88 |
+
return {
|
89 |
+
'submit': "playSound('sound_submit');",
|
90 |
+
'achievement': "playSound('sound_achievement');",
|
91 |
+
'complete': "playSound('sound_complete');",
|
92 |
+
'bgm_play': "toggleBGM(true);",
|
93 |
+
'bgm_pause': "toggleBGM(false);"
|
94 |
+
}
|