Split any YouTube song into stems
TypeScript
feat: persist per-song pan settings
omerdda7765d34ff
diff --git a/src/main/index.ts b/src/main/index.tsindex 6d3341c..1b7b858 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -30,7 +30,10 @@ import {getStatus} from './env'import { loadSettings, saveSettings } from './settings'-import { loadSongs, removeSong, stemBuffers, stemsDir, stemsFor, mixWavPath, AUDIO_EXTENSIONS } from './library'+import {+ loadSongs, removeSong, stemBuffers, stemsDir, stemsFor, mixWavPath,+ AUDIO_EXTENSIONS, loadStemPan, saveStemPan+} from './library'import { startJob, startLocalJob, cancelJob, searchYouTube } from './pipeline'import { initUpdater } from './updater'import { runSmoke } from './smoke'@@ -244,6 +247,8 @@ app.whenReady().then(async () => {ipcMain.handle('env:update-ytdlp', async () => updateYtDlp())ipcMain.handle('library:list', () => loadSongs())+ ipcMain.handle('song:pan:get', (_e, videoId: string) => loadStemPan(videoId))+ ipcMain.handle('song:pan:set', (_e, videoId: string, pans: unknown) => saveStemPan(videoId, pans))ipcMain.handle('library:delete', (_e, videoId: string) => removeSong(videoId))ipcMain.handle('song:buffers', (_e, videoId: string) => {const song = loadSongs().find((s) => s.videoId === videoId)diff --git a/src/main/library.ts b/src/main/library.tsindex e7cfe7e..9a962a0 100644--- a/src/main/library.ts+++ b/src/main/library.ts@@ -2,7 +2,7 @@ import { existsSync, readFileSync, writeFileSync, rmSync, mkdirSync, openSync, rimport { readFile } from 'fs/promises'import { join } from 'path'import { userDataDir } from './env'-import { DEFAULT_STEMS, type Song } from '../shared/types'+import { DEFAULT_STEMS, type Song, type StemPanMap, type StemId } from '../shared/types'// drives both the file-dialog filter and the pre-flight extension check in// startLocalJob; anything here is fair game for ffmpeg to decode@@ -78,6 +78,43 @@ export function stemsFor(song?: Song | null): string[] {return song?.stems?.length ? song.stems : DEFAULT_STEMS}+function panSong(videoId: string): Song {+ const song = loadSongs().find((s) => s.videoId === videoId)+ if (!song || typeof videoId !== 'string' || !/^[\w-]+$/.test(videoId)) {+ throw new Error('Unknown song')+ }+ return song+}++function normalizePan(song: Song, value: unknown): StemPanMap {+ const pans: StemPanMap = {}+ if (!value || typeof value !== 'object' || Array.isArray(value)) return pans+ const allowed = new Set<StemId>(['vocals', 'drums', 'bass', 'other', 'piano', 'guitar'])+ for (const id of stemsFor(song)) {+ if (!allowed.has(id as StemId)) continue+ const pan = (value as Record<string, unknown>)[id]+ if (typeof pan === 'number' && Number.isFinite(pan)) {+ pans[id as StemId] = Math.max(-1, Math.min(1, pan))+ }+ }+ return pans+}++export function loadStemPan(videoId: string): StemPanMap {+ const song = panSong(videoId)+ try {+ return normalizePan(song, JSON.parse(readFileSync(join(songDir(videoId), 'pan.json'), 'utf8')))+ } catch {+ return {}+ }+}++export function saveStemPan(videoId: string, pans: unknown): void {+ const song = panSong(videoId)+ // Do not recreate a deleted song directory for a late player save.+ writeFileSync(join(songDir(videoId), 'pan.json'), JSON.stringify(normalizePan(song, pans), null, 2))+}+export function stemsPresent(videoId: string, stems: string[]): boolean {const dir = stemsDir(videoId)if (!existsSync(dir)) return falsediff --git a/src/preload/index.ts b/src/preload/index.tsindex d456a56..b76e5d6 100644--- a/src/preload/index.ts+++ b/src/preload/index.ts@@ -16,6 +16,8 @@ const api: StemKitApi = {listSongs: () => ipcRenderer.invoke('library:list'),deleteSong: (videoId) => ipcRenderer.invoke('library:delete', videoId),getBuffers: (videoId) => ipcRenderer.invoke('song:buffers', videoId),+ getStemPan: (videoId) => ipcRenderer.invoke('song:pan:get', videoId),+ setStemPan: (videoId, pans) => ipcRenderer.invoke('song:pan:set', videoId, pans),exportStem: (videoId, stem) => ipcRenderer.invoke('stem:export', videoId, stem),exportAllStems: (videoId) => ipcRenderer.invoke('stems:export-all', videoId),searchYouTube: (query) => ipcRenderer.invoke('search:youtube', query),diff --git a/src/shared/types.ts b/src/shared/types.tsindex b446aed..7c9dd54 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -1,4 +1,5 @@export type StemId = 'vocals' | 'drums' | 'bass' | 'other' | 'piano' | 'guitar'+export type StemPanMap = Partial<Record<StemId, number>>// which GPU vendor GPU acceleration targets: NVIDIA via CUDA torch (windows// + linux), AMD via ROCm torch (linux only)@@ -122,6 +123,8 @@ export interface StemKitApi {listSongs(): Promise<Song[]>deleteSong(videoId: string): Promise<void>getBuffers(videoId: string): Promise<Record<string, Uint8Array>>+ getStemPan(videoId: string): Promise<StemPanMap>+ setStemPan(videoId: string, pans: StemPanMap): Promise<void>exportStem(videoId: string, stem: string): Promise<{ saved: boolean; path?: string }>exportAllStems(videoId: string): Promise<{ saved: boolean; path?: string; count?: number }>searchYouTube(query: string): Promise<SearchResult[]>