#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import queue
import threading
import subprocess
import datetime
import time
import codecs

import pyaudio
import speech_recognition as sr
import wave
import pygame.mixer
import json

from googletrans import Translator
from gtts import gTTS
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud import LanguageTranslatorV2
from watson_developer_cloud import TextToSpeechV1
import requests
import io
import uuid
import types
import xml.etree.ElementTree as ET
import ctypes

print(os.path.dirname(__file__))
print(os.path.basename(__file__))
print(sys.version_info)



srr  = sr.Recognizer()
srr2 = sr.Recognizer()

# Azure
AZURE_SPEECH_KEY = 'xx'

# Google
google_translator = Translator()
GOOGLE_CLOUD_SPEECH_CREDENTIALS = r"""
{
}
"""

# Watson
watson_STT = None
watson_TTS = None
watson_translator = None
try:
    watson_STT = SpeechToTextV1(
            username='xx',
            password='xx',
            x_watson_learning_opt_out=False)
    watson_translator = LanguageTranslatorV2(
            username='xx',
            password='xx')
    watson_TTS = TextToSpeechV1(
            username='xx',
            password='xx',
            x_watson_learning_opt_out=True)
except:
    watson_STT = None
    watson_TTS = None
    watson_translator = None

class BingSpeechAPI:
    global AZURE_SPEECH_KEY
    def __init__(self):
        self.key = AZURE_SPEECH_KEY
        self.access_token = None
        self.expire_time = None
        self.locales = {
            'ar-eg': {'Female': 'Microsoft Server Speech Text to Speech Voice (ar-EG, Hoda)'},
            'de-DE': {'Female': 'Microsoft Server Speech Text to Speech Voice (de-DE, Hedda)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (de-DE, Stefan, Apollo)'},
            'en-AU': {'Female': 'Microsoft Server Speech Text to Speech Voice (en-AU, Catherine)'},
            'en-CA': {'Female': 'Microsoft Server Speech Text to Speech Voice (en-CA, Linda)'},
            'en-GB': {'Female': 'Microsoft Server Speech Text to Speech Voice (en-GB, Susan, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)'},
            'en-IN': {'Male': 'Microsoft Server Speech Text to Speech Voice (en-IN, Ravi, Apollo)'},
            'en-US': {'Female': 'Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)'},
            'es-ES': {'Female': 'Microsoft Server Speech Text to Speech Voice (es-ES, Laura, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (es-ES, Pablo, Apollo)'},
            'es-MX': {'Male': 'Microsoft Server Speech Text to Speech Voice (es-MX, Raul, Apollo)'},
            'fr-CA': {'Female': 'Microsoft Server Speech Text to Speech Voice (fr-CA, Caroline)'},
            'fr-FR': {'Female': 'Microsoft Server Speech Text to Speech Voice (fr-FR, Julie, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (fr-FR, Paul, Apollo)'},
            'it-IT': {'Male': 'Microsoft Server Speech Text to Speech Voice (it-IT, Cosimo, Apollo)'},
            'ja-JP': {'Female': 'Microsoft Server Speech Text to Speech Voice (ja-JP, Ayumi, Apollo)',
                      'Femal2': 'Microsoft Server Speech Text to Speech Voice (ja-JP, HarukaRUS)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (ja-JP, Ichiro, Apollo)'},
            'pt-BR': {'Male': 'Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)'},
            'ru-RU': {'Female': 'Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (ru-RU, Pavel, Apollo)'},
            'zh-CN': {'Female': 'Microsoft Server Speech Text to Speech Voice (zh-CN, HuihuiRUS)',
                      'Female2': 'Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (zh-CN, Kangkang, Apollo)'},
            'zh-HK': {'Female': 'Microsoft Server Speech Text to Speech Voice (zh-HK, Tracy, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (zh-HK, Danny, Apollo)'},
            'zh-TW': {'Female': 'Microsoft Server Speech Text to Speech Voice (zh-TW, Yating, Apollo)',
                      'Male': 'Microsoft Server Speech Text to Speech Voice (zh-TW, Zhiwei, Apollo)'}
        }

    def authenticate(self):
            credential_url = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'
            headers = {'Ocp-Apim-Subscription-Key': self.key}

            response = requests.post(credential_url, headers=headers)

            if response.status_code != 200:
                raise RequestError('http request error with status code {}'.format(response.status_code))

            self.access_token = response.content

    def recognize(self, audio_data, language='ja-JP'):
        self.authenticate()
        if isinstance(audio_data, types.GeneratorType):
            def generate(audio):
                yield self.get_wav_header()
                for a in audio:
                    yield a
        
            datas = generate(audio_data)
        else:
            datas = self.to_wav(audio_data)

        params = {
            'version': '3.0',
            'requestid': uuid.uuid4(),
            'appID': 'xx',
            'format': 'json',
            'locale': language,
            'device.os': 'wp7',
            'scenarios': 'ulm',
            'instanceid': uuid.uuid4(),
        }

        headers = {
            'Authorization': 'Bearer ' + self.access_token.decode(encoding='utf-8'),
            'Content-Type': 'audio/wav; samplerate=16000',
        }

        url = 'https://speech.platform.bing.com/recognize/query'
        response = requests.post(url, params=params, headers=headers, data=datas)

        if response.status_code != 200:
            raise RequestError('http request error with status code {}'.format(response.status_code))

        #response.encoding='UTF-8'
        result = response.json()

        if 'header' not in result or 'lexical' not in result['header']:
            raise ValueError('Unexpected response: {}'.format(result))
        return result['header']['lexical']

    def synthesize(self, text, language='ja-JP', gender='Female'):
        self.authenticate()

        if language not in self.locales.keys():
            raise ValueError('language is not supported.')
        lang = self.locales.get(language)
        #if len(lang.keys()) == 1:
        #    gender       = lang.keys()[0]
        #    #service_name = lang.keys()[0][0]
        service_name = lang[gender]
        if gender in ['Female','Femal2']:
            gender = 'Female'

        body  = "<speak version='1.0' xml:lang='en-US'>"
        body += "<voice xml:lang='%s' xml:gender='%s' name='%s'>" % (language, gender, service_name)
        #if language!='ja-JP':
        #    body += "<prosody rate='slow'>"
        #    body += text
        #    body += '</prosody>'
        #else:
        body += text
        body += '</voice></speak>'
        datas = body.encode('UTF-8')

        headers = {
            'Content-type': 'application/ssml+xml; charset=utf8',
            'X-Microsoft-OutputFormat': 'raw-16khz-16bit-mono-pcm',
            'Authorization': 'Bearer ' + self.access_token.decode(encoding='utf-8'),
            'X-Search-AppId': 'xx',
            'X-Search-ClientID': str(uuid.uuid1()).replace('-', ''),
            'User-Agent': 'TTSForPython',
        }

        url = 'https://speech.platform.bing.com/synthesize'
        response = requests.post(url, headers=headers, data=datas, stream=None)
        data = response.content

        return data

    @staticmethod
    def to_wav(raw_data):
        # generate the WAV file contents
        with io.BytesIO() as wav_file:
            wav_writer = wave.open(wav_file, 'wb')
            try:  # note that we can't use context manager, since that was only added in Python 3.4
                wav_writer.setframerate(16000)
                wav_writer.setsampwidth(2)
                wav_writer.setnchannels(1)
                wav_writer.writeframes(raw_data)
                wav_data = wav_file.getvalue()
            finally:  # make sure resources are cleaned up
                wav_writer.close()
        return wav_data

    @staticmethod
    def get_wav_header():
        # generate the WAV header
        with io.BytesIO() as f:
            w = wave.open(f, 'wb')
            try:
                w.setframerate(16000)
                w.setsampwidth(2)
                w.setnchannels(1)
                w.writeframes('')
                header = f.getvalue()
            finally:
                w.close()
        return header



def qVoiceInput(useApi, inpLang='ja', waveFile=None, outputQ=None, ):
    result=''

    api=useApi
    lng=''
    mdl=''
    if api=='watson' or api=='azure':
        if inpLang=='ar':
            lng='ar-AR'
            mdl='ar-AR_BroadbandModel'
        if inpLang=='en' or inpLang=='en-US':
            lng='en-US'
            mdl='en-US_BroadbandModel'
        if inpLang=='es':
            lng='es-ES'
            mdl='es-ES_BroadbandModel'
        if inpLang=='de':
            lng='de-DE'
            mdl='de-DE_BroadbandModel'
        if inpLang=='fr':
            lng='fr-FR'
            mdl='fr-FR_BroadbandModel'
        if inpLang=='it':
            lng='it-IT'
            mdl='it-IT_BroadbandModel'
        if inpLang=='ja' or inpLang=='ja-JP':
            lng='ja-JP'
            mdl='ja-JP_BroadbandModel'
        if inpLang=='pt':
            lng='pt-BR'
            mdl='pt-BR_BroadbandModel'
        if inpLang=='zh' or inpLang=='zh-CN':
            lng='zh-CN'
            mdl='zh-CN_BroadbandModel'
        if lng == '':
            api='free'

    if api=='watson':
        try:
            rb=open(waveFile, 'rb')
            res = watson_STT.recognize(rb, model=mdl, content_type='audio/wav',
               timestamps=True, word_confidence=True)
            rb.close()
            rb=None

            result = res['results'][0]['alternatives'][0]['transcript']
            if inpLang=='ja' or inpLang=='ja-JP':
                 result = str(result).replace(' ', '')
            result = str(result).strip()
        except:
            print(res)

    if api=='azure':
        rs = wave.open(waveFile, 'rb')
        rawdata = rs.readframes(rs.getnframes())
        rs.close()
        rs = None
        #with sr.AudioFile(waveFile) as source:
        #    audio = srr2.record(source)

        try:
            bing = BingSpeechAPI()
            result = bing.recognize(rawdata, language=lng)
            #result = srr2.recognize_bing(audio, key='xx', language=lng)
            if result.lower() == 'x':
                result = ''
        except:
            pass

    if api=='google' or api=='free':
        with sr.AudioFile(waveFile) as source:
            audio = srr2.record(source)

        try:
            if api=='free':
                result=srr2.recognize_google(audio, language=inpLang)
            else:
                result = srr2.recognize_google_cloud(audio,
                    credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS,
                    language=inpLang)
            result = str(result).strip()
        except:
            pass

        if result != '':
            if useApi != 'google' and useApi != 'free':
                result = '!Google, ' + result

    return result



