Skip to main content

Japanese/English

fastrtc-jp

License: MIT Python 3.12+ PyPI version

fastrtc用の日本語TTSとSTT追加キット(日本語音声合成・認識モジュール)

概要

fastrtcは高性能なリアルタイム通信フレームワークですが、現状では日本語の音声合成(TTS)および音声認識(STT)機能が不十分です。このプロジェクトは、fastrtcに日本語対応の音声処理機能を追加するための拡張パッケージです。

主な機能:

  • 日本語に特化した音声合成モデル(VOICEVOX、Style-Bert-VITS2, gTTS)
  • 日本語に対応した音声認識モデル(MlxWhisper, Vosk、Google Speech Recognition)

fastrtcの詳細な使い方については、fastrtc公式ドキュメントを参照してください。

提供モデル一覧

クラス 説明 追加パッケージ 特徴
VoicevoxTTSModel VOICEVOXのapiで音声合成するクラス - 高品質な日本語音声、多様な話者、感情表現が可能
StyleBertVits2 Style-Bert-VITS2で音声合成するクラス style-bert-vits2 / pyopenjtalk 高品質な音声合成が可能、事前学習済みモデルに対応
GTTSModel Google Text-to-Speechで音声合成するクラス gtts インターネット接続が必要、自然な発話
VoskSTT Voskエンジンで音声認識するクラス vosk オフライン動作可能、軽量
MlxWhisper mlx-whisperで音声認識するクラス mlx-whisper 高精度な音声認識、Apple Silicon最適化
GoogleSTT SpeechRecognizerのGoogleエンジンで音声認識するクラス SpeechRecognition==3.10.0 高精度、インターネット接続が必要

システム要件

  • Python 3.12以上
  • Ubuntu 24.04 LTS, macOS Sonoma 15.4 にて動作確認
  • 各モデルの追加要件は以下の「インストール」セクションを参照

インストール

基本的な使い方として、Python仮想環境を作成してからpipでインストールすることをお勧めします。

# 仮想環境の作成と有効化(オプション)
python -m venv .venv
source .venv/bin/activate
# pipをアップデート
.venv/bin/python3 -m pip install -U pip setuptools
# 基本パッケージのインストール
pip install fastrtc-jp

追加モジュール

必要に応じて、以下の追加パッケージをインストールしてください:

音声合成(TTS)

VOICEVOXを使用する場合:

VOICEVOXは、無料で使える高品質な日本語音声合成エンジンです。 VOICEVOXと音声モデルの使用条件については公式ドキュメントなどで確認して下さい。 VOICEVOX公式を参照し、APIでアクセスできる環境を準備してください。

  • VoicevoxTTSModelクラス

環境変数VOICEVOX_HOSTLISTにVOICEVOXサーバのアドレスとポートを設定して下さい。 .envファイルもしくはconfig.envファイルに以下のように記述して下さい。 記述がない場合は下記のデフォルトが設定されます。

VOICEVOX_HOSTLIST=http://127.0.0.1:50021
  • VoicevoxTTSOptionsクラス

    • speaker_name: キャラクターの名前
    • speaker_style: 声のスタイル
    • speaker_id: 話者ID(整数)
    • speedScale: 話速スケール(デフォルト: 1.0)

    http://127.0.0.1:50021/speakersなどで、キャラクターの名前、スタイル、idを調べて、話者IDもしくは、名前とスタイルを設定して下さい。

Style-Bert-VITS2を使用する場合:

Style-Bert-Vits2は、高品質な音声合成が可能なTTSモデルです。事前学習済みモデルを使って、自然な日本語音声を生成できます。 Style-Bert-Vits2と音声モデルの使用条件については公式ドキュメントなどで確認して下さい。 詳細はstyle-bert-vits2リポジトリを参照して下さい。 pythonのstyle-bert-vits2パッケージをpythonコードから利用するには、APIサーバを起動してAPIをコールする方法と、直接実行する方式があります。このパッケージでは、直接実行する方式を実装しています。

pip install fastrtc-jp[sbv2]
  • StyleBertVits2クラス

標準音声モデルもしくは、別途、音声モデルを使用することができます。

