state.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 # python libraries
00004 import StringIO
00005 import wave
00006 
00007 # local libraries
00008 from rospeex_core import logging_util
00009 from rospeex_core.sr.base import IState
00010 from rospeex_core.sr.base import PacketType
00011 from rospeex_core.sr.base import SessionState
00012 from sync_client import SyncClient
00013 
00014 
00015 # get logger
00016 logger = logging_util.get_logger(__name__)
00017 
00018 
00019 class InitState(IState):
00020     def __init__(self, api_key, language):
00021         self._api_key = api_key
00022         self._language = language
00023 
00024     def run(self, packet_data):
00025         pass    # pragma: no cover
00026 
00027     def next(self, packet_type):
00028         if packet_type == PacketType.START:
00029             logger.debug('Change session state INIT -> START')
00030             return StartState(self._api_key, self._language)
00031 
00032         else:
00033             logger.debug('Change session state INIT -> ERROR')
00034             return ErrorState(self._api_key, self._language)
00035 
00036     def result(self):
00037         return None     # pragma: no cover
00038 
00039     def state(self):
00040         return SessionState.INIT
00041 
00042 
00043 class StartState(IState):
00044     def __init__(self, api_key, language):
00045         self._api_key = api_key
00046         self._language = language
00047 
00048     def run(self, packet_data):
00049         pass
00050 
00051     def next(self, packet_type):
00052         if packet_type == PacketType.DATA:
00053             logger.debug('Change session state START -> DATA')
00054             return DataState(self._api_key, self._language, None)
00055 
00056         elif packet_type == PacketType.END:
00057             logger.debug('Change session state START -> END')
00058             return EndState(self._api_key, self._language, None)
00059 
00060         else:
00061             logger.debug('Change session state START -> ERROR')
00062             return ErrorState(self._api_key, self._language)
00063 
00064     def result(self):
00065         return None     # pragma: no cover
00066 
00067     def state(self):
00068         return SessionState.START
00069 
00070 
00071 class DataState(IState):
00072     def __init__(self, api_key, language, data):
00073         self._api_key = api_key
00074         self._language = language
00075         self._data = data
00076 
00077     def run(self, packet_data):
00078         if self._data is None:
00079             self._data = packet_data
00080         else:
00081             self._data += packet_data
00082 
00083     def next(self, packet_type):
00084         if packet_type == PacketType.DATA:
00085             logger.debug('Change session state DATA -> DATA')
00086             return DataState(self._api_key, self._language, self._data)
00087 
00088         elif packet_type == PacketType.END:
00089             logger.debug('Change session state DATA -> END')
00090             return EndState(self._api_key, self._language, self._data)
00091 
00092         else:
00093             logger.debug('Change session state DATA -> ERROR')
00094             return ErrorState(self._api_key, self._language)
00095 
00096     def result(self):
00097         return None     # pragma: no cover
00098 
00099     def state(self):
00100         return SessionState.DATA
00101 
00102 
00103 class EndState(IState):
00104     def __init__(self, api_key, language, data):
00105         self._api_key = api_key
00106         self._language = language
00107         self._data = data
00108         self._result_text = ''
00109 
00110     def run(self, packet_data):
00111         # create wav format data
00112         output = StringIO.StringIO()
00113         wav_data = wave.open(output, 'wb')
00114         wav_data.setnchannels(SyncClient.CHANNELS)
00115         wav_data.setframerate(SyncClient.FRAMERATE)
00116         wav_data.setsampwidth(SyncClient.SAMPWIDTH)
00117         wav_data.writeframes(self._data)
00118 
00119         # send data to client
00120         client = SyncClient(self._api_key)
00121         self._result_text = client.request(output.getvalue(), self._language)
00122 
00123     def next(self, packet_type):
00124         if packet_type == PacketType.START:
00125             logger.debug('Change session state END -> START')
00126             return StartState(self._api_key, self._language)
00127 
00128         else:
00129             logger.debug('Change session state END -> ERROR')
00130             return ErrorState(self._api_key, self._language)
00131 
00132     def result(self):
00133         return self._result_text
00134 
00135     def state(self):
00136         return SessionState.END
00137 
00138 
00139 class ErrorState(IState):
00140     def __init__(self, api_key, language):
00141         self._api_key = api_key
00142         self._language = language
00143 
00144     def run(self, packet_data):
00145         pass
00146 
00147     def next(self, packet_type):
00148         if packet_type == PacketType.START:
00149             logger.debug('Change session state ERROR -> START')
00150             return StartState(self._api_key, self._language)
00151 
00152         else:
00153             logger.debug('Change session state ERROR -> ERROR')
00154             return ErrorState(self._api_key, self._language)
00155 
00156     def result(self):
00157         return None     # pragma: no cover
00158 
00159     def state(self):
00160         return SessionState.ERROR


rospeex_core
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:10