LoginSignup
4
4

More than 3 years have passed since last update.

VOICEROID2で自動読み上げ&保存させてみた2

Last updated at Posted at 2020-02-01

はじめに

前回の記事を参照していただけるとわかる通り、前回は、自動保存する際に同一ファイルに保存するだけでした。しかしhtmlファイルを書いてjavascriptで音声ファイルを読み込ませてみた結果新しく開きなおすか、別ファイルを読み込まないとダメ?見たいでした。(詳しくは分かりません笑)

具体的に

前回作ったmainプログラムファイルに、グローバル変数を用意して送信される数をカウントする。

名前を付けて保存のところで名前を付ける欄 [コンボボックス] を、Inspectツールを使って探して場所を探します。そして、mainで用意したグローバル変数をそこに入れて、別の名前つけるという構造になっています。

メイン画面

aitalk.py
import wave
import winsound as ws
import tkinter
import sys
import tkinter.messagebox as tkm
import time
from voice2 import talkVOICEROID2
from voiceroid2_1 import talkVOICEROID2_1
from voiceroid2_2 import talkVOICEROID2_2
from voiceroid2_3 import talkVOICEROID2_3
from voiceroid2_4 import talkVOICEROID2_4
import time

#globalで使えるよう定義する(回数カウント用)
setnumber = 0

root=tkinter.Tk()
root.geometry("1920x1080")
root.title(u"aoi talk")
# なんでもいいので指定の場所にpng画像を配置して下さい。 以下 例
kotonoha=tkinter.PhotoImage(file="C:/Users/***/Desktop/voice/01.png")
canvas=tkinter.Canvas(bg="white",width=475,height=750)
canvas.place(x=1300,y=250)
canvas.create_image(0,0, image=kotonoha, anchor=tkinter.NW)

def addlist(text):
    mysay="you: "+ text
    print(mysay)
    listbox.insert(tkinter.END,mysay)
    chat = "Aoi: " + talk(text)
    Entry1.delete(0, tkinter.END)
    chatCut(chat)

def chatCut(chat):
    aoi=chat
    addRep(aoi)

def addRep(aoi):
    listbox.insert(tkinter.END, aoi)
    global setnumber
    setnumber+=1
    #ボイス処理
    voiceroid=aoi[5:]
    voiceroid=voiceroid+"っ"
    word=len(voiceroid)
    destime=round(word/7+0.1,1)
    #destime=round(word/7+1.1,1)
    #デスクトップ上で喋らせない場合以下のコメントアウトを付ける
    talkVOICEROID2(voiceroid)
    time.sleep(destime)
    print(destime)
    #-----------zzz
    talkVOICEROID2_1(voiceroid)
    time.sleep(0.3)
    talkVOICEROID2_2(voiceroid,setnumber)

    time.sleep(0.2)
    talkVOICEROID2_3(voiceroid)
    time.sleep(0.2)
    talkVOICEROID2_4(voiceroid)

def talk(say):
    if say == 'end':
        return ('ではまた')
    else:
        return (say)

static=tkinter.Label(text=u"葵ちゃんに話しかけてね!")
static.pack()

Entry1=tkinter.Entry(width=50)
Entry1.insert(tkinter.END,u"こんにちは")
Entry1.pack()

button=tkinter.Button(text=u"送信", width=50,command=lambda: addlist(Entry1.get()))
button.pack()

listbox=tkinter.Listbox(width=55,height=15)
listbox.pack()

root.mainloop()

処理部分

voiceroid2_2.pyファイルのみ変更

voiceroid2_2.py
#2回目
# -*- coding: utf-8 -*-
import pywinauto

def search_child_byclassname_2(class_name, uiaElementInfo, target_all = False):
    target = []
    # 全ての子要素検索
    for childElement in uiaElementInfo.children():
        # ClassNameの一致確認

        if childElement.class_name == class_name:
            if target_all == False:
                return childElement
            else:
                target.append(childElement)
    if target_all == False:
        # 無かったらFalse
        return False
    else:
        return target

def search_child_byname_2(name, uiaElementInfo):
    # 全ての子要素検索
    for childElement in uiaElementInfo.children():
        # Nameの一致確認
        if childElement.name == name:
            return childElement
    # 無かったらFalse
    return False

def talkVOICEROID2_2(speakPhrase,num):
    setnumber=str(num) + ".wav"
    print(setnumber)
    # デスクトップのエレメント
    parentUIAElement = pywinauto.uia_element_info.UIAElementInfo()
    # voiceroidを捜索する
    voiceroid2 = search_child_byname_2("VOICEROID2",parentUIAElement)
    # *がついている場合
    if voiceroid2 == False:
        voiceroid2 = search_child_byname_2("VOICEROID2*",parentUIAElement)

    #ここから変更
    # 名前を付けて保存 要素のElementInfoを取得
    saveEle = search_child_byclassname_2("#32770",voiceroid2)

    #名前編集
    win = search_child_byclassname_2("DUIViewWndClassName",saveEle)

    winwin = search_child_byclassname_2("AppControlHost",win)

    filewin = search_child_byclassname_2("Edit",winwin)

    textBoxEditControl = pywinauto.controls.uia_controls.EditWrapper(filewin)
    textBoxEditControl.set_edit_text(setnumber)

    playsaveEle = search_child_byclassname_2("Button",saveEle,target_all = False)

    # ボタンコントロール取得
    playButtonControl = pywinauto.controls.uia_controls.ButtonWrapper(playsaveEle)

    # 再生ボタン押下
    playButtonControl.click()

最後に

今回も見事別ファイルに保存できるようになり成功しました!!
もしかしたら人によってはWindows内の構造が違うかもしれないので注意してくださいね!
(;^ω^)

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4