Skip to content

Split any YouTube song into stems

TypeScript

feat: wrap signalsmith-stretch as a pitch shifter

David Navia61179cf764e5
diff --git a/package-lock.json b/package-lock.json
index df53bcb..418932c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,7 +8,8 @@
"name": "stemkit",
"version": "0.2.0",
"dependencies": {
- "electron-updater": "^6.8.9"
+ "electron-updater": "^6.8.9",
+ "signalsmith-stretch": "1.3.2"
},
"devDependencies": {
"@tailwindcss/vite": "^4.0.6",
@@ -6969,6 +6970,12 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/signalsmith-stretch": {
+ "version": "1.3.2",
+ "resolved": "https://registry.npmjs.org/signalsmith-stretch/-/signalsmith-stretch-1.3.2.tgz",
+ "integrity": "sha512-tJqRbwPCoWLSHXwO29UQ75u72IwPsHns3RG+TKzuOAp7OduJiJMzEtz32JEFbPFQcTR7aiKCIVc+/Kzw8bMZUw==",
+ "license": "MIT"
+ },
"node_modules/simple-update-notifier": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz",
diff --git a/package.json b/package.json
index 7f38a34..287f272 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,8 @@
"dist:all": "node scripts/run.cjs dist:all"
},
"dependencies": {
- "electron-updater": "^6.8.9"
+ "electron-updater": "^6.8.9",
+ "signalsmith-stretch": "1.3.2"
},
"devDependencies": {
"@tailwindcss/vite": "^4.0.6",
diff --git a/scripts/run.cjs b/scripts/run.cjs
index 9a3e0e4..6f15a64 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']],
+ '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']],
// 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/renderer/index.html b/src/renderer/index.html
index 3a5cf6e..cf063ec 100644
--- a/src/renderer/index.html
+++ b/src/renderer/index.html
@@ -5,7 +5,7 @@
<title>StemKit</title>
<meta
http-equiv="Content-Security-Policy"
- content="default-src 'self'; script-src 'self' https://www.youtube.com https://s.ytimg.com 'unsafe-inline'; frame-src https://www.youtube.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://i.ytimg.com https://*.ytimg.com; connect-src 'self'; media-src 'self' blob:"
+ content="default-src 'self'; script-src 'self' blob: 'wasm-unsafe-eval' https://www.youtube.com https://s.ytimg.com 'unsafe-inline'; frame-src https://www.youtube.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://i.ytimg.com https://*.ytimg.com; connect-src 'self'; media-src 'self' blob:"
/>
</head>
<body>
diff --git a/src/renderer/src/lib/pitch.ts b/src/renderer/src/lib/pitch.ts
new file mode 100644
index 0000000..fd33fe7
--- /dev/null
+++ b/src/renderer/src/lib/pitch.ts
@@ -0,0 +1,45 @@
+import SignalsmithStretch from 'signalsmith-stretch'
+import { clampPitch } from '../../../shared/pitch-range'
+
+export { PITCH_MIN, PITCH_MAX, clampPitch } from '../../../shared/pitch-range'
+
+export interface PitchShifter {
+ input: AudioNode
+ output: AudioNode
+ /** Seconds the shifter delays the signal. */
+ latency: number
+ setSemitones(n: number): void
+ dispose(): void
+}
+
+export type ShifterFactory = (ctx: BaseAudioContext) => Promise<PitchShifter>
+
+const LOAD_TIMEOUT_MS = 5000
+
+export const createPitchShifter: ShifterFactory = async (ctx) => {
+ // If WASM is blocked (e.g. CSP) the factory never settles, so bound the wait
+ let timer: ReturnType<typeof setTimeout> | undefined
+ const timeout = new Promise<never>((_, reject) => {
+ timer = setTimeout(() => reject(new Error('Pitch shifter failed to load')), LOAD_TIMEOUT_MS)
+ })
+ let node: Awaited<ReturnType<typeof SignalsmithStretch>>
+ try {
+ node = await Promise.race([SignalsmithStretch(ctx), timeout])
+ } finally {
+ clearTimeout(timer)
+ }
+ const latency = await node.latency()
+ // Without active: true the node outputs silence
+ const apply = (n: number): void =>
+ node.schedule({ active: true, semitones: clampPitch(n), formantCompensation: true })
+ return {
+ input: node,
+ output: node,
+ latency,
+ setSemitones: apply,
+ dispose() {
+ node.schedule({ active: false })
+ node.disconnect()
+ }
+ }
+}
diff --git a/src/renderer/src/lib/signalsmith-stretch.d.ts b/src/renderer/src/lib/signalsmith-stretch.d.ts
new file mode 100644
index 0000000..2690eb2
--- /dev/null
+++ b/src/renderer/src/lib/signalsmith-stretch.d.ts
@@ -0,0 +1,8 @@
+// Minimal types for only what pitch.ts uses
+declare module 'signalsmith-stretch' {
+ export interface StretchNode extends AudioNode {
+ schedule(change: { active?: boolean; semitones?: number; formantCompensation?: boolean }): void
+ latency(): Promise<number>
+ }
+ export default function SignalsmithStretch(ctx: BaseAudioContext): Promise<StretchNode>
+}
diff --git a/src/shared/pitch-range.ts b/src/shared/pitch-range.ts
new file mode 100644
index 0000000..dc1e053
--- /dev/null
+++ b/src/shared/pitch-range.ts
@@ -0,0 +1,9 @@
+// Shared so the main process can clamp stored values without importing renderer code
+export const PITCH_MIN = -6
+export const PITCH_MAX = 6
+
+export function clampPitch(n: unknown): number {
+ if (typeof n !== 'number' || !Number.isFinite(n)) return 0
+ // `+ 0` turns -0 (from rounding e.g. -0.4) into 0
+ return Math.min(PITCH_MAX, Math.max(PITCH_MIN, Math.round(n))) + 0
+}
diff --git a/tests/pitch.test.mjs b/tests/pitch.test.mjs
new file mode 100644
index 0000000..f433036
--- /dev/null
+++ b/tests/pitch.test.mjs
@@ -0,0 +1,26 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { PITCH_MIN, PITCH_MAX, clampPitch } from '../src/shared/pitch-range.ts'
+
+test('pitch range is ±6 semitones', () => {
+ assert.equal(PITCH_MIN, -6)
+ assert.equal(PITCH_MAX, 6)
+})
+
+test('clampPitch clamps to the range', () => {
+ assert.equal(clampPitch(-7), -6)
+ assert.equal(clampPitch(99), 6)
+})
+
+test('clampPitch rounds to an integer', () => {
+ assert.equal(clampPitch(2.6), 3)
+ assert.equal(clampPitch(-0.4), 0)
+ assert.ok(Object.is(clampPitch(-0.4), 0))
+})
+
+test('clampPitch maps non-numeric and non-finite input to 0', () => {
+ assert.equal(clampPitch('x'), 0)
+ assert.equal(clampPitch(NaN), 0)
+ assert.equal(clampPitch(Infinity), 0)
+ assert.equal(clampPitch(undefined), 0)
+})