Created
September 16, 2024 09:59
-
-
Save 6uclz1/0d406b7f2051b920308e198c86a5cc96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyperclip | |
import time | |
# 前回のクリップボードの内容を保存する変数 | |
previous_text = "" | |
import requests | |
import io | |
import soundfile | |
import sounddevice as sd | |
# Sbv2Adapterというクラスを定義します | |
class Sbv2Adapter: | |
URL = "http://127.0.0.1:5000/voice" | |
def __init__(self) -> None: | |
pass | |
# 音声を取得するためのクエリを作成するメソッドです | |
def _create_audio_query(self, text: str) -> dict: | |
# パラメータを定義します | |
params = { | |
"text": text, | |
"speaker_id": 0, | |
"model_id": 0, | |
"length": 1, | |
"sdp_ratio": 0.2, | |
"noise": 0.6, | |
"noisew": 0.8, | |
"auto_split": True, | |
"split_interval": 1, | |
"language": "JP", | |
"style": "Neutral", | |
"style_weight": 5, | |
} | |
return params | |
# 音声をリクエストするメソッドです | |
def _create_request_audio(self, query_data: dict) -> bytes: | |
headers = {"accept": "audio/wav"} | |
response = requests.post(self.URL, params=query_data, headers=headers) | |
# エラーハンドリング | |
if response.status_code != 200: | |
raise Exception(f"Error: {response.status_code}") | |
return response.content | |
# 音声を取得するメソッドです | |
def get_voice(self, text: str): | |
query_data = self._create_audio_query(text) | |
audio_bytes = self._create_request_audio(query_data) | |
audio_stream = io.BytesIO(audio_bytes) | |
data, sample_rate = soundfile.read(audio_stream) | |
return data, sample_rate | |
adapter = Sbv2Adapter() | |
while True: | |
# 現在のクリップボードの内容を取得 | |
current_text = pyperclip.paste() | |
# クリップボードの内容が前回と異なる場合にprint出力 | |
if current_text != previous_text: | |
previous_text = current_text | |
print(current_text.splitlines()) | |
for t in current_text.splitlines(): | |
if t != '': | |
print(f"クリップボードの内容: {t}") | |
data, sample_rate = adapter.get_voice(t) | |
sd.play(data, sample_rate) | |
sd.wait() | |
# 3秒ごとにクリップボードをチェック | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment