Skip to content

Split any YouTube song into stems

TypeScript

fix: allow retrying a failed pitch shifter load

David Naviace467582cabf
diff --git a/src/renderer/src/lib/engine.ts b/src/renderer/src/lib/engine.ts
index 82d6b62..81a1570 100644
--- a/src/renderer/src/lib/engine.ts
+++ b/src/renderer/src/lib/engine.ts
@@ -95,9 +95,13 @@ export class StemEngine {
return
}
let shifter: PitchShifter
+ const load = (this.shifterLoad ??= this.shifterFactory(this.ctx!))
try {
- shifter = await (this.shifterLoad ??= this.shifterFactory(this.ctx!))
+ shifter = await load
} catch (err) {
+ // forget the failed load so the next press retries; overlapping calls
+ // awaiting the same load must not clear a newer attempt
+ if (this.shifterLoad === load) this.shifterLoad = null
this.pitchError = err instanceof Error ? err.message : String(err)
this.appliedPitch = 0
this.route(null)
diff --git a/src/renderer/src/lib/pitch.ts b/src/renderer/src/lib/pitch.ts
index fd33fe7..a5a1021 100644
--- a/src/renderer/src/lib/pitch.ts
+++ b/src/renderer/src/lib/pitch.ts
@@ -22,13 +22,25 @@ export const createPitchShifter: ShifterFactory = async (ctx) => {
const timeout = new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new Error('Pitch shifter failed to load')), LOAD_TIMEOUT_MS)
})
- let node: Awaited<ReturnType<typeof SignalsmithStretch>>
+ const creating = SignalsmithStretch(ctx)
+ let node: Awaited<typeof creating>
+ let latency: number
try {
- node = await Promise.race([SignalsmithStretch(ctx), timeout])
+ ;[node, latency] = await Promise.race([
+ creating.then(async (n) => [n, await n.latency()] as const),
+ timeout
+ ])
+ } catch (err) {
+ // a load that finishes after the timeout would leave an orphan worklet
+ // running; retries make that repeatable, so shut it down when it lands
+ void creating.then((n) => {
+ n.schedule({ active: false })
+ n.disconnect()
+ }, () => {})
+ throw err
} finally {
clearTimeout(timer)
}
- const latency = await node.latency()
// Without active: true the node outputs silence
const apply = (n: number): void =>
node.schedule({ active: true, semitones: clampPitch(n), formantCompensation: true })
diff --git a/tests/engine.test.mjs b/tests/engine.test.mjs
index a16c744..ed19f59 100644
--- a/tests/engine.test.mjs
+++ b/tests/engine.test.mjs
@@ -209,3 +209,19 @@ test('pitch set before the audio context exists applies once playback starts', a
assert.equal(master.outputs[0], calls[0].shifter.input)
assert.equal(engine.pitch(), -2)
})
+
+test('a failed load can be retried by pressing again', async () => {
+ const { calls, factory } = fakeFactory()
+ const { engine, master } = playing(factory)
+ const first = engine.setPitch(2)
+ calls[0].reject(new Error('Pitch shifter failed to load'))
+ await first
+ assert.equal(engine.pitchError, 'Pitch shifter failed to load')
+ const second = engine.setPitch(2)
+ assert.equal(calls.length, 2, 'the factory is called again')
+ calls[1].resolve()
+ await second
+ assert.equal(engine.pitchError, null)
+ assert.equal(engine.pitch(), 2)
+ assert.deepEqual(master.outputs, [calls[1].shifter.input])
+})