File size: 1,180 Bytes
bfe18f9
5635c14
 
bfe18f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5635c14
bfe18f9
5635c14
bfe18f9
5635c14
 
 
 
 
bfe18f9
 
5635c14
 
 
 
bfe18f9
 
 
c8c3aff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, Form
from fastapi.responses import StreamingResponse
import io
import wave
from piper.voice import PiperVoice

app = FastAPI()

# Load the voice model
model_path = 'model.onnx'
voice = PiperVoice.load(model_path)

@app.post("/synthesize/")
def synthesize_text(text: str = Form(...)):
    """
    Endpoint to synthesize text to speech.

    Args:
        text (str): The text to synthesize.

    Returns:
        StreamingResponse: The audio data as a stream.
    """
    audio_buffer = io.BytesIO()

    # Synthesize speech and write to an in-memory WAV file
    with wave.open(audio_buffer, 'wb') as wav_file:
        wav_file.setnchannels(1)  # Set the number of audio channels
        wav_file.setsampwidth(2)  # Set sample width to 2 bytes
        wav_file.setframerate(16000)  # Set the sampling rate
        audio = voice.synthesize(text, wav_file)

    # Seek to the beginning of the buffer so it can be read from the start
    audio_buffer.seek(0)

    return StreamingResponse(audio_buffer, media_type="audio/wav")

@app.get("/")
def read_root():
    return {"message": "Welcome to the Piper TTS API. Use /synthesize/ to synthesize speech."}