Spaces:
Paused
Paused
File size: 10,054 Bytes
2276d59 62527b4 2276d59 62527b4 2276d59 62527b4 2276d59 62527b4 2276d59 62527b4 2276d59 62527b4 2276d59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | import multer from 'multer';
import { logger } from '../utils/infra/logger.util.js';
import ffmpeg from 'fluent-ffmpeg';
import { dirname, join, extname, basename } from 'node:path';
import { resolveWithin } from '../utils/network/security.util.js';
import { existsSync, mkdirSync, readFileSync, unlink } from 'node:fs';
import wav from 'wav-decoder';
import { fileURLToPath } from 'node:url';
import { Request, Response } from 'express';
import { randomBytes } from 'node:crypto';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const TEMP_DIR = join(__dirname, '../../temp');
const uploadDir = join(TEMP_DIR, 'uploads');
const processedDir = join(TEMP_DIR, 'processed');
import { ChordsResult } from '../types/index.js';
interface EssentiaVector {
delete: () => void;
}
interface EssentiaPCVVector extends EssentiaVector {
push_back: (val: EssentiaVector) => void;
}
interface EssentiaInstance {
arrayToVector: (arr: Float32Array) => EssentiaVector;
vectorToArray: (vec: EssentiaVector) => string[] | number[];
KeyExtractor: (vec: EssentiaVector) => { key: string; scale: string };
Windowing: (vec: EssentiaVector) => { frame: EssentiaVector };
Spectrum: (vec: EssentiaVector) => { spectrum: EssentiaVector };
SpectralPeaks: (vec: EssentiaVector) => {
frequencies: EssentiaVector;
magnitudes: EssentiaVector;
};
HPCP: (
freqs: EssentiaVector,
mags: EssentiaVector
) => { hpcp: EssentiaVector };
ChordsDetection: (vec: EssentiaPCVVector) => { chords: EssentiaVector };
module: {
VectorVectorFloat: new () => EssentiaPCVVector;
};
}
let essentia: EssentiaInstance | null = null;
async function getEssentia(): Promise<EssentiaInstance | null> {
if (essentia) return essentia;
try {
const { default: EssentiaModule } =
await (import('essentia.js') as unknown as {
default: {
Essentia: new (wasm: unknown) => EssentiaInstance;
EssentiaWASM: unknown;
};
});
essentia = new EssentiaModule.Essentia(EssentiaModule.EssentiaWASM);
return essentia;
} catch (error: unknown) {
logger.error('❌ Essentia WASM failed', (error as Error).message);
return null;
}
}
[uploadDir, processedDir].forEach((dir) => {
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
});
const storage = multer.diskStorage({
destination: (_req, _file, callback) => {
callback(null, uploadDir);
},
filename: (_req, file, callback) => {
const safeName = file.originalname.replace(/[^a-zA-Z0-9.-]/gu, '_');
callback(null, `${Date.now()}-${safeName}`);
},
});
export const upload = multer({
storage,
limits: { fileSize: 100 * 1024 * 1024 }, // max 100mb
});
const keyMap: Record<string, number> = {
C: 0,
'C#': 1,
Db: 1,
D: 2,
'D#': 3,
Eb: 3,
E: 4,
F: 5,
'F#': 6,
Gb: 6,
G: 7,
'G#': 8,
Ab: 8,
A: 9,
'A#': 10,
Bb: 10,
B: 11,
};
const _generateChords = (
essentiaInstance: EssentiaInstance,
signal: Float32Array
) => {
const frameSize = 4096;
const hopSize = 2048;
const pcpVector = new essentiaInstance.module.VectorVectorFloat();
for (let i = 0; i < Math.min(signal.length, 44100 * 30); i += hopSize) {
if (i + frameSize > signal.length) break;
const frame = signal.slice(i, i + frameSize);
const frameVec = essentiaInstance.arrayToVector(frame);
const windowed = essentiaInstance.Windowing(frameVec).frame;
const spectrum = essentiaInstance.Spectrum(windowed).spectrum;
const peaks = essentiaInstance.SpectralPeaks(spectrum);
const hpcp = essentiaInstance.HPCP(
peaks.frequencies,
peaks.magnitudes
).hpcp;
pcpVector.push_back(hpcp);
frameVec.delete();
}
const chordsResult = essentiaInstance.ChordsDetection(pcpVector);
const uniqueChords: string[] = [];
if (chordsResult?.chords) {
const chordsArray = essentiaInstance.vectorToArray(
chordsResult.chords
) as string[];
chordsArray.forEach((chord: string) => {
if (uniqueChords[uniqueChords.length - 1] !== chord) {
uniqueChords.push(chord);
}
});
}
pcpVector.delete();
return uniqueChords;
};
const _handleAudioDecoding = (
essentiaInstance: EssentiaInstance,
buffer: Buffer,
tempWavPath: string,
resolve: (value: ChordsResult | PromiseLike<ChordsResult>) => void,
reject: (reason?: unknown) => void
) => {
wav
.decode(buffer)
.then((audioData: { channelData: Float32Array[] }) => {
const signal = audioData.channelData[0];
const audioVector = essentiaInstance.arrayToVector(signal);
const keyResult = essentiaInstance.KeyExtractor(audioVector);
const uniqueChords = _generateChords(essentiaInstance, signal);
audioVector.delete();
unlink(tempWavPath, (_error) => {});
resolve({
key: keyResult.key,
scale: keyResult.scale,
chords: uniqueChords.filter((chord) => chord !== 'N').slice(0, 12),
});
})
.catch((error: Error) => {
unlink(tempWavPath, (_error) => {});
reject(error);
});
};
const detectKeyFromFile = async (filePath: string): Promise<ChordsResult> => {
const essentiaInstance = await getEssentia();
if (!essentiaInstance) throw new Error('Essentia engine not available');
return new Promise((resolve, reject) => {
const randomId = randomBytes(4).toString('hex');
const tempWavPath = join(uploadDir, `temp-${Date.now()}-${randomId}.wav`);
ffmpeg(filePath)
.toFormat('wav')
.audioChannels(1)
.audioFrequency(44100)
.on('end', () => {
const buffer = readFileSync(tempWavPath);
_handleAudioDecoding(
essentiaInstance,
buffer,
tempWavPath,
resolve,
reject
);
})
.on('error', (error: Error) => {
unlink(tempWavPath, (_error) => {});
reject(error);
})
.save(tempWavPath);
});
};
export const detectKey = async (req: Request, res: Response): Promise<void> => {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
try {
const result = await detectKeyFromFile(req.file.path);
res.json(result);
} catch (error) {
logger.error('[KeyChanger] Detection Error:', error);
res.status(500).json({ error: 'Audio analysis failed' });
} finally {
unlink(req.file.path, (_error) => {});
}
};
export const detectProcessedKey = async (
req: Request,
res: Response
): Promise<void> => {
const filename = String(req.params.filename);
const filePath = resolveWithin(processedDir, filename);
if (!filePath) {
res.status(400).json({ error: 'Invalid path' });
return;
}
if (!existsSync(filePath)) {
res.status(404).json({ error: 'File not found' });
return;
}
try {
const result = await detectKeyFromFile(filePath);
res.json(result);
} catch (error) {
logger.error('[KeyChanger] Processed Detection Error:', error);
res.status(500).json({ error: 'Analysis failed' });
}
};
export const convertKey = (req: Request, res: Response): void => {
if (!req.file) {
res.status(400).json({ error: 'No file uploaded' });
return;
}
const { originalKey, targetKey } = req.body;
if (
!originalKey ||
!targetKey ||
keyMap[originalKey] === undefined ||
keyMap[targetKey] === undefined
) {
unlink(req.file.path, (_error) => {});
res.status(400).json({ error: 'Invalid keys provided' });
return;
}
const originalVal = keyMap[originalKey];
const targetVal = keyMap[targetKey];
let semitones = targetVal - originalVal;
if (semitones > 6) semitones -= 12;
if (semitones < -6) semitones += 12;
const pitchScale = Math.pow(2, semitones / 12);
const inputPath = req.file.path;
const extension = extname(req.file.originalname);
const baseNamePart = basename(req.file.originalname, extension).replace(
/[^a-zA-Z0-9\s.-]/gu,
'_'
);
const outputFilename = `${Date.now()}__${targetKey}__${baseNamePart}${extension}`;
const outputPath = join(processedDir, outputFilename);
ffmpeg(inputPath)
.audioFilters(`rubberband=pitch=${pitchScale}`)
.on('end', () => {
const protocol =
(req.headers['x-forwarded-proto'] as string) || req.protocol;
const host = req.headers.host;
res.json({
success: true,
filename: outputFilename,
downloadUrl: `${protocol}://${host}/api/key-changer/download/${outputFilename}`,
});
unlink(inputPath, (_error) => {});
})
.on('error', (error: unknown) => {
const errorObj = error as Error;
logger.error('[KeyChanger] Conversion Error:', errorObj.message);
if (!res.headersSent)
res
.status(500)
.json({ error: 'Conversion failed.', details: errorObj.message });
unlink(inputPath, (_error) => {});
})
.save(outputPath);
};
export const downloadFile = (req: Request, res: Response): void => {
const filename = String(req.params.filename);
const filePath = resolveWithin(processedDir, filename);
if (!filePath) {
res.status(400).json({ error: 'Invalid path' });
return;
}
if (existsSync(filePath)) {
let prettyName = filename;
const parts = filename.split('__');
if (parts.length >= 3) {
const key = parts[1];
const nameWithExt = parts.slice(2).join('__');
const extension = extname(nameWithExt);
const baseNamePart = basename(nameWithExt, extension);
const cleanName = baseNamePart.replace(/_+/gu, ' ').trim();
prettyName = `(${key}) ${cleanName}${extension}`;
}
res.download(filePath, prettyName, (error) => {
if (error) {
const errorWithCode = error as Error & { code?: string };
if (
errorWithCode.code === 'ECONNABORTED' ||
errorWithCode.code === 'EPIPE'
) {
return;
}
logger.error('[KeyChanger] Download Error:', error);
}
});
} else {
res.status(404).json({ error: 'File not found' });
}
};
|