Skip to main content

Web Speech API Integration

VozCraft leverages the browser’s native Web Speech API to provide high-quality text-to-speech synthesis without requiring external services or API keys. This page documents how the application uses SpeechSynthesis and SpeechSynthesisUtterance to generate natural-sounding voice output.

Overview

The Web Speech API provides two main interfaces for TTS:
  • SpeechSynthesis: Controls speech synthesis and manages the speech queue
  • SpeechSynthesisUtterance: Represents a speech request with configurable properties
The Web Speech API is supported in all modern browsers including Chrome, Firefox, Safari, and Edge. No external dependencies or API keys are required.

Core Implementation

The speak() Function

The heart of VozCraft’s TTS functionality is the speak() function in App.jsx. This function creates and configures speech utterances with customized voice parameters:
App.jsx (lines 649-685)

Key Components

1. SpeechSynthesisUtterance Properties

VozCraft configures three main utterance properties:
Specifies the BCP 47 language tag for the speech synthesis engine.
VozCraft supports 22 languages and regional variants:
  • Spanish: es-MX, es-ES, es-AR, es-CO, es-CL, es-VE
  • English: en-US, en-GB, en-AU, en-IN
  • Portuguese: pt-BR, pt-PT
  • And 10+ other languages
Controls the voice pitch. VozCraft combines gender and mood pitch multipliers:
The pitch value is clamped between 0.1 and 2.0 to prevent extreme distortion.
Controls speech speed. VozCraft applies multiple rate modifiers:
Controls playback volume. Certain moods affect volume:

2. Voice Selection Algorithm

VozCraft implements intelligent voice selection based on language and gender preference:
The voice selection algorithm searches for gender-specific voice names in both English and Spanish, ensuring proper voice selection across different operating systems.

Speech Control Functions

Starting Speech

The handleGenerar function initiates speech synthesis:
App.jsx (lines 692-711)

Stopping Speech

App.jsx (lines 687-690)
window.speechSynthesis.cancel() immediately stops all speech and clears the speech queue. Any pending utterances are discarded.

Playing from History

VozCraft allows replaying previously generated audio with the same settings:
App.jsx (lines 713-719)

Event Handling

The SpeechSynthesisUtterance interface provides lifecycle events:
Fired when speech begins. VozCraft uses this to:
  • Update UI state (setReproduciendo(true))
  • Show visual feedback in the audio player
  • Disable the generate button

Browser Compatibility

Checking for Support

Always check for Web Speech API support:

Voice Loading

Voices may load asynchronously in some browsers:
Browser Support:
  • ✅ Chrome 33+ (full support)
  • ✅ Firefox 49+ (full support)
  • ✅ Safari 7+ (full support)
  • ✅ Edge 14+ (full support)
  • ✅ Opera 21+ (full support)

Voice Configuration Data

VozCraft defines voice parameters using configuration objects:
App.jsx (lines 4-53)

Advanced Features

Duration Estimation

VozCraft estimates audio duration for the progress bar:
App.jsx (lines 278-284)
The formula texto.length / (14 * effectiveRate) assumes approximately 14 characters per second at normal speed, adjusted by the effective rate.

Progress Tracking

The audio player tracks progress using interval-based estimation:
App.jsx (lines 286-300)

Best Practices

1

Always cancel before starting

Call window.speechSynthesis.cancel() before creating new utterances to prevent queue buildup:
2

Clamp parameter values

Always validate and clamp pitch, rate, and volume to valid ranges:
3

Handle voice loading

Wait for voices to load before attempting synthesis:
4

Implement error handling

Always provide onerror handlers to gracefully handle synthesis failures:

Limitations

Known limitations of the Web Speech API:
  1. Voice availability varies by OS: Windows, macOS, iOS, and Android have different voice libraries
  2. No fine-grained pause control: Cannot pause/resume mid-utterance reliably
  3. No precise progress events: Must estimate duration and progress
  4. Queue interruption: New utterances cancel previous ones when using cancel()
  5. Character limits: Some browsers impose limits on utterance length (typically 4000-5000 chars)

Performance Considerations

  • Memory management: Store only one utterance reference at a time
  • Queue management: Cancel previous utterances before starting new ones
  • Long text handling: VozCraft limits input to 5000 characters
  • Event cleanup: Always clear intervals and listeners in useEffect cleanup

Next Steps

Audio Processing

Learn how VozCraft generates downloadable audio files

PWA Setup

Explore the Progressive Web App configuration