Skip to main content

A module kit for Fast RTC in Japanese

Project description

fastrtc-jp

License: MIT Python 3.11+ PyPI version

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

概要

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

主な機能:

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

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

提供モデル一覧

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

システム要件

  • Python 3.12以上
  • ubuntu 24.04 及び MacOS 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: 話者ID(整数)VOICEVOXのドキュメントなどでIDを探して下さい。
    • speedScale: 話速スケール(デフォルト: 1.0)
StyleBertVits2を使用する場合:

Style-Bert-Vits2は、高品質な音声合成が可能なTTSモデルです。事前学習済みモデルを使って、自然な日本語音声を生成できます。 Style-Bert-Vits2と音声モデルの使用条件については公式ドキュメントなどで確認して下さい。 詳細はstyle-bert-vits2リポジトリを参照して下さい。

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

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

  • StyleBertVits2Optionsクラス

    • model_path: モデルファイルのパス
    • config_path: 設定ファイルのパス
    • style_vec_path: スタイルベクトルファイルのパス
    • device: 使用するデバイス(デフォルト: "cpu")
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の公式を参照してください。 一応、日本語のモデルを自動でダウンロードするようにしています。

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対応のモデルを自動的にダウンロードするようにしています。

GoogleSTTを使用する場合:

詳細はspeech-recognitionリポジトリを参照して下さい。 v3.10でgoogleのapiを使用するので、インターネット接続が必要です。

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=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.2.tar.gz (17.6 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.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastrtc_jp-0.1.2.tar.gz
  • Upload date:
  • Size: 17.6 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.2.tar.gz
Algorithm Hash digest
SHA256 b65c1f130841bf18ad22b798fdb7ea1cb800cb1c61c80519a3b5ff2db166fcc6
MD5 d5e24dbb15f22b082f80484988be1da5
BLAKE2b-256 f4313bfa89711ce6b34f4a86d4767f8c75387fe521a0ccf8f52303cb283ba2c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastrtc_jp-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb3b2e317d4286a1f5f584ab85c0b076729f49bfac56476d6a3bde2c46813b2f
MD5 10f81830ad637d760b9d66aafcc9f198
BLAKE2b-256 9ffbd53652e20e183947fcce67954cf221a7f64f7ec462d7d231aa10d43d5c36

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