-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayback.py
More file actions
211 lines (165 loc) · 6.5 KB
/
playback.py
File metadata and controls
211 lines (165 loc) · 6.5 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
"""Audio playback — NSSound on macOS, pygame on Windows/Linux."""
from __future__ import annotations
import sys
import threading
from pathlib import Path
from typing import Any
import state
# ── Backend selection ────────────────────────────────────────────────────────
_USE_NSSOUND = sys.platform == "darwin"
_NSSound = None
_current_index = -1
_ns_sound = None
# Initialize pygame mixer on Windows/Linux
if not _USE_NSSOUND:
import pygame.mixer as _mixer
_mixer.init(frequency=48000, size=-16, channels=2, buffer=512)
# ── Internal helpers ─────────────────────────────────────────────────────────
def _ensure_nssound():
"""Lazy-load NSSound."""
global _NSSound
if _NSSound is None and _USE_NSSOUND:
from AppKit import NSSound
_NSSound = NSSound
return _NSSound
def _preview_items():
"""Return list of preview entry dicts from state."""
return state.get("preview_entries", [])
def _load_index(idx: int) -> dict[str, Any]:
"""Load file at index and return entry data."""
global _current_index
items = _preview_items()
if not items or not (0 <= idx < len(items)):
return {"success": False, "error": "Invalid index"}
_current_index = idx
entry = items[idx]
playback_file = Path(entry["srcpath"]) if entry.get("srcpath") else None
state._state["playback_file"] = str(playback_file) if playback_file else None
state._playback_file = playback_file
state.push_keys(["playback_file"])
return {"success": True, "file": str(playback_file), "index": idx}
def _is_busy() -> bool:
"""Return True if audio is currently playing."""
if _USE_NSSOUND:
return _ns_sound is not None and _ns_sound.isPlaying()
else:
return _mixer.music.get_busy()
def _poll_playback():
"""Poll for playback completion."""
if _is_busy():
threading.Timer(0.2, _poll_playback).start()
else:
state._state["is_playing"] = False
state._is_playing = False
state.push_keys(["is_playing"])
# ── Public API ───────────────────────────────────────────────────────────────
def play_file(srcpath: str) -> dict[str, Any]:
"""Play a specific file by path. Returns status dict."""
global _ns_sound, _current_index
# Find index of this file in preview
items = _preview_items()
for i, entry in enumerate(items):
if entry.get("srcpath") == srcpath:
_current_index = i
break
playback_file = Path(srcpath)
if not playback_file.is_file():
return {"success": False, "error": "File not found"}
# Stop any current playback
stop()
try:
if _USE_NSSOUND:
NSSound = _ensure_nssound()
_ns_sound = NSSound.alloc().initWithContentsOfFile_byReference_(
str(playback_file), True)
if _ns_sound:
_ns_sound.play()
state._state["is_playing"] = True
state._is_playing = True
state._state["playback_file"] = str(playback_file)
state._playback_file = playback_file
state.push_keys(["is_playing", "playback_file"])
threading.Timer(0.2, _poll_playback).start()
return {"success": True, "file": str(playback_file)}
else:
return {"success": False, "error": "Failed to load audio"}
else:
_mixer.music.load(str(playback_file))
_mixer.music.play()
state._state["is_playing"] = True
state._is_playing = True
state._state["playback_file"] = str(playback_file)
state._playback_file = playback_file
state.push_keys(["is_playing", "playback_file"])
threading.Timer(0.2, _poll_playback).start()
return {"success": True, "file": str(playback_file)}
except Exception as e:
state._state["is_playing"] = False
state._is_playing = False
state.push_keys(["is_playing"])
return {"success": False, "error": str(e)}
def play() -> dict[str, Any]:
"""Play the currently selected file; if already playing, stop (toggle)."""
if _is_busy():
stop()
return {"success": True, "action": "stopped"}
if _current_index < 0:
# Try to play first item
items = _preview_items()
if items:
return play_file(items[0]["srcpath"])
return {"success": False, "error": "No files to play"}
items = _preview_items()
if _current_index < len(items):
return play_file(items[_current_index]["srcpath"])
return {"success": False, "error": "Invalid selection"}
def stop() -> None:
"""Stop playback."""
global _ns_sound
if _USE_NSSOUND:
if _ns_sound and _ns_sound.isPlaying():
_ns_sound.stop()
_ns_sound = None
else:
_mixer.music.stop()
state._state["is_playing"] = False
state._is_playing = False
state.push_keys(["is_playing"])
def reset() -> None:
"""Stop playback and reset current index."""
global _current_index
stop()
_current_index = -1
state._playback_file = None
state._state["playback_file"] = None
state.push_keys(["playback_file"])
def next_file() -> dict[str, Any]:
"""Play next file in list."""
stop()
items = _preview_items()
if not items:
return {"success": False, "error": "No files"}
idx = min(_current_index + 1, len(items) - 1) if _current_index >= 0 else 0
result = _load_index(idx)
if result["success"]:
return play()
return result
def prev_file() -> dict[str, Any]:
"""Play previous file in list."""
stop()
items = _preview_items()
if not items:
return {"success": False, "error": "No files"}
idx = max(_current_index - 1, 0) if _current_index >= 0 else 0
result = _load_index(idx)
if result["success"]:
return play()
return result
def get_current_index() -> int:
"""Return current playback index."""
return _current_index
def set_current_index(idx: int) -> None:
"""Set current playback index without playing."""
global _current_index
_current_index = idx
_load_index(idx)