Python3系
テキストファイル(utf8)を読み込んで、その内容をぴこぴこすぴーち!!に転送する
※エラー処理とか最低限
pip install websocket-client
read.txt
病ンデる東北きりたん>こんにちは
病ンデる東北きりたん>東北きりたんです
病ンデる琴葉茜>こんにちは
病ンデる琴葉茜>琴葉茜です
text2speach.py
import sys
import json
import codecs
import ssl
import websocket
#
class App():
#
def __init__( self, url, txt ):
#
self.ws = websocket.WebSocketApp(
"ws://" + url + "/ws/main",
on_message = self.on_message,
on_open = self.on_open,
on_error = self.on_error
)
#
self.txt = []
with codecs.open( txt, encoding = "utf-8" ) as fp:
for line in fp:
line = line.rstrip()
if len( line ) == 0:
continue
self.txt.append( line )
#
self.alias = {}
#
self.ws.run_forever( sslopt = { "cert_reqs" : ssl.CERT_NONE } )
#
def on_open( self, ws ):
print( "open" )
#
if len( self.txt ) == 0:
self.ws.close()
return
# 空文を送ってon_message待ちにする
self.send( ">" )
#
def on_message( self, ws, msg ):
cmd = json.loads( msg )
print( cmd )
# 前回の発話が終わってから次の文を送信
# (連続で送りつけてもqueueには積む)
if cmd[ "cmd" ] == "voice_end":
# バッファが無くなれば終了
if len( self.txt ) == 0:
self.ws.close()
return
txt = self.txt[ 0 ]
self.txt.pop( 0 )
print( txt )
self.send( txt )
#
def on_error( self, ws, error ):
print( error )
#
def send( self, msg ):
title, text = msg.split( ">", 1 )
if title in self.alias:
title = self.alias[ title ]
self.ws.send(
json.dumps(
{
"cmd" : "msg",
"msg" : text,
"title" : title,
"type" : "Voiceroid2"
}
)
)
#
def main():
url = "localhost:8008"
txt = "read.txt"
url = sys.argv[ 1 ]
txt = sys.argv[ 2 ]
App( url, txt )
return 0
if __name__ == "__main__":
sys.exit( main() )