Skip to content

Split any YouTube song into stems

TypeScript

feat: add export selection and naming rules

David Navia586c07cbbf1c
diff --git a/scripts/run.cjs b/scripts/run.cjs
index d382814..097b71d 100644
--- a/scripts/run.cjs
+++ b/scripts/run.cjs
@@ -73,7 +73,7 @@ function findNode(requiredMajor) {
}
const STEPS = {
- 'test:audio': [['node', '--experimental-strip-types', '--test', 'tests/engine.test.mjs', 'tests/pan-storage.test.mjs', 'tests/keys.test.mjs', 'tests/key-storage.test.mjs', 'tests/pitch.test.mjs', 'tests/pitch-storage.test.mjs', 'tests/export-audio.test.mjs']],
+ 'test:audio': [['node', '--experimental-strip-types', '--test', 'tests/engine.test.mjs', 'tests/pan-storage.test.mjs', 'tests/keys.test.mjs', 'tests/key-storage.test.mjs', 'tests/pitch.test.mjs', 'tests/pitch-storage.test.mjs', 'tests/export-audio.test.mjs', 'tests/export-rules.test.mjs']],
// STEMKIT_PY points at an interpreter that has numpy (e.g. the dev venv)
'test:py': [[process.env.STEMKIT_PY || (IS_WIN ? 'python' : 'python3'), '-m', 'unittest', 'discover', '-s', 'python/tests', '-v']],
dev: [['electron-vite', 'dev']],
diff --git a/src/shared/export-rules.ts b/src/shared/export-rules.ts
new file mode 100644
index 0000000..145dc1b
--- /dev/null
+++ b/src/shared/export-rules.ts
@@ -0,0 +1,50 @@
+// Pure rules for the Export dialog: what starts checked, when export is allowed, and how files are named.
+// Title sanitizing for the filesystem happens in the main process; these only build names.
+
+export const MIX_ID = 'mix'
+
+export type ExportMode = 'mixed' | 'separate'
+
+// Mirrors Player's audibility rule so the dialog opens with what the user currently hears.
+export function initialSelection(
+ stems: string[],
+ mutes: Set<string>,
+ solos: Set<string>
+): Set<string> {
+ return new Set(
+ stems.filter(
+ (id) => id !== MIX_ID && !mutes.has(id) && (solos.size === 0 || solos.has(id))
+ )
+ )
+}
+
+export function canExport(selected: Set<string>, mode: ExportMode): boolean {
+ // a mixed file is built from stems only, so the original mix does not count there
+ if (mode === 'mixed') return [...selected].some((id) => id !== MIX_ID)
+ return selected.size > 0
+}
+
+// Deliberately not formatShift: that one uses U+2212 for display, filenames need plain ASCII.
+function shiftSuffix(semitones: number): string {
+ if (semitones === 0) return ''
+ return ` (${semitones > 0 ? '+' : '-'}${Math.abs(semitones)})`
+}
+
+export function mixedBaseName(
+ title: string,
+ selected: string[],
+ allStems: string[],
+ semitones: number
+): string {
+ const chosen = allStems.filter((id) => selected.includes(id))
+ const isInstrumental =
+ allStems.includes('vocals') &&
+ chosen.length === allStems.length - 1 &&
+ !chosen.includes('vocals')
+ const part = isInstrumental ? 'instrumental' : chosen.join('+')
+ return `${title} - ${part}${shiftSuffix(semitones)}`
+}
+
+export function separateFolderName(title: string, semitones: number): string {
+ return `${title}${shiftSuffix(semitones)}`
+}
diff --git a/tests/export-rules.test.mjs b/tests/export-rules.test.mjs
new file mode 100644
index 0000000..40bbb63
--- /dev/null
+++ b/tests/export-rules.test.mjs
@@ -0,0 +1,52 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import {
+ MIX_ID,
+ initialSelection,
+ canExport,
+ mixedBaseName,
+ separateFolderName
+} from '../src/shared/export-rules.ts'
+
+const ALL = ['vocals', 'drums', 'bass', 'other']
+
+test('initialSelection takes the soloed stems when there are solos', () => {
+ const sel = initialSelection(ALL, new Set(), new Set(['drums', 'bass', 'other']))
+ assert.deepEqual([...sel].sort(), ['bass', 'drums', 'other'])
+})
+
+test('initialSelection drops muted stems when nothing is soloed', () => {
+ const sel = initialSelection(ALL, new Set(['bass']), new Set())
+ assert.deepEqual([...sel].sort(), ['drums', 'other', 'vocals'])
+})
+
+test('initialSelection never includes the mix id', () => {
+ assert.equal(initialSelection([...ALL, MIX_ID], new Set(), new Set()).has(MIX_ID), false)
+})
+
+test('canExport needs at least one stem; the mix only counts when separate', () => {
+ assert.equal(canExport(new Set(), 'mixed'), false)
+ assert.equal(canExport(new Set([MIX_ID]), 'mixed'), false)
+ assert.equal(canExport(new Set([MIX_ID]), 'separate'), true)
+ assert.equal(canExport(new Set(['drums']), 'mixed'), true)
+})
+
+test('mixedBaseName calls everything-but-vocals "instrumental"', () => {
+ assert.equal(mixedBaseName('Song', ['drums', 'bass', 'other'], ALL, 0), 'Song - instrumental')
+ assert.equal(mixedBaseName('Song', ['drums', 'bass', 'other'], ALL, -3), 'Song - instrumental (-3)')
+})
+
+test('mixedBaseName lists stems in allStems order with ASCII signs', () => {
+ assert.equal(mixedBaseName('Song', ['bass', 'drums'], ALL, 0), 'Song - drums+bass')
+ assert.equal(mixedBaseName('Song', ['bass', 'drums'], ALL, 2), 'Song - drums+bass (+2)')
+})
+
+test('mixedBaseName does not say instrumental without a vocals stem', () => {
+ assert.equal(mixedBaseName('Song', ['drums', 'bass'], ['drums', 'bass'], 0), 'Song - drums+bass')
+})
+
+test('separateFolderName appends the shift only when non-zero', () => {
+ assert.equal(separateFolderName('Song', 0), 'Song')
+ assert.equal(separateFolderName('Song', 2), 'Song (+2)')
+ assert.equal(separateFolderName('Song', -3), 'Song (-3)')
+})