|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Fix existing WAV files by adding proper headers |
| 5 | + * Converts raw PCM data to valid WAV format |
| 6 | + */ |
| 7 | + |
| 8 | +import { readdirSync, readFileSync, writeFileSync, statSync, renameSync } from 'fs'; |
| 9 | +import { join, dirname } from 'path'; |
| 10 | +import { fileURLToPath } from 'url'; |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = dirname(__filename); |
| 14 | + |
| 15 | +const AUDIO_OUTPUT_DIR = join(__dirname, '../website/static/audio'); |
| 16 | + |
| 17 | +function createWavHeader(pcmDataLength) { |
| 18 | + const header = Buffer.alloc(44); |
| 19 | + |
| 20 | + // RIFF chunk descriptor |
| 21 | + header.write('RIFF', 0); |
| 22 | + header.writeUInt32LE(36 + pcmDataLength, 4); |
| 23 | + header.write('WAVE', 8); |
| 24 | + |
| 25 | + // fmt subchunk |
| 26 | + header.write('fmt ', 12); |
| 27 | + header.writeUInt32LE(16, 16); |
| 28 | + header.writeUInt16LE(1, 20); |
| 29 | + header.writeUInt16LE(1, 22); |
| 30 | + header.writeUInt32LE(24000, 24); |
| 31 | + header.writeUInt32LE(24000 * 1 * 2, 28); |
| 32 | + header.writeUInt16LE(1 * 2, 32); |
| 33 | + header.writeUInt16LE(16, 34); |
| 34 | + |
| 35 | + // data subchunk |
| 36 | + header.write('data', 36); |
| 37 | + header.writeUInt32LE(pcmDataLength, 40); |
| 38 | + |
| 39 | + return header; |
| 40 | +} |
| 41 | + |
| 42 | +function findWavFiles(dir) { |
| 43 | + const files = []; |
| 44 | + |
| 45 | + function traverse(currentDir) { |
| 46 | + const items = readdirSync(currentDir); |
| 47 | + |
| 48 | + for (const item of items) { |
| 49 | + const fullPath = join(currentDir, item); |
| 50 | + const stat = statSync(fullPath); |
| 51 | + |
| 52 | + if (stat.isDirectory()) { |
| 53 | + traverse(fullPath); |
| 54 | + } else if (item.endsWith('.wav')) { |
| 55 | + files.push(fullPath); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + traverse(dir); |
| 61 | + return files; |
| 62 | +} |
| 63 | + |
| 64 | +function isValidWav(buffer) { |
| 65 | + if (buffer.length < 12) return false; |
| 66 | + const header = buffer.slice(0, 12).toString('ascii', 0, 12); |
| 67 | + return header.startsWith('RIFF') && header.includes('WAVE'); |
| 68 | +} |
| 69 | + |
| 70 | +async function fixWavFile(filePath) { |
| 71 | + console.log(`\n📄 ${filePath}`); |
| 72 | + |
| 73 | + const buffer = readFileSync(filePath); |
| 74 | + |
| 75 | + // Check if already valid |
| 76 | + if (isValidWav(buffer)) { |
| 77 | + console.log(' ✅ Already valid WAV file - skipping'); |
| 78 | + return { status: 'skipped', path: filePath }; |
| 79 | + } |
| 80 | + |
| 81 | + console.log(' 🔧 Adding WAV header...'); |
| 82 | + |
| 83 | + // Buffer is raw PCM - add WAV header |
| 84 | + const wavHeader = createWavHeader(buffer.length); |
| 85 | + const wavBuffer = Buffer.concat([wavHeader, buffer]); |
| 86 | + |
| 87 | + // Backup original |
| 88 | + const backupPath = filePath + '.bak'; |
| 89 | + renameSync(filePath, backupPath); |
| 90 | + |
| 91 | + // Write fixed file |
| 92 | + writeFileSync(filePath, wavBuffer); |
| 93 | + |
| 94 | + // Verify |
| 95 | + const verifyBuffer = readFileSync(filePath); |
| 96 | + if (isValidWav(verifyBuffer)) { |
| 97 | + console.log(' ✅ Fixed successfully'); |
| 98 | + console.log(` Original: ${(buffer.length / 1024 / 1024).toFixed(2)} MB`); |
| 99 | + console.log(` Fixed: ${(wavBuffer.length / 1024 / 1024).toFixed(2)} MB`); |
| 100 | + console.log(` Backup: ${backupPath}`); |
| 101 | + return { status: 'fixed', path: filePath, backup: backupPath }; |
| 102 | + } else { |
| 103 | + console.log(' ❌ Fix failed - restoring backup'); |
| 104 | + renameSync(backupPath, filePath); |
| 105 | + return { status: 'failed', path: filePath }; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +async function main() { |
| 110 | + console.log('🔧 WAV File Repair Utility\n'); |
| 111 | + console.log(`📂 Audio directory: ${AUDIO_OUTPUT_DIR}\n`); |
| 112 | + |
| 113 | + const files = findWavFiles(AUDIO_OUTPUT_DIR); |
| 114 | + console.log(`Found ${files.length} WAV files\n`); |
| 115 | + |
| 116 | + if (files.length === 0) { |
| 117 | + console.log('No WAV files to process.'); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + console.log('='.repeat(60)); |
| 122 | + |
| 123 | + const results = { fixed: 0, skipped: 0, failed: 0 }; |
| 124 | + |
| 125 | + for (const file of files) { |
| 126 | + try { |
| 127 | + const result = await fixWavFile(file); |
| 128 | + results[result.status]++; |
| 129 | + } catch (error) { |
| 130 | + console.error(` ❌ Error: ${error.message}`); |
| 131 | + results.failed++; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + console.log('\n' + '='.repeat(60)); |
| 136 | + console.log('\n📊 Summary:'); |
| 137 | + console.log(` ✅ Fixed: ${results.fixed}`); |
| 138 | + console.log(` ⏭️ Skipped (already valid): ${results.skipped}`); |
| 139 | + console.log(` ❌ Failed: ${results.failed}`); |
| 140 | + |
| 141 | + if (results.fixed > 0) { |
| 142 | + console.log('\n💡 Tip: .bak files can be deleted after verifying audio playback'); |
| 143 | + } |
| 144 | + |
| 145 | + console.log('\n✨ Done!\n'); |
| 146 | +} |
| 147 | + |
| 148 | +main().catch(error => { |
| 149 | + console.error('\n💥 Fatal error:', error); |
| 150 | + process.exit(1); |
| 151 | +}); |
0 commit comments