自分の声でDISCORDの読み上げBOTを作成[COEIROINK]
※すでにmycoeiroinkにてモデル作成を済んでいる方に向けた記事です。
モデル作成、discordのbot作成は以下を参考に
環境ー用意するものー
・COEIROINK V2 COEIROINK:ダウンロード
・pycharm ※pythonのverは3.12を使用しています。
Download PyCharm: Python IDE for Professional Developers by JetBrains
・ffmpeg
https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-2024-02-26-git-a3ca4beeaa-full_build.7z
・7z Download (7-zip.org)
手順
1.coeiroinkにmycoeiroinkにて作成したモデルをspeaker_infoのファイルに入れておきます
2.coeiroinkとpycharmを起動します。
3.pycharmで新たにスクリプトを作成し以下のコードのYOUR_PATHを書き換えて実行
import json
# JSONファイルをUTF-8エンコーディングで開く
with open(r'YOUR_PATH\metas.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# JSONデータを使って何かを行う
print(data)
meats.jsonは
COEIROINK_WIN_GPU_v.2.3.4\speaker_info\作ったモデル\meats.json
に位置するのでYOUR_PATHは環境に応じてpathを入れてください
以下のような結果が得られ、これは後に使います。
※つくよみちゃんの場合
{'speakerName': 'つくよみちゃん', 'speakerUuid': '3c37646f-3881-5374-2a83-149267990abc', 'styles': [{'styleName': 'れいせい', 'styleId': 0}]}
4.pycharmで新たにスクリプトを作成して以下のコードを書き換えて実行
YOUR_SPEAKER_UUID
YOUR_SPEAKER_ID
上記の2つを手順3のjsonを基に変えてください。
この時、botのtokenもYOUR_BOT_TOKENから置き換えてください。
import os
import discord
import ffmpeg
import json
import requests
import asyncio
from discord.ext import commands
# DiscordのBotトークン
TOKEN = 'YOUR_BOT_TOKEN'
# CoeroinnkのAPIエンドポイントとスピーカー情報
COEIROINK_API_URL = 'http://localhost:50032/v1/predict'
SPEAKER_UUID = 'YOUR_SPEAKER_UUID' # スピーカーUUID
SPEAKER_ID = YOUR_SPEAKER_ID # スピーカーID
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
in_voice = {}
vc = None
call_channel = {}
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command(name='join')
async def join(ctx):
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
if ctx.guild.voice_client is None:
vc = await channel.connect()
await ctx.send(f'Joined {channel.name}')
in_voice[ctx.guild.id] = vc
call_channel[ctx.guild.id] = ctx.channel
else:
await ctx.send('Already in a voice channel')
@bot.command(name='leave')
async def leave(ctx):
if ctx.guild.id in in_voice:
vc = in_voice[ctx.guild.id]
await vc.disconnect()
await ctx.send('Left the voice channel')
del in_voice[ctx.guild.id]
del call_channel[ctx.guild.id]
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.guild and message.guild.id in in_voice and message.content and not message.content.startswith('!'):
if call_channel[message.guild.id] != message.channel:
return
vc = in_voice[message.guild.id]
# Coeroinnkを使用して音声を生成
response = requests.post(
COEIROINK_API_URL,
json={
'text': message.content,
'speakerUuid': SPEAKER_UUID,
'styleId': SPEAKER_ID,
'prosodyDetail': None,
'speedScale': 1
})
# 一意の一時ファイル名を生成
temp_file = f'temp_{message.id}.wav'
# 生成した音声を一時ファイルに保存
with open(temp_file, 'wb') as f:
f.write(response.content)
# 生成した音声を再生
vc.play(discord.FFmpegPCMAudio(temp_file, executable="C:\\ffmpeg-2024-02-26-git-a3ca4beeaa-full_build\\bin\\ffmpeg.exe"), after=lambda e: os.remove(temp_file))
await bot.process_commands(message)
bot.run(TOKEN)
エラーの対処
必要なライブラリはpycharmのターミナルでinstallしてください。
※コードを実行しffmpegがみつからない場合は
スタートメニュー→設定→システム→詳細情報→システムの詳細設定→環境変数
より変数名FFMPEG_PATHでffmpeg.exeを指定してください。
できなかった場合は変数名PATHで試してみてください
botコマンド一覧
!join bot参加
!leave bot退出
有料note宣伝
追加内容
・コマンドをいくつか追加しました。
・話者を追加できるようにしました。
追加コマンド
・!use コマンドと使い方の一覧がでます
・!speed 話者の話す早さの変更(0.5~2.0)
・!speaker 話者を変更することができます。初期値1
・!speaker_name 話者の一覧をみれます
・!role : そのロールをつけている人のみ読み上げるように変更できます。空で入力すると現在のロールが確認できます。
・!role_reset : 読み上げ対象を全員に戻します。
・!name : メンションで読み上げ対象を選択します。
・!name_reset : 読み上げ対象を全員に戻します。
この記事が気に入ったらサポートをしてみませんか?