def qTranslator(useApi, inpLang='ja', outLang='en', transText=u'こんにちわ', outputQ=None, ):
    result=''

    if transText[0:9] != '!Google, ':
        trntxt=transText
    else:
        trntxt=transText[9:]

    api=useApi
    inp=''
    out=''
    if inpLang == outLang:
        api = 'none'

    if api=='watson' or api=='azure':
        if inpLang=='ar':
            inp='ar-AR'
        if inpLang=='en' or inpLang=='en-US':
            inp='en-US'
        if inpLang=='es':
            inp='es-ES'
        if inpLang=='de':
            inp='de-DE'
        if inpLang=='fr':
            inp='fr-FR'
        if inpLang=='it':
            inp='it-IT'
        if inpLang=='ja' or inpLang=='ja-JP':
            inp='ja-JP'
        if inpLang=='pt':
            inp='pt-BR'
        if inpLang=='zh' or inpLang=='zh-CN':
            inp='zh-CN'

        if outLang=='ar':
            out='ar-AR'
        if outLang=='en' or outLang=='en-US':
            out='en-US'
        if outLang=='es':
            out='es-US'
        if outLang=='de':
            out='de-DE'
        if outLang=='fr':
            out='fr-FR'
        if outLang=='it':
            out='it-IT'
        if outLang=='ja' or outLang=='ja-JP':
            out='ja-JP'
        if outLang=='pt':
            out='pt-BR'
        if outLang=='zh' or outLang=='zh-CN':
            out='zh-CN'

        if inp == '' or out == '':
            api='free'

    t=trntxt.replace(' ','_')
    t=t.replace(u'　','_')
    t=t.replace('"','_')
    t=t.replace('$','_')
    t=t.replace('%','_')
    t=t.replace('&','_')
    t=t.replace("'",'_')
    t=t.replace('\\','_')
    t=t.replace('|','_')
    t=t.replace('*','_')
    t=t.replace('/','_')
    t=t.replace('?','_')
    t=t.replace(':',',')
    t=t.replace('<','_')
    t=t.replace('>','_')

    cacheFile='temp/cache/' + t + '_' + inpLang + '_' + outLang + '_' + api + '_utf8.txt'

    apirun = True
    if api != 'none':
        if os.path.exists(cacheFile):
            try:
                txt = ''
                rt = codecs.open(cacheFile, 'r', 'utf-8')
                for t in rt:
                    txt = (txt + ' ' + str(t)).strip()
                rt.close
                rt = None

                result=txt
                if reslut != '':
                    apirun = False
            except:
                rt = None

    if apirun == True:

        if api == 'none':
           result = trntxt

        if api=='watson':
            res  = ''
            try:
                if inp=='en-US' or out=='en-US':
                    data = watson_translator.translate(text=trntxt, source=inp, target=out)
                    try:
                        res = data['translations'][0]['translation']
                        res = str(res).strip()
                    except:
                        print(data)
                else:
                    data1 = watson_translator.translate(text=trntxt, source=inp, target='en-US')
                    try:
                        res1 = data1['translations'][0]['translation']
                        res1 = str(res1).strip()
                        data2 = watson_translator.translate(text=res1, source='en-US', target=out)
                        try:
                            res2 = data2['translations'][0]['translation']
                            res2 = str(res2).strip()
                            res  = res2
                        except:
                            print(data2)
                    except:
                        print(data1)
            except:
                pass
            if res != '':
                result=res
            else:
                api='free'

        if api=='azure':
            res  = ''
            try:
                token = requests.post('https://api.cognitive.microsoft.com/sts/v1.0/issueToken',
                    headers = {'Content-Type': 'application/json',
                               'Ocp-Apim-Subscription-Key': 'xx',})
                res = requests.get('https://api.microsofttranslator.com/v2/http.svc/Translate',
                    headers = {'Content-Type': 'application/xml',},
                    params={'appid': 'Bearer ' + token.text,
                            'text': trntxt,
                            'from': inp,
                            'to': out,
                            'category': 'general',})
                resxml = res.text
                res = ET.fromstring(resxml).text
            except:
                pass
            if res != '':
                result=res
                if result.lower() == 'x':
                    result = ''
            else:
                api='free'

        if api=='google' or api=='free':
            res = ''
            txtary = google_translator.translate([trntxt], src=inpLang, dest=outLang)
            for t in txtary:
                res += t.text
            res = str(res).strip()
            if res != '':
                result=res
                if useApi != 'google' and useApi != 'free':
                    result= '!Google, ' + res

    if apirun == True and result != '':
        if api != 'none':
            try:
                w = codecs.open(cacheFile, 'w', 'utf-8')
                w.write(result)
                w.close()
                w = None
            except:
                w = None

    return result



qVoiceOutput_count=0
def qVoiceOutput(useApi, outLang='en', outText='Hallo', tempFile=None, outq=None, outputQ=None, ):
    global qVoiceOutput_count
    qVoiceOutput_count += 1

    if outText[0:9] != '!Google, ':
        outtxt=outText
    else:
        outtxt=outText[9:]

    api=useApi
    lng  =''
    voice=''
    if api=='watson' or api=='azure':
        if outLang=='en' or outLang=='en-US':
            lng  ='en-US'
            voice='en-US_AllisonVoice'
        if outLang=='es':
            lng  ='es-US'
            voice='es-US_SofiaVoice'
        if outLang=='de':
            lng  ='de-DE'
            voice='de-DE_BirgitVoice'
        if outLang=='fr':
            lng  ='fr-FR'
            voice='fr-FR_ReneeVoice'
        if outLang=='it':
            lng  ='it-IT'
            voice='it-IT_FrancescaVoice'
        if outLang=='ja' or outLang=='ja-JP':
            lng  ='ja-JP'
            voice='ja-JP_EmiVoice'
        if outLang=='pt':
            lng  ='pt-BR'
            voice='pt-BR_IsabelaVoice'
        if lng == '':
            api='free'

    t=outtxt.replace(' ','_')
    t=t.replace(u'　','_')
    t=t.replace('"','_')
    t=t.replace('$','_')
    t=t.replace('%','_')
    t=t.replace('&','_')
    t=t.replace("'",'_')
    t=t.replace('\\','_')
    t=t.replace('|','_')
    t=t.replace('*','_')
    t=t.replace('/','_')
    t=t.replace('?','_')
    t=t.replace(':',',')
    t=t.replace('<','_')
    t=t.replace('>','_')

    now=datetime.datetime.now()
    #stamp=now.strftime('%S')
    #stamp=now.strftime('%S.%f')
    stamp='{0:02d}'.format(qVoiceOutput_count % 100)
    if api != 'azure':
        cacheFile='temp/cache/' + t + '_' + outLang + '_' + api + '.mp3'
        tempFilex='temp/temp_voice_' + stamp + '.mp3'
    else:
        cacheFile='temp/cache/' + t + '_' + outLang + '_' + api + '.wav'
        tempFilex='temp/temp_voice_' + stamp + '.wav'

    if tempFile is None:
        tempFile = tempFilex

    apirun = True
    if os.path.exists(cacheFile):

        try:
            rb = open(cacheFile, 'rb')
            wav=rb.read()
            rb.close
            rb = None

            wb = open(tempFile, 'wb')
            wb.write(wav)
            wb.close
            wb = None

            apirun = False
        except:
            rb = None
            wb = None

    if apirun == True:

        if tempFile is None:
            tempFile=tempFilex

        if api=='watson':
            audio=watson_TTS.synthesize(outtxt, accept='audio/mp3', voice=voice)
            wb = open(tempFile, 'wb')
            wb.write(audio)
            wb.close()
            wb = None

        if api=='azure':
            bing = BingSpeechAPI()
            if lng=='ja-JP':
                audio = bing.synthesize(outtxt, language=lng, gender='Femal2')
                #audio = bing.synthesize(outtxt, language=lng, gender='Female')
            else:
                if lng != 'it-IT' and lng != 'pt-BR':
                    audio = bing.synthesize(outtxt, language=lng, gender='Female')
                else:
                    audio = bing.synthesize(outtxt, language=lng, gender='Male')
            wb = open(tempFile, 'wb')
            wb.write(bing.to_wav(audio))
            wb.close()
            wb = None

        if api=='google' or api=='free':
            tts = gTTS(text=outtxt, lang=outLang, slow=False)
            tts.save(tempFile)
            if useApi != 'google' and useApi != 'free':
                outtxt = '!Google, ' + outtxt

    if apirun == True:
        try:
            rb = open(tempFile, 'rb')
            wav=rb.read()
            rb.close
            rb = None

            wb = open(cacheFile, 'wb')
            wb.write(wav)
            wb.close
            wb = None
        except:
            rb = None
            wb = None

    qPlay(tempFile, outputQ=outputQ)

    return outtxt



