Skip to content

Split any YouTube song into stems

TypeScript

fix: attempt key detection once and skip slot after separation

David Naviad86e9e15296f
diff --git a/src/main/keydetect.ts b/src/main/keydetect.ts
index 53e7ad6..25b7249 100644
--- a/src/main/keydetect.ts
+++ b/src/main/keydetect.ts
@@ -74,14 +74,15 @@ export function detectOnce(videoId: string, run = runKeyScript): Promise<SongKey
if (existing) return existing
const song = loadSongs().find((s) => s.videoId === videoId)
if (!song) return Promise.resolve({ detected: null })
- // a stored result is final; a stored override-only entry still needs detecting
- if (song.key?.detected) return Promise.resolve(song.key)
+ // detection is attempted once per song: any stored key, even
+ // { detected: null } (attempted and failed), is final
+ if (song.key) return Promise.resolve(song.key)
const job = (async (): Promise<SongKey> => {
const release = await acquireSeparation()
try {
const result = await run(keyInputs(videoId, stemsFor(song)))
- if (!result.detected) return song.key ?? result
- // keep a user override that was set before detection finished
+ // keep a user override that was set before detection finished; a
+ // failure is stored too so it is not retried
const current = loadSongs().find((s) => s.videoId === videoId)?.key
const key: SongKey = current?.override ? { ...result, override: current.override } : result
patchSongKey(videoId, key)
diff --git a/src/main/pipeline.ts b/src/main/pipeline.ts
index f18994f..d22a54c 100644
--- a/src/main/pipeline.ts
+++ b/src/main/pipeline.ts
@@ -174,12 +174,9 @@ async function convertToWav(job: ActiveJob, inputPath: string): Promise<void> {
// runKeyScript never throws: a failed detection just leaves the song keyless
async function detectKey(job: ActiveJob, producedStems: string[]): Promise<SongKey> {
progress(job, 'finalize', 100, 'Detecting key')
- const release = await acquireSeparation()
- try {
- return await runKeyScript(keyInputs(job.videoId, producedStems))
- } finally {
- release()
- }
+ // no separation slot: ours is already released, and queueing behind another
+ // song's whole split would delay this song's library entry
+ return runKeyScript(keyInputs(job.videoId, producedStems))
}
function finalizeJob(
diff --git a/tests/key-storage.test.mjs b/tests/key-storage.test.mjs
index e6c48cf..0af782d 100644
--- a/tests/key-storage.test.mjs
+++ b/tests/key-storage.test.mjs
@@ -90,3 +90,26 @@ test('detectOnce dedupes concurrent calls, caches, and skips unknown songs', asy
assert.deepEqual(await detectOnce('nope', run), { detected: null })
assert.equal(calls, 1)
})
+
+test('detectOnce attempts once: a failure is stored and never retried', async () => {
+ saveSongs([mk('f')])
+ mkdirSync(join(root, 'songs', 'f'), { recursive: true })
+ let calls = 0
+ const run = async () => {
+ calls++
+ return { detected: null }
+ }
+ assert.deepEqual(await detectOnce('f', run), { detected: null })
+ assert.deepEqual(loadSongs()[0].key, { detected: null })
+ assert.deepEqual(await detectOnce('f', run), { detected: null })
+ assert.equal(calls, 1)
+})
+
+test('an override on a keyless song marks it attempted', async () => {
+ saveSongs([mk('g')])
+ setKeyOverride('g', 'Am')
+ assert.deepEqual(loadSongs()[0].key, { detected: null, override: 'Am' })
+ let calls = 0
+ await detectOnce('g', async () => (calls++, { detected: 'C' }))
+ assert.equal(calls, 0)
+})