클라우드 음성 합성 API는 앱의 텍스트를 외부 서버로 보낸다. Piper는 경량 신경망으로 텍스트를 음성으로 변환하는 로컬 전용 TTS(text-to-speech, 텍스트 음성 변환) 엔진이다. Piper를 설치하고 네트워크를 차단한 상태에서 음성 합성을 테스트했다.
Piper는 Raspberry Pi 같은 소형 기기에서 실시간보다 빠르게 실행되도록 설계된 경량 신경 음성 합성 모델을 사용한다. 소스 코드는 MIT 라이선스를 따른다(*1).
1. Piper 설치 및 음성 모델 다운로드
네트워크에 접속할 수 있을 때 빌드 과정에서 영어 음성 모델(en_US-lessac-medium)을 다운로드한다.
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'piper-tts==1.4.2'
RUN python -m piper.download_voices en_US-lessac-medium --data-dir /opt/piper-voices
WORKDIR /work
podman build -t piper-sandbox .
2. 네트워크를 차단한 상태에서 음성 합성 실행
import time
import wave
from piper import PiperVoice
voice = PiperVoice.load(
"/opt/piper-voices/en_US-lessac-medium.onnx",
config_path="/opt/piper-voices/en_US-lessac-medium.onnx.json",
)
text = "Open source software lets anyone read, modify, and redistribute the code."
t0 = time.time()
with wave.open("/work/output.wav", "wb") as wav_file:
voice.synthesize_wav(text, wav_file)
elapsed = time.time() - t0
with wave.open("/work/output.wav", "rb") as wav_file:
duration = wav_file.getnframes() / wav_file.getframerate()
print(f"synthesis time: {elapsed:.2f}s")
print(f"audio duration: {duration:.2f}s")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work piper-sandbox \
python tts_demo.py
-v 옵션을 사용하면 지정한 디렉터리를 컨테이너 내부에서 읽고 쓸 수 있다. 실험용으로 비워 둔 디렉터리에서 명령을 실행한다.
명령의 반환 결과는 다음과 같았다.
synthesis time: 1.51s
audio duration: 4.47s
sample rate: 22050 Hz
프로그램은 1.51초 만에 4.47초 분량의 음성을 생성했다. 합성 시간 1.51초가 오디오 길이 4.47초보다 짧으므로 Piper는 실시간보다 빠르게 실행된다는 설계 목표를 달성했다. 생성된 파일을 불러와 RIFF/WAVE, 16bit PCM, mono, 22050Hz 형식임을 확인했고, 손상도 발견되지 않았다. 이 기록에서는 음성의 명료도를 평가하지 않았다.
텍스트를 회사 외부로 보내지 않고 음성 합성하기
앱은 음성으로 변환할 텍스트를 외부 서비스로 보낼 수 있다. Piper는 모델을 시스템에 보관하므로 앱은 텍스트를 회사 외부로 보내지 않고 음성을 합성할 수 있다. Piper가 실시간보다 빠르게 실행되는 것을 확인했으며, 따라서 실시간 성능이 필요한 애플리케이션에 사용할 수 있다.
출처
*1 Piper (GitHub), https://github.com/rhasspy/piper, MIT License