Skip to content

Split any YouTube song into stems

TypeScript

fix(export): narrow export IPC inputs and fetch only checked stems

David Navia53f584df1e94
diff --git a/src/main/export-session.ts b/src/main/export-session.ts
index 4c3885c..9843ce3 100644
--- a/src/main/export-session.ts
+++ b/src/main/export-session.ts
@@ -13,8 +13,8 @@ const sessions = new Map<string, Session>()
// export actually wrote this run (the file, or the folder of a separate export)
const revealable = new Set<string>()
-export function canReveal(path: string): boolean {
- return revealable.has(path)
+export function canReveal(path: unknown): path is string {
+ return typeof path === 'string' && revealable.has(path)
}
export function openSession(kind: 'file' | 'folder', path: string, format: 'wav' | 'aac'): string {
diff --git a/src/main/index.ts b/src/main/index.ts
index a141ce5..25e99c8 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -30,7 +30,7 @@ import {
} from './env'
import { loadSettings, saveSettings } from './settings'
import {
- loadSongs, removeSong, clearLibrary, stemBuffers, stemsDir, sanitizeName,
+ loadSongs, removeSong, clearLibrary, stemBuffers, requestedStems, stemsDir, sanitizeName,
loadMix, transcodeToAac, loadStemPan, saveStemPan, loadPitch, savePitch, setKeyOverride
} from './library'
import { AUDIO_EXTENSIONS } from '../shared/local'
@@ -251,9 +251,9 @@ app.whenReady().then(async () => {
cancelJob()
clearLibrary()
})
- ipcMain.handle('song:buffers', (_e, videoId: string) => {
+ ipcMain.handle('song:buffers', (_e, videoId: string, stems?: unknown) => {
const song = loadSongs().find((s) => s.videoId === videoId)
- return stemBuffers(videoId, song?.stems)
+ return stemBuffers(videoId, requestedStems(song, stems))
})
ipcMain.handle('jobs:start', async (_e, url: string, model?: string, stems?: string[]) => {
@@ -342,8 +342,8 @@ app.whenReady().then(async () => {
}
)
ipcMain.handle('export:end', (_e, token: string) => closeSession(token))
- ipcMain.handle('export:reveal', (_e, path: string) => {
- if (!canReveal(String(path))) throw new Error('Not an exported file')
+ ipcMain.handle('export:reveal', (_e, path: unknown) => {
+ if (!canReveal(path)) throw new Error('Not an exported file')
shell.showItemInFolder(path)
})
ipcMain.handle('song:mix', (_e, videoId: string) => loadMix(videoId))
diff --git a/src/main/library.ts b/src/main/library.ts
index 2bcac7d..e1a15d9 100644
--- a/src/main/library.ts
+++ b/src/main/library.ts
@@ -149,6 +149,14 @@ export function stemsFor(song?: Song | null): string[] {
return song?.stems?.length ? song.stems : DEFAULT_STEMS
}
+// the renderer may ask for a subset (export reads only the checked stems);
+// anything not among the song's own stems is dropped, never read from disk
+export function requestedStems(song: Song | null | undefined, requested: unknown): string[] {
+ const all = stemsFor(song)
+ if (!Array.isArray(requested)) return all
+ return all.filter((id) => requested.includes(id))
+}
+
function panSong(videoId: string): Song {
const song = loadSongs().find((s) => s.videoId === videoId)
if (!song || typeof videoId !== 'string' || !/^[\w-]+$/.test(videoId)) {
diff --git a/src/preload/index.ts b/src/preload/index.ts
index b46df29..aab0cb0 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -16,7 +16,7 @@ const api: StemKitApi = {
listSongs: () => ipcRenderer.invoke('library:list'),
deleteSong: (videoId) => ipcRenderer.invoke('library:delete', videoId),
clearLibrary: () => ipcRenderer.invoke('library:clear'),
- getBuffers: (videoId) => ipcRenderer.invoke('song:buffers', videoId),
+ getBuffers: (videoId, stems) => ipcRenderer.invoke('song:buffers', videoId, stems),
getStemPan: (videoId) => ipcRenderer.invoke('song:pan:get', videoId),
detectKey: (videoId, force) => ipcRenderer.invoke('song:key:detect', videoId, force === true),
setKeyOverride: (videoId, override) => ipcRenderer.invoke('song:key:set', videoId, override),
diff --git a/src/renderer/src/lib/exporter.ts b/src/renderer/src/lib/exporter.ts
index f11d124..8e6eeaa 100644
--- a/src/renderer/src/lib/exporter.ts
+++ b/src/renderer/src/lib/exporter.ts
@@ -119,7 +119,7 @@ export async function exportMixed(opts: ExportOptions): Promise<string | null> {
)
if (!session) return null
try {
- const payload = await window.stemkit.getBuffers(opts.videoId)
+ const payload = await window.stemkit.getBuffers(opts.videoId, ids)
const sources: RenderSource[] = []
for (const id of ids) sources.push(await stemSource(opts, payload, id))
const channels = await renderOffline(sources, opts.semitones)
@@ -145,7 +145,7 @@ export async function exportSeparate(
if (!session) return null
let count = 0
try {
- const payload = ids.length > 0 ? await window.stemkit.getBuffers(opts.videoId) : {}
+ const payload = ids.length > 0 ? await window.stemkit.getBuffers(opts.videoId, ids) : {}
// one at a time: every decode and render holds a full-length stereo copy in memory
for (const id of ids) {
const channels = await renderOffline([await stemSource(opts, payload, id)], opts.semitones)
diff --git a/src/shared/types.ts b/src/shared/types.ts
index d592321..a53e9a4 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -134,7 +134,8 @@ export interface StemKitApi {
listSongs(): Promise<Song[]>
deleteSong(videoId: string): Promise<void>
clearLibrary(): Promise<void>
- getBuffers(videoId: string): Promise<Record<string, Uint8Array>>
+ // stems: only these (export); omitted = every stem of the song
+ getBuffers(videoId: string, stems?: string[]): Promise<Record<string, Uint8Array>>
getStemPan(videoId: string): Promise<StemPanMap>
// force retries a detection that already ran (normally a stored result is final)
detectKey(videoId: string, force?: boolean): Promise<SongKey>
diff --git a/tests/export-session.test.mjs b/tests/export-session.test.mjs
index 83e637b..123cdeb 100644
--- a/tests/export-session.test.mjs
+++ b/tests/export-session.test.mjs
@@ -118,3 +118,9 @@ test('only paths written by a session can be revealed', async () => {
closeSession(file)
closeSession(folder)
})
+
+test('reveal refuses anything that is not a plain string path', () => {
+ assert.equal(canReveal(['/x.wav']), false)
+ assert.equal(canReveal(undefined), false)
+ assert.equal(canReveal({ toString: () => '/x.wav' }), false)
+})
diff --git a/tests/exporter.test.mjs b/tests/exporter.test.mjs
index 7d6f94b..39bb445 100644
--- a/tests/exporter.test.mjs
+++ b/tests/exporter.test.mjs
@@ -49,8 +49,8 @@ beforeEach(() => {
mixPayload = bytes('mix')
globalThis.window = {
stemkit: {
- getBuffers: async () => {
- log.push('getBuffers')
+ getBuffers: async (_id, stems) => {
+ log.push(`getBuffers:${stems ? [...stems].sort().join(',') : 'all'}`)
return { vocals: bytes('vocals'), drums: bytes('drums'), bass: bytes('bass'), other: bytes('other') }
},
getMix: async () => {
@@ -105,3 +105,11 @@ test('exportSeparate with a missing original mix fails before opening a session
)
assert.equal(log.some((l) => l.startsWith('begin') || l.startsWith('write')), false)
})
+
+test('exports only fetch the raw bytes of the checked stems', async () => {
+ await exportMixed({ ...base, selected: ['bass', 'drums'] })
+ assert.ok(log.includes('getBuffers:bass,drums'), log.join(' '))
+ log = []
+ await exportSeparate({ ...base, selected: ['other'], includeMix: false })
+ assert.ok(log.includes('getBuffers:other'), log.join(' '))
+})
diff --git a/tests/pan-storage.test.mjs b/tests/pan-storage.test.mjs
index e8641a0..dbe5158 100644
--- a/tests/pan-storage.test.mjs
+++ b/tests/pan-storage.test.mjs
@@ -9,7 +9,7 @@ const root = mkdtempSync(join(tmpdir(), 'stemkit-pan-'))
after(() => rmSync(root, { recursive: true, force: true }))
const libraryUrl = new URL('../src/main/library.ts', import.meta.url).href
const hooks = registerTsHooks({ stubEnv: `export const userDataDir = () => ${JSON.stringify(root)}` })
-const { saveSongs, loadStemPan, saveStemPan, removeSong } = await import(libraryUrl)
+const { saveSongs, loadStemPan, saveStemPan, removeSong, requestedStems } = await import(libraryUrl)
hooks.deregister()
const song = { videoId: 'song-one', stems: ['vocals', 'drums'], title: 'Test', duration: 10, addedAt: 1 }
@@ -30,3 +30,11 @@ test('pan persists per song, validates values, handles old/corrupt files, and de
assert.throws(() => saveStemPan(song.videoId, { vocals: 1 }), /Unknown song/)
assert.equal(existsSync(join(root, 'songs', song.videoId)), false)
})
+
+test('a renderer stem request is narrowed to the song\'s own stems', () => {
+ const song = { videoId: 'x', title: 'X', duration: 1, addedAt: 0, stems: ['vocals', 'drums', 'bass', 'other'] }
+ assert.deepEqual(requestedStems(song, undefined), ['vocals', 'drums', 'bass', 'other'])
+ assert.deepEqual(requestedStems(song, ['bass', 'drums']), ['drums', 'bass'])
+ assert.deepEqual(requestedStems(song, ['../../etc/passwd', 'bass']), ['bass'])
+ assert.deepEqual(requestedStems(song, 'bass'), ['vocals', 'drums', 'bass', 'other'])
+})