Audio Processing & Encoding
VozCraft includes advanced audio processing capabilities that allow users to download their generated speech as high-quality audio files. This page documents the audio generation pipeline, WAV encoding implementation, and file download mechanisms.Overview
The audio processing system consists of three main components:- Audio Generation: Uses Web Audio API to synthesize audio from speech parameters
- WAV Encoding: Encodes raw audio samples into WAV file format
- File Download: Creates downloadable Blob objects for MP3/WAV files
VozCraft generates audio entirely in the browser using the Web Audio API’s OfflineAudioContext, requiring no server-side processing.
Audio Generation Pipeline
The generateAndDownloadAudio Function
This is the primary function that orchestrates audio generation:App.jsx (lines 492-554)
Audio Generation Steps
1
Calculate audio parameters
First, the function calculates the effective speech rate and estimates the audio duration:The formula assumes 14 characters per second at normal speed, adjusted by the effective rate multiplier.
2
Create OfflineAudioContext
Initialize an offline rendering context with calculated parameters:
Sample Rate: 22.05 kHz provides good quality for speech while keeping file sizes reasonable. CD-quality audio is 44.1 kHz.
3
Generate voice waveform
Create a sawtooth oscillator with frequency modulation:
Why sawtooth waveform?
Why sawtooth waveform?
Sawtooth waves contain all harmonics and create a rich, buzzy sound that works well for voice synthesis. The harmonic content is then shaped by formant filters to create vowel-like sounds.
4
Apply formant filters
Create bandpass filters to simulate vocal tract resonances:
5
Create amplitude envelope
Shape the volume over time to simulate syllables:Each word is divided into syllables (estimated as word_length / 3), and each syllable gets an attack-decay envelope.
6
Add noise for consonants
Generate white noise and filter it to simulate fricatives:
7
Render audio
Connect the audio graph and render:
WAV Encoding
The encodeWAV Function
This function converts raw PCM audio samples to WAV file format:App.jsx (lines 556-571)
WAV File Structure
The WAV format consists of three main sections:RIFF Header (12 bytes)
RIFF Header (12 bytes)
fmt Chunk (24 bytes)
fmt Chunk (24 bytes)
data Chunk (8 bytes + audio data)
data Chunk (8 bytes + audio data)
PCM Sample Conversion
The Web Audio API provides samples as 32-bit floats in the range [-1.0, 1.0]. These must be converted to 16-bit signed integers:16-bit PCM range:
- Minimum: -32768 (0x8000)
- Maximum: 32767 (0x7FFF)
- Zero: 0
File Download System
The downloadBlob Function
Creates a temporary download link and triggers the browser’s download:App.jsx (lines 573-577)
1
Create object URL
URL.createObjectURL() creates a temporary URL pointing to the Blob data:2
Create anchor element
Programmatically create an invisible
<a> tag:3
Trigger download
Click the anchor to initiate download:
4
Cleanup
Revoke the object URL to free memory:
Download Handlers
Audio File Download
App.jsx (lines 726-729)
- WAV Format
- MP3 Format
Uncompressed PCM audio:
- File size: ~2.6 MB per minute (16-bit, 22.05 kHz, mono)
- Quality: Lossless
- Compatibility: Universal
- Use case: Maximum quality, audio editing
Transcript Download
VozCraft also allows downloading text transcripts:App.jsx (lines 579-608)
Audio Processing Performance
Benchmarks
Typical processing times on modern hardware:Processing happens asynchronously using
OfflineAudioContext.startRendering(), which returns a Promise. The UI remains responsive during rendering.Memory Usage
Advanced Features
Dynamic Frequency Modulation
VozCraft applies pitch variation to create more natural-sounding speech:Syllable-Based Envelopes
File Format Details
WAV Specifications
File Size Calculation
Error Handling
- Out of memory: Very long audio (>10 minutes)
- Browser restrictions: iOS Safari has stricter limits
- OfflineAudioContext limits: Maximum context length varies by browser
Best Practices
1
Validate input length
2
Use appropriate sample rates
- 22.05 kHz: Speech, podcasts (good balance)
- 44.1 kHz: Music, professional audio
- 8 kHz: Phone quality (not recommended)
3
Clamp audio samples
4
Clean up resources
Browser Compatibility
Web Audio API Support:
- ✅ Chrome 35+
- ✅ Firefox 25+
- ✅ Safari 14.1+
- ✅ Edge 79+
- ✅ Opera 22+
- ✅ All modern browsers
- ⚠️ Safari has lower maximum context lengths
Related Resources
Next Steps
Web Speech API
Learn about real-time speech synthesis
PWA Setup
Configure Progressive Web App features