def qVoiceApiInfo(inpApi=None, trnApi=None, outApi=None, outputQ=None, ):

    if not inpApi is None:
        vtext = 'Speech recognition uses '
        if inpApi == 'free':
            vtext += 'free Google speech API,'
            vhalo  = 'Hello, I am free Google.'
        if inpApi == 'google':
            vtext += 'Google Cloud Platform,'
            vhalo  = 'Hello, I am Google.'
        if inpApi == 'watson':
            vtext += 'IBM Watson Developer Cloud,'
            vhalo  = 'Hello, I am IBM Watson.'
        if inpApi == 'azure':
            vtext += 'Microsoft Azure Cognitive Services,'
            vhalo  = 'Hello, I am Microsoft Azure.'
        vtext += ' Speech to Text function,'

        qVoiceOutput(useApi=inpApi, outLang='en', outText=vhalo, outputQ=outputQ)
        qVoiceOutput(useApi=inpApi, outLang='en', outText=vtext, outputQ=outputQ)

    if not trnApi is None:
        vtext = 'Text Translation uses '
        if trnApi == 'free':
            vtext += 'free Google speech API,'
            vhalo  = 'Hello, I am free Google.'
        if trnApi == 'google':
            vtext += 'Google Cloud Platform,'
            vhalo  = 'Hello, I am Google.'
        if trnApi == 'watson':
            vtext += 'IBM Watson Developer Cloud,'
            vhalo  = 'Hello, I am IBM Watson.'
        if trnApi == 'azure':
            vtext += 'Microsoft Azure Cognitive Services,'
            vhalo  = 'Hello, I am Microsoft Azure.'
        vtext += ' Translation function,'

        if trnApi != inpApi:
            qVoiceOutput(useApi=trnApi, outLang='en', outText=vhalo, outputQ=outputQ)
        qVoiceOutput(useApi=trnApi, outLang='en', outText=vtext, outputQ=outputQ)

    if not outApi is None:
        vtext = 'Text Vocalization uses '
        if outApi == 'free':
            vtext += 'free Google speech API,'
            vhalo  = 'Hello, I am free Google.'
        if outApi == 'google':
            vtext += 'Google Cloud Platform,'
            vhalo  = 'Hello, I am Google.'
        if outApi == 'watson':
            vtext += 'IBM Watson Developer Cloud,'
            vhalo  = 'Hello, I am IBM Watson.'
        if outApi == 'azure':
            vtext += 'Microsoft Azure Cognitive Services,'
            vhalo  = 'Hello, I am Microsoft Azure.'
        vtext += ' Text to Speech function,'

        if outApi != inpApi and outApi != trnApi:
            qVoiceOutput(useApi=outApi, outLang='en', outText=vhalo, outputQ=outputQ)
        qVoiceOutput(useApi=outApi, outLang='en', outText=vtext, outputQ=outputQ)



def qPlay(tempFile=None, outputQ=None, sync=True):
    global inpBusy
    global inpLast
    global playBusy
    global playLast

    if (not tempFile is None) and (not outputQ is None):
        outputQ.put(['file', tempFile])
        outputQ.put(['file', '_!playing'])
    else:

        if not tempFile is None:

            playBusy = False

            if (outputQ is None) and (sync == True):
                if inpBusy != False:
                    print('qPlay___:' + str(inpBusy) + '...wait')
                    chktime = time.time()
                    while (inpBusy != False) and (int(time.time() - chktime) < 5):
                        time.sleep(0.1)
                        inpSec = int(time.time() - inpLast)
                #inpSec  = int(time.time() - inpLast)
                #if inpSec < 1:
                #    chktime = time.time()
                #    while (inpSec < 1)  and (int(time.time() - chktime) < 5):
                #        time.sleep(0.1)
                #        inpSec = int(time.time() - inpLast)

            playBusy = 'Playing'
            playLast = time.time()

            #if os.name != 'nt':
            #    pygame.mixer.init()
            #    pygame.mixer.music.load(tempFile)
            #    pygame.mixer.music.play()
            #    if sync == True:
            #        while pygame.mixer.music.get_busy():
            #            time.sleep(0.3)
            #        pygame.mixer.music.stop()
            #else:
            if tempFile == '_ready':
                tempFile = '_sound_ready.mp3'
            if tempFile == '_accept':
                tempFile = '_sound_accept.mp3'
            if tempFile == '_ok':
                tempFile = '_sound_ok.mp3'
            if tempFile == '_ng':
                tempFile = '_sound_ng.mp3'
            if tempFile == '_shutter':
                tempFile = '_sound_shutter.mp3'
            if os.path.exists(tempFile):
                cmd =  ['sox', tempFile, '-d', '-q']
                #cmd = ['sox', tempFile, '-b', '8', '-u', '-r', '8000', '-c', '1', '-d', '-q']
                #cmd = ['sox', tempFile, '-r', '8000', '-c', '1', '-d', '-q']
                p=subprocess.Popen(cmd)
                if sync == True:
                    p.wait()

            playBusy = False
            playLast = time.time()

    playBusy = False



def sub_input_wait(mictype, julius_adingui, julius_beatgui, outputQ, outputQ2, playtextQ, playtextQ2,):
    global inpBusy
    global inpLast
    global compBusy
    global compLast
    global textBusy
    global textLast
    global outBusy
    global outLast
    global playBusy
    global playLast

    if mictype != 'usb':
        inpWait  = 0
        compWait = 0
        textWait = 3
        outWait  = 2
        playWait = 2
    else:
        inpWait  = 0
        compWait = 0
        textWait = 3
        outWait  = 2
        playWait = 1

    inpSec  = int(time.time() - inpLast)
    compSec = int(time.time() - compLast)
    textSec = int(time.time() - textLast)
    outSec  = int(time.time() - outLast)
    playSec = int(time.time() - playLast)
    while (playtextQ.qsize() > 0 or playtextQ2.qsize() > 0) \
       or (outputQ.qsize() > 0   or outputQ2.qsize() > 0)   \
       or (compBusy != False and compWait > 0) \
       or (compSec  < compWait) \
       or (textBusy != False and textWait > 0) \
       or (textSec  < textWait) \
       or (outBusy  != False  and outWait > 0) \
       or (outSec   < outWait ) \
       or (playBusy != False and playWait > 0) \
       or (playSec  < playWait) :

        if not julius_adingui is None and os.name == 'nt':
            if int(time.time() - julius_beatgui) >= 1:
                try:
                    hWnd = ctypes.windll.user32.FindWindowW('SDL_app',None)
                    if hWnd is not 0:
                        ctypes.windll.user32.ShowWindow(hWnd, 2)
                except:
                    pass

        if (playtextQ.qsize() > 0 or playtextQ2.qsize() > 0):
            print('input___:playtextQ...wait')
            chktime = time.time()
            while (playtextQ.qsize() > 0 or playtextQ2.qsize() > 0) \
              and (int(time.time() - chktime) < 5):
                time.sleep(0.1)

        if (outputQ.qsize() > 0 or outputQ2.qsize() > 0):
            print('input___:outputQ...wait')
            chktime = time.time()
            while (outputQ.qsize() > 0 or outputQ2.qsize() > 0) \
              and (int(time.time() - chktime) < 5):
                time.sleep(0.1)

        compSec = int(time.time() - compLast)
        if (compBusy != False and compWait > 0) or (compSec < compWait):
            print('input___:compBusy ' + str(compBusy) + '...wait')
            chktime = time.time()
            while (compBusy != False and compWait > 0) and (int(time.time() - chktime) < 5):
                time.sleep(0.1)
                compSec = int(time.time() - compLast)
            while (compSec < compWait):
                time.sleep(0.1)
                compSec = int(time.time() - compLast)

        textSec = int(time.time() - textLast)
        if (textBusy != False and textWait > 0) or (textSec < textWait):
            print('input___:textBusy ' + str(textBusy) + '...wait')
            chktime = time.time()
            while (textBusy != False and textWait > 0) and (int(time.time() - chktime) < 5):
                time.sleep(0.1)
                textSec = int(time.time() - textLast)
            while (textSec < textWait):
                time.sleep(0.1)
                textSec = int(time.time() - textLast)

        outSec = int(time.time() - outLast)
        if (outBusy != False and outWait > 0) or (outSec < outWait):
            print('input___:outBusy ' + str(outBusy) + '...wait')
            chktime = time.time()
            while (outBusy != False and outWait > 0) and (int(time.time() - chktime) < 5):
                time.sleep(0.1)
                outSec = int(time.time() - outLast)
            while (outSec < outWait):
                time.sleep(0.1)
                outSec = int(time.time() - outLast)

        playSec = int(time.time() - playLast)
        if (playBusy != False and playWait > 0) or (playLast < playWait):
            print('input___:playBusy ' + str(playBusy) + '...wait')
            chktime = time.time()
            while (playBusy != False and playWait > 0) and (int(time.time() - chktime) < 5):
                time.sleep(0.1)
                playSec = int(time.time() - playLast)
            while (playSec < playWait):
                time.sleep(0.1)
                playSec = int(time.time() - playLast)



