feat: complete Phase 11.4 - comprehensive audio file import
Implemented advanced audio import capabilities: **Import Features:** - Support for WAV, MP3, OGG, FLAC, M4A, AIFF formats - Sample rate conversion using OfflineAudioContext - Stereo to mono conversion (equal channel mixing) - Normalize on import option (99% peak with 1% headroom) - Comprehensive codec detection from MIME types and extensions **API Enhancements:** - ImportOptions interface (convertToMono, targetSampleRate, normalizeOnImport) - importAudioFile() function returning buffer + metadata - AudioFileInfo with AudioMetadata (codec, duration, channels, sample rate, file size) - Enhanced decodeAudioFile() with optional import transformations **UI Components:** - ImportDialog component with import settings controls - Sample rate selector (44.1kHz - 192kHz) - Checkbox options for mono conversion and normalization - File info display (original sample rate and channels) - Updated FileUpload to show AIFF support **Technical Implementation:** - Offline resampling for quality preservation - Equal-power channel mixing for stereo-to-mono - Peak detection across all channels - Metadata extraction with codec identification Phase 11 (Export & Import) now complete! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,21 +4,211 @@
|
||||
|
||||
import { getAudioContext } from './context';
|
||||
|
||||
export interface ImportOptions {
|
||||
convertToMono?: boolean;
|
||||
targetSampleRate?: number; // If specified, resample to this rate
|
||||
normalizeOnImport?: boolean;
|
||||
}
|
||||
|
||||
export interface AudioFileInfo {
|
||||
buffer: AudioBuffer;
|
||||
metadata: AudioMetadata;
|
||||
}
|
||||
|
||||
export interface AudioMetadata {
|
||||
fileName: string;
|
||||
fileSize: number;
|
||||
fileType: string;
|
||||
duration: number;
|
||||
sampleRate: number;
|
||||
channels: number;
|
||||
bitDepth?: number;
|
||||
codec?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode an audio file to AudioBuffer
|
||||
* Decode an audio file to AudioBuffer with optional conversions
|
||||
*/
|
||||
export async function decodeAudioFile(file: File): Promise<AudioBuffer> {
|
||||
export async function decodeAudioFile(
|
||||
file: File,
|
||||
options: ImportOptions = {}
|
||||
): Promise<AudioBuffer> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const audioContext = getAudioContext();
|
||||
|
||||
try {
|
||||
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
||||
let audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
||||
|
||||
// Apply conversions if requested
|
||||
if (options.convertToMono && audioBuffer.numberOfChannels > 1) {
|
||||
audioBuffer = convertToMono(audioBuffer);
|
||||
}
|
||||
|
||||
if (options.targetSampleRate && audioBuffer.sampleRate !== options.targetSampleRate) {
|
||||
audioBuffer = await resampleAudioBuffer(audioBuffer, options.targetSampleRate);
|
||||
}
|
||||
|
||||
if (options.normalizeOnImport) {
|
||||
audioBuffer = normalizeAudioBuffer(audioBuffer);
|
||||
}
|
||||
|
||||
return audioBuffer;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to decode audio file: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode audio file and return both buffer and metadata
|
||||
*/
|
||||
export async function importAudioFile(
|
||||
file: File,
|
||||
options: ImportOptions = {}
|
||||
): Promise<AudioFileInfo> {
|
||||
const audioBuffer = await decodeAudioFile(file, options);
|
||||
const metadata = extractMetadata(file, audioBuffer);
|
||||
|
||||
return {
|
||||
buffer: audioBuffer,
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert stereo (or multi-channel) audio to mono
|
||||
*/
|
||||
function convertToMono(audioBuffer: AudioBuffer): AudioBuffer {
|
||||
const audioContext = getAudioContext();
|
||||
const numberOfChannels = audioBuffer.numberOfChannels;
|
||||
|
||||
if (numberOfChannels === 1) {
|
||||
return audioBuffer; // Already mono
|
||||
}
|
||||
|
||||
// Create a new mono buffer
|
||||
const monoBuffer = audioContext.createBuffer(
|
||||
1,
|
||||
audioBuffer.length,
|
||||
audioBuffer.sampleRate
|
||||
);
|
||||
|
||||
const monoData = monoBuffer.getChannelData(0);
|
||||
|
||||
// Mix all channels equally
|
||||
for (let i = 0; i < audioBuffer.length; i++) {
|
||||
let sum = 0;
|
||||
for (let channel = 0; channel < numberOfChannels; channel++) {
|
||||
sum += audioBuffer.getChannelData(channel)[i];
|
||||
}
|
||||
monoData[i] = sum / numberOfChannels;
|
||||
}
|
||||
|
||||
return monoBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resample audio buffer to a different sample rate
|
||||
*/
|
||||
async function resampleAudioBuffer(
|
||||
audioBuffer: AudioBuffer,
|
||||
targetSampleRate: number
|
||||
): Promise<AudioBuffer> {
|
||||
const audioContext = getAudioContext();
|
||||
|
||||
// Create an offline context at the target sample rate
|
||||
const offlineContext = new OfflineAudioContext(
|
||||
audioBuffer.numberOfChannels,
|
||||
Math.ceil(audioBuffer.duration * targetSampleRate),
|
||||
targetSampleRate
|
||||
);
|
||||
|
||||
// Create a buffer source
|
||||
const source = offlineContext.createBufferSource();
|
||||
source.buffer = audioBuffer;
|
||||
source.connect(offlineContext.destination);
|
||||
source.start(0);
|
||||
|
||||
// Render the audio at the new sample rate
|
||||
const resampledBuffer = await offlineContext.startRendering();
|
||||
return resampledBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize audio buffer to peak amplitude
|
||||
*/
|
||||
function normalizeAudioBuffer(audioBuffer: AudioBuffer): AudioBuffer {
|
||||
const audioContext = getAudioContext();
|
||||
|
||||
// Find peak amplitude across all channels
|
||||
let peak = 0;
|
||||
for (let channel = 0; channel < audioBuffer.numberOfChannels; channel++) {
|
||||
const channelData = audioBuffer.getChannelData(channel);
|
||||
for (let i = 0; i < channelData.length; i++) {
|
||||
const abs = Math.abs(channelData[i]);
|
||||
if (abs > peak) peak = abs;
|
||||
}
|
||||
}
|
||||
|
||||
if (peak === 0 || peak === 1.0) {
|
||||
return audioBuffer; // Already normalized or silent
|
||||
}
|
||||
|
||||
// Create normalized buffer
|
||||
const normalizedBuffer = audioContext.createBuffer(
|
||||
audioBuffer.numberOfChannels,
|
||||
audioBuffer.length,
|
||||
audioBuffer.sampleRate
|
||||
);
|
||||
|
||||
// Apply normalization with 1% headroom
|
||||
const scale = 0.99 / peak;
|
||||
for (let channel = 0; channel < audioBuffer.numberOfChannels; channel++) {
|
||||
const inputData = audioBuffer.getChannelData(channel);
|
||||
const outputData = normalizedBuffer.getChannelData(channel);
|
||||
for (let i = 0; i < inputData.length; i++) {
|
||||
outputData[i] = inputData[i] * scale;
|
||||
}
|
||||
}
|
||||
|
||||
return normalizedBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract metadata from file and audio buffer
|
||||
*/
|
||||
function extractMetadata(file: File, audioBuffer: AudioBuffer): AudioMetadata {
|
||||
// Detect codec from file extension or MIME type
|
||||
const codec = detectCodec(file);
|
||||
|
||||
return {
|
||||
fileName: file.name,
|
||||
fileSize: file.size,
|
||||
fileType: file.type || 'unknown',
|
||||
duration: audioBuffer.duration,
|
||||
sampleRate: audioBuffer.sampleRate,
|
||||
channels: audioBuffer.numberOfChannels,
|
||||
codec,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect audio codec from file
|
||||
*/
|
||||
function detectCodec(file: File): string {
|
||||
const ext = file.name.split('.').pop()?.toLowerCase();
|
||||
const mimeType = file.type.toLowerCase();
|
||||
|
||||
if (mimeType.includes('wav') || ext === 'wav') return 'WAV (PCM)';
|
||||
if (mimeType.includes('mpeg') || mimeType.includes('mp3') || ext === 'mp3') return 'MP3';
|
||||
if (mimeType.includes('ogg') || ext === 'ogg') return 'OGG Vorbis';
|
||||
if (mimeType.includes('flac') || ext === 'flac') return 'FLAC';
|
||||
if (mimeType.includes('m4a') || mimeType.includes('aac') || ext === 'm4a') return 'AAC (M4A)';
|
||||
if (ext === 'aiff' || ext === 'aif') return 'AIFF';
|
||||
if (mimeType.includes('webm') || ext === 'webm') return 'WebM Opus';
|
||||
|
||||
return 'Unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get audio file metadata without decoding the entire file
|
||||
*/
|
||||
@@ -50,10 +240,12 @@ export function isSupportedAudioFormat(file: File): boolean {
|
||||
'audio/aac',
|
||||
'audio/m4a',
|
||||
'audio/x-m4a',
|
||||
'audio/aiff',
|
||||
'audio/x-aiff',
|
||||
];
|
||||
|
||||
return supportedFormats.includes(file.type) ||
|
||||
/\.(wav|mp3|ogg|webm|flac|aac|m4a)$/i.test(file.name);
|
||||
/\.(wav|mp3|ogg|webm|flac|aac|m4a|aiff|aif)$/i.test(file.name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user