StyleBertVits2Options( device="cpu",
    model_file = "model/jvnv-M1-jp/jvnv-M1-jp_e158_s14000.safetensors",
    config_file = "model/jvnv-M1-jp/config.json",
    style_file = "model/jvnv-M1-jp/style_vectors.npy",
)
GTTSModelを使用する場合:

詳細はgTTSリポジトリを参照して下さい。 gTTSはgoogleのapiを使用しますので、インターネット接続が必要です。

pip install fastrtc-jp[gtts]
  • GTTSModelクラス

Google Text-to-Speechを使用した音声合成モデルです。

  • GTTSOptionsクラス

    • speed: 話す速度(デフォルト1.0)

音声認識(STT)

VoskSTTを使用する場合:

Voskは、オフラインで動作する音声認識エンジンです。 詳細はVoskの公式を参照してください。 一応、日本語のモデルを自動でダウンロードするようにしています。ダウンロード先は、$HOME/.cache/voskです。

pip install fastrtc-jp[vosk]
  • VoskSTTクラス

現在は、'vosk-model-ja-0.22'に固定です。

mlx-whisperを使用する場合:

mlx-whisperはApple Silicon向けに最適化されたWhisperモデルの実装です。 詳細は、MLX Examplesリポジトリを参照してください。

pip install fastrtc-jp[mlx]
  • MlxWhisperクラス

現在は、'mlx-community/whisper-medium-mlx-q4'に固定です。 mlx-communityから、mlx対応のモデルを自動的にダウンロードするようにしています。ダウンロード先は、$HOME/.cache/huggingface/hubです。

GoogleSTTを使用する場合:
pip install fastrtc-jp[sr]
  • GoogleSTTクラス Googleの音声認識エンジンを使用します。インターネット接続が必要です。

使用例

基本的なエコーバックサンプル

マイクの音声をそのままスピーカーにエコーバックするシンプルな例です。

import sys, os
import numpy as np

from fastrtc import ReplyOnPause
from fastrtc.reply_on_pause import AlgoOptions
from fastrtc.stream import Stream

"""
マイクの音声をそのままスピーカーにエコーバックするだけのサンプル
"""
def echoback(audio: tuple[int, np.ndarray]):
    print(f"shape:{audio[1].shape} dtype:{audio[1].dtype} {audio[0]}Hz {audio[1].shape[1]/audio[0]}秒の音声が入力されました。")
    yield audio

def example_echoback():
    algo_options = AlgoOptions(
        audio_chunk_duration=0.6,
        started_talking_threshold=0.5,
        speech_threshold=0.1,
    )
    stream = Stream(
        handler=ReplyOnPause(
            echoback,
            algo_options=algo_options,
            input_sample_rate=16000,
            output_sample_rate=16000,
        ),
        modality="audio", 
        mode="send-receive",
    )

    stream.ui.launch()

if __name__ == "__main__":
    example_echoback()

VOICEVOXで音声合成するサンプル

import sys, os
import numpy as np

from fastrtc import ReplyOnPause
from fastrtc.reply_on_pause import AlgoOptions
from fastrtc.stream import Stream

from fastrtc_jp.text_to_speech.voicevox import VoicevoxTTSModel, VoicevoxTTSOptions

"""
VOICEVOXで音声合成するだけのサンプル
"""

tts_model = VoicevoxTTSModel()  # デフォルトはlocalhostの50021ポートに接続
voicevox_opt = VoicevoxTTSOptions(
    speaker=8,  # つむぎ
    speedScale=1.0,  # 話速(1.0が標準)
)

def voicevox(audio: tuple[int, np.ndarray]):
    print(f"shape:{audio[1].shape} dtype:{audio[1].dtype} {audio[0]}Hz {audio[1].shape[1]/audio[0]}秒の音声が入力されました。")
    response = "やっほー、今日も元気だ。やきとり食べよう。"
    for audio_chunk in tts_model.stream_tts_sync(response, voicevox_opt):
        print("Sending audio")
        yield audio_chunk