def sub_input(cn_r, cn_s, outputQ, outputQ2, playtextQ, playtextQ2,):
    global inpApi
    global trnApi
    global outApi
    global inpLang
    global trnLang
    global outLang
    global txtLang
    global micLevel

    global inpBusy
    global inpLast

    print('input___:init')

    runmode  = cn_r.get()
    micdev   = cn_r.get()
    mictype  = cn_r.get()
    micguide = cn_r.get()
    micON    = cn_r.get()
    recSJISf = cn_r.get()
    cn_r.task_done()

    print('input___:runmode =' + str(runmode ))
    print('input___:micdev  =' + str(micdev  ))
    print('input___:mictype =' + str(mictype ))
    print('input___:micguide=' + str(micguide))
    print('input___:micON   =' + str(micON   ))
    print('input___:recSJISf=' + str(recSJISf))

    print('input___:start')

    listner = 'sr'

    julius_adintool = None
    julius_adingui  = None
    julius_count    = 0
    julius_beattool = time.time()
    julius_beatgui  = time.time()
    if int(micLevel) != 0:
        adintool_exe = 'julius/adintool.exe'
        adintool_gui = 'julius/adintool-gui.exe'
        if os.path.exists(adintool_exe):
            if os.path.exists(adintool_gui):
                listner = 'julius'

    onece = True
    while True:
        if cn_r.qsize() > 0:
            cn_r_get = cn_r.get()
            mode_get = cn_r_get[0]
            data_get = cn_r_get[1]
            cn_r.task_done()

            if mode_get is None:
                print('input___:None=break')
                break

            if cn_r.qsize() != 0 or cn_s.qsize() > 2:
                print('input___: queue overflow warning!',cn_r.qsize(),cn_s.qsize())

            if mode_get == 'START':
                cn_s.put(['INIT', ''])

            elif mode_get != 'INIT' and mode_get != 'READY' and mode_get != 'NEXT':
                cn_s.put(['PASS', ''])

            elif str(micdev).lower() == 'file':
                micWave='temp/temp_micWave.wav'
                bakWave='temp/temp_bakWave.wav'

                inpBusy = micWave
                inpLast = time.time()

                now=datetime.datetime.now()
                stamp=now.strftime('%Y%m%d-%H%M%S')
                voicef = 'temp/voices/' + stamp + '_voice.wav'

                if os.path.exists(micWave):
                    if os.path.exists(bakWave):
                        os.remove(bakWave)

                    chktime = time.time()
                    while (os.path.exists(micWave)) and (int(time.time() - chktime) < 2):
                        try:
                            os.rename(micWave, bakWave)

                            if runmode != 'number':
                                sox = subprocess.Popen(['sox', bakWave, '-r', '16000', '-b', '16', '-c', '1', voicef, \
                                                        'equalizer', '800', '1.0q', '7', 'gain', '-n', ])
                            else:
                                sox = subprocess.Popen(['sox', bakWave, '-r', '16000', '-b', '16', '-c', '1', voicef, \
                                                        'equalizer', '800', '1.0q', '7', 'gain', '-n', 'tempo', '0.9' ])
                            sox.wait()
                            sox.terminate()
                            sox = None

                        except:
                            pass
                        time.sleep(0.1)

                if not os.path.exists(voicef):
                    inpBusy = False
                    inpLast = time.time()
                    cn_s.put(['NG', ''])
                    time.sleep(0.5)

                else:
                    #print('input___:file=' + micWave + ' to ' + voicef)
                    inpBusy = False
                    inpLast = time.time()
                    cn_s.put(['stamp',  stamp])
                    cn_s.put(['voicef', voicef])
                    time.sleep(0.5)

            else:
                if micON != 'None':
                    if not os.path.exists(micON):
                        print('input___:Synchronize(' + micON + ')...wait')
                        while not os.path.exists(micON):
                            time.sleep(0.3)
                if recSJISf != 'Default' and micON != 'None':
                    if os.path.exists(recSJISf):
                        print('input___:Synchronize(' + recSJISf + ')...wait')
                        while os.path.exists(recSJISf):
                            time.sleep(0.3)

                if listner == 'julius':
                    for i in range(1,99):
                        julius_file  = 'temp/voices/julius.' + '{0:04d}'.format(i) + '.wav'
                        if os.path.exists(julius_file):
                            try:
                                os.remove(julius_file)
                            except:
                                pass

                    if mictype == 'usb':
                        if julius_adintool is None:
                            while int(time.time() - julius_beatgui) < 2:
                                time.sleep(0.1)
                            julius_adintool = subprocess.Popen([adintool_exe, '-in', 'mic', \
                                              '-rewind', '2000', '-headmargin', '500', '-tailmargin', '500', '-lv', str(micLevel), \
                                              '-out', 'file', '-filename', 'temp/voices/julius', '-startid', '1'], \
                                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, )
                            julius_count    = 0
                            julius_beattool = time.time()

                        if julius_adingui is None:
                            while int(time.time() - julius_beattool) < 2:
                                time.sleep(0.1)
                            #startup = subprocess.STARTUPINFO()
                            #startup.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                            #startup.wShowWindow = subprocess.SW_HIDE
                            julius_adingui  = subprocess.Popen([adintool_gui, '-in', 'mic', \
                                              '-rewind', '2000', '-headmargin', '500', '-tailmargin', '500', '-lv', str(micLevel),], \
                                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, )
                                              #stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startup)
                            julius_beatgui  = time.time()

                inpBusy = False
                sub_input_wait(mictype, julius_adingui, julius_beatgui, outputQ, outputQ2, playtextQ, playtextQ2,)

                if micguide != 'off':
                    qPlay('_ready', outputQ=None)

                inpBusy = 'Listning'
                inpLast = time.time()

                stamp   = ''
                voicef  = ''

                if listner == 'julius':
                    julius_hit_file = ''
                    julius_hit_sec  = 99

                    if julius_adintool is None:
                            while int(time.time() - julius_beatgui) < 2:
                                time.sleep(0.1)
                            julius_adintool = subprocess.Popen([adintool_exe, '-in', 'mic', \
                                              '-rewind', '2000', '-headmargin', '500', '-tailmargin', '500', '-lv', str(micLevel), \
                                              '-out', 'file', '-filename', 'temp/voices/julius', '-startid', '1'], \
                                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, )
                            julius_count    = 0
                            julius_beattool = time.time()

                    julius_count += 1
                    julius_file  = 'temp/voices/julius.' + '{0:04d}'.format(julius_count  ) + '.wav'
                    julius_file1 = 'temp/voices/julius.' + '{0:04d}'.format(julius_count+1) + '.wav'
                    julius_file2 = 'temp/voices/julius.' + '{0:04d}'.format(julius_count+2) + '.wav'
                    while os.path.exists(julius_file):
                        try:
                            os.remove(julius_file)
                            julius_count += 1
                            julius_file  = 'temp/voices/julius.' + '{0:04d}'.format(julius_count) + '.wav'
                        except:
                            break

                    print('input___:☆Waitting...(' + julius_file + ')')
                    wait_start=time.time()

                    julius_sec = int(time.time() - wait_start)
                    while julius_sec <= 10:

                        if julius_adingui is None:
                            while int(time.time() - julius_beattool) < 2:
                                time.sleep(0.1)
                            #startup = subprocess.STARTUPINFO()
                            #startup.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                            #startup.wShowWindow = subprocess.SW_HIDE
                            julius_adingui  = subprocess.Popen([adintool_gui, '-in', 'mic', \
                                              '-rewind', '2000', '-headmargin', '500', '-tailmargin', '500', '-lv', str(micLevel),], \
                                              stdout=subprocess.PIPE, stderr=subprocess.PIPE, )
                                              #stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startup)
                            julius_beatgui  = time.time()

                        now=datetime.datetime.now()
                        stamp=now.strftime('%Y%m%d-%H%M%S')
                        voicef = 'temp/voices/' + stamp + '_voice.wav'

                        julius_hit_file = ''
                        if julius_hit_file == '':
                            if os.path.exists(julius_file2):
                                julius_hit_file = julius_file2
                                if julius_hit_sec > julius_sec:
                                    julius_hit_sec = julius_sec
                                try:
                                    os.rename(julius_file2, voicef)
                                    break
                                except:
                                   pass
                        if julius_hit_file == '':
                            if os.path.exists(julius_file1):
                                julius_hit_file = julius_file1
                                if julius_hit_sec > julius_sec:
                                    julius_hit_sec = julius_sec
                                try:
                                    os.rename(julius_file1, voicef)
                                    break
                                except:
                                    pass
                        if julius_hit_file == '':
                            if os.path.exists(julius_file):
                                julius_hit_file = julius_file
                                if julius_hit_sec > julius_sec:
                                    julius_hit_sec = julius_sec
                                try:
                                    os.rename(julius_file, voicef)
                                    break
                                except:
                                    pass

                        if not julius_adingui is None and os.name == 'nt':
                            if int(time.time() - julius_beatgui) >= 1:
                                try:
                                    hWnd = ctypes.windll.user32.FindWindowW('SDL_app',None)
                                    if hWnd is not 0:
                                        if julius_hit_file != '' or micguide == 'off':
                                            ctypes.windll.user32.ShowWindow(hWnd, 1)
                                            ctypes.windll.user32.SetForegroundWindow(hWnd)
                                        else:
                                            ctypes.windll.user32.ShowWindow(hWnd, 2)
                                except:
                                    pass

                        stamp  = ''
                        voicef = ''
                        time.sleep(0.1)
                        julius_sec = int(time.time() - wait_start)

                if listner == 'sr':
                    print('input___:☆Listning...(' + inpLang + ')')
                    try:
                        with sr.Microphone(int(micdev)) as source:
                            try:
                                speech = srr.listen(source, timeout=5, phrase_time_limit=10)
                                data    =speech.get_wav_data(16000,2)

                                now=datetime.datetime.now()
                                stamp=now.strftime('%Y%m%d-%H%M%S')
                                voicef = 'temp/voices/' + stamp + '_voice.wav'
                                wb = open(voicef, 'wb')
                                wb.write(data)
                                wb.close
                                wb = None

                                srr.dynamic_energy_threshold = True
                                srr.adjust_for_ambient_noise(source, duration=1)
                            except:
                                srr.dynamic_energy_threshold = True
                                srr.adjust_for_ambient_noise(source, duration=5)
                                stamp  = ''
                                voicef = ''
                    except:
                        pass

                if listner == 'julius':
                    if voicef == '':
                        if julius_hit_sec <= 3 and int(micLevel) < 8888:
                            micLevel = int(int(micLevel) / 0.70)
                            if int(micLevel) > 8888:
                                micLevel = 8888
                            print('input___:micLevel=' + str(micLevel))

                if voicef != '':
                    rb = open(voicef, 'rb')
                    datasize = sys.getsizeof(rb.read())
                    rb.close
                    rb = None
                    print('input___:', datasize, 'byte')
                    if datasize<30000 or datasize>500000:
                        os.remove(voicef)
                        #stamp  = ''
                        voicef = ''
                        if listner == 'julius':
                            if datasize<30000 and int(micLevel) >= 1000:
                                if int(micLevel) == 1000:
                                    micLevel = 3000
                                micLevel = int(int(micLevel) * 0.90)
                                if int(micLevel) < 1000:
                                    micLevel = 1000
                                print('input___:micLevel=' + str(micLevel))
                            if datasize>500000 and int(micLevel) < 8888:
                                micLevel = int(int(micLevel) / 0.70)
                                if int(micLevel) > 8888:
                                    micLevel = 8888
                                print('input___:micLevel=' + str(micLevel))

                if voicef != '':
                    if micON != 'None' and not os.path.exists(micON):
                        print('input___:Synchronize(' + micON + ')...pass')
                        os.remove(voicef)
                        #stamp  = ''
                        voicef = ''

                if voicef != '':
                    voicef2 = 'temp/voices/' + stamp + '_voice2.wav'
                    try:
                        if runmode != 'number':
                            sox = subprocess.Popen(['sox', voicef, '-r', '16000', '-b', '16', '-c', '1', voicef2, \
                                                    'equalizer', '800', '1.0q', '7', 'gain', '-n', ])
                        else:
                            sox = subprocess.Popen(['sox', voicef, '-r', '16000', '-b', '16', '-c', '1', voicef2, \
                                                    'equalizer', '800', '1.0q', '7', 'gain', '-n', 'tempo', '0.9' ])
                        sox.wait()
                        sox.terminate()
                        sox = None
                        os.remove(voicef)
                        os.rename(voicef2, voicef)
                    except:
                        voicef = ''

                if voicef == '':
                    inpBusy = False
                    inpLast = time.time()

                    cn_s.put(['NG', ''])
                    if stamp != '':
                        qPlay('_ng', outputQ=None)

                else:
                    print('input___:accepting...')
                    inpBusy = False
                    inpLast = time.time()

                    cn_s.put(['stamp',  stamp])
                    cn_s.put(['voicef', voicef])
                    qPlay('_accept', outputQ=None)

                if not julius_adingui is None:
                    try:
                        julius_adingui.wait(0.05)
                        julius_adingui = None
                    except:
                        pass

                if not julius_adingui is None and os.name == 'nt':
                    if int(time.time() - julius_beatgui) >= 1:
                        try:
                            hWnd = ctypes.windll.user32.FindWindowW('SDL_app',None)
                            if hWnd is not 0:
                                ctypes.windll.user32.ShowWindow(hWnd, 2)
                        except:
                            pass

                if not julius_adingui is None:
                    #if mictype != 'usb':
                        julius_adingui.terminate()
                        stdout, stderr  = julius_adingui.communicate()
                        julius_adingui  = None

                if not julius_adintool is None:
                        julius_adintool.terminate()
                        stdout, stderr  = julius_adintool.communicate()
                        julius_adintool = None

                time.sleep(2)

        inpBusy = False

        if cn_r.qsize() == 0:
            time.sleep(0.03)

    print('input___:terminate')
    if not julius_adintool is None:
        julius_adintool.terminate()
        julius_adintool = None
    if not julius_adingui is None:
        julius_adingui.terminate()
        julius_adingui = None

    print('input___:end')



