diff options
Diffstat (limited to 'tools/timeline_editor')
| -rw-r--r-- | tools/timeline_editor/timeline-playback.js | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/tools/timeline_editor/timeline-playback.js b/tools/timeline_editor/timeline-playback.js index 1bcdcd0..bfeb75a 100644 --- a/tools/timeline_editor/timeline-playback.js +++ b/tools/timeline_editor/timeline-playback.js @@ -67,17 +67,36 @@ export class PlaybackController { async loadAudioFile(file) { try { const arrayBuffer = await file.arrayBuffer(); + + // Detect original WAV sample rate before decoding + const dataView = new DataView(arrayBuffer); + let originalSampleRate = 32000; // Default assumption + + // Parse WAV header to get original sample rate + // "RIFF" at 0, "WAVE" at 8, "fmt " at 12, sample rate at 24 + if (dataView.getUint32(0, false) === 0x52494646 && // "RIFF" + dataView.getUint32(8, false) === 0x57415645) { // "WAVE" + originalSampleRate = dataView.getUint32(24, true); // Little-endian + console.log(`Detected WAV sample rate: ${originalSampleRate}Hz`); + } + if (!this.state.audioContext) { this.state.audioContext = new (window.AudioContext || window.webkitAudioContext)(); } + this.state.audioBuffer = await this.state.audioContext.decodeAudioData(arrayBuffer); this.state.audioDuration = this.state.audioBuffer.duration; + this.state.originalSampleRate = originalSampleRate; + this.state.resampleRatio = this.state.audioContext.sampleRate / originalSampleRate; + + console.log(`AudioContext rate: ${this.state.audioContext.sampleRate}Hz, resample ratio: ${this.state.resampleRatio.toFixed(3)}x`); + this.renderWaveform(); this.dom.playbackControls.style.display = 'flex'; this.dom.playbackIndicator.style.display = 'block'; this.dom.clearAudioBtn.disabled = false; this.dom.replayBtn.disabled = false; - this.showMessage(`Audio loaded: ${this.state.audioDuration.toFixed(2)}s`, 'success'); + this.showMessage(`Audio loaded: ${this.state.audioDuration.toFixed(2)}s @ ${originalSampleRate}Hz`, 'success'); this.renderCallback('audioLoaded'); } catch (err) { this.showMessage(`Error loading audio: ${err.message}`, 'error'); |
