(chunkIdx)
| 190 | // Returns a Promise<Map<name, AudioBuffer>>. Deduplicates: if a fetch for |
| 191 | // chunkIdx is already in flight, returns the same promise. |
| 192 | function _fetchChunk(chunkIdx) { |
| 193 | const hit = _cache.get(chunkIdx); |
| 194 | if (hit) return hit.promise; |
| 195 | |
| 196 | const entry = { promise: null, result: null }; |
| 197 | entry.promise = (async () => { |
| 198 | const pairs = await Promise.all( |
| 199 | [...stemMap.entries()] |
| 200 | .filter(([, s]) => s.header) |
| 201 | .map(async ([name, stem]) => { |
| 202 | try { |
| 203 | const pcm = await _fetchPcm(stem, chunkIdx); |
| 204 | if (!pcm) return [name, null]; |
| 205 | return [name, _pcmToAudioBuffer(ctx, pcm, stem.header)]; |
| 206 | } catch (e) { |
| 207 | console.warn(`[chunked] chunk ${chunkIdx} stem ${name}:`, e); |
| 208 | return [name, null]; |
| 209 | } |
| 210 | }) |
| 211 | ); |
| 212 | const map = new Map(pairs.filter(([, b]) => b)); |
| 213 | entry.result = map; |
| 214 | // Keep at most the previous chunk + current in cache to bound memory. |
| 215 | for (const k of _cache.keys()) { |
| 216 | if (k < chunkIdx - 1) _cache.delete(k); |
| 217 | } |
| 218 | return map; |
| 219 | })(); |
| 220 | |
| 221 | _cache.set(chunkIdx, entry); |
| 222 | return entry.promise; |
| 223 | } |
| 224 | |
| 225 | // --- node lifecycle --- |
| 226 |
no test coverage detected