julius=None
def sub_compute(cn_r, cn_s, outputQ=None, ):
    global julius

    global inpApi
    global trnApi
    global outApi
    global inpLang
    global trnLang
    global outLang
    global txtLang
    global micLevel

    global compBusy
    global compLast

    print('compute_:init')

    runmode  = cn_r.get()
    mictype  = cn_r.get()
    micguide = cn_r.get()
    recSJISf = cn_r.get()
    extpgm   = cn_r.get()
    cn_r.task_done()

    print('compute_:runmode =' + str(runmode ))
    print('compute_:mictype =' + str(mictype ))
    print('compute_:micguide=' + str(micguide))
    print('compute_:recSJISf=' + str(recSJISf))
    print('compute_:extpgm  =' + str(extpgm  ))

    feedback  = False
    miss_beat = 0

    if julius == None:
        if runmode == 'number':
            julius=subprocess.Popen(['julius/julius.exe', '-input', 'adinnet', '-adport', '5538', \
                                     '-C', 'julius/_jconf_20180313dnn999.jconf', '-dnnconf', 'julius/julius.dnnconf', \
                                     '-charconv', 'utf-8', 'sjis', '-logfile', 'temp/temp_julius.log', '-quiet', ], \
                                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, )
        else:
            julius=subprocess.Popen(['julius/julius.exe', '-input', 'adinnet', '-adport', '5538', \
                                     '-C', 'julius/_jconf_20180313dnn.jconf', '-dnnconf', 'julius/julius.dnnconf', \
                                     '-charconv', 'utf-8', 'sjis', '-logfile', 'temp/temp_julius.log', '-quiet', ], \
                                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, )
            chktime = time.time()
            chkhit  = ''
            while (int(time.time() - chktime) < 5):
                t = julius.stdout.readline()
                t = t.replace('\r', '')
                t = t.replace('\n', '')
                #print(t)
                if t != '':
                    chkhit = t
                else:
                    if chkhit != '':
                        break
                time.sleep(0.01)
                chktime = time.time()

    print('compute_:start')

    while True:
        if cn_r.qsize() > 0:
            cn_r_get = cn_r.get()
            mode_get = cn_r_get[0]
            data_get = cn_r_get[1]
            cn_r.task_done()

            if mode_get is None:
                print('compute_:None=break')
                break

            if mode_get == 'stamp':
                stamp    = data_get
                cn_r_get = cn_r.get()
                dummy    = cn_r_get[0]
                recVoice = cn_r_get[1]
                cn_r.task_done()

            if cn_r.qsize() != 0 or cn_s.qsize() > 2:
                print('compute_: queue overflow warning!',cn_r.qsize(),cn_s.qsize())

            if mode_get != 'stamp':
                cn_s.put(['PASS', ''])
            else:

                compBusy = 'feedback play'
                compLast = time.time()

                if feedback == True:
                    print('compute_:feedback playing...')
                    qPlay(recVoice, outputQ=outputQ)

                compBusy = 'recognition (1)'
                compLast = time.time()

                jultxt=''
                inptxt=''
                if julius == None:
                    jultxt = '!'
                else:
                    tempList = 'temp/temp_juliuslist.txt'
                    tl = codecs.open(tempList, 'w', 'utf-8')
                    tl.write(recVoice.replace('\\','/') + '\n')
                    tl.close()
                    tl = None

                    adintool=subprocess.Popen(['julius/adintool.exe', '-input', 'file', '-filelist', tempList, \
                                               '-out', 'adinnet', '-server', 'localhost', '-port', '5538', '-nosegment',], \
                                               stdout=subprocess.PIPE, stderr=subprocess.PIPE, )
                    adintool.wait()
                    adintool.terminate()
                    stdout, stderr = adintool.communicate()
                    adintool= None

                    jultxt = ''
                    chktime = time.time()
                    while True:
                        t = julius.stdout.readline()
                        t = t.replace('\r', '')
                        t = t.replace('\n', '')
                        #print(t)
                        if t != '':
                            if t[:10]=='sentence1:':
                                jultxt = t[10:]
                        else:
                            if jultxt != '':
                                break
                        time.sleep(0.01)
                        chktime = time.time()

                    jultxt = jultxt.strip()
                    jultxt = jultxt.replace(u'　', '')
                    jultxt = jultxt.replace(' ', '')
                    if len(jultxt) > 0:
                        if jultxt[len(jultxt)-1:len(jultxt)] == u'。':
                            jultxt = jultxt[0:len(jultxt)-1]

                    if jultxt != '':
                        print(stamp + ' Recognition  [' + jultxt + '] (julius)')

                if jultxt != '':
                    compBusy = 'recognition (2)'
                    compLast = time.time()

                    if inpApi == 'julius':
                        inptxt = jultxt
                    else:
                        inptxt=qVoiceInput(useApi=inpApi, inpLang=inpLang, \
                                           waveFile=recVoice, outputQ=outputQ)

                        if inptxt != '':
                            if inptxt[0:9] != '!Google, ':
                                print(stamp + ' Recognition  [' + inptxt + '] (' + inpApi + ')')
                            else:
                                inptxt = inptxt[9:]
                                print(stamp + ' Recognition  [' + inptxt + '] (!Google)')
                        else:
                            inptxt = jultxt

                if inptxt == '':
                    os.remove(recVoice)
                    print(stamp + ' ! ')
                    if micguide != 'off':
                        qPlay('_ng', outputQ=outputQ)

                    if 1==1:
                        if recSJISf == 'Default':
                            recTextx = 'temp/temp_recSJIS.txt'
                        else:
                            recTextx = recSJISf
                        try:
                            w = codecs.open(recTextx, 'w', 'shift_jis')
                            w.write('')
                            w.close()
                            w = None
                        except:
                            w = None

                trntxt=''
                if inptxt != '':
                    compBusy = 'recognition (3)'
                    compLast = time.time()

                    txt = inptxt
                    if runmode == 'number':
                        txt = txt.replace(u'ゼロ', '0')
                        txt = txt.replace(u'０',   '0')
                        txt = txt.replace(u'１',   '1')
                        txt = txt.replace(u'２',   '2')
                        txt = txt.replace(u'３',   '3')
                        txt = txt.replace(u'４',   '4')
                        txt = txt.replace(u'５',   '5')
                        txt = txt.replace(u'６',   '6')
                        txt = txt.replace(u'７',   '7')
                        txt = txt.replace(u'８',   '8')
                        txt = txt.replace(u'９',   '9')
                        if txt.isdigit():
                            trntxt = txt
                        else:
                            txt = txt.replace('0',   'ゼロ')

                    if trntxt == '':
                        trntxt=qTranslator(useApi=trnApi, inpLang=inpLang, outLang=trnLang,
                                           transText=txt, outputQ=outputQ)
                        if trntxt != '':
                            if trntxt[0:9] != '!Google, ':
                                print(stamp + ' Translation  [' + trntxt + '] (' + trnApi + ')')
                            else:
                                trntxt = trntxt[9:]
                                print(stamp + ' Translation  [' + trntxt + '] (!Google)')
                            #qPlay('_ok', outputQ=outputQ)

                    if trntxt == '':
                        os.remove(recVoice)
                        print(stamp + ' ! ')
                        if micguide != 'off':
                            qPlay('_ng', outputQ=outputQ)

                        if recSJISf == 'Default':
                            recTextx = 'temp/temp_recSJIS.txt'
                        else:
                            recTextx = recSJISf
                        try:
                            w = codecs.open(recTextx, 'w', 'shift_jis')
                            w.write('')
                            w.close()
                            w = None
                        except:
                            w = None

                        sec = int(time.time() - miss_beat)
                        miss_beat = time.time()
                        if int(micLevel) != 0:
                            if sec>120 and int(micLevel) >= 1000:
                                    if int(micLevel) == 1000:
                                        micLevel = 3000
                                    micLevel = int(int(micLevel) * 0.90)
                                    if int(micLevel) < 1000:
                                        micLevel = 1000
                                    print('compute_:micLevel=' + str(micLevel))
                            if sec<120 and int(micLevel) < 8888:
                                    micLevel = int(int(micLevel) / 0.70)
                                    if int(micLevel) > 8888:
                                        micLevel = 8888
                                    print('compute_:micLevel=' + str(micLevel))

                if trntxt != '':
                    if runmode == 'number' or runmode == 'numeric':
                        trntxt = trntxt.lower()
                        if trntxt[len(trntxt)-1:] == '.':
                            trntxt = trntxt[0:len(trntxt)-1]

                        num = trntxt.lower()
                        num = num.replace(u'０', '0')
                        num = num.replace('zero', '0')
                        num = num.replace('one', '1')
                        num = num.replace('two', '2')
                        num = num.replace('three', '3')
                        num = num.replace('four', '4')
                        num = num.replace('five', '5')
                        num = num.replace('six', '6')
                        num = num.replace('seven', '7')
                        num = num.replace('eight', '8')
                        num = num.replace('nine', '9')
                        num = num.replace(' ', '')
                        num = num.replace(',', '')

                        if num != '' and num != trntxt and num.isdigit():
                            print(stamp + ' ' + trntxt + ' -> ' + str(num))
                            trntxt = str(num)

                if trntxt != '':
                    compBusy = 'file output'
                    compLast = time.time()

                    if inpLang=='ja':
                        recSJISx = 'temp/voices/' + stamp + '_sjis.txt'
                        try:
                            w = codecs.open(recSJISx, 'w', 'shift_jis')
                            w.write(inptxt)
                            w.close()
                            w = None
                        except:
                            w = None

                    recText1 = 'temp/voices/' + stamp + '_text_' + inpLang + '.txt'
                    try:
                        w = codecs.open(recText1, 'w', 'utf-8')
                        w.write(inptxt)
                        w.close()
                        w = None
                    except:
                        w = None

                    recTran1 = 'temp/voices/' + stamp + '_text_' + trnLang + '.txt'
                    try:
                        w = codecs.open(recTran1, 'w', 'utf-8')
                        w.write(trntxt)
                        w.close()
                        w = None
                    except:
                        w = None

                    if extpgm != 'None':
                        if os.path.exists(extpgm):
                            #os.system(extpgm + ' ' + stamp)
                            p=subprocess.Popen([extpgm, stamp])
                            #p.wait()

                    recVoice2 = 'temp/temp_recVoice.wav'
                    try:
                        wb = open(recVoice2, 'wb')
                        wb.write(voice)
                        wb.close
                        wb = None
                    except:
                        wb = None

                    recTran2 = 'temp/temp_recTranslator.txt'
                    try:
                        w = codecs.open(recTran2, 'w', 'utf-8')
                        w.write(trntxt)
                        w.close()
                        w = None
                    except:
                        w = None

                    recText2 = 'temp/temp_recUTF8.txt'
                    try:
                        w = codecs.open(recText2, 'w', 'utf-8')
                        w.write(inptxt)
                        w.close()
                        w = None
                    except:
                        w = None

                    if recSJISf == 'Default':
                        recTextx = 'temp/temp_recSJIS.txt'
                    else:
                        recTextx = recSJISf

                    try:
                        w = codecs.open(recTextx, 'w', 'shift_jis')
                        w.write(inptxt)
                        w.close()
                        w = None
                    except:
                        w = None
                        try:
                            w2 = codecs.open(recTextx, 'w', 'utf-8')
                            w2.write(inptxt)
                            w2.close()
                            w2 = None
                        except:
                            w2 = None

                if trntxt != '':
                    compBusy = 'processing'
                    compLast = time.time()

                    txt = inptxt

                    if inptxt == u'バルス' or inptxt == u'システム シャットダウン' or \
                       trntxt.lower() == 'system shutdown':
                        txt = u'システムシャットダウン'

                    if inptxt == u'今何時' or inptxt == u'今 何時':
                        now2=datetime.datetime.now()
                        txt  = u'現在の時刻は、'
                        txt += now2.strftime('%H') + u'時'
                        txt += now2.strftime('%M') + u'分です'

                    if inptxt == u'フィードバック' or inptxt == u'デバッグ' or \
                       trntxt.lower() == 'feedback' or trntxt.lower() == 'debug':
                        if feedback != True:
                            feedback = True
                            txt = u'音声フィードバック開始します'
                        else:
                            feedback = False
                            txt = u'音声フィードバック終了します'

                    if inptxt == u'ワトソン' or inptxt == u'Ｗａｔｓｏｎ' or inptxt.lower() == 'watson' or \
                       inptxt == u'アイビーエム' or inptxt.lower() == 'ibm' or \
                       trntxt.lower() == 'watson' or trntxt.lower() == 'ibm':
                        txt  = u'AIを IBM Watson に切り替えます'
                    if inptxt == u'グーグル' or inptxt == u'Ｇｏｏｇｌｅ' or inptxt == u'ｇｏｏｇｌｅ' or \
                       inptxt.lower() == 'google' or trntxt.lower() == 'google':
                        txt  = u'AIを Google に切り替えます'
                    if inptxt == u'アジュール' or inptxt == u'Ａｚｕｒｅ' or inptxt.lower() == 'azure' or \
                       inptxt == u'マイクロソフト' or inptxt.lower() == 'microsoft' or \
                       trntxt.lower() == 'azure' or trntxt.lower() == 'microsoft':
                        txt = u'AIを Microsoft Azure に切り替えます'

                    if txt != inptxt:
                        trntxt=qTranslator(useApi=trnApi, inpLang=inpLang, outLang=trnLang,
                                   transText=txt, outputQ=outputQ)
                        if trntxt != '':
                            qVoiceOutput(useApi=outApi, outLang=outLang, outText=trntxt,outputQ=outputQ)

                if trntxt != '':
                    if inptxt == u'バルス' or inptxt == u'システム シャットダウン' or \
                       inptxt == u'システムシャットダウン' or trntxt.lower() == 'system shutdown':
                        cn_s.put(['END', ''])
                        break

                    if inptxt == u'今何時' or inptxt == u'今 何時' or \
                       inptxt == u'フィードバック' or inptxt == u'デバッグ' or \
                       trntxt.lower() == 'feedback' or trntxt.lower() == 'debug':
                        trntxt = ''

                    if inptxt == u'ワトソン' or inptxt == u'Ｗａｔｓｏｎ' or inptxt.lower() == 'watson' or \
                       inptxt == u'アイビーエム' or inptxt.lower() == 'ibm' or \
                       trntxt.lower() == 'watson' or trntxt.lower() == 'ibm':
                        inpApi = 'watson'
                        trnApi = 'watson'
                        outApi = 'watson'
                        if runmode == 'translator' or runmode == 'learning':
                            qVoiceApiInfo(trnApi=trnApi, outputQ=outputQ)
                        trntxt = ''

                    if inptxt == u'グーグル' or inptxt == u'Ｇｏｏｇｌｅ' or inptxt == u'ｇｏｏｇｌｅ' or \
                       inptxt.lower() == 'google' or trntxt.lower() == 'google':
                        inpApi = 'google'
                        trnApi = 'google'
                        outApi = 'google'
                        if runmode == 'translator' or runmode == 'learning':
                            qVoiceApiInfo(trnApi=trnApi, outputQ=outputQ)
                        trntxt = ''

                    if inptxt == u'アジュール' or inptxt == u'Ａｚｕｒｅ' or inptxt.lower() == 'azure' or \
                       inptxt == u'マイクロソフト' or inptxt.lower() == 'microsoft' or \
                       trntxt.lower() == 'azure' or trntxt.lower() == 'microsoft':
                        inpApi = 'azure'
                        trnApi = 'azure'
                        outApi = 'azure'
                        if runmode == 'translator' or runmode == 'learning':
                            qVoiceApiInfo(trnApi=trnApi, outputQ=outputQ)
                        trntxt = ''

                if trntxt != '':
                    if runmode == 'translator' or runmode == 'learning':
                        compBusy = 'translator'
                        compLast = time.time()

                        res = sub_compute_translator(runmode, stamp, inptxt, trntxt, outputQ,)

                if trntxt != '':
                    cn_s.put(['OK', ''])
                else:
                    cn_s.put(['NG', ''])

        compBusy = False

        if cn_r.qsize() == 0:
            time.sleep(0.03)

    print('compute_:terminate')

    #julius.wait()
    julius.terminate()
    stdout, stderr = julius.communicate()
    julius = None

    print('compute_:end')



def sub_compute_translator(runmode, stamp, inptxt, trntxt, outputQ=None,):
    global inpApi
    global trnApi
    global outApi
    global inpLang
    global trnLang
    global outLang
    global txtLang

    if inptxt != '':
        txt=inptxt
        lng=trnLang

        if inptxt == u'リセット' or inptxt == 'reset':
            txt = u'設定をリセットします'
            inpApi  = 'google'
            inpLang = 'ja'
            trnApi  = 'azure'
            trnLang = 'en'
            outApi  = 'watson'
            outLang = 'en'

        if inptxt == u'スワップ' or inptxt == 'swap':
            txt = u'翻訳音声を入れ替えます'
            trnLang = inpLang
            outLang = inpLang
            inpLang = lng

        if inptxt == u'何語' or inptxt == u'何語ですか' or inptxt == u'何語 ですか':
            txt  = u'この機能は、'
            txt += u'日本語、英語、アラビア語、スペイン語、ドイツ語、フランス語、'
            txt += u'イタリア語、ポルトガル語、ロシア語、トルコ語、ウクライナ語、'
            txt += u'中国語ならびに韓国語'
            txt += u'に翻訳できます。あなたは何語を話しますか？'

        if inptxt == u'日本語':
            trnLang = 'ja'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'英語':
            trnLang = 'en'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'アラビア語' or inptxt == u'アラビア 語':
            trnLang = 'ar'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'スペイン語' or inptxt == u'スペイン 語':
            trnLang = 'es'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'ドイツ語' or inptxt == u'ドイツ 語':
            trnLang = 'de'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'フランス語' or inptxt == u'フランス 語':
            trnLang = 'fr'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'イタリア語' or inptxt == u'イタリア 語':
            trnLang = 'it'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'ポルトガル語' or inptxt == u'ポルトガル 語':
            trnLang = 'pt'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'ロシア語' or inptxt == u'ロシア 語':
            trnLang = 'ru'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'トルコ語' or inptxt == u'トルコ 語':
            trnLang = 'tr'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'ウクライナ語' or inptxt == u'ウクライナ 語':
            trnLang = 'uk'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'中国語'or inptxt == u'中国 語':
            trnLang = 'zh-CN'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))
        if inptxt == u'韓国語' or inptxt == u'韓国 語':
            trnLang = 'ko'
            outLang = trnLang
            print('compute_:outLang=' + str(outLang))

        if txt != inptxt or lng != trnLang:
            trntxt=qTranslator(useApi=trnApi, inpLang=inpLang, outLang=trnLang,
                               transText=txt, outputQ=outputQ)

    if trntxt != '':
        outtxt = trntxt

        print(stamp + ' Vocalization [' + outtxt + '] (' + outApi + ')')
        outVoice = 'temp/voices/' + stamp + '_voice_' + outLang + '.mp3'

        res = qVoiceOutput(useApi=outApi, outLang=outLang, outText=outtxt,
                           tempFile=outVoice, outputQ=outputQ)
        if res[0:9] == '!Google, ':
            print(stamp + ' Vocalization [' + outtxt + '] (!Google)')

    if trntxt != '' and runmode == 'learning':

        print(stamp + ' Vocalization [' + inptxt + '] (' + outApi + ')')
        outVoice = 'temp/voices/' + stamp + '_voice_' + inpLang + '.mp3'

        res = qVoiceOutput(useApi=outApi, outLang=inpLang, outText=inptxt,
                           tempFile=outVoice, outputQ=outputQ)
        if res[0:9] == '!Google, ':
            print(stamp + ' Vocalization [' + inptxt + '] (!Google)')

    if inptxt == u'リセット' or inptxt == 'reset':
        qVoiceApiInfo(inpApi=inpApi, trnApi=trnApi, outApi=outApi, outputQ=outputQ)



