Surn commited on
Commit
a94fc8f
·
1 Parent(s): 1a35382

add sounds.py

Browse files
Files changed (1) hide show
  1. battlewords/sounds.py +74 -0
battlewords/sounds.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # file: battlewords/sounds.py
2
+
3
+ import os
4
+ import tempfile
5
+ import torch
6
+ from diffusers import StableAudioPipeline
7
+ import scipy.io.wavfile as wav
8
+ import base64
9
+
10
+ # Predefined prompts for sound effects
11
+ EFFECT_PROMPTS = {
12
+ "correct guess": "A short, sharp ding sound for a correct guess",
13
+ "incorrect guess": "A low buzz sound for an incorrect guess",
14
+ "miss": "A soft thud sound for a miss",
15
+ "hit": "A bright chime sound for a hit",
16
+ "congratulations": "A triumphant fanfare sound for congratulations"
17
+ }
18
+
19
+ _sound_cache = {}
20
+
21
+ def generate_sound_effect(effect: str) -> str:
22
+ """
23
+ Generate a sound effect using Stable Audio Open based on the effect string.
24
+ Returns the path to the generated WAV file.
25
+ """
26
+ if effect not in EFFECT_PROMPTS:
27
+ raise ValueError(f"Unknown effect: {effect}. Available effects: {list(EFFECT_PROMPTS.keys())}")
28
+
29
+ # Check cache first
30
+ if effect in _sound_cache:
31
+ return _sound_cache[effect]
32
+
33
+ # Load the model (cached globally)
34
+ if not hasattr(generate_sound_effect, 'pipe'):
35
+ generate_sound_effect.pipe = StableAudioPipeline.from_pretrained(
36
+ "stabilityai/stable-audio-open-1.0",
37
+ torch_dtype=torch.float16
38
+ )
39
+ if torch.cuda.is_available():
40
+ generate_sound_effect.pipe = generate_sound_effect.pipe.to("cuda")
41
+
42
+ prompt = EFFECT_PROMPTS[effect]
43
+
44
+ # Generate audio
45
+ audio = generate_sound_effect.pipe(
46
+ prompt,
47
+ duration=2, # Short duration for sound effects
48
+ num_inference_steps=50
49
+ ).audio
50
+
51
+ # Save to temporary file
52
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmpfile:
53
+ wav.write(tmpfile.name, 44100, audio[0])
54
+ path = tmpfile.name
55
+
56
+ # Cache the path
57
+ _sound_cache[effect] = path
58
+ return path
59
+
60
+ def get_sound_effect_path(effect: str) -> str:
61
+ """
62
+ Get the path to a sound effect, generating it if necessary.
63
+ """
64
+ return generate_sound_effect(effect)
65
+
66
+ def get_sound_effect_data_url(effect: str) -> str:
67
+ """
68
+ Get a data URL for the sound effect, suitable for embedding in HTML.
69
+ """
70
+ path = generate_sound_effect(effect)
71
+ with open(path, "rb") as f:
72
+ data = f.read()
73
+ encoded = base64.b64encode(data).decode()
74
+ return f"data:audio/wav;base64,{encoded}"