Skip to main content

A module kit for Fast RTC in Japanese

Project description

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公式ドキュメントを参照してください。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastrtc_jp-0.1.3.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastrtc_jp-0.1.3-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file fastrtc_jp-0.1.3.tar.gz.

File metadata

  • Download URL: fastrtc_jp-0.1.3.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for fastrtc_jp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 33b3472de6c75b08cfd01f8fb96e09336f7e593d3b3d3af5d3d3571f0ad06b7c
MD5 383da779568cecc8c51a4b9277028609
BLAKE2b-256 3b9ccff4fc95f50199e58ff1406b872f642cc2a8763886daafd81e2659cdf136

See more details on using hashes here.

File details

Details for the file fastrtc_jp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: fastrtc_jp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for fastrtc_jp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4a07fbab4e0e4259b884ef67be08e4e22c25f40200930497e0b16073d0bd2090
MD5 6acfdc69eb2594d353f85e9db7f2a4fb
BLAKE2b-256 145ea08f3e338485af0766f9523dedae840d99f6b6c1d294b77e720d013e2358

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page