diff --git a/js/api.js b/js/api.js index 0649f26..ce18fdc 100644 --- a/js/api.js +++ b/js/api.js @@ -1113,7 +1113,7 @@ export class LosslessAPI { try { // MP3_320 is not a native TIDAL quality, we download LOSSLESS and convert const downloadQuality = quality === 'MP3_320' ? 'LOSSLESS' : quality; - + const lookup = await this.getTrack(id, downloadQuality); let streamUrl; let blob; diff --git a/js/id3-writer.js b/js/id3-writer.js index ffaf394..7e4977b 100644 --- a/js/id3-writer.js +++ b/js/id3-writer.js @@ -53,13 +53,13 @@ function createTextFrame(frameId, text) { // ID3v2.3 UTF-16 encoding with BOM const bom = new Uint8Array([0xff, 0xfe]); // UTF-16LE BOM const utf16Bytes = new Uint8Array(text.length * 2); - + for (let i = 0; i < text.length; i++) { const charCode = text.charCodeAt(i); utf16Bytes[i * 2] = charCode & 0xff; utf16Bytes[i * 2 + 1] = (charCode >> 8) & 0xff; } - + const frameSize = 1 + bom.length + utf16Bytes.length; const frame = new Uint8Array(10 + frameSize); const view = new DataView(frame.buffer); diff --git a/js/metadata.js b/js/metadata.js index d83b4d6..a20aa72 100644 --- a/js/metadata.js +++ b/js/metadata.js @@ -53,9 +53,9 @@ export async function addMetadataToAudio(audioBlob, track, api, _quality) { // DASH Hi-Res streams may return fragmented MP4 instead of raw FLAC const buffer = await audioBlob.slice(0, 12).arrayBuffer(); const view = new DataView(buffer); - + const format = detectAudioFormat(view, audioBlob.type); - + switch (format) { case 'flac': return await addFlacMetadata(audioBlob, track, api); diff --git a/js/mp3-encoder.js b/js/mp3-encoder.js index d5549d6..114f827 100644 --- a/js/mp3-encoder.js +++ b/js/mp3-encoder.js @@ -8,16 +8,16 @@ class MP3EncodingError extends Error { async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) { const audioData = await audioBlob.arrayBuffer(); - + return new Promise((resolve, reject) => { const worker = new Worker(new URL('./mp3-encoder.worker.js', import.meta.url), { type: 'module' }); - + // Handle abort signal const abortHandler = () => { worker.terminate(); reject(new MP3EncodingError('MP3 encoding aborted')); }; - + if (signal) { if (signal.aborted) { abortHandler(); @@ -25,10 +25,10 @@ async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) { } signal.addEventListener('abort', abortHandler); } - + worker.onmessage = (e) => { const { type, blob, message, stage, progress } = e.data; - + if (type === 'complete') { if (signal) signal.removeEventListener('abort', abortHandler); worker.terminate(); @@ -43,17 +43,20 @@ async function encodeToMp3Worker(audioBlob, onProgress = null, signal = null) { console.log('[FFmpeg]', message); } }; - + worker.onerror = (error) => { if (signal) signal.removeEventListener('abort', abortHandler); worker.terminate(); reject(new MP3EncodingError('Worker failed: ' + error.message)); }; - + // Transfer audio data to worker - worker.postMessage({ - audioData - }, [audioData]); + worker.postMessage( + { + audioData, + }, + [audioData] + ); }); } @@ -63,7 +66,7 @@ export async function encodeToMp3(audioBlob, onProgress = null, signal = null) { if (typeof Worker !== 'undefined') { return await encodeToMp3Worker(audioBlob, onProgress, signal); } - + throw new MP3EncodingError('Web Workers are required for MP3 encoding'); } catch (error) { console.error('MP3 encoding failed:', error); diff --git a/js/mp3-encoder.worker.js b/js/mp3-encoder.worker.js index dbe10cb..2a2176c 100644 --- a/js/mp3-encoder.worker.js +++ b/js/mp3-encoder.worker.js @@ -6,63 +6,68 @@ let loadingPromise = null; async function loadFFmpeg() { if (loadingPromise) return loadingPromise; - + loadingPromise = (async () => { ffmpeg = new FFmpeg(); - + ffmpeg.on('log', ({ message }) => { self.postMessage({ type: 'log', message }); }); - + ffmpeg.on('progress', ({ progress, time }) => { - self.postMessage({ - type: 'progress', - stage: 'encoding', + self.postMessage({ + type: 'progress', + stage: 'encoding', progress: progress * 100, - time + time, }); }); - + self.postMessage({ type: 'progress', stage: 'loading', message: 'Loading FFmpeg...' }); - + const baseURL = 'https://unpkg.com/@ffmpeg/core@0.12.6/dist/esm'; await ffmpeg.load({ coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, 'text/javascript'), - wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm') + wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, 'application/wasm'), }); })(); - + return loadingPromise; } self.onmessage = async (e) => { const { audioData } = e.data; - + try { await loadFFmpeg(); - + self.postMessage({ type: 'progress', stage: 'encoding', message: 'Encoding to MP3 320kbps...' }); - + try { // Write input file to FFmpeg virtual filesystem await ffmpeg.writeFile('input', new Uint8Array(audioData)); - + // Encode to MP3 with 320kbps CBR, strip source metadata to avoid duplicate ID3 tags await ffmpeg.exec([ - '-i', 'input', - '-map_metadata', '-1', - '-c:a', 'libmp3lame', - '-b:a', '320k', - '-ar', '44100', - 'output.mp3' + '-i', + 'input', + '-map_metadata', + '-1', + '-c:a', + 'libmp3lame', + '-b:a', + '320k', + '-ar', + '44100', + 'output.mp3', ]); - + self.postMessage({ type: 'progress', stage: 'finalizing', message: 'Finalizing MP3...' }); - + // Read output file - use Uint8Array directly to avoid extra bytes from ArrayBuffer const data = await ffmpeg.readFile('output.mp3'); const mp3Blob = new Blob([data], { type: 'audio/mpeg' }); - + self.postMessage({ type: 'complete', blob: mp3Blob }); } finally { // Always cleanup virtual filesystem files diff --git a/js/utils.js b/js/utils.js index 2d4faca..9bfbda1 100644 --- a/js/utils.js +++ b/js/utils.js @@ -150,12 +150,12 @@ export const detectAudioFormat = (view, mimeType = '') => { export const getExtensionFromBlob = async (blob) => { const buffer = await blob.slice(0, 12).arrayBuffer(); const view = new DataView(buffer); - + const format = detectAudioFormat(view, blob.type); - + if (format === 'mp4') return 'm4a'; if (format) return format; - + // Default fallback return 'flac'; };