-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio.py
More file actions
284 lines (235 loc) · 8.35 KB
/
audio.py
File metadata and controls
284 lines (235 loc) · 8.35 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""Audio module - Simple WAV file alarm.
Put your alarm.wav file in the 'audio/' folder.
The alarm will play on loop when triggered and stop INSTANTLY when stopped.
"""
import threading
import time
import sys
import os
from pathlib import Path
from typing import Optional
from queue import Queue, Empty
from PyQt6.QtCore import QThread
from config import Config
# Check for pygame (for better audio looping)
PYGAME_AVAILABLE = False
try:
import pygame
PYGAME_AVAILABLE = True
except ImportError:
pass
# Check for pyttsx3
TTS_AVAILABLE = False
try:
import pyttsx3
TTS_AVAILABLE = True
except ImportError:
pass
# Check for winsound (Windows only)
WINSOUND_AVAILABLE = False
if sys.platform == 'win32':
try:
import winsound
WINSOUND_AVAILABLE = True
except ImportError:
pass
class TTSEngine(QThread):
"""Text-to-speech engine with queue."""
_instance = None
_lock = threading.Lock()
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if getattr(self, "_initialized", False):
return
super().__init__()
self._initialized = True
self._running = False
self._queue: Queue = Queue()
self._engine = None
self._engine_lock = threading.Lock()
def run(self):
self._running = True
if TTS_AVAILABLE:
try:
self._engine = pyttsx3.init()
self._engine.setProperty('rate', 150)
self._engine.setProperty('volume', 0.9)
except Exception as e:
print(f"[TTS] Init error: {e}")
self._engine = None
while self._running:
try:
text = self._queue.get(timeout=0.5)
if text is None:
break
self._speak_internal(text)
except Empty:
continue
except Exception as e:
print(f"[TTS] Error: {e}")
def _speak_internal(self, text: str):
if not self._engine:
return
with self._engine_lock:
try:
self._engine.say(text)
self._engine.runAndWait()
except Exception as e:
print(f"[TTS] Speak error: {e}")
def speak(self, text: str):
if not text:
return
while not self._queue.empty():
try:
self._queue.get_nowait()
except:
break
self._queue.put(text)
def stop(self):
self._running = False
self._queue.put(None)
class WavAlarm:
"""Simple WAV file alarm.
Put your alarm.wav file in the 'audio/' folder.
- Plays alarm.wav on LOOP when start() is called
- Stops INSTANTLY when stop() is called
- No delays, no blocking
Usage:
alarm = WavAlarm()
alarm.start() # Starts playing immediately
alarm.stop() # Stops INSTANTLY
"""
def __init__(self, config: Config = None):
self.config = config
self._is_playing = False
self._is_muted = False
# Find alarm.wav file
self.audio_dir = Path(os.path.dirname(os.path.abspath(__file__))) / "audio"
self.alarm_file = self.audio_dir / "alarm.wav"
# Create audio directory if it doesn't exist
self.audio_dir.mkdir(exist_ok=True)
# Pygame for better looping
self.pygame_sound = None
if PYGAME_AVAILABLE:
pygame.mixer.init()
if self.alarm_file.exists():
try:
self.pygame_sound = pygame.mixer.Sound(str(self.alarm_file))
except:
self.pygame_sound = None
# Check if alarm file exists
if self.alarm_file.exists():
print(f"[Alarm] Found alarm file: {self.alarm_file}")
else:
print(f"[Alarm] WARNING: No alarm.wav found at {self.alarm_file}")
print(f"[Alarm] Please put an alarm.wav file in the 'audio/' folder")
@property
def is_playing(self) -> bool:
return self._is_playing
@property
def is_muted(self) -> bool:
return self._is_muted
def start(self):
"""Start playing the alarm WAV file on loop."""
if self._is_playing:
return # Already playing
if self._is_muted:
self._is_playing = True
print("[Alarm] Started (muted)")
return
if not self.alarm_file.exists():
print(f"[Alarm] Cannot start - no alarm.wav file found")
self._is_playing = True # Mark as playing for UI
return
self._is_playing = True
if self.pygame_sound and not self._is_muted:
try:
self.pygame_sound.play(loops=-1) # Loop indefinitely
print("[Alarm] STARTED - Playing alarm.wav on seamless loop with pygame")
return
except Exception as e:
print(f"[Alarm] Pygame error: {e}")
if WINSOUND_AVAILABLE:
try:
# Play WAV file on loop, asynchronously (non-blocking)
# SND_ASYNC = play in background
# SND_LOOP = loop continuously
# SND_FILENAME = play from file
winsound.PlaySound(
str(self.alarm_file),
winsound.SND_FILENAME | winsound.SND_ASYNC | winsound.SND_LOOP
)
print("[Alarm] STARTED - Playing alarm.wav on loop")
except Exception as e:
print(f"[Alarm] Error starting: {e}")
else:
print("[Alarm] Started (no audio available)")
def stop(self):
"""Stop the alarm INSTANTLY."""
if not self._is_playing:
return # Already stopped
self._is_playing = False
if self.pygame_sound:
try:
self.pygame_sound.stop()
print("[Alarm] STOPPED (pygame)")
return
except Exception as e:
print(f"[Alarm] Pygame stop error: {e}")
if WINSOUND_AVAILABLE:
try:
# Stop ALL sounds immediately
# SND_PURGE stops any playing sound
winsound.PlaySound(None, winsound.SND_PURGE)
print("[Alarm] STOPPED")
except Exception as e:
print(f"[Alarm] Error stopping: {e}")
else:
print("[Alarm] Stopped")
def mute(self):
"""Mute the alarm."""
self._is_muted = True
if self._is_playing:
if self.pygame_sound:
try:
pygame.mixer.pause()
print("[Alarm] Muted (pygame)")
return
except:
pass
# Stop the sound but keep is_playing True
if WINSOUND_AVAILABLE:
try:
winsound.PlaySound(None, winsound.SND_PURGE)
except:
pass
print("[Alarm] Muted")
def unmute(self):
"""Unmute the alarm."""
self._is_muted = False
if self._is_playing:
if self.pygame_sound:
try:
pygame.mixer.unpause()
print("[Alarm] Unmuted (pygame)")
return
except:
pass
# Resume playing
if WINSOUND_AVAILABLE and self.alarm_file.exists():
try:
winsound.PlaySound(
str(self.alarm_file),
winsound.SND_FILENAME | winsound.SND_ASYNC | winsound.SND_LOOP
)
except:
pass
print("[Alarm] Unmuted")
# Alias for backward compatibility
ContinuousAlarm = WavAlarm