def sub_output(cn_r,cn_s):
    global inpApi
    global trnApi
    global outApi
    global inpLang
    global trnLang
    global outLang
    global txtLang

    global outBusy
    global outLast

    print('output__:init')

    runmode = cn_r.get()
    mictype = cn_r.get()
    micguide= cn_r.get()
    cn_r.task_done()

    print('output__:runmode =' + str(runmode ))
    print('output__:mictype =' + str(mictype ))
    print('output__:micguide=' + str(micguide))

    print('output__:start')

    while True:
        if cn_r.qsize() > 0:
            cn_s.put(['BUSY', ''])

            cn_r_get = cn_r.get()
            mode_get = cn_r_get[0]
            data_get = cn_r_get[1]
            cn_r.task_done()

            if mode_get is None:
                print('output__:None=break')
                break

            if cn_r.qsize() > 2 or cn_s.qsize() > 4:
                print('output__: queue overflow warning!',cn_r.qsize(),cn_s.qsize())

            file = ''
            if mode_get == 'file':
                outBusy = 'Playing'
                outLast = time.time()

                file = data_get
                #print('output__:' + file)
                qPlay(file, outputQ=None, sync=True)

                outBusy = False
                outLast = time.time()

            cn_s.put(['OK', file])

        outBusy = False

        if cn_r.qsize() == 0:
            time.sleep(0.03)

    print('output__:end')



def sub_playtext(cn_r,cn_s,outputQ=None):
    global inpApi
    global trnApi
    global outApi
    global inpLang
    global trnLang
    global outLang
    global txtLang

    global playBusy
    global playLast

    print('playtext:init')

    runmode = cn_r.get()
    mictype = cn_r.get()
    micguide= cn_r.get()
    cn_r.task_done()

    print('playtext:runmode =' + str(runmode ))
    print('playtext:mictype =' + str(mictype ))
    print('playtext:micguide=' + str(micguide))

    print('playtext:start')

    while True:
        if cn_r.qsize() > 0:
            cn_s.put(['BUSY', ''])

            cn_r_get = cn_r.get()
            mode_get = cn_r_get[0]
            data_get = cn_r_get[1]
            cn_r.task_done()

            if mode_get is None:
                print('playtext:None=break')
                break

            if cn_r.qsize() != 0 or cn_s.qsize() > 4:
                print('playtext: queue overflow warning!',cn_r.qsize(),cn_s.qsize())

            if mode_get != 'PASS':

                # file check

                playFile1='temp/temp_playSJIS.txt'
                playFile2='temp/@temp_playSJIS.txt'
                if os.path.exists(playFile1):
                    #print('playtext:' + playFile1)
                    if os.path.exists(playFile2):
                        try:
                            os.remove(playFile2)
                        except:
                            pass
                    if not os.path.exists(playFile2):
                        try:
                            os.rename(playFile1, playFile2)

                            textBusy = 'Text Accept'
                            textLast = time.time()

                            try:
                                txt = ''
                                rt = codecs.open(playFile2, 'r', 'shift_jis')
                                for t in rt:
                                    txt = (txt + ' ' + str(t)).strip()
                                rt.close
                                rt = None
                                os.remove(playFile2)

                                if txt == '_ok' or txt == '_ng' or \
                                   txt == '_ready' or txt == '_accept' or \
                                   txt == '_shutter':
                                    textBusy = 'Text Playing'
                                    textLast = time.time()

                                    outputQ.put(['file', txt])
                                    outputQ.put(['file', '_!playing'])
                                    print('playtext:BGM=' + txt)

                                    textBusy = False
                                    textLast = time.time()

                                else:
                                    outtxt = txt
                                    if txtLang != outLang:
                                        textBusy = 'Text Translation'
                                        textLast = time.time()

                                        outtxt=qTranslator(useApi=trnApi, inpLang=txtLang, outLang=outLang,
                                                           transText=txt, outputQ=outputQ)
                                        print('playtext: Translation  [' + txt + '] (' + trnApi + ')')

                                    textBusy = 'Text Vocalization'
                                    textLast = time.time()

                                    qVoiceOutput(useApi=outApi, outLang=outLang, outText=outtxt, outputQ=outputQ)
                                    print('playtext: Vocalization [' + outtxt + '] (' + outApi + ')')

                                    textBusy = False
                                    textLast = time.time()

                            except:
                                rt = None

                            textBusy = False
                            textLast = time.time()

                        except:
                            pass

            cn_s.put(['OK', ''])

        textBusy = False

        if cn_r.qsize() == 0:
            time.sleep(0.5)

    print('playtext:end')



inpApi   = 'free'
trnApi   = 'free'
outApi   = 'free'
inpLang  = 'ja'
trnLang  = 'en'
outLang  = 'en'
txtLang  = 'ja'
micLevel = '1555'

inpBusy  = False
inpLast  = 0
compBusy = False
compLast = 0
textBusy = False
textLast = 0
outBusy  = False
outLast  = 0
playBusy = False
playLast = 0

if __name__ == '__main__':
    #global inpApi
    #global trnApi
    #global outApi
    #global inpLang
    #global trnLang
    #global outLang
    #global txtLang
    #global micLevel

    #global inpBusy
    #global inpLast
    #global compBusy
    #global compLast
    #global textBusy
    #global textLast
    #global outBusy
    #global outLast
    #global playBusy
    #global playLast

    print('__main__:init')
    print('__main__:exsample.py micdev, mictype, micLevel, micguide, api, input-lang, output-lang, textif-lang, runmode, mic-syncf, recSJISf, extpgm,')

    recSJIS ='temp/temp_recSJIS.txt'
    recUTF8 ='temp/temp_recUTF8.txt'
    recTrans='temp/temp_recTranslator.txt'
    if os.path.exists(recSJIS):
        os.remove(recSJIS)
    if os.path.exists(recUTF8):
        os.remove(recUTF8)
    if os.path.exists(recTrans):
        os.remove(recTrans)

    micdev   = '0'
    mictype  = 'bluetooth'
    micguide = 'on'
    runmode  = 'translator'
    micON    = 'None'
    recSJISf = 'Default'
    extpgm   = 'None'
    if len(sys.argv)>=2:
        micdev   = sys.argv[1]
    if len(sys.argv)>=3:
        mictype  = str(sys.argv[2]).lower()
    if len(sys.argv)>=4:
        micLevel = sys.argv[3]
    if len(sys.argv)>=5:
        micguide = str(sys.argv[4]).lower()
    if len(sys.argv)>=6:
        if str(sys.argv[5]).lower() != 'julius':
            inpApi   = str(sys.argv[5]).lower()
            trnApi   = str(sys.argv[5]).lower()
            outApi   = str(sys.argv[5]).lower()
        else:
            inpApi   = str(sys.argv[5]).lower()
    if len(sys.argv)>=7:
        inpLang  = sys.argv[6]
    if len(sys.argv)>=8:
        outLang  = sys.argv[7]
    if len(sys.argv)>=9:
        txtLang  = sys.argv[8]
    if len(sys.argv)>=10:
        runmode  = str(sys.argv[9]).lower()
    if len(sys.argv)>=11:
        micON    = sys.argv[10]
    if len(sys.argv)>=12:
        recSJISf = sys.argv[11]
    if len(sys.argv)>=13:
        extpgm   = sys.argv[12]
    if inpLang=='ja-JP':
        inpLang  = 'ja'
    if outLang=='en-US':
        outLang  = 'en'
    if txtLang=='ja-JP':
        txtLang  = 'ja'
    if inpLang=='ja' and outLang=='ja':
        trnLang  = 'en'
    if runmode == 'translator' or runmode == 'learning':
        trnLang  = outLang

    if recSJISf != 'Default':
        if os.path.exists(recSJISf):
            os.remove(recSJISf)

    print('')
    print('__main__:micdev  =' + str(micdev  ))
    print('__main__:mictype =' + str(mictype ))
    print('__main__:micLevel=' + str(micLevel))
    print('__main__:micguide=' + str(micguide))
    print('__main__:input   =' + str(inpLang ))
    print('__main__:output  =' + str(outLang ))
    print('__main__:text    =' + str(txtLang ))
    print('__main__:mode    =' + str(runmode ))
    print('__main__:micON   =' + str(micON   ))
    print('__main__:recSJISf=' + str(recSJISf))
    print('__main__:extpgm  =' + str(extpgm  ))

    print('')
    print('__main__:infomation')
    print('[ keywords ]')
    print(u'システムシャットダウン,')
    print(u'今何時,フィードバック,')
    print(u'ワトソン,グーグル,マイクロソフト,')
    if runmode == 'translator' or runmode == 'learning':
        print('[ translator and learning ]')
        print(u'リセット,スワップ,')
        print(u'何語ですか,')
        print(u'日本語、英語、アラビア語、スペイン語、ドイツ語、フランス語、')
        print(u'イタリア語、ポルトガル語、ロシア語、トルコ語、ウクライナ語、')
        print(u'中国語ならびに韓国語')

    print('')
    print('__main__:start')
    main_start   = time.time()

    output_s     = queue.Queue()
    output_r     = queue.Queue()
    output_proc  = None
    output_beat  = 0
    output_skip  = 0
    compute_s    = queue.Queue()
    compute_r    = queue.Queue()
    compute_proc = None
    compute_beat = 0
    input_s      = queue.Queue()
    input_r      = queue.Queue()
    input_proc   = None
    input_beat   = 0
    playtext_s   = queue.Queue()
    playtext_r   = queue.Queue()
    playtext_proc= None
    playtext_beat= 0
    playtext_skip= 0
    while True:

        # Thread timeout check

        if (input_r.qsize() == 0) and (input_beat != 0):
            sec = int(time.time() - input_beat)
            if sec > 60:
                print('__main__:input_proc 60s')
                #input_beat = time.time()
                print('__main__:input_proc break')
                try:
                    dummy=subprocess.Popen(['taskkill', '/im', 'adintool-gui.exe', '/f',])
                    time.sleep(1.0)
                    dummy=subprocess.Popen(['taskkill', '/im', 'adintool.exe', '/f',])
                    time.sleep(1.0)
                except:
                    pass
                input_s.put([None, None])
                time.sleep(3.0)
                input_proc = None
                input_beat = 0

        if (compute_r.qsize() == 0) and (compute_beat != 0):
            sec = int(time.time() - compute_beat)
            if sec > 60:
                print('__main__:compute_proc 60s')
                #compute_beat = time.time()
                print('__main__:compute_proc break')
                compute_s.put([None, None])
                time.sleep(3.0)
                compute_proc = None
                compute_beat = 0

        if (output_r.qsize() == 0) and (output_beat != 0):
            sec = int(time.time() - output_beat)
            if sec > 60:
                print('__main__:output_proc 60s')
                #output_beat = time.time()
                print('__main__:output_proc break')
                output_s.put([None, None])
                time.sleep(3.0)
                output_proc = None
                output_beat = 0
                output_skip = 0

        if (playtext_r.qsize() == 0) and (playtext_beat != 0):
            sec = int(time.time() - playtext_beat)
            if sec > 60:
                print('__main__:playtext_proc 60s')
                #playtext_beat = time.time()
                print('__main__:playtext_proc break')
                playtext_s.put([None, None])
                time.sleep(3.0)
                playtext_proc = None
                playtext_beat = 0
                playtext_skip = 0

        # Thread start

        if output_proc is None:
            while output_s.qsize() > 0:
                dummy = output_s.get()
            while output_r.qsize() > 0:
                dummy = output_r.get()
            output_proc = threading.Thread(target=sub_output, args=(output_s,output_r,))
            output_proc.daemon = True
            output_s.put(runmode )
            output_s.put(mictype)
            output_s.put(micguide)
            output_proc.start()
            time.sleep(1.0)

            output_s.put(['START', ''])
            output_beat = time.time()
            output_skip = 0

        if compute_proc is None:
            while compute_s.qsize() > 0:
                dummy = compute_s.get()
            while compute_r.qsize() > 0:
                dummy = compute_r.get()
            compute_proc = threading.Thread(target=sub_compute, args=(compute_s,compute_r,output_s,))
            compute_proc.daemon = True
            compute_s.put(runmode )
            compute_s.put(mictype )
            compute_s.put(micguide)
            compute_s.put(recSJISf)
            compute_s.put(extpgm  )
            compute_proc.start()
            time.sleep(1.0)

            compute_s.put(['START', ''])
            compute_beat = time.time()

        if input_proc is None:
            while input_s.qsize() > 0:
                dummy = input_s.get()
            while input_r.qsize() > 0:
                dummy = input_r.get()
            input_proc = threading.Thread(target=sub_input, args=(input_s,input_r,output_s,output_r,playtext_s,playtext_r,))
            input_proc.daemon = True
            input_s.put(runmode )
            input_s.put(micdev  )
            input_s.put(mictype )
            input_s.put(micguide)
            input_s.put(micON   )
            input_s.put(recSJISf)
            input_proc.start()
            time.sleep(1.0)

            input_s.put(['START', ''])
            input_beat = time.time()

        if playtext_proc is None:
            while playtext_s.qsize() > 0:
                dummy = playtext_s.get()
            while playtext_r.qsize() > 0:
                dummy = playtext_r.get()
            playtext_proc = threading.Thread(target=sub_playtext, args=(playtext_s,playtext_r,output_s,))
            playtext_proc.daemon = True
            playtext_s.put(runmode )
            playtext_s.put(mictype )
            playtext_s.put(micguide)
            playtext_proc.start()
            time.sleep(1.0)

            playtext_s.put(['START', ''])
            playtext_beat = time.time()
            playtext_skip = 0

        # processing

        if output_r.qsize() > 0:
            output_get = output_r.get()
            output_res = output_get[0]
            output_dat = output_get[1]
            output_r.task_done()
        if output_r.qsize() == 0 and output_s.qsize() == 0:
            output_skip += 1
        else:
            output_skip = 0
        if output_skip > 50:
            output_s.put(['RUN', ''])
            output_beat = time.time()
            output_skip = 0

        if input_r.qsize() > 0:
            input_get = input_r.get()
            input_res = input_get[0]
            input_dat = input_get[1]
            input_r.task_done()
            if input_res == 'INIT':
                input_s.put(['INIT', ''])
                input_beat = time.time()
            elif input_res == 'PASS':
                input_s.put(['READY', ''])
                input_beat = time.time()
            elif input_res == 'NG':
                input_s.put(['NEXT', ''])
                input_beat = time.time()
            else:
                stamp     = input_dat
                input_get = input_r.get()
                dummy     = input_get[0]
                voicef    = input_get[1]
                input_r.task_done()

                compute_s.put(['stamp',  stamp])
                compute_s.put(['voicef', voicef])
                compute_beat = time.time()
                if recSJISf != 'Default' or micON != 'None':
                    if compute_r.qsize() == 0:
                        chktime = time.time()
                        while (compute_r.qsize() == 0) and (int(time.time() - chktime) < 5):
                            time.sleep(0.1)

                input_s.put(['READY', ''])
                input_beat = time.time()

        if compute_r.qsize() > 0:
            compute_get = compute_r.get()
            compute_res = compute_get[0]
            compute_dat = compute_get[1]
            compute_r.task_done()
            compute_beat = 0

            if compute_res == 'END':
                break

        if playtext_r.qsize() > 0:
            playtext_get = playtext_r.get()
            playtext_res = playtext_get[0]
            playtext_dat = playtext_get[1]
            playtext_r.task_done()
        if playtext_r.qsize() == 0 and playtext_s.qsize() == 0:
            playtext_skip += 1
        else:
            playtext_skip = 0
        if playtext_skip > 50:
            playtext_s.put(['RUN', ''])
            playtext_beat = time.time()
            playtext_skip = 0

        time.sleep(0.01)



    print('__main__:terminate')

    input_s.put(   [None, None])
    compute_s.put( [None, None])
    output_s.put(  [None, None])
    playtext_s.put([None, None])

    input_proc.join()
    compute_proc.join()
    output_proc.join()
    playtext_proc.join()

    print('__main__:bye!')