def example_voicevox():
    algo_options = AlgoOptions(
        audio_chunk_duration=0.6,
        started_talking_threshold=0.5,
        speech_threshold=0.1,
    )
    stream = Stream(
        handler=ReplyOnPause(
            voicevox,
            algo_options=algo_options,
            input_sample_rate=16000,
            output_sample_rate=16000,
        ),
        modality="audio", 
        mode="send-receive",
    )

    stream.ui.launch()

if __name__ == "__main__":
    example_voicevox()

音声認識と音声合成を組み合わせたサンプル

import sys, os
import numpy as np

from fastrtc import ReplyOnPause
from fastrtc.reply_on_pause import AlgoOptions
from fastrtc.stream import Stream

from fastrtc_jp.speech_to_text.sr_google import GoogleSTT
# from fastrtc_jp.speech_to_text.vosk import VoskSTT
from fastrtc_jp.text_to_speech.voicevox import VoicevoxTTSModel, VoicevoxTTSOptions
# from fastrtc_jp.text_to_speech.gtts import GTTSModel, GTTSOptions

"""
マイクの音声をSTT->TTSしてエコーバックするサンプル
"""

# 音声認識モデルの初期化
stt_model = GoogleSTT()
# stt_model = VoskSTT()  # Voskを使用する場合

# 音声合成モデルの初期化
tts_model = VoicevoxTTSModel()
voicevox_opt = VoicevoxTTSOptions(
    speaker_id=8,  # つむぎ
    speedScale=1.0,
)
# tts_model = GTTSModel()  # gTTSを使用する場合

def echoback(audio: tuple[int, np.ndarray]):
    print(f"shape:{audio[1].shape} dtype:{audio[1].dtype} {audio[0]}Hz {audio[1].shape[1]/audio[0]}秒の音声が入力されました。")
    # 音声認識
    user_input = stt_model.stt(audio)
    print(f"音声認識結果: {user_input}")
    
    # 認識した文章をそのまま音声合成してエコーバック
    response = user_input
    for audio_chunk in tts_model.stream_tts_sync(response, voicevox_opt):
        print("Sending audio")
        yield audio_chunk

def example_echoback():
    algo_options = AlgoOptions(
        audio_chunk_duration=0.6,
        started_talking_threshold=0.5,
        speech_threshold=0.1,
    )
    stream = Stream(
        handler=ReplyOnPause(
            echoback,
            algo_options=algo_options,
            input_sample_rate=16000,
            output_sample_rate=16000,
        ),
        modality="audio", 
        mode="send-receive",
    )

    stream.ui.launch()

if __name__ == "__main__":
    example_echoback()

ライセンス

このプロジェクトはMITライセンスの下で公開されています。詳細はLICENSEファイルを参照してください。


fastrtcの詳細な使い方については、fastrtc公式ドキュメントを参照してください。

Metadata

Release files for fastrtc-jp 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastrtc-jp 0.1.5
File Size Uploaded Provenance Actions
fastrtc_jp-0.1.5.tar.gz 43.8 kB Without provenance Details for fastrtc_jp-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastrtc-jp 0.1.5
File Interpreter ABI Platform Actions
fastrtc_jp-0.1.5-py3-none-any.whl Python 3 none any Details for fastrtc_jp-0.1.5-py3-none-any.whl

Total release size: 85.0 kB

Release files / fastrtc_jp-0.1.5.tar.gz

Download URL fastrtc_jp-0.1.5.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a2340bfa87c737e790a4d5f182a29c1fd7cad9da7215210f8defd40d07abc3ee
BLAKE2b-256 checksum
How to use checksums
c4fa4aa68451be70a07ee4e4fe27a7aaa6e5822e392b067f3604e4341daec05c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Release files / fastrtc_jp-0.1.5-py3-none-any.whl

Download URL fastrtc_jp-0.1.5-py3-none-any.whl
Size 41.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36e4fe1db860bfb6dfa09d5addc2456ad3213024f92ec13062752935b62594af
BLAKE2b-256 checksum
How to use checksums
598913720ee37f8afb0184bec3f854b246acb446b8df2a6607a8d10f54736fc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.17

Security for fastrtc-jp 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page