feat: implement Phase 2 - Web Audio API engine and waveform visualization
Phase 2 Complete Features: - Web Audio API context management with browser compatibility - Audio file upload with drag-and-drop support - Audio decoding for multiple formats (WAV, MP3, OGG, FLAC, AAC, M4A) - AudioPlayer class with full playback control - Waveform visualization using Canvas API - Real-time waveform rendering with progress indicator - Playback controls (play, pause, stop, seek) - Volume control with mute/unmute - Timeline scrubbing - Audio file information display Components: - AudioEditor: Main editor container - FileUpload: Drag-and-drop file upload component - AudioInfo: Display audio file metadata - Waveform: Canvas-based waveform visualization - PlaybackControls: Transport controls with volume slider Audio Engine: - lib/audio/context.ts: AudioContext management - lib/audio/decoder.ts: Audio file decoding utilities - lib/audio/player.ts: AudioPlayer class for playback - lib/waveform/peaks.ts: Waveform peak generation Hooks: - useAudioPlayer: Complete audio player state management Types: - types/audio.ts: TypeScript definitions for audio types Features Working: ✓ Load audio files via drag-and-drop or file picker ✓ Display waveform with real-time progress ✓ Play/pause/stop controls ✓ Seek by clicking on waveform or using timeline slider ✓ Volume control with visual feedback ✓ Audio file metadata display (duration, sample rate, channels) ✓ Toast notifications for user feedback ✓ SSR-safe audio context initialization ✓ Dark/light theme support Tech Stack: - Web Audio API for playback - Canvas API for waveform rendering - React 19 hooks for state management - TypeScript for type safety Build verified and working ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
67
lib/audio/context.ts
Normal file
67
lib/audio/context.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Web Audio API context management
|
||||
*/
|
||||
|
||||
let audioContext: AudioContext | null = null;
|
||||
|
||||
/**
|
||||
* Get or create the global AudioContext
|
||||
*/
|
||||
export function getAudioContext(): AudioContext {
|
||||
if (typeof window === 'undefined') {
|
||||
throw new Error('AudioContext is only available in the browser');
|
||||
}
|
||||
|
||||
if (!audioContext) {
|
||||
// Create AudioContext with fallback for older browsers
|
||||
const AudioContextClass = window.AudioContext || (window as any).webkitAudioContext;
|
||||
|
||||
if (!AudioContextClass) {
|
||||
throw new Error('Web Audio API is not supported in this browser');
|
||||
}
|
||||
|
||||
audioContext = new AudioContextClass();
|
||||
}
|
||||
|
||||
// Resume context if it's suspended (required by browser autoplay policies)
|
||||
if (audioContext.state === 'suspended') {
|
||||
audioContext.resume();
|
||||
}
|
||||
|
||||
return audioContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the AudioContext
|
||||
*/
|
||||
export async function closeAudioContext(): Promise<void> {
|
||||
if (audioContext) {
|
||||
await audioContext.close();
|
||||
audioContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current AudioContext state
|
||||
*/
|
||||
export function getAudioContextState(): AudioContextState | null {
|
||||
return audioContext?.state ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume the AudioContext (required after user interaction in some browsers)
|
||||
*/
|
||||
export async function resumeAudioContext(): Promise<void> {
|
||||
if (audioContext && audioContext.state === 'suspended') {
|
||||
await audioContext.resume();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Suspend the AudioContext
|
||||
*/
|
||||
export async function suspendAudioContext(): Promise<void> {
|
||||
if (audioContext && audioContext.state === 'running') {
|
||||
await audioContext.suspend();
|
||||
}
|
||||
}
|
||||
79
lib/audio/decoder.ts
Normal file
79
lib/audio/decoder.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Audio file decoding utilities
|
||||
*/
|
||||
|
||||
import { getAudioContext } from './context';
|
||||
|
||||
/**
|
||||
* Decode an audio file to AudioBuffer
|
||||
*/
|
||||
export async function decodeAudioFile(file: File): Promise<AudioBuffer> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const audioContext = getAudioContext();
|
||||
|
||||
try {
|
||||
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
||||
return audioBuffer;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to decode audio file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audio file metadata without decoding the entire file
|
||||
*/
|
||||
export async function getAudioFileMetadata(file: File): Promise<{
|
||||
name: string;
|
||||
size: number;
|
||||
type: string;
|
||||
}> {
|
||||
return {
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
type: file.type,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file is a supported audio format
|
||||
*/
|
||||
export function isSupportedAudioFormat(file: File): boolean {
|
||||
const supportedFormats = [
|
||||
'audio/wav',
|
||||
'audio/wave',
|
||||
'audio/x-wav',
|
||||
'audio/mpeg',
|
||||
'audio/mp3',
|
||||
'audio/ogg',
|
||||
'audio/webm',
|
||||
'audio/flac',
|
||||
'audio/aac',
|
||||
'audio/m4a',
|
||||
'audio/x-m4a',
|
||||
];
|
||||
|
||||
return supportedFormats.includes(file.type) ||
|
||||
/\.(wav|mp3|ogg|webm|flac|aac|m4a)$/i.test(file.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format duration in seconds to MM:SS format
|
||||
*/
|
||||
export function formatDuration(seconds: number): string {
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format file size to human-readable format
|
||||
*/
|
||||
export function formatFileSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 B';
|
||||
|
||||
const k = 1024;
|
||||
const sizes = ['B', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
||||
}
|
||||
187
lib/audio/player.ts
Normal file
187
lib/audio/player.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* Audio playback controller
|
||||
*/
|
||||
|
||||
import { getAudioContext, resumeAudioContext } from './context';
|
||||
|
||||
export class AudioPlayer {
|
||||
private audioContext: AudioContext;
|
||||
private audioBuffer: AudioBuffer | null = null;
|
||||
private sourceNode: AudioBufferSourceNode | null = null;
|
||||
private gainNode: GainNode;
|
||||
private startTime: number = 0;
|
||||
private pauseTime: number = 0;
|
||||
private isPlaying: boolean = false;
|
||||
private isPaused: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.audioContext = getAudioContext();
|
||||
this.gainNode = this.audioContext.createGain();
|
||||
this.gainNode.connect(this.audioContext.destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an audio buffer for playback
|
||||
*/
|
||||
loadBuffer(buffer: AudioBuffer): void {
|
||||
this.stop();
|
||||
this.audioBuffer = buffer;
|
||||
this.pauseTime = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start playback from current position
|
||||
*/
|
||||
async play(startOffset: number = 0): Promise<void> {
|
||||
if (!this.audioBuffer) {
|
||||
throw new Error('No audio buffer loaded');
|
||||
}
|
||||
|
||||
// Resume audio context if needed
|
||||
await resumeAudioContext();
|
||||
|
||||
// Stop any existing playback
|
||||
this.stop();
|
||||
|
||||
// Create new source node
|
||||
this.sourceNode = this.audioContext.createBufferSource();
|
||||
this.sourceNode.buffer = this.audioBuffer;
|
||||
this.sourceNode.connect(this.gainNode);
|
||||
|
||||
// Calculate start offset
|
||||
const offset = this.isPaused ? this.pauseTime : startOffset;
|
||||
this.startTime = this.audioContext.currentTime - offset;
|
||||
|
||||
// Start playback
|
||||
this.sourceNode.start(0, offset);
|
||||
this.isPlaying = true;
|
||||
this.isPaused = false;
|
||||
|
||||
// Handle playback end
|
||||
this.sourceNode.onended = () => {
|
||||
if (this.isPlaying) {
|
||||
this.isPlaying = false;
|
||||
this.isPaused = false;
|
||||
this.pauseTime = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause playback
|
||||
*/
|
||||
pause(): void {
|
||||
if (!this.isPlaying) return;
|
||||
|
||||
this.pauseTime = this.getCurrentTime();
|
||||
this.stop();
|
||||
this.isPaused = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop playback
|
||||
*/
|
||||
stop(): void {
|
||||
if (this.sourceNode) {
|
||||
try {
|
||||
this.sourceNode.stop();
|
||||
} catch (error) {
|
||||
// Ignore errors if already stopped
|
||||
}
|
||||
this.sourceNode.disconnect();
|
||||
this.sourceNode = null;
|
||||
}
|
||||
this.isPlaying = false;
|
||||
this.isPaused = false;
|
||||
this.pauseTime = 0;
|
||||
this.startTime = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current playback time in seconds
|
||||
*/
|
||||
getCurrentTime(): number {
|
||||
if (!this.audioBuffer) return 0;
|
||||
|
||||
if (this.isPlaying) {
|
||||
const currentTime = this.audioContext.currentTime - this.startTime;
|
||||
return Math.min(currentTime, this.audioBuffer.duration);
|
||||
}
|
||||
|
||||
return this.pauseTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to a specific time
|
||||
*/
|
||||
async seek(time: number): Promise<void> {
|
||||
if (!this.audioBuffer) return;
|
||||
|
||||
const wasPlaying = this.isPlaying;
|
||||
const clampedTime = Math.max(0, Math.min(time, this.audioBuffer.duration));
|
||||
|
||||
this.stop();
|
||||
this.pauseTime = clampedTime;
|
||||
|
||||
if (wasPlaying) {
|
||||
await this.play(clampedTime);
|
||||
} else {
|
||||
this.isPaused = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set playback volume (0 to 1)
|
||||
*/
|
||||
setVolume(volume: number): void {
|
||||
const clampedVolume = Math.max(0, Math.min(1, volume));
|
||||
this.gainNode.gain.setValueAtTime(clampedVolume, this.audioContext.currentTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current volume
|
||||
*/
|
||||
getVolume(): number {
|
||||
return this.gainNode.gain.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get playback state
|
||||
*/
|
||||
getState(): {
|
||||
isPlaying: boolean;
|
||||
isPaused: boolean;
|
||||
currentTime: number;
|
||||
duration: number;
|
||||
} {
|
||||
return {
|
||||
isPlaying: this.isPlaying,
|
||||
isPaused: this.isPaused,
|
||||
currentTime: this.getCurrentTime(),
|
||||
duration: this.audioBuffer?.duration ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audio buffer
|
||||
*/
|
||||
getBuffer(): AudioBuffer | null {
|
||||
return this.audioBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if audio is loaded
|
||||
*/
|
||||
hasBuffer(): boolean {
|
||||
return this.audioBuffer !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup resources
|
||||
*/
|
||||
dispose(): void {
|
||||
this.stop();
|
||||
this.gainNode.disconnect();
|
||||
this.audioBuffer = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user