Compare commits
No commits in common. "610d8350afd415b491d7ce836760f446582f6c2c" and "53b8655dd6cc797e0145cfb97d5e42f0b2a11e58" have entirely different histories.
610d8350af
...
53b8655dd6
2 changed files with 19 additions and 87 deletions
|
@ -27,16 +27,19 @@ export default defineComponent({
|
||||||
console.debug(`[AUDIOAREA] Initializing ${props.definition.src}...`);
|
console.debug(`[AUDIOAREA] Initializing ${props.definition.src}...`);
|
||||||
console.debug(props.definition);
|
console.debug(props.definition);
|
||||||
|
|
||||||
|
// Silent 1ms audio as placeholder (data URI of a minimal silent MP3)
|
||||||
|
// This prevents browser from attempting to load any audio until we're ready
|
||||||
|
const SILENT_AUDIO = 'data:audio/mp3;base64,SUQzAwAAAAAAJlRJVDIAAAAHAAAAU2lsZW50/+MYxAAEaAIAAgAAAAkBngQAAABMQVJHAAAABQAA/+MYxA8EaAIAAgAAABAAAP/7kMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/4xjEMgAAAAACAAAAAAAAAP/jGMRFAAAAAAAAAAAAAAAAAAAA';
|
||||||
|
|
||||||
// Preload the audio file completely to avoid keeping connections open
|
// Preload the audio file completely to avoid keeping connections open
|
||||||
// Use the global audio loading queue to throttle concurrent loads
|
// Use the global audio loading queue to throttle concurrent loads
|
||||||
const preloadAudio = (src: string) => {
|
const preloadAudio = (src: string) => {
|
||||||
console.debug(`[AUDIOAREA] Queueing audio for preload: ${src}`);
|
console.debug(`[AUDIOAREA] Queueing audio for preload: ${src}`);
|
||||||
|
|
||||||
// Set audioSrc to empty initially
|
// Set placeholder silent audio to avoid errors without triggering real load
|
||||||
audioSrc.value = "";
|
audioSrc.value = SILENT_AUDIO;
|
||||||
|
|
||||||
// Queue the audio for loading through our global service
|
// Queue the audio for loading through our global service
|
||||||
// Our improved AudioLoader will cache and deduplicate requests
|
|
||||||
queueAudioForLoading(src)
|
queueAudioForLoading(src)
|
||||||
.then((blobUrl) => {
|
.then((blobUrl) => {
|
||||||
// Use blob URL to avoid keeping connections open
|
// Use blob URL to avoid keeping connections open
|
||||||
|
@ -60,8 +63,6 @@ export default defineComponent({
|
||||||
const vol_b = 1 - vol_x;
|
const vol_b = 1 - vol_x;
|
||||||
|
|
||||||
const onBBoxChange = () => {
|
const onBBoxChange = () => {
|
||||||
if (!audioSrc.value) return;
|
|
||||||
|
|
||||||
const x = props.bbox.x + props.bbox.w / 2;
|
const x = props.bbox.x + props.bbox.w / 2;
|
||||||
const y = props.bbox.y + props.bbox.h / 2;
|
const y = props.bbox.y + props.bbox.h / 2;
|
||||||
const distance = Math.sqrt(
|
const distance = Math.sqrt(
|
||||||
|
|
|
@ -13,12 +13,6 @@ const audioQueue: Array<{
|
||||||
onError: (error: Error) => void;
|
onError: (error: Error) => void;
|
||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
// Cache of loaded audio files (src -> blobUrl)
|
|
||||||
const loadedAudioCache: Record<string, string> = {};
|
|
||||||
|
|
||||||
// Keep track of pending loads to avoid duplicates
|
|
||||||
const pendingLoads: Set<string> = new Set();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue an audio file for loading, respecting the global concurrent loading limit
|
* Queue an audio file for loading, respecting the global concurrent loading limit
|
||||||
* Returns a promise that resolves with the blob URL when loading is complete
|
* Returns a promise that resolves with the blob URL when loading is complete
|
||||||
|
@ -28,35 +22,6 @@ export function queueAudioForLoading(
|
||||||
onComplete?: (blobUrl: string) => void,
|
onComplete?: (blobUrl: string) => void,
|
||||||
onError?: (error: Error) => void
|
onError?: (error: Error) => void
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// Return cached result immediately if available
|
|
||||||
if (loadedAudioCache[src]) {
|
|
||||||
console.debug(`[AudioLoader] Using cached audio for ${src}`);
|
|
||||||
const blobUrl = loadedAudioCache[src];
|
|
||||||
if (onComplete) setTimeout(() => onComplete(blobUrl), 0);
|
|
||||||
return Promise.resolve(blobUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this source is already being loaded, add to existing promises
|
|
||||||
if (pendingLoads.has(src)) {
|
|
||||||
console.debug(`[AudioLoader] Already loading ${src}, adding to pending requests`);
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
audioQueue.push({
|
|
||||||
src,
|
|
||||||
onComplete: (blobUrl) => {
|
|
||||||
if (onComplete) onComplete(blobUrl);
|
|
||||||
resolve(blobUrl);
|
|
||||||
},
|
|
||||||
onError: (error) => {
|
|
||||||
if (onError) onError(error);
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark as pending
|
|
||||||
pendingLoads.add(src);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// Add to queue
|
// Add to queue
|
||||||
audioQueue.push({
|
audioQueue.push({
|
||||||
|
@ -80,56 +45,28 @@ export function queueAudioForLoading(
|
||||||
* Process the next items in the queue if we have capacity
|
* Process the next items in the queue if we have capacity
|
||||||
*/
|
*/
|
||||||
function processQueue() {
|
function processQueue() {
|
||||||
// Group queue items by src to avoid duplicate loads
|
// Load more audio files if we have capacity and items in the queue
|
||||||
const nextBatch: Record<string, Array<{
|
|
||||||
onComplete: (blobUrl: string) => void;
|
|
||||||
onError: (error: Error) => void;
|
|
||||||
}>> = {};
|
|
||||||
|
|
||||||
// Find next items to process while respecting MAX_CONCURRENT_LOADS
|
|
||||||
while (activeLoads < MAX_CONCURRENT_LOADS && audioQueue.length > 0) {
|
while (activeLoads < MAX_CONCURRENT_LOADS && audioQueue.length > 0) {
|
||||||
const next = audioQueue.shift();
|
const next = audioQueue.shift();
|
||||||
if (!next) continue;
|
if (next) {
|
||||||
|
loadAudio(next.src, next.onComplete, next.onError);
|
||||||
// If already cached, complete immediately without consuming a slot
|
|
||||||
if (loadedAudioCache[next.src]) {
|
|
||||||
next.onComplete(loadedAudioCache[next.src]);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group by src
|
|
||||||
if (!nextBatch[next.src]) {
|
|
||||||
nextBatch[next.src] = [];
|
|
||||||
// Each unique src counts as one active load
|
|
||||||
activeLoads++;
|
|
||||||
}
|
|
||||||
|
|
||||||
nextBatch[next.src].push({
|
|
||||||
onComplete: next.onComplete,
|
|
||||||
onError: next.onError
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start loading each unique audio file
|
|
||||||
Object.entries(nextBatch).forEach(([src, handlers]) => {
|
|
||||||
loadAudio(src, handlers);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal function to handle the actual audio loading using fetch API
|
* Internal function to handle the actual audio loading using fetch API
|
||||||
* This ensures the entire audio file is downloaded
|
* This ensures the entire audio file is downloaded
|
||||||
* @param src The source URL to load
|
|
||||||
* @param handlers Array of handlers to call when loading completes or fails
|
|
||||||
*/
|
*/
|
||||||
async function loadAudio(
|
async function loadAudio(
|
||||||
src: string,
|
src: string,
|
||||||
handlers: Array<{
|
onComplete: (blobUrl: string) => void,
|
||||||
onComplete: (blobUrl: string) => void;
|
onError: (error: Error) => void
|
||||||
onError: (error: Error) => void;
|
|
||||||
}>
|
|
||||||
) {
|
) {
|
||||||
console.debug(`[AudioLoader] Loading ${src} (${handlers.length} listeners)`);
|
// Increment active loads counter
|
||||||
|
activeLoads++;
|
||||||
|
|
||||||
|
console.debug(`[AudioLoader] Loading ${src}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Fetch the entire audio file
|
// Fetch the entire audio file
|
||||||
|
@ -145,22 +82,16 @@ async function loadAudio(
|
||||||
// Create a blob URL to use as the audio source
|
// Create a blob URL to use as the audio source
|
||||||
const blobUrl = URL.createObjectURL(blob);
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
|
|
||||||
// Store in cache
|
|
||||||
loadedAudioCache[src] = blobUrl;
|
|
||||||
|
|
||||||
console.debug(`[AudioLoader] Successfully loaded ${src}`);
|
console.debug(`[AudioLoader] Successfully loaded ${src}`);
|
||||||
|
|
||||||
// Call all completion handlers
|
// Call completion handler
|
||||||
handlers.forEach(handler => handler.onComplete(blobUrl));
|
onComplete(blobUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`[AudioLoader] Error loading audio: ${error}`);
|
console.error(`[AudioLoader] Error loading audio: ${error}`);
|
||||||
|
|
||||||
// Call all error handlers
|
// Call error handler
|
||||||
handlers.forEach(handler => handler.onError(error as Error));
|
onError(error as Error);
|
||||||
} finally {
|
} finally {
|
||||||
// Remove from pending loads
|
|
||||||
pendingLoads.delete(src);
|
|
||||||
|
|
||||||
// Decrement counter when audio is loaded (or failed)
|
// Decrement counter when audio is loaded (or failed)
|
||||||
activeLoads--;
|
activeLoads--;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue