Skip to main content

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:
  1. Audio Generation: Uses Web Audio API to synthesize audio from speech parameters
  2. WAV Encoding: Encodes raw audio samples into WAV file format
  3. 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:
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:
Formants are resonant frequencies of the vocal tract. F1 (800 Hz) and F2 (2200 Hz) are the most important for vowel perception.
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:

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
The conversion multiplies by 32768 for negative values and 32767 for positive values to maximize dynamic range while preventing overflow.

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:
Always revoke object URLs after use to prevent memory leaks. Object URLs persist until the page is closed or explicitly revoked.

Download Handlers

Audio File Download

App.jsx (lines 726-729)
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)
Transcript files include metadata (date, voice, speed, mood) along with the text, making them useful for archiving and documentation.

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

Memory considerations:
  • Each minute of audio requires ~5.3 MB in memory during processing
  • Offline contexts hold audio data until garbage collected
  • Mobile browsers may have stricter memory limits
  • Consider chunking very long audio (>10 minutes)

Advanced Features

Dynamic Frequency Modulation

VozCraft applies pitch variation to create more natural-sounding speech:
This creates pitch variations of ±12% every 300ms, simulating natural speech prosody.

Syllable-Based Envelopes

Each word is divided into estimated syllables (word_length / 3), and each syllable gets an attack-decay envelope for a more rhythmic sound.

File Format Details

WAV Specifications

File Size Calculation

Error Handling

Common error scenarios:
  • 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

Always clamp samples to prevent clipping and distortion.
4

Clean up resources

Revoke object URLs to prevent memory leaks.

Browser Compatibility

Web Audio API Support:
  • ✅ Chrome 35+
  • ✅ Firefox 25+
  • ✅ Safari 14.1+
  • ✅ Edge 79+
  • ✅ Opera 22+
OfflineAudioContext Support:
  • ✅ All modern browsers
  • ⚠️ Safari has lower maximum context lengths

Next Steps

Web Speech API

Learn about real-time speech synthesis

PWA Setup

Configure Progressive Web App features