Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- MusicManager.js +28 -8
MusicManager.js
CHANGED
|
@@ -109,13 +109,32 @@ export class MusicManager {
|
|
| 109 |
];
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
async start() {
|
| 113 |
if (this.isStarted) return;
|
|
|
|
| 114 |
|
| 115 |
// Create AudioContext for PCM playback
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
// Create analyser node for visualizer
|
| 121 |
this._analyserNode = this._audioCtx.createAnalyser();
|
|
@@ -132,20 +151,21 @@ export class MusicManager {
|
|
| 132 |
};
|
| 133 |
|
| 134 |
// Connect to backend WebSocket with auto-reconnect
|
| 135 |
-
// Use wss:// on HTTPS (HF Spaces), ws:// on HTTP (local dev)
|
| 136 |
const wsProto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 137 |
this._wsUrl = `${wsProto}//${window.location.host}/ws`;
|
|
|
|
| 138 |
this._reconnectAttempts = 0;
|
| 139 |
this._maxReconnectAttempts = 10;
|
| 140 |
this._connectWebSocket();
|
| 141 |
}
|
| 142 |
|
| 143 |
_connectWebSocket() {
|
|
|
|
| 144 |
this._ws = new WebSocket(this._wsUrl);
|
| 145 |
this._ws.binaryType = 'arraybuffer';
|
| 146 |
|
| 147 |
this._ws.onopen = () => {
|
| 148 |
-
|
| 149 |
this._wsReady = true;
|
| 150 |
this._reconnectAttempts = 0;
|
| 151 |
this._nextPlayTime = 0; // reset audio scheduling on reconnect
|
|
@@ -176,11 +196,11 @@ export class MusicManager {
|
|
| 176 |
};
|
| 177 |
|
| 178 |
this._ws.onerror = (e) => {
|
| 179 |
-
|
| 180 |
};
|
| 181 |
|
| 182 |
-
this._ws.onclose = () => {
|
| 183 |
-
|
| 184 |
this._wsReady = false;
|
| 185 |
// Auto-reconnect with backoff
|
| 186 |
if (this._reconnectAttempts < this._maxReconnectAttempts) {
|
|
|
|
| 109 |
];
|
| 110 |
}
|
| 111 |
|
| 112 |
+
_debug(msg) {
|
| 113 |
+
console.log('[Lyria] ' + msg);
|
| 114 |
+
let el = document.getElementById('lyria-debug');
|
| 115 |
+
if (!el) {
|
| 116 |
+
el = document.createElement('div');
|
| 117 |
+
el.id = 'lyria-debug';
|
| 118 |
+
el.style.cssText = 'position:fixed;bottom:4px;left:4px;padding:4px 8px;border-radius:4px;font:10px monospace;z-index:9999;pointer-events:none;background:rgba(0,0,0,0.7);color:#0f0;max-width:400px;word-wrap:break-word';
|
| 119 |
+
document.body.appendChild(el);
|
| 120 |
+
}
|
| 121 |
+
el.textContent = msg;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
async start() {
|
| 125 |
if (this.isStarted) return;
|
| 126 |
+
this._debug('start() called');
|
| 127 |
|
| 128 |
// Create AudioContext for PCM playback
|
| 129 |
+
try {
|
| 130 |
+
this._audioCtx = new (window.AudioContext || window.webkitAudioContext)({
|
| 131 |
+
sampleRate: 48000
|
| 132 |
+
});
|
| 133 |
+
this._debug('AudioContext: ' + this._audioCtx.state);
|
| 134 |
+
} catch(e) {
|
| 135 |
+
this._debug('AudioContext FAILED: ' + e.message);
|
| 136 |
+
return;
|
| 137 |
+
}
|
| 138 |
|
| 139 |
// Create analyser node for visualizer
|
| 140 |
this._analyserNode = this._audioCtx.createAnalyser();
|
|
|
|
| 151 |
};
|
| 152 |
|
| 153 |
// Connect to backend WebSocket with auto-reconnect
|
|
|
|
| 154 |
const wsProto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 155 |
this._wsUrl = `${wsProto}//${window.location.host}/ws`;
|
| 156 |
+
this._debug('WS URL: ' + this._wsUrl);
|
| 157 |
this._reconnectAttempts = 0;
|
| 158 |
this._maxReconnectAttempts = 10;
|
| 159 |
this._connectWebSocket();
|
| 160 |
}
|
| 161 |
|
| 162 |
_connectWebSocket() {
|
| 163 |
+
this._debug('Connecting WS...');
|
| 164 |
this._ws = new WebSocket(this._wsUrl);
|
| 165 |
this._ws.binaryType = 'arraybuffer';
|
| 166 |
|
| 167 |
this._ws.onopen = () => {
|
| 168 |
+
this._debug('WS CONNECTED');
|
| 169 |
this._wsReady = true;
|
| 170 |
this._reconnectAttempts = 0;
|
| 171 |
this._nextPlayTime = 0; // reset audio scheduling on reconnect
|
|
|
|
| 196 |
};
|
| 197 |
|
| 198 |
this._ws.onerror = (e) => {
|
| 199 |
+
this._debug('WS ERROR: ' + (e.message || 'unknown'));
|
| 200 |
};
|
| 201 |
|
| 202 |
+
this._ws.onclose = (e) => {
|
| 203 |
+
this._debug('WS CLOSED: code=' + e.code + ' reason=' + e.reason);
|
| 204 |
this._wsReady = false;
|
| 205 |
// Auto-reconnect with backoff
|
| 206 |
if (this._reconnectAttempts < this._maxReconnectAttempts) {
|