Skip to content

Split any YouTube song into stems

TypeScript

feat(export): report the export result with a show-in-folder button

David Naviafed0f9214df7
diff --git a/src/main/export-session.ts b/src/main/export-session.ts
index dd3a0ad..4c3885c 100644
--- a/src/main/export-session.ts
+++ b/src/main/export-session.ts
@@ -9,6 +9,13 @@ import { sanitizeName } from './library'
// the format is pinned at begin: the dialog already showed that extension to the user
type Session = { kind: 'file' | 'folder'; path: string; format: 'wav' | 'aac' }
const sessions = new Map<string, Session>()
+// "Show in folder" takes a path from the renderer, so main only reveals what an
+// 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 openSession(kind: 'file' | 'folder', path: string, format: 'wav' | 'aac'): string {
const token = randomUUID()
@@ -40,8 +47,19 @@ export async function writeToSession(
}
if (format === 'wav') {
await writeFile(dest, wav)
- return dest
+ } else {
+ await transcodeVia(dest, wav, transcode)
}
+ revealable.add(dest)
+ if (session.kind === 'folder') revealable.add(session.path)
+ return dest
+}
+
+async function transcodeVia(
+ dest: string,
+ wav: Uint8Array,
+ transcode: (src: string, dest: string) => Promise<void>
+): Promise<void> {
// next to the destination so the temp file lives on the same volume
const tmp = join(dirname(dest), `.stemkit-${randomUUID()}.wav`)
try {
@@ -54,7 +72,6 @@ export async function writeToSession(
} finally {
await rm(tmp, { force: true })
}
- return dest
}
export function closeSession(token: string): void {
diff --git a/src/main/index.ts b/src/main/index.ts
index f9dbdb3..424247f 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -34,7 +34,7 @@ import {
loadMix, transcodeToAac, loadStemPan, saveStemPan, loadPitch, savePitch, setKeyOverride
} from './library'
import { AUDIO_EXTENSIONS } from '../shared/local'
-import { openSession, writeToSession, closeSession } from './export-session'
+import { openSession, writeToSession, closeSession, canReveal } from './export-session'
import { detectOnce } from './keydetect'
import { startJob, startLocalJob, cancelJob, searchYouTube } from './pipeline'
import { initUpdater } from './updater'
@@ -340,6 +340,10 @@ 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')
+ shell.showItemInFolder(path)
+ })
ipcMain.handle('song:mix', (_e, videoId: string) => loadMix(videoId))
ipcMain.handle('search:youtube', (_e, query: string) => searchYouTube(query))
diff --git a/src/preload/index.ts b/src/preload/index.ts
index ddc93b7..b46df29 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -27,6 +27,7 @@ const api: StemKitApi = {
exportBegin: (kind, name) => ipcRenderer.invoke('export:begin', kind, name),
exportWrite: (token, fileName, wav) => ipcRenderer.invoke('export:write', token, fileName, wav),
exportEnd: (token) => ipcRenderer.invoke('export:end', token),
+ exportReveal: (path) => ipcRenderer.invoke('export:reveal', path),
getMix: (videoId) => ipcRenderer.invoke('song:mix', videoId),
searchYouTube: (query) => ipcRenderer.invoke('search:youtube', query),
startJob: (url, model, stems) => ipcRenderer.invoke('jobs:start', url, model, stems),
diff --git a/src/renderer/src/components/Player.tsx b/src/renderer/src/components/Player.tsx
index 7d4641c..bbba194 100644
--- a/src/renderer/src/components/Player.tsx
+++ b/src/renderer/src/components/Player.tsx
@@ -97,6 +97,8 @@ export function Player({ song, settings }: Props): React.ReactElement {
const [pitchError, setPitchError] = useState<string | null>(null)
const [preset, setPreset] = useState<PresetId | 'custom'>('all')
const [exportOpen, setExportOpen] = useState(false)
+ // the dialog closes on success, so the result is reported here
+ const [exportNotice, setExportNotice] = useState<{ text: string; path: string } | null>(null)
const stemMeta = useMemo(() => buildStemMeta(Object.keys(buffers) as StemId[]), [buffers])
@@ -131,6 +133,7 @@ export function Player({ song, settings }: Props): React.ReactElement {
setSolos(new Set())
setPreset('all')
setExportOpen(false)
+ setExportNotice(null)
engine.stopAll()
// the engine applies pitch lazily, so the UI keeps its own value
setSemitones(0)
@@ -378,8 +381,17 @@ export function Player({ song, settings }: Props): React.ReactElement {
semitones: shifted ? semitones : 0
}
// null means the user cancelled the picker
- if (mode === 'mixed') return (await exportMixed(opts)) !== null
- return (await exportSeparate({ ...opts, includeMix: selected.has(MIX_ID) })) !== null
+ if (mode === 'mixed') {
+ const path = await exportMixed(opts)
+ if (path === null) return false
+ setExportNotice({ text: `Exported to ${path}`, path })
+ return true
+ }
+ const result = await exportSeparate({ ...opts, includeMix: selected.has(MIX_ID) })
+ if (result === null) return false
+ const files = result.count === 1 ? '1 file' : `${result.count} files`
+ setExportNotice({ text: `Exported ${files} to ${result.path}`, path: result.path })
+ return true
}
const applyPreset = (p: PresetId): void => {
@@ -591,6 +603,17 @@ export function Player({ song, settings }: Props): React.ReactElement {
{panError && <p role="status" className="mt-3 text-xs text-amber-300">{panError}</p>}
{pitchError && <p role="status" className="mt-3 text-xs text-amber-300">{pitchError}</p>}
+ {exportNotice && (
+ <p role="status" className="mt-3 flex items-center gap-2 text-xs text-emerald-300">
+ <span className="min-w-0 break-all">{exportNotice.text}</span>
+ <button
+ onClick={() => void window.stemkit.exportReveal(exportNotice.path).catch(() => {})}
+ className="no-drag shrink-0 rounded-md px-2 py-0.5 text-white/70 hover:text-white hover:bg-white/10 transition-colors"
+ >
+ Show in folder
+ </button>
+ </p>
+ )}
<div className="mt-4 space-y-2">
{decoding
? [...Array(4)].map((_, i) => (
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 35419ee..d592321 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -146,6 +146,8 @@ export interface StemKitApi {
exportBegin(kind: 'file' | 'folder', name: string): Promise<{ token: string; path: string } | null>
exportWrite(token: string, fileName: string | null, wav: Uint8Array): Promise<string>
exportEnd(token: string): Promise<void>
+ // opens the OS file manager at a path an export wrote this run (others are refused)
+ exportReveal(path: string): Promise<void>
getMix(videoId: string): Promise<Uint8Array | null>
searchYouTube(query: string): Promise<SearchResult[]>
startJob(url: string, model?: string, stems?: string[]): Promise<{ started: boolean }>
diff --git a/tests/export-session.test.mjs b/tests/export-session.test.mjs
index b127a51..83e637b 100644
--- a/tests/export-session.test.mjs
+++ b/tests/export-session.test.mjs
@@ -1,14 +1,14 @@
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
import { registerTsHooks } from './helpers/ts-resolve.mjs'
-import { mkdtempSync, readFileSync, existsSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
+import { mkdtempSync, readFileSync, existsSync, readdirSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'stemkit-session-'))
after(() => rmSync(root, { recursive: true, force: true }))
const hooks = registerTsHooks({ stubEnv: `export const userDataDir = () => ${JSON.stringify(root)}` })
-const { openSession, writeToSession, closeSession } = await import(
+const { openSession, writeToSession, closeSession, canReveal } = await import(
new URL('../src/main/export-session.ts', import.meta.url).href
)
hooks.deregister()
@@ -99,3 +99,22 @@ test('failing transcode removes a partial destination file', async () => {
assert.deepEqual(readdirSync(dir), [])
closeSession(token)
})
+
+test('only paths written by a session can be revealed', async () => {
+ const dir = mkdtempSync(join(root, 'reveal-'))
+ const file = openSession('file', join(dir, 'mixed.wav'), 'wav')
+ assert.equal(canReveal(join(dir, 'mixed.wav')), false)
+ const out = await writeToSession(file, null, wav, noTranscode)
+ assert.equal(canReveal(out), true)
+ const sub = join(dir, 'Song (+2)')
+ mkdirSync(sub)
+ const folder = openSession('folder', sub, 'wav')
+ assert.equal(canReveal(sub), false)
+ await writeToSession(folder, 'drums', wav, noTranscode)
+ assert.equal(canReveal(sub), true)
+ assert.equal(canReveal(join(sub, 'drums.wav')), true)
+ assert.equal(canReveal(dir), false)
+ assert.equal(canReveal('/etc/passwd'), false)
+ closeSession(file)
+ closeSession(folder)
+})