跳到正文
DEV Community· VoiceDeveloper·· 4 小時前AI 評分26

如何使用 ElevenLabs 構建語音驅動的無障礙工具

Build a Voice-Powered Accessibility Tool

AI 導讀

這份開發教學示範如何結合 ElevenLabs、語音辨識及聲音複製,構建語音無障礙工具。Python 流程以 Google Web Speech API 將麥克風輸入轉成文字,再由 ElevenLabs 合成複製聲音並播放。內容亦涵蓋網頁整合,以及背景噪音、延遲和 API 速率限制的處理方法。

正文

Why Voice‑Powered Accessibility Matters

If you’ve ever watched a video with no subtitles, tried to read an article on a mobile screen, or used a screen reader that sounded robotic, you’ll understand how frustrating it can be when the technology that should help falls short. Voice‑powered accessibility tools—think screen readers, text‑to‑speech (TTS) assistants, and voice‑controlled navigation—can make a world of difference for people with visual impairments, reading difficulties, or motor challenges.

Building a lightweight, custom solution lets you tailor the experience to your audience’s needs, add brand‑specific voices, and keep performance high. In this post we’ll walk through a practical, developer‑friendly stack that combines speech recognition, TTS, and voice cloning, with a focus on the ElevenLabs platform for realistic voice synthesis.


The Core Building Blocks

Component Purpose Typical Library
Speech Recognition Convert spoken input into text speech_recognition, vosk, or cloud APIs
Text‑to‑Speech Read text aloud in natural‑sounding voice ElevenLabs, Google TTS, Amazon Polly
Voice Cloning Create a custom voice that sounds like a specific person ElevenLabs, Resemble.ai
State Management Keep track of conversation flow or user context Redux, Zustand, simple in‑memory dict
Frontend UI for users to interact, show transcripts, or display captions React, Vue, or vanilla JS

While you can mix and match any of the above, ElevenLabs shines as a single provider that handles both TTS and voice cloning with a clean REST API and SDKs. The platform’s neural voices are remarkably natural, and the voice cloning workflow is straightforward—just upload a short audio sample, and you get a fully‑functional voice model.


Getting Started with ElevenLabs

  1. Create an Account

    Sign up at the ElevenLabs portal. When you’re ready to start, you’ll need an API key. (You can keep the key secret by storing it in your environment.)

  2. Install the SDK

   pip install elevenlabs
  1. Set Your API Key
   from elevenlabs import set_api_key
   set_api_key("YOUR_ELEVENLABS_API_KEY")
  1. Try a Quick Sample
   from elevenlabs import generate, play

   audio = generate(
       text="Hello, world! This is a quick test of ElevenLabs TTS.",
       voice="en-US-Standard-A"
   )
   play(audio)

Feel free to swap "en-US-Standard-A" with any of the voices available in your account. When you’re ready to clone a voice, we’ll dive into that next.


Cloning a Custom Voice

Voice cloning lets you create a voice that sounds like a specific person—your brand’s spokesperson, a beloved character, or a user‑generated voice. ElevenLabs’ cloning workflow is a few lines of code:

from elevenlabs import clone, generate, play

# 1️⃣ Upload a short audio clip (30–60 seconds) of the target speaker
audio_path = "speaker_sample.wav"

# 2️⃣ Create the clone
clone_response = clone(
    audio=audio_path,
    name="CustomSpeaker"
)

# 3️⃣ Generate speech using the new voice
audio = generate(
    text="This is a sentence spoken in your custom voice.",
    voice=clone_response["voice_id"]
)

# 4️⃣ Play the result
play(audio)

Tip: Make sure the sample audio is clean—no background noise, consistent volume, and a clear speaking rate. The better the sample, the more natural the cloned voice will sound.


Building the End‑to‑End Flow

Below is a minimal Python script that ties speech recognition, TTS, and voice cloning together. It listens to your microphone, converts speech to text, and immediately reads the text back in your cloned voice.

import os
import speech_recognition as sr
from elevenlabs import generate, play, clone

# ---- Config ----
API_KEY = os.getenv("ELEVENLABS_API_KEY")
VOICE_CLONE_NAME = "CustomSpeaker"
AUDIO_SAMPLE = "sample.wav"

# ---- Setup ----
set_api_key(API_KEY)

# Create or reuse a cloned voice
if not os.path.exists(f"{VOICE_CLONE_NAME}_id.txt"):
    clone_resp = clone(audio=AUDIO_SAMPLE, name=VOICE_CLONE_NAME)
    with open(f"{VOICE_CLONE_NAME}_id.txt", "w") as f:
        f.write(clone_resp["voice_id"])
else:
    with open(f"{VOICE_CLONE_NAME}_id.txt") as f:
        voice_id = f.read().strip()

# ---- Recognition & TTS Loop ----
rec = sr.Recognizer()
mic = sr.Microphone()

print("Speak something…")
with mic as source:
    audio = rec.listen(source)

try:
    text = rec.recognize_google(audio)
    print(f"You said: {text}")

    # Generate speech in the cloned voice
    speech = generate(text=text, voice=voice_id)
    play(speech)

except sr.UnknownValueError:
    print("Could not understand audio.")
except sr.RequestError as e:
    print(f"Recognition service error: {e}")

What’s Happening?

  1. Clone or Load Voice – We create a voice clone once and persist its ID for future runs.
  2. Listen – Using speech_recognition, we capture a single utterance from the microphone.
  3. Transcribe – The Google Web Speech API (free for small projects) turns audio to text.
  4. Speak Back – ElevenLabs’ generate function synthesizes the text using the cloned voice.
  5. Play – The resulting audio is streamed back to your speakers.

This script can be the backbone of a screen‑reader‑style app, a voice‑controlled navigation aid, or a hands‑free reading assistant.


Integrating into a Web App

If you want to expose this functionality via a browser, you can use JavaScript with ElevenLabs’ REST endpoint. Below is a quick example that records audio, sends it to ElevenLabs for TTS, and plays the result.

<!DOCTYPE html>
<html>
<head>
  <title>Voice‑Powered Demo</title>
</head>
<body>
  <button id="record">Record & Speak</button>
  <script>
    const apiKey = 'YOUR_ELEVENLABS_API_KEY';
    const voiceId = 'YOUR_CLONED_VOICE_ID';

    document.getElementById('record').onclick = async () => {
      // Record 5 seconds of audio
      const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
      const mediaRecorder = new MediaRecorder(stream);
      const chunks = [];

      mediaRecorder.ondataavailable = e => chunks.push(e.data);
      mediaRecorder.onstop = async () => {
        const blob = new Blob(chunks, { type: 'audio/webm' });
        const formData = new FormData();
        formData.append('audio', blob);

        // Send audio to ElevenLabs for TTS
        const response = await fetch('https://api.elevenlabs.io/v1/text-to-speech/' + voiceId, {
          method: 'POST',
          headers: { 'xi-api-key': apiKey },
          body: new FormData([
            ['text', 'Hello from your custom voice!'],
            ['voice_settings', JSON.stringify({ stability: 0.5, similarity_boost: 0.75 })]
          ])
        });

        const audioBlob = await response.blob();
        const audioUrl = URL.createObjectURL(audioBlob);
        const audio = new Audio(audioUrl);
        audio.play();
      };

      mediaRecorder.start();
      setTimeout(() => mediaRecorder.stop(), 5000);
    };
  </script>
</body>
</html>

Note: The code above uses a static text string for simplicity. In a real application you’d likely send the recorded speech to a speech‑to‑text service first, then feed the resulting text to ElevenLabs for TTS.


Handling Edge Cases

Issue Quick Fix
Background Noise Apply a noise‑reduction library (e.g., pydub) before sending audio to the recognizer.
Latency Cache the cloned voice ID and pre‑fetch TTS audio for frequently used phrases.
API Rate Limits Queue requests or switch to a paid tier if you hit the free quota.
Cross‑Browser Support Use the Web Audio API polyfill for older browsers.

Deployment Tips

  1. Environment Variables – Keep your ElevenLabs API key secret by storing it in a .env file and using a library like python-dotenv.
  2. Dockerize – Bundle your script into a Docker container for consistent runtime across machines.
  3. Monitoring – Log API responses and latency to spot bottlenecks early.
  4. Accessibility Audits – Use tools like Lighthouse to verify that your web app meets WCAG guidelines.

Wrap‑Up

Voice‑powered accessibility tools are no longer a niche luxury—they’re becoming a standard part of inclusive design. By leveraging ElevenLabs for realistic TTS and voice cloning, you can create a custom, natural‑sounding assistant that feels like it was built just for your users.

The code snippets above give you a solid starting point, but the real power comes from iterating on user feedback and fine‑tuning the voice parameters. Whether you’re building a screen reader, a hands‑free note‑taking app, or a voice‑controlled kiosk, the stack we explored is lightweight, developer‑friendly, and ready for production.

Ready to bring your voice‑powered accessibility tool to life? Sign up at ElevenLabs with this link, get your API key, and start cloning voices today: https://try.elevenlabs.io/kr07zfuqn1bp

Happy coding, and may your words always be heard!

來源:DEV Community · dev.to