From 1f37bbd261bfdecf162a2be4e2778135d198005d Mon Sep 17 00:00:00 2001 From: jm12138 <2286040843@qq.com> Date: Sat, 27 Jan 2024 23:39:12 +0800 Subject: [PATCH] update v0.2.0 --- README.md | 46 +++-- README_CN.md | 46 +++-- msc/__init__.py | 187 ++++++++------------ msc/msp.py | 335 +++++++++++++++++------------------ msc/qise.py | 323 +++++++++++++++++----------------- msc/qisr.py | 450 ++++++++++++++++++++++++------------------------ msc/qivw.py | 128 +++++++------- msc/qtts.py | 281 +++++++++++++++--------------- setup.py | 2 +- 9 files changed, 873 insertions(+), 925 deletions(-) diff --git a/README.md b/README.md index f70f5a5..237a1f2 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,6 @@ * Voice Wakeup (KWS) using Mic Input ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio from ctypes import string_at, c_void_p @@ -43,8 +36,13 @@ # Set APP ID appid = '' + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' + # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode('UTF-8')) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Stop Event stop_event = Event() @@ -149,13 +147,6 @@ * Speech Recognizer (ASR) using Mic Input ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio @@ -166,10 +157,15 @@ ) # Set APP ID - appid = "" + appid = '' + + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Set Domain domain = "iat" @@ -189,13 +185,6 @@ * Speech Synthesizer (TTS) ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio @@ -204,10 +193,15 @@ output_stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, output=True) # Set APP ID - appid = "" + appid = '' + + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Set Text Encoding text_encoding = "UTF8" diff --git a/README_CN.md b/README_CN.md index def0deb..4e65f9a 100644 --- a/README_CN.md +++ b/README_CN.md @@ -20,13 +20,6 @@ * 语音唤醒 ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio from ctypes import string_at, c_void_p @@ -45,8 +38,13 @@ # Set APP ID appid = '' + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' + # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode('UTF-8')) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Stop Event stop_event = Event() @@ -151,13 +149,6 @@ * 语音识别 ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio @@ -168,10 +159,15 @@ ) # Set APP ID - appid = "" + appid = '' + + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Set Domain domain = "iat" @@ -191,13 +187,6 @@ * 语音合成 ```python - import os - - # Set MSC SDK DLL/SO File Path - # X86: SDK_DIR/bin/msc.dll - # X64: SDK_DIR/bin/msc_x64.dll - os.environ['MSC_SDK_PATH'] = '' - import msc import pyaudio @@ -206,10 +195,15 @@ output_stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, output=True) # Set APP ID - appid = "" + appid = '' + + # Set MSC SDK DLL/SO File Path + # X86: SDK_DIR/bin/msc.dll + # X64: SDK_DIR/bin/msc_x64.dll + sdk_path = '' # Set MSC Client - client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) + client = msc.MSC(sdk_path=sdk_path, params=f"appid={appid}".encode('UTF-8')) # Set Text Encoding text_encoding = "UTF8" diff --git a/msc/__init__.py b/msc/__init__.py index 4ddff24..b1dc938 100644 --- a/msc/__init__.py +++ b/msc/__init__.py @@ -1,18 +1,14 @@ +from ctypes import cdll +from ctypes import c_void_p from typing import Callable from typing import Generator -from ctypes import c_void_p from threading import Event from pyaudio import Stream -from .msp import msc from .msp import MSPAssert -from .msp import MSPLogin -from .msp import MSPLogout -from .msp import MSPSetParam -from .msp import MSPGetParam -from .msp import MSPUploadData -from .msp import MSPGetVersion + +from .msp import MSP from .msp import MSPStatus from .msp import MSPAudioSampleStatus from .msp import MSPRECStatus @@ -21,46 +17,33 @@ from .msp import MSPHCRDataStatus from .msp import MSPIVWMSGStatus from .msp import MSPDATASampleStatus -from .qisr import GrammarCallBack -from .qisr import LexiconCallBack -from .qisr import QISRSessionBegin -from .qisr import QISRAudioWrite -from .qisr import QISRGetResult -from .qisr import QISRSessionEnd -from .qisr import QISRGetParam -from .qisr import QISRBuildGrammar -from .qisr import QISRUpdateLexicon -from .qtts import QTTSSessionBegin -from .qtts import QTTSTextPut -from .qtts import QTTSAudioGet -from .qtts import QTTSSessionEnd -from .qtts import QTTSGetParam -from .qivw import msgProcCallBack -from .qivw import QIVWSessionBegin -from .qivw import QIVWSessionEnd -from .qivw import QIVWAudioWrite -from .qivw import QIVWRegisterNotify -from .qise import QISESessionBegin -from .qise import QISESessionEnd -from .qise import QISETextPut -from .qise import QISEAudioWrite -from .qise import QISEGetResult - - -class MSC: - def __init__(self, params: bytes) -> None: - MSPLogin(usr=None, pwd=None, params=params) - - @staticmethod + +from .qise import QISE +from .qisr import QISR +from .qivw import QIVW +from .qtts import QTTS + + +class MSC(MSP, QISR, QTTS, QIVW, QISE): + def __init__(self, sdk_path: str, params: bytes) -> None: + self.msc = cdll.LoadLibrary(sdk_path) + MSP.__init__(self, self.msc) + QISR.__init__(self, self.msc) + QTTS.__init__(self, self.msc) + QIVW.__init__(self, self.msc) + QISE.__init__(self, self.msc) + + self.MSPLogin(usr=b"", pwd=b"", params=params) + def asr( - params: bytes, stream: Stream, chunk_size: int = 2048 + self, params: bytes, stream: Stream, chunk_size: int = 2048 ) -> Generator[bytes, None, None]: # Session Begin - sessionID = QISRSessionBegin(grammarList=None, params=params) + sessionID = self.QISRSessionBegin(grammarList=b"", params=params) # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISRAudioWrite( + epStatus, recogStatus = self.QISRAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_FIRST, @@ -70,7 +53,7 @@ def asr( if epStatus != MSPEPStatus.MSP_EP_AFTER_SPEECH: # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISRAudioWrite( + epStatus, recogStatus = self.QISRAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_CONTINUE, @@ -78,62 +61,63 @@ def asr( else: # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISRAudioWrite( + epStatus, recogStatus = self.QISRAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_LAST, ) if recogStatus == MSPRECStatus.MSP_REC_STATUS_SUCCESS: # Get Result - result, rsltStatus = QISRGetResult(sessionID, waitTime=1000) + result, rsltStatus = self.QISRGetResult(sessionID, waitTime=1000) if result: yield result if rsltStatus == MSPRECStatus.MSP_REC_STATUS_COMPLETE: # Session End - QISRSessionEnd(sessionID, b"Normal End.") + self.QISRSessionEnd(sessionID, b"Normal End.") break - @staticmethod - def tts(params: bytes, text: bytes) -> Generator[bytes, None, None]: + def tts(self, params: bytes, text: bytes) -> Generator[bytes, None, None]: # Session Begin - sessionID = QTTSSessionBegin(params=params) + sessionID = self.QTTSSessionBegin(params=params) # Text Put - QTTSTextPut(sessionID=sessionID, textString=text, params=None) + self.QTTSTextPut(sessionID=sessionID, textString=text, params=b"") while True: # Audio Data Get - audioData, synthStatus = QTTSAudioGet(sessionID) + audioData, synthStatus = self.QTTSAudioGet(sessionID) # Yield Audio Data yield audioData if synthStatus == MSPTTSStatus.MSP_TTS_FLAG_DATA_END: # Session End - QTTSSessionEnd(sessionID, b"Normal End.") + self.QTTSSessionEnd(sessionID, b"Normal End.") break - @staticmethod def kws( + self, params: bytes, message_callback: Callable[[bytes, int, int, int, c_void_p, c_void_p], int], stream: Stream, chunk_size: int = 2048, - user_data=None, - stop_event: Event = None, + user_data=b"", + stop_event: Event = Event(), ) -> None: # Session Begin - sessionID = QIVWSessionBegin(grammarList=None, params=params) + sessionID = self.QIVWSessionBegin(grammarList=b"", params=params) # Register Notify - msgProcCb = msgProcCallBack(message_callback) - QIVWRegisterNotify(sessionID=sessionID, msgProcCb=msgProcCb, userData=user_data) + msgProcCb = self.msgProcCallBack(message_callback) + self.QIVWRegisterNotify( + sessionID=sessionID, msgProcCb=msgProcCb, userData=user_data + ) # Audio Write audioData = stream.read(chunk_size) - QIVWAudioWrite( + self.QIVWAudioWrite( sessionID=sessionID, audioData=audioData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_FIRST, @@ -142,7 +126,7 @@ def kws( while not stop_event.is_set(): # Audio Write audioData = stream.read(chunk_size) - QIVWAudioWrite( + self.QIVWAudioWrite( sessionID=sessionID, audioData=audioData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_CONTINUE, @@ -150,27 +134,26 @@ def kws( # Audio Write audioData = stream.read(chunk_size) - QIVWAudioWrite( + self.QIVWAudioWrite( sessionID=sessionID, audioData=audioData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_LAST, ) # Session End - QIVWSessionEnd(sessionID, b"Normal End.") + self.QIVWSessionEnd(sessionID, b"Normal End.") - @staticmethod def ase( - params: bytes, text: bytes, stream: Stream, chunk_size: int = 2048 + self, params: bytes, text: bytes, stream: Stream, chunk_size: int = 2048 ) -> Generator[bytes, None, None]: # Session Begin - sessionID = QISESessionBegin(params=params, userModelId=None) + sessionID = self.QISESessionBegin(params=params, userModelId=b"") - QISETextPut(sessionID=sessionID, textString=text, params=None) + self.QISETextPut(sessionID=sessionID, textString=text, params=b"") # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISEAudioWrite( + epStatus, recogStatus = self.QISEAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_FIRST, @@ -180,7 +163,7 @@ def ase( if epStatus != MSPEPStatus.MSP_EP_AFTER_SPEECH: # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISEAudioWrite( + epStatus, recogStatus = self.QISEAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_CONTINUE, @@ -188,14 +171,14 @@ def ase( else: # Audio Write waveData = stream.read(chunk_size) - epStatus, recogStatus = QISEAudioWrite( + epStatus, recogStatus = self.QISEAudioWrite( sessionID=sessionID, waveData=waveData, audioStatus=MSPAudioSampleStatus.MSP_AUDIO_SAMPLE_LAST, ) if recogStatus == MSPRECStatus.MSP_REC_STATUS_SUCCESS: # Get Result - result, rsltStatus = QISEGetResult(sessionID) + result, rsltStatus = self.QISEGetResult(sessionID) if result: # Yield Result @@ -203,56 +186,30 @@ def ase( if rsltStatus == MSPRECStatus.MSP_REC_STATUS_COMPLETE: # Session End - QISESessionEnd(sessionID, b"Normal End.") + self.QISESessionEnd(sessionID, b"Normal End.") break def __del__(self) -> None: - MSPLogout() + self.MSPLogout() -__version__ = "0.1.0" +__version__ = "0.2.0" __all__ = [ - __version__, - msc, - MSC, - MSPStatus, - MSPAudioSampleStatus, - MSPRECStatus, - MSPEPStatus, - MSPTTSStatus, - MSPHCRDataStatus, - MSPIVWMSGStatus, - MSPDATASampleStatus, - MSPAssert, - MSPLogin, - MSPLogout, - MSPSetParam, - MSPGetParam, - MSPUploadData, - MSPGetVersion, - QISRSessionBegin, - QISRAudioWrite, - QISRGetResult, - QISRSessionEnd, - QISRGetParam, - QISRBuildGrammar, - QISRUpdateLexicon, - LexiconCallBack, - GrammarCallBack, - QTTSSessionBegin, - QTTSTextPut, - QTTSAudioGet, - QTTSSessionEnd, - QTTSGetParam, - msgProcCallBack, - QIVWSessionBegin, - QIVWSessionEnd, - QIVWAudioWrite, - QIVWRegisterNotify, - QISESessionBegin, - QISESessionEnd, - QISETextPut, - QISEAudioWrite, - QISEGetResult, + "__version__", + "MSC", + "MSPStatus", + "MSPAudioSampleStatus", + "MSPRECStatus", + "MSPEPStatus", + "MSPTTSStatus", + "MSPHCRDataStatus", + "MSPIVWMSGStatus", + "MSPDATASampleStatus", + "MSPAssert", + "QISE", + "QISR", + "QIVW", + "QTTS", + "MSP", ] diff --git a/msc/msp.py b/msc/msp.py index baa50fc..3ff2c4d 100644 --- a/msc/msp.py +++ b/msc/msp.py @@ -1,6 +1,3 @@ -import os - -from ctypes import cdll from ctypes import CDLL from ctypes import POINTER @@ -8,14 +5,6 @@ from ctypes import c_int, c_uint, c_void_p, c_char_p -def LoadMSC() -> CDLL: - name = os.getenv('MSC_SDK_PATH') - assert name is not None, 'MSC_SDK_PATH is not set.' - return cdll.LoadLibrary(name) - -msc = LoadMSC() - - class MSPStatus: MSP_SUCCESS = 0 MSP_ERROR_FAIL = -1 @@ -1050,169 +1039,169 @@ def MSPAssert(errorCode: int, errorMsg: str): ) -""" -/** - * @fn MSPLogin - * @brief user login interface - * - * User login. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* usr - [in] user name. - * @param const char* pwd - [in] password. - * @param const char* params - [in] parameters when user login. - * @see - */ -int MSPAPI MSPLogin(const char* usr, const char* pwd, const char* params); -""" -msc.MSPLogin.argtypes = [c_char_p, c_char_p, c_char_p] -msc.MSPLogin.restype = c_int - - -def MSPLogin(usr: bytes, pwd: bytes, params: bytes): - errorCode: int = msc.MSPLogin(usr, pwd, params) - MSPAssert(errorCode, "MSPLogin failed") - - -""" -/** - * @fn MSPLogout - * @brief user logout interface - * - * User logout - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @see - */ -int MSPAPI MSPLogout(); -""" -msc.MSPLogout.argtypes = [] -msc.MSPLogout.restype = c_int - - -def MSPLogout(): - errorCode: int = msc.MSPLogout() - MSPAssert(errorCode, "MSPLogout failed") - - -""" -/** - * @fn MSPSetParam - * @brief set params of msc - * - * set param of msc - * - * @return int - Return 0 if success, otherwise return errcode. - * @param const char* paramName - [in] param name. - * @param const char* paramValue - [in] param value - * @see - */ -int MSPAPI MSPSetParam( const char* paramName, const char* paramValue ); -""" -msc.MSPSetParam.argtypes = [c_char_p, c_char_p] -msc.MSPSetParam.restype = c_int - - -def MSPSetParam(paramName: bytes, paramValue: bytes): - errorCode: int = msc.MSPSetParam(paramName, paramValue) - MSPAssert(errorCode, "MSPSetParam failed") - - -""" -/** - * @fn MSPGetParam - * @brief get params of msc - * - * get param of msc - * - * @return int - Return 0 if success, otherwise return errcode. - * @param const char* paramName - [in] param name. - * @param char* paramValue - [in/out] param value - * @param unsigned int* valueLen - [in/out] param value (buffer) length - * @see - */ -int MSPAPI MSPGetParam( const char *paramName, char *paramValue, unsigned int *valueLen ); -""" -msc.MSPGetParam.argtypes = [c_char_p, c_char_p, POINTER(c_uint)] -msc.MSPGetParam.restype = c_int - - -def MSPGetParam(paramName: bytes, paramValue: bytes) -> bytes: - valueLen = c_uint(len(paramValue)) - errorCode: int = msc.MSPGetParam(paramName, paramValue, byref(valueLen)) - MSPAssert(errorCode, "MSPGetParam failed") - return string_at(paramValue, valueLen.value) - - -""" -/** - * @fn MSPUploadData - * @brief Upload User Specific Data - * - * Upload data such as user config, custom grammar, etc. - * - * @return const char* MSPAPI - data id returned by Server, used for special command. - * @param const char* dataName - [in] data name, should be unique to diff other data. - * @param void* data - [in] the data buffer pointer, data could be binary. - * @param unsigned int dataLen - [in] length of data. - * @param const char* params - [in] parameters about uploading data. - * @param int* errorCode - [out] Return 0 in success, otherwise return error code. - * @see - */ -const char* MSPAPI MSPUploadData(const char* dataName, void* data, unsigned int dataLen, const char* params, int* errorCode); -""" -msc.MSPUploadData.argtypes = [c_char_p, c_void_p, c_uint, c_char_p, POINTER(c_int)] -msc.MSPUploadData.restype = c_char_p - - -def MSPUploadData(dataName: bytes, data: bytes, params: bytes) -> bytes: - dataLen = len(data) - errorCode = c_int() - res: bytes = msc.MSPUploadData(dataName, data, dataLen, params, byref(errorCode)) - MSPAssert(errorCode.value, "MSPUploadData failed") - return res - - -""" -/** - * @fn MSPGetVersion - * @brief Get version of MSC or Local Engine - * - * Get version of MSC or Local Engine - * - * @return const char * MSPAPI - Return version value if success, NULL if fail. - * @param const char *verName - [in] version name, could be "msc", "aitalk", "aisound", "ivw". - * @param int *errorCode - [out] Return 0 in success, otherwise return error code. - * @see - */ -const char* MSPAPI MSPGetVersion(const char *verName, int *errorCode); -""" -msc.MSPGetVersion.argtypes = [c_char_p, POINTER(c_int)] -msc.MSPGetVersion.restype = c_char_p - - -def MSPGetVersion(verName: bytes) -> bytes: - errorCode = c_int() - version: bytes = msc.MSPGetVersion(verName, byref(errorCode)) - MSPAssert(errorCode.value, "MSPGetVersion failed") - return version +class MSP: + def __init__(self, msc: CDLL): + self.msc = msc + self.msc.MSPLogin.argtypes = [c_char_p, c_char_p, c_char_p] + self.msc.MSPLogin.restype = c_int + + self.msc.MSPLogout.argtypes = [] + self.msc.MSPLogout.restype = c_int + + self.msc.MSPSetParam.argtypes = [c_char_p, c_char_p] + self.msc.MSPSetParam.restype = c_int + + self.msc.MSPGetParam.argtypes = [c_char_p, c_char_p, POINTER(c_uint)] + self.msc.MSPGetParam.restype = c_int + + self.msc.MSPUploadData.argtypes = [ + c_char_p, + c_void_p, + c_uint, + c_char_p, + POINTER(c_int), + ] + self.msc.MSPUploadData.restype = c_char_p + + self.msc.MSPGetVersion.argtypes = [c_char_p, POINTER(c_int)] + self.msc.MSPGetVersion.restype = c_char_p + + """ + /** + * @fn MSPLogin + * @brief user login interface + * + * User login. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* usr - [in] user name. + * @param const char* pwd - [in] password. + * @param const char* params - [in] parameters when user login. + * @see + */ + int MSPAPI MSPLogin(const char* usr, const char* pwd, const char* params); + """ + + def MSPLogin(self, usr: bytes, pwd: bytes, params: bytes): + errorCode: int = self.msc.MSPLogin(usr, pwd, params) + MSPAssert(errorCode, "MSPLogin failed") + + """ + /** + * @fn MSPLogout + * @brief user logout interface + * + * User logout + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @see + */ + int MSPAPI MSPLogout(); + """ + + def MSPLogout(self): + errorCode: int = self.msc.MSPLogout() + MSPAssert(errorCode, "MSPLogout failed") + + """ + /** + * @fn MSPSetParam + * @brief set params of msc + * + * set param of msc + * + * @return int - Return 0 if success, otherwise return errcode. + * @param const char* paramName - [in] param name. + * @param const char* paramValue - [in] param value + * @see + */ + int MSPAPI MSPSetParam( const char* paramName, const char* paramValue ); + """ + + def MSPSetParam(self, paramName: bytes, paramValue: bytes): + errorCode: int = self.msc.MSPSetParam(paramName, paramValue) + MSPAssert(errorCode, "MSPSetParam failed") + + """ + /** + * @fn MSPGetParam + * @brief get params of msc + * + * get param of msc + * + * @return int - Return 0 if success, otherwise return errcode. + * @param const char* paramName - [in] param name. + * @param char* paramValue - [in/out] param value + * @param unsigned int* valueLen - [in/out] param value (buffer) length + * @see + */ + int MSPAPI MSPGetParam( const char *paramName, char *paramValue, unsigned int *valueLen ); + """ + + def MSPGetParam(self, paramName: bytes, paramValue: bytes) -> bytes: + valueLen = c_uint(len(paramValue)) + errorCode: int = self.msc.MSPGetParam(paramName, paramValue, byref(valueLen)) + MSPAssert(errorCode, "MSPGetParam failed") + return string_at(paramValue, valueLen.value) + + """ + /** + * @fn MSPUploadData + * @brief Upload User Specific Data + * + * Upload data such as user config, custom grammar, etc. + * + * @return const char* MSPAPI - data id returned by Server, used for special command. + * @param const char* dataName - [in] data name, should be unique to diff other data. + * @param void* data - [in] the data buffer pointer, data could be binary. + * @param unsigned int dataLen - [in] length of data. + * @param const char* params - [in] parameters about uploading data. + * @param int* errorCode - [out] Return 0 in success, otherwise return error code. + * @see + */ + const char* MSPAPI MSPUploadData(const char* dataName, void* data, unsigned int dataLen, const char* params, int* errorCode); + """ + + def MSPUploadData(self, dataName: bytes, data: bytes, params: bytes) -> bytes: + dataLen = len(data) + errorCode = c_int() + res: bytes = self.msc.MSPUploadData( + dataName, data, dataLen, params, byref(errorCode) + ) + MSPAssert(errorCode.value, "MSPUploadData failed") + return res + + """ + /** + * @fn MSPGetVersion + * @brief Get version of MSC or Local Engine + * + * Get version of MSC or Local Engine + * + * @return const char * MSPAPI - Return version value if success, NULL if fail. + * @param const char *verName - [in] version name, could be "msc", "aitalk", "aisound", "ivw". + * @param int *errorCode - [out] Return 0 in success, otherwise return error code. + * @see + */ + const char* MSPAPI MSPGetVersion(const char *verName, int *errorCode); + """ + + def MSPGetVersion(self, verName: bytes) -> bytes: + errorCode = c_int() + version: bytes = self.msc.MSPGetVersion(verName, byref(errorCode)) + MSPAssert(errorCode.value, "MSPGetVersion failed") + return version __all__ = [ - msc, - MSPStatus, - MSPAudioSampleStatus, - MSPRECStatus, - MSPEPStatus, - MSPTTSStatus, - MSPHCRDataStatus, - MSPIVWMSGStatus, - MSPDATASampleStatus, - MSPAssert, - MSPLogin, - MSPLogout, - MSPSetParam, - MSPGetParam, - MSPUploadData, - MSPGetVersion, + "MSPStatus", + "MSPAudioSampleStatus", + "MSPRECStatus", + "MSPEPStatus", + "MSPTTSStatus", + "MSPHCRDataStatus", + "MSPIVWMSGStatus", + "MSPDATASampleStatus", + "MSPAssert", + "MSP", ] diff --git a/msc/qise.py b/msc/qise.py index 5388f36..fb59667 100644 --- a/msc/qise.py +++ b/msc/qise.py @@ -1,169 +1,172 @@ +from ctypes import CDLL from ctypes import POINTER from ctypes import byref, string_at from ctypes import c_int, c_uint, c_void_p, c_char_p from typing import Tuple -from .msp import msc from .msp import MSPAssert -''' -/** - * @fn QISESessionBegin - * @brief Begin a Evaluation Session - * - * Create a evaluation session to evaluate audio data - * - * @return const char* MSPAPI - Return the new session id in success, otherwise return NULL. - * @param const char* params - [in] Parameters to create session. - * @param const char* userModelId - [in] user model id. - * @param int *errorCode - [out] Return 0 in success, otherwise return error code. - * @see - */ -const char* MSPAPI QISESessionBegin(const char* params, const char* userModelId, int* errorCode); -''' -msc.QISESessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] -msc.QISESessionBegin.restype = c_char_p - - -def QISESessionBegin(params: bytes, userModelId: bytes) -> bytes: - errorCode = c_int() - sessionID: bytes = msc.QISESessionBegin( - params, userModelId, byref(errorCode)) - MSPAssert(errorCode.value, "QISESessionBegin failed") - return sessionID - - -''' -/** - * @fn QISETextPut - * @brief Put Text - * - * Writing text string to evaluator. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by QISESessionBegin. - * @param const char* textString - [in] Text buffer. - * @param unsigned int textLen - [in] Text length in bytes. - * @param const char* params - [in] Parameters describing the text. - * @see - */ -int MSPAPI QISETextPut(const char* sessionID, const char* textString, unsigned int textLen, const char* params); -''' -msc.QISETextPut.argtypes = [c_char_p, c_char_p, c_uint, c_char_p] -msc.QISETextPut.restype = c_int - - -def QISETextPut(sessionID: bytes, textString: bytes, params: bytes): - textLen = len(textString) - errorCode: int = msc.QISETextPut(sessionID, textString, textLen, params) - MSPAssert(errorCode, "QISETextPut failed") - - -''' -/** - * @fn QISEAudioWrite - * @brief Write Audio - * - * Writing binary audio data to evaluator. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by QISESessionBegin. - * @param const void* waveData - [in] Audio data to write. - * @param unsigned int waveLen - [in] Audio length in bytes. - * @param int audioStatus - [in] Audio status. - * @param int *epStatus - [out] EP or vad status. - * @param int *evlStatus - [out] Status of evaluation result, 0: success, 1: no match, 2: incomplete, 5:speech complete. - * @see - */ -int MSPAPI QISEAudioWrite(const char* sessionID, const void* waveData, unsigned int waveLen, int audioStatus, int *epStatus, int *Status); -''' -msc.QISEAudioWrite.argtypes = [ - c_char_p, - c_void_p, - c_uint, - c_int, - POINTER(c_int), - POINTER(c_int), -] -msc.QISEAudioWrite.restype = c_int - - -def QISEAudioWrite( - sessionID: bytes, waveData: bytes, audioStatus: int -) -> Tuple[int, int]: - waveLen = len(waveData) - epStatus = c_int() - recogStatus = c_int() - errorCode: int = msc.QISEAudioWrite( - sessionID, waveData, waveLen, audioStatus, byref( - epStatus), byref(recogStatus) - ) - MSPAssert(errorCode, "QISEAudioWrite failed") - return epStatus.value, recogStatus.value - - -''' -/** - * @fn QISEGetResult - * @brief Get Evaluation Result - * - * Get evaluation result. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by QISESessionBegin. - * @param int* rsltLen - [out] Length of result returned. - * @param int* rsltStatus - [out] Status of evaluation result returned. - * @param int* errorCode - [out] Return 0 in success, otherwise return error code. - * @see - */ -const char * MSPAPI QISEGetResult(const char* sessionID, unsigned int* rsltLen, int* rsltStatus, int *errorCode); -''' -msc.QISEGetResult.argtypes = [c_char_p, POINTER( - c_uint), POINTER(c_int), POINTER(c_int)] -msc.QISEGetResult.restype = c_char_p - - -def QISEGetResult(sessionID: bytes) -> Tuple[bytes, int]: - rsltLen = c_uint() - rsltStatus = c_int() - errorCode = c_int() - result: bytes = msc.QISEGetResult( - sessionID, byref(rsltLen), byref(rsltStatus), byref(errorCode) - ) - MSPAssert(errorCode.value, "QISEGetResult failed") - # return result, rsltStatus.value - return string_at(result, rsltLen), rsltStatus.value - - -''' -/** - * @fn QISESessionEnd - * @brief End a ISR Session - * - * End a evaluation session, release all resource. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by QISESessionBegin. - * @param const char* hints - [in] Reason to end current session. - * @see - */ -int MSPAPI QISESessionEnd(const char* sessionID, const char* hints); -''' -msc.QISESessionEnd.argtypes = [c_char_p, c_char_p] -msc.QISESessionEnd.restype = c_int - - -def QISESessionEnd(sessionID: bytes, hints: bytes): - errorCode: int = msc.QISESessionEnd(sessionID, hints) - MSPAssert(errorCode, "QISESessionEnd failed") - - -__all__ = [ - QISESessionBegin, - QISESessionEnd, - QISETextPut, - QISEAudioWrite, - QISEGetResult, -] +class QISE: + def __init__(self, msc: CDLL) -> None: + self.msc = msc + + self.msc.QISESessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] + self.msc.QISESessionBegin.restype = c_char_p + + self.msc.QISETextPut.argtypes = [c_char_p, c_char_p, c_uint, c_char_p] + self.msc.QISETextPut.restype = c_int + + self.msc.QISEAudioWrite.argtypes = [ + c_char_p, + c_void_p, + c_uint, + c_int, + POINTER(c_int), + POINTER(c_int), + ] + self.msc.QISEAudioWrite.restype = c_int + + self.msc.QISEGetResult.argtypes = [ + c_char_p, + POINTER(c_uint), + POINTER(c_int), + POINTER(c_int), + ] + self.msc.QISEGetResult.restype = c_char_p + + self.msc.QISESessionEnd.argtypes = [c_char_p, c_char_p] + self.msc.QISESessionEnd.restype = c_int + + """ + /** + * @fn QISESessionBegin + * @brief Begin a Evaluation Session + * + * Create a evaluation session to evaluate audio data + * + * @return const char* MSPAPI - Return the new session id in success, otherwise return NULL. + * @param const char* params - [in] Parameters to create session. + * @param const char* userModelId - [in] user model id. + * @param int *errorCode - [out] Return 0 in success, otherwise return error code. + * @see + */ + const char* MSPAPI QISESessionBegin(const char* params, const char* userModelId, int* errorCode); + """ + + def QISESessionBegin(self, params: bytes, userModelId: bytes) -> bytes: + errorCode = c_int() + sessionID: bytes = self.msc.QISESessionBegin( + params, userModelId, byref(errorCode) + ) + MSPAssert(errorCode.value, "QISESessionBegin failed") + return sessionID + + """ + /** + * @fn QISETextPut + * @brief Put Text + * + * Writing text string to evaluator. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by QISESessionBegin. + * @param const char* textString - [in] Text buffer. + * @param unsigned int textLen - [in] Text length in bytes. + * @param const char* params - [in] Parameters describing the text. + * @see + */ + int MSPAPI QISETextPut(const char* sessionID, const char* textString, unsigned int textLen, const char* params); + """ + + def QISETextPut(self, sessionID: bytes, textString: bytes, params: bytes): + textLen = len(textString) + errorCode: int = self.msc.QISETextPut(sessionID, textString, textLen, params) + MSPAssert(errorCode, "QISETextPut failed") + + """ + /** + * @fn QISEAudioWrite + * @brief Write Audio + * + * Writing binary audio data to evaluator. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by QISESessionBegin. + * @param const void* waveData - [in] Audio data to write. + * @param unsigned int waveLen - [in] Audio length in bytes. + * @param int audioStatus - [in] Audio status. + * @param int *epStatus - [out] EP or vad status. + * @param int *evlStatus - [out] Status of evaluation result, 0: success, 1: no match, 2: incomplete, 5:speech complete. + * @see + */ + int MSPAPI QISEAudioWrite(const char* sessionID, const void* waveData, unsigned int waveLen, int audioStatus, int *epStatus, int *Status); + """ + + def QISEAudioWrite( + self, sessionID: bytes, waveData: bytes, audioStatus: int + ) -> Tuple[int, int]: + waveLen = len(waveData) + epStatus = c_int() + recogStatus = c_int() + errorCode: int = self.msc.QISEAudioWrite( + sessionID, + waveData, + waveLen, + audioStatus, + byref(epStatus), + byref(recogStatus), + ) + MSPAssert(errorCode, "QISEAudioWrite failed") + return epStatus.value, recogStatus.value + + """ + /** + * @fn QISEGetResult + * @brief Get Evaluation Result + * + * Get evaluation result. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by QISESessionBegin. + * @param int* rsltLen - [out] Length of result returned. + * @param int* rsltStatus - [out] Status of evaluation result returned. + * @param int* errorCode - [out] Return 0 in success, otherwise return error code. + * @see + */ + const char * MSPAPI QISEGetResult(const char* sessionID, unsigned int* rsltLen, int* rsltStatus, int *errorCode); + """ + + def QISEGetResult(self, sessionID: bytes) -> Tuple[bytes, int]: + rsltLen = c_uint() + rsltStatus = c_int() + errorCode = c_int() + result: bytes = self.msc.QISEGetResult( + sessionID, byref(rsltLen), byref(rsltStatus), byref(errorCode) + ) + MSPAssert(errorCode.value, "QISEGetResult failed") + # return result, rsltStatus.value + return string_at(result, rsltLen.value), rsltStatus.value + + """ + /** + * @fn QISESessionEnd + * @brief End a ISR Session + * + * End a evaluation session, release all resource. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by QISESessionBegin. + * @param const char* hints - [in] Reason to end current session. + * @see + */ + int MSPAPI QISESessionEnd(const char* sessionID, const char* hints); + """ + + def QISESessionEnd(self, sessionID: bytes, hints: bytes): + errorCode: int = self.msc.QISESessionEnd(sessionID, hints) + MSPAssert(errorCode, "QISESessionEnd failed") + + +__all__ = ["QISE"] diff --git a/msc/qisr.py b/msc/qisr.py index e2f8359..38bacaa 100644 --- a/msc/qisr.py +++ b/msc/qisr.py @@ -1,234 +1,236 @@ from _ctypes import CFuncPtr +from ctypes import CDLL from ctypes import byref, string_at from ctypes import CFUNCTYPE, POINTER from ctypes import c_int, c_uint, c_void_p, c_char_p from typing import Tuple -from .msp import msc from .msp import MSPAssert -''' -/** - * @fn QISRSessionBegin - * @brief Begin a Recognizer Session - * - * Create a recognizer session to recognize audio data - * - * @return return sessionID of current session, NULL is failed. - * @param const char* grammarList - [in] grammars list, inline grammar support only one. - * @param const char* params - [in] parameters when the session created. - * @param int *errorCode - [out] return 0 on success, otherwise return error code. - * @see - */ -const char* MSPAPI QISRSessionBegin(const char* grammarList, const char* params, int* errorCode); -''' -msc.QISRSessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] -msc.QISRSessionBegin.restype = c_char_p - - -def QISRSessionBegin(grammarList: bytes, params: bytes) -> bytes: - errorCode = c_int() - sessionID: bytes = msc.QISRSessionBegin( - grammarList, params, byref(errorCode)) - MSPAssert(errorCode.value, "QISRSessionBegin failed") - return sessionID - - -''' -/** - * @fn QISRAudioWrite - * @brief Write Audio Data to Recognizer Session - * - * Writing binary audio data to recognizer. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by recog_begin - * @param const void* waveData - [in] Binary data of waveform - * @param unsigned int waveLen - [in] Waveform data size in bytes - * @param int audioStatus - [in] Audio status, can be - * @param int *epStatus - [out] ISRepState - * @param int *recogStatus - [out] ISRrecRecognizerStatus, see isr_rec.h - * @see - */ -int MSPAPI QISRAudioWrite(const char* sessionID, const void* waveData, unsigned int waveLen, int audioStatus, int *epStatus, int *recogStatus); -''' -msc.QISRAudioWrite.argtypes = [ - c_char_p, - c_void_p, - c_uint, - c_int, - POINTER(c_int), - POINTER(c_int), -] -msc.QISRAudioWrite.restype = c_int - - -def QISRAudioWrite( - sessionID: bytes, waveData: bytes, audioStatus: int -) -> Tuple[int, int]: - waveLen = len(waveData) - epStatus = c_int() - recogStatus = c_int() - errorCode: int = msc.QISRAudioWrite( - sessionID, waveData, waveLen, audioStatus, byref( - epStatus), byref(recogStatus) - ) - MSPAssert(errorCode, "QISRAudioWrite failed") - return epStatus.value, recogStatus.value - - -''' -/** - * @fn QISRGetResult - * @brief Get Recognize Result in Specified Format - * - * Get recognize result in Specified format. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] session id returned by session begin - * @param int* rsltStatus - [out] status of recognition result, 0: success, 1: no match, 2: incomplete, 5:speech complete - * @param int *errorCode - [out] return 0 on success, otherwise return error code. - * @see - */ -const char * MSPAPI QISRGetResult(const char* sessionID, int* rsltStatus, int waitTime, int *errorCode); -''' -msc.QISRGetResult.argtypes = [c_char_p, POINTER(c_int), c_int, POINTER(c_int)] -msc.QISRGetResult.restype = c_char_p - - -def QISRGetResult(sessionID: bytes, waitTime: int) -> Tuple[bytes, int]: - rsltStatus = c_int() - errorCode = c_int() - result: bytes = msc.QISRGetResult( - sessionID, byref(rsltStatus), waitTime, byref(errorCode) - ) - MSPAssert(errorCode.value, "QISRGetResult failed") - return result, rsltStatus.value - - -''' -/** - * @fn QISRSessionEnd - * @brief End a Recognizer Session - * - * End the recognizer session, release all resource. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] session id string to end - * @param const char* hints - [in] user hints to end session, hints will be logged to CallLog - * @see - */ -int MSPAPI QISRSessionEnd(const char* sessionID, const char* hints); -''' -msc.QISRSessionEnd.argtypes = [c_char_p, c_char_p] -msc.QISRSessionEnd.restype = c_int - - -def QISRSessionEnd(sessionID: bytes, hints: bytes): - errorCode: int = msc.QISRSessionEnd(sessionID, hints) - MSPAssert(errorCode, "QISRSessionEnd failed") - - -''' -/** - * @fn QISRGetParam - * @brief get params related with msc - * - * the params could be local or server param, we only support netflow params "upflow" & "downflow" now - * - * @return int - Return 0 if success, otherwise return errcode. - * @param const char* sessionID - [in] session id of related param, set NULL to got global param - * @param const char* paramName - [in] param name,could pass more than one param split by ','';'or'\n' - * @param const char* paramValue - [in] param value buffer, malloced by user - * @param int *valueLen - [in, out] pass in length of value buffer, and return length of value string - * @see - */ -int MSPAPI QISRGetParam(const char* sessionID, const char* paramName, char* paramValue, unsigned int* valueLen); -''' -msc.QISRGetParam.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_uint)] -msc.QISRGetParam.restype = c_int - - -def QISRGetParam(sessionID: bytes, paramName: bytes, paramValue: bytes) -> bytes: - valueLen = c_uint(len(paramValue)) - errorCode = c_int() - errorCode: int = msc.QISRGetParam( - sessionID, paramName, paramValue, byref(valueLen) - ) - MSPAssert(errorCode.value, "QISRGetParam failed") - return string_at(paramValue, valueLen.value) - - -''' -typedef int ( *GrammarCallBack)( int, const char*, void*); -int MSPAPI QISRBuildGrammar(const char *grammarType, const char *grammarContent, unsigned int grammarLength, const char *params, GrammarCallBack callback, void *userData); -''' -GrammarCallBack = CFUNCTYPE(c_int, c_int, c_char_p, c_void_p) -msc.QISRBuildGrammar.argtypes = [ - c_char_p, - c_char_p, - c_uint, - c_char_p, - GrammarCallBack, - c_void_p, -] -msc.QISRBuildGrammar.restype = c_int - - -def QISRBuildGrammar( - grammarType: bytes, - grammarContent: bytes, - params: bytes, - callback: CFuncPtr, - userData: bytes, -): - grammarLength = len(grammarContent) - errorCode: int = msc.QISRBuildGrammar( - grammarType, grammarContent, grammarLength, params, callback, userData - ) - MSPAssert(errorCode, "QISRBuildGrammar failed") - - -''' -typedef int ( *LexiconCallBack)( int, const char*, void*); -int MSPAPI QISRUpdateLexicon(const char *lexiconName, const char *lexiconContent, unsigned int lexiconLength, const char *params, LexiconCallBack callback, void *userData); -''' -LexiconCallBack = CFUNCTYPE(c_int, c_int, c_char_p, c_void_p) -msc.QISRUpdateLexicon.argtypes = [ - c_char_p, - c_char_p, - c_uint, - c_char_p, - LexiconCallBack, - c_void_p, -] -msc.QISRUpdateLexicon.restype = c_int - - -def QISRUpdateLexicon( - lexiconName: bytes, - lexiconContent: bytes, - lexiconLength: int, - params: bytes, - callback: CFuncPtr, - userData: bytes, -): - errorCode: int = msc.QISRUpdateLexicon( - lexiconName, lexiconContent, lexiconLength, params, callback, userData - ) - MSPAssert(errorCode, "QISRUpdateLexicon failed") - - -__all__ = [ - QISRSessionBegin, - QISRAudioWrite, - QISRGetResult, - QISRSessionEnd, - QISRGetParam, - QISRBuildGrammar, - QISRUpdateLexicon, - LexiconCallBack, - GrammarCallBack, -] +class QISR: + def __init__(self, msc: CDLL) -> None: + self.msc = msc + + self.msc.QISRSessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] + self.msc.QISRSessionBegin.restype = c_char_p + + self.msc.QISRAudioWrite.argtypes = [ + c_char_p, + c_void_p, + c_uint, + c_int, + POINTER(c_int), + POINTER(c_int), + ] + self.msc.QISRAudioWrite.restype = c_int + + self.msc.QISRGetResult.argtypes = [ + c_char_p, + POINTER(c_int), + c_int, + POINTER(c_int), + ] + self.msc.QISRGetResult.restype = c_char_p + + self.msc.QISRSessionEnd.argtypes = [c_char_p, c_char_p] + self.msc.QISRSessionEnd.restype = c_int + + self.GrammarCallBack = CFUNCTYPE(c_int, c_int, c_char_p, c_void_p) + self.msc.QISRBuildGrammar.argtypes = [ + c_char_p, + c_char_p, + c_uint, + c_char_p, + self.GrammarCallBack, + c_void_p, + ] + self.msc.QISRBuildGrammar.restype = c_int + + self.LexiconCallBack = CFUNCTYPE(c_int, c_int, c_char_p, c_void_p) + self.msc.QISRUpdateLexicon.argtypes = [ + c_char_p, + c_char_p, + c_uint, + c_char_p, + self.LexiconCallBack, + c_void_p, + ] + self.msc.QISRUpdateLexicon.restype = c_int + + self.msc.QISRGetParam.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_uint)] + self.msc.QISRGetParam.restype = c_int + + """ + /** + * @fn QISRSessionBegin + * @brief Begin a Recognizer Session + * + * Create a recognizer session to recognize audio data + * + * @return return sessionID of current session, NULL is failed. + * @param const char* grammarList - [in] grammars list, inline grammar support only one. + * @param const char* params - [in] parameters when the session created. + * @param int *errorCode - [out] return 0 on success, otherwise return error code. + * @see + */ + const char* MSPAPI QISRSessionBegin(const char* grammarList, const char* params, int* errorCode); + """ + + def QISRSessionBegin(self, grammarList: bytes, params: bytes) -> bytes: + errorCode = c_int() + sessionID: bytes = self.msc.QISRSessionBegin( + grammarList, params, byref(errorCode) + ) + MSPAssert(errorCode.value, "QISRSessionBegin failed") + return sessionID + + """ + /** + * @fn QISRAudioWrite + * @brief Write Audio Data to Recognizer Session + * + * Writing binary audio data to recognizer. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by recog_begin + * @param const void* waveData - [in] Binary data of waveform + * @param unsigned int waveLen - [in] Waveform data size in bytes + * @param int audioStatus - [in] Audio status, can be + * @param int *epStatus - [out] ISRepState + * @param int *recogStatus - [out] ISRrecRecognizerStatus, see isr_rec.h + * @see + */ + int MSPAPI QISRAudioWrite(const char* sessionID, const void* waveData, unsigned int waveLen, int audioStatus, int *epStatus, int *recogStatus); + """ + + def QISRAudioWrite( + self, sessionID: bytes, waveData: bytes, audioStatus: int + ) -> Tuple[int, int]: + waveLen = len(waveData) + epStatus = c_int() + recogStatus = c_int() + errorCode: int = self.msc.QISRAudioWrite( + sessionID, + waveData, + waveLen, + audioStatus, + byref(epStatus), + byref(recogStatus), + ) + MSPAssert(errorCode, "QISRAudioWrite failed") + return epStatus.value, recogStatus.value + + """ + /** + * @fn QISRGetResult + * @brief Get Recognize Result in Specified Format + * + * Get recognize result in Specified format. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] session id returned by session begin + * @param int* rsltStatus - [out] status of recognition result, 0: success, 1: no match, 2: incomplete, 5:speech complete + * @param int *errorCode - [out] return 0 on success, otherwise return error code. + * @see + */ + const char * MSPAPI QISRGetResult(const char* sessionID, int* rsltStatus, int waitTime, int *errorCode); + """ + + def QISRGetResult(self, sessionID: bytes, waitTime: int) -> Tuple[bytes, int]: + rsltStatus = c_int() + errorCode = c_int() + result: bytes = self.msc.QISRGetResult( + sessionID, byref(rsltStatus), waitTime, byref(errorCode) + ) + MSPAssert(errorCode.value, "QISRGetResult failed") + return result, rsltStatus.value + + """ + /** + * @fn QISRSessionEnd + * @brief End a Recognizer Session + * + * End the recognizer session, release all resource. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] session id string to end + * @param const char* hints - [in] user hints to end session, hints will be logged to CallLog + * @see + */ + int MSPAPI QISRSessionEnd(const char* sessionID, const char* hints); + """ + + def QISRSessionEnd(self, sessionID: bytes, hints: bytes): + errorCode: int = self.msc.QISRSessionEnd(sessionID, hints) + MSPAssert(errorCode, "QISRSessionEnd failed") + + """ + /** + * @fn QISRGetParam + * @brief get params related with msc + * + * the params could be local or server param, we only support netflow params "upflow" & "downflow" now + * + * @return int - Return 0 if success, otherwise return errcode. + * @param const char* sessionID - [in] session id of related param, set NULL to got global param + * @param const char* paramName - [in] param name,could pass more than one param split by ','';'or'\n' + * @param const char* paramValue - [in] param value buffer, malloced by user + * @param int *valueLen - [in, out] pass in length of value buffer, and return length of value string + * @see + */ + int MSPAPI QISRGetParam(const char* sessionID, const char* paramName, char* paramValue, unsigned int* valueLen); + """ + + def QISRGetParam( + self, sessionID: bytes, paramName: bytes, paramValue: bytes + ) -> bytes: + valueLen = c_uint(len(paramValue)) + errorCode = c_int() + errorCode: c_int = self.msc.QISRGetParam( + sessionID, paramName, paramValue, byref(valueLen) + ) + MSPAssert(errorCode.value, "QISRGetParam failed") + return string_at(paramValue, valueLen.value) + + """ + typedef int ( *GrammarCallBack)( int, const char*, void*); + int MSPAPI QISRBuildGrammar(const char *grammarType, const char *grammarContent, unsigned int grammarLength, const char *params, GrammarCallBack callback, void *userData); + """ + + def QISRBuildGrammar( + self, + grammarType: bytes, + grammarContent: bytes, + params: bytes, + callback: CFuncPtr, + userData: bytes, + ): + grammarLength = len(grammarContent) + errorCode: int = self.msc.QISRBuildGrammar( + grammarType, grammarContent, grammarLength, params, callback, userData + ) + MSPAssert(errorCode, "QISRBuildGrammar failed") + + """ + typedef int ( *LexiconCallBack)( int, const char*, void*); + int MSPAPI QISRUpdateLexicon(const char *lexiconName, const char *lexiconContent, unsigned int lexiconLength, const char *params, LexiconCallBack callback, void *userData); + """ + + def QISRUpdateLexicon( + self, + lexiconName: bytes, + lexiconContent: bytes, + lexiconLength: int, + params: bytes, + callback: CFuncPtr, + userData: bytes, + ): + errorCode: int = self.msc.QISRUpdateLexicon( + lexiconName, lexiconContent, lexiconLength, params, callback, userData + ) + MSPAssert(errorCode, "QISRUpdateLexicon failed") + + +__all__ = ["QISR"] diff --git a/msc/qivw.py b/msc/qivw.py index f8d1f4c..dfcd73f 100644 --- a/msc/qivw.py +++ b/msc/qivw.py @@ -1,69 +1,75 @@ from _ctypes import CFuncPtr +from ctypes import CDLL from ctypes import byref from ctypes import CFUNCTYPE, POINTER from ctypes import c_int, c_uint, c_void_p, c_char_p -from .msp import msc from .msp import MSPAssert -""" -const char* MSPAPI QIVWSessionBegin(const char *grammarList, const char *params, int *errorCode); -""" -msc.QIVWSessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] -msc.QIVWSessionBegin.restype = c_char_p - - -def QIVWSessionBegin(grammarList: bytes, params: bytes) -> bytes: - errorCode = c_int() - sessionID: bytes = msc.QIVWSessionBegin(grammarList, params, byref(errorCode)) - MSPAssert(errorCode.value, "QIVWSessionBegin failed") - return sessionID - - -""" -int MSPAPI QIVWSessionEnd(const char *sessionID, const char *hints); -""" -msc.QIVWSessionEnd.argtypes = [c_char_p, c_char_p] -msc.QIVWSessionEnd.restype = c_int - - -def QIVWSessionEnd(sessionID: bytes, hints: bytes): - errorCode: int = msc.QIVWSessionEnd(sessionID, hints) - MSPAssert(errorCode, "QIVWSessionEnd failed") - - -""" -int MSPAPI QIVWAudioWrite(const char *sessionID, const void *audioData, unsigned int audioLen, int audioStatus); -""" -msc.QIVWAudioWrite.argtypes = [c_char_p, c_void_p, c_uint, c_int] -msc.QIVWAudioWrite.restype = c_int - - -def QIVWAudioWrite(sessionID: bytes, audioData: bytes, audioStatus: int): - audioLen = len(audioData) - errorCode: int = msc.QIVWAudioWrite(sessionID, audioData, audioLen, audioStatus) - MSPAssert(errorCode, "QIVWAudioWrite failed") - - -""" -typedef int( *ivw_ntf_handler)( const char *sessionID, int msg, int param1, int param2, const void *info, void *userData ); -int MSPAPI QIVWRegisterNotify(const char *sessionID, ivw_ntf_handler msgProcCb, void *userData); -""" -msgProcCallBack = CFUNCTYPE(c_int, c_char_p, c_int, c_int, c_int, c_void_p, c_void_p) -msc.QIVWRegisterNotify.argtypes = [c_char_p, msgProcCallBack, c_void_p] -msc.QIVWRegisterNotify.restype = c_int - - -def QIVWRegisterNotify(sessionID: bytes, msgProcCb: CFuncPtr, userData: bytes): - errorCode: int = msc.QIVWRegisterNotify(sessionID, msgProcCb, userData) - MSPAssert(errorCode, "QIVWRegisterNotify failed") - - -__all__ = [ - msgProcCallBack, - QIVWSessionBegin, - QIVWSessionEnd, - QIVWAudioWrite, - QIVWRegisterNotify, -] +class QIVW: + def __init__(self, msc: CDLL) -> None: + self.msc = msc + self.msc.QIVWSessionBegin.argtypes = [c_char_p, c_char_p, POINTER(c_int)] + self.msc.QIVWSessionBegin.restype = c_char_p + + self.msc.QIVWSessionEnd.argtypes = [c_char_p, c_char_p] + self.msc.QIVWSessionEnd.restype = c_int + + self.msc.QIVWAudioWrite.argtypes = [c_char_p, c_void_p, c_uint, c_int] + self.msc.QIVWAudioWrite.restype = c_int + + self.msgProcCallBack = CFUNCTYPE( + c_int, c_char_p, c_int, c_int, c_int, c_void_p, c_void_p + ) + self.msc.QIVWRegisterNotify.argtypes = [ + c_char_p, + self.msgProcCallBack, + c_void_p, + ] + self.msc.QIVWRegisterNotify.restype = c_int + + """ + const char* MSPAPI QIVWSessionBegin(const char *grammarList, const char *params, int *errorCode); + """ + + def QIVWSessionBegin(self, grammarList: bytes, params: bytes) -> bytes: + errorCode = c_int() + sessionID: bytes = self.msc.QIVWSessionBegin( + grammarList, params, byref(errorCode) + ) + MSPAssert(errorCode.value, "QIVWSessionBegin failed") + return sessionID + + """ + int MSPAPI QIVWSessionEnd(const char *sessionID, const char *hints); + """ + + def QIVWSessionEnd(self, sessionID: bytes, hints: bytes): + errorCode: int = self.msc.QIVWSessionEnd(sessionID, hints) + MSPAssert(errorCode, "QIVWSessionEnd failed") + + """ + int MSPAPI QIVWAudioWrite(const char *sessionID, const void *audioData, unsigned int audioLen, int audioStatus); + """ + + def QIVWAudioWrite(self, sessionID: bytes, audioData: bytes, audioStatus: int): + audioLen = len(audioData) + errorCode: int = self.msc.QIVWAudioWrite( + sessionID, audioData, audioLen, audioStatus + ) + MSPAssert(errorCode, "QIVWAudioWrite failed") + + """ + typedef int( *ivw_ntf_handler)( const char *sessionID, int msg, int param1, int param2, const void *info, void *userData ); + int MSPAPI QIVWRegisterNotify(const char *sessionID, ivw_ntf_handler msgProcCb, void *userData); + """ + + def QIVWRegisterNotify( + self, sessionID: bytes, msgProcCb: CFuncPtr, userData: bytes + ): + errorCode: int = self.msc.QIVWRegisterNotify(sessionID, msgProcCb, userData) + MSPAssert(errorCode, "QIVWRegisterNotify failed") + + +__all__ = ["QIVW"] diff --git a/msc/qtts.py b/msc/qtts.py index 442faa4..b5d72f5 100644 --- a/msc/qtts.py +++ b/msc/qtts.py @@ -1,148 +1,151 @@ +from ctypes import CDLL from ctypes import POINTER from ctypes import byref, string_at from ctypes import c_int, c_uint, c_void_p, c_char_p from typing import Tuple -from .msp import msc from .msp import MSPAssert -""" -/** - * @fn QTTSSessionBegin - * @brief Begin a TTS Session - * - * Create a tts session to synthesize data. - * - * @return const char* - Return the new session id in success, otherwise return NULL, error code. - * @param const char* params - [in] parameters when the session created. - * @param const char** sessionID - [out] return a string to this session. - * @see - */ -const char* MSPAPI QTTSSessionBegin(const char* params, int* errorCode); -""" -msc.QTTSSessionBegin.argtypes = [c_char_p, POINTER(c_int)] -msc.QTTSSessionBegin.restype = c_char_p - - -def QTTSSessionBegin(params: bytes) -> bytes: - errorCode = c_int() - sessionID: bytes = msc.QTTSSessionBegin(params, byref(errorCode)) - MSPAssert(errorCode.value, "QTTSSessionBegin failed") - return sessionID - - -""" -/** - * @fn QTTSTextPut - * @brief Put Text Buffer to TTS Session - * - * Writing text string to synthesizer. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* sessionID - [in] The session id returned by sesson begin - * @param const char* textString - [in] text buffer - * @param unsigned int textLen - [in] text size in bytes - * @see - */ -int MSPAPI QTTSTextPut(const char* sessionID, const char* textString, unsigned int textLen, const char* params); -""" -msc.QTTSTextPut.argtypes = [c_char_p, c_char_p, c_uint, c_char_p] -msc.QTTSTextPut.restype = c_int - - -def QTTSTextPut(sessionID: bytes, textString: bytes, params: bytes): - textLen = len(textString) - errorCode: int = msc.QTTSTextPut(sessionID, textString, textLen, params) - MSPAssert(errorCode, "QTTSTextPut failed") - - -""" -/** - * @fn QTTSAudioGet - * @brief Synthesize text to audio - * - * Synthesize text to audio, and return audio information. - * - * @return const void* - Return current synthesized audio data buffer, size returned by QTTSTextSynth. - * @param const char* sessionID - [in] session id returned by session begin - * @param unsigned int* audioLen - [out] synthesized audio size in bytes - * @param int* synthStatus - [out] synthesizing status - * @param int* errorCode - [out] error code if failed, 0 to success. - * @see - */ -const void* MSPAPI QTTSAudioGet(const char* sessionID, unsigned int* audioLen, int* synthStatus, int* errorCode); -""" -msc.QTTSAudioGet.argtypes = [c_char_p, POINTER(c_uint), POINTER(c_int), POINTER(c_int)] -msc.QTTSAudioGet.restype = c_void_p - - -def QTTSAudioGet(sessionID: bytes) -> Tuple[bytes, int]: - audioLen = c_uint() - synthStatus = c_int() - errorCode = c_int() - audioData: c_void_p = msc.QTTSAudioGet( - sessionID, byref(audioLen), byref(synthStatus), byref(errorCode) - ) - MSPAssert(errorCode.value, "QTTSAudioGet failed") - return string_at(audioData, audioLen.value), synthStatus.value - - -""" -/** - * @fn QTTSSessionEnd - * @brief End a Recognizer Session - * - * End the recognizer session, release all resource. - * - * @return int MSPAPI - Return 0 in success, otherwise return error code. - * @param const char* session_id - [in] session id string to end - * @param const char* hints - [in] user hints to end session, hints will be logged to CallLog - * @see - */ -int MSPAPI QTTSSessionEnd(const char* sessionID, const char* hints); -""" -msc.QTTSSessionEnd.argtypes = [c_char_p, c_char_p] -msc.QTTSSessionEnd.restype = c_int - - -def QTTSSessionEnd(sessionID: bytes, hints: bytes): - errorCode: int = msc.QTTSSessionEnd(sessionID, hints) - MSPAssert(errorCode, "QTTSSessionEnd failed") - - -""" -/** - * @fn QTTSGetParam - * @brief get params related with msc - * - * the params could be local or server param, we only support netflow params "upflow" & "downflow" now - * - * @return int - Return 0 if success, otherwise return errcode. - * @param const char* sessionID - [in] session id of related param, set NULL to got global param - * @param const char* paramName - [in] param name,could pass more than one param split by ','';'or'\n' - * @param const char* paramValue - [in] param value buffer, malloced by user - * @param int *valueLen - [in, out] pass in length of value buffer, and return length of value string - * @see - */ -int MSPAPI QTTSGetParam(const char* sessionID, const char* paramName, char* paramValue, unsigned int* valueLen); -""" -msc.QTTSGetParam.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_uint)] -msc.QTTSGetParam.restype = c_int - - -def QTTSGetParam(sessionID: bytes, paramName: bytes, paramValue: bytes) -> bytes: - valueLen = c_uint(len(paramValue)) - errorCode: int = msc.QTTSGetParam(sessionID, paramName, paramValue, byref(valueLen)) - MSPAssert(errorCode, "QTTSGetParam failed") - return string_at(paramValue, valueLen.value) - - -__all__ = [ - QTTSSessionBegin, - QTTSTextPut, - QTTSAudioGet, - QTTSSessionEnd, - QTTSGetParam, -] +class QTTS: + def __init__(self, msc: CDLL) -> None: + self.msc = msc + + self.msc.QTTSSessionBegin.argtypes = [c_char_p, POINTER(c_int)] + self.msc.QTTSSessionBegin.restype = c_char_p + + self.msc.QTTSTextPut.argtypes = [c_char_p, c_char_p, c_uint, c_char_p] + self.msc.QTTSTextPut.restype = c_int + + self.msc.QTTSAudioGet.argtypes = [ + c_char_p, + POINTER(c_uint), + POINTER(c_int), + POINTER(c_int), + ] + self.msc.QTTSAudioGet.restype = c_void_p + + self.msc.QTTSSessionEnd.argtypes = [c_char_p, c_char_p] + self.msc.QTTSSessionEnd.restype = c_int + + self.msc.QTTSGetParam.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_uint)] + self.msc.QTTSGetParam.restype = c_int + + """ + /** + * @fn QTTSSessionBegin + * @brief Begin a TTS Session + * + * Create a tts session to synthesize data. + * + * @return const char* - Return the new session id in success, otherwise return NULL, error code. + * @param const char* params - [in] parameters when the session created. + * @param const char** sessionID - [out] return a string to this session. + * @see + */ + const char* MSPAPI QTTSSessionBegin(const char* params, int* errorCode); + """ + + def QTTSSessionBegin(self, params: bytes) -> bytes: + errorCode = c_int() + sessionID: bytes = self.msc.QTTSSessionBegin(params, byref(errorCode)) + MSPAssert(errorCode.value, "QTTSSessionBegin failed") + return sessionID + + """ + /** + * @fn QTTSTextPut + * @brief Put Text Buffer to TTS Session + * + * Writing text string to synthesizer. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* sessionID - [in] The session id returned by sesson begin + * @param const char* textString - [in] text buffer + * @param unsigned int textLen - [in] text size in bytes + * @see + */ + int MSPAPI QTTSTextPut(const char* sessionID, const char* textString, unsigned int textLen, const char* params); + """ + + def QTTSTextPut(self, sessionID: bytes, textString: bytes, params: bytes): + textLen = len(textString) + errorCode: int = self.msc.QTTSTextPut(sessionID, textString, textLen, params) + MSPAssert(errorCode, "QTTSTextPut failed") + + """ + /** + * @fn QTTSAudioGet + * @brief Synthesize text to audio + * + * Synthesize text to audio, and return audio information. + * + * @return const void* - Return current synthesized audio data buffer, size returned by QTTSTextSynth. + * @param const char* sessionID - [in] session id returned by session begin + * @param unsigned int* audioLen - [out] synthesized audio size in bytes + * @param int* synthStatus - [out] synthesizing status + * @param int* errorCode - [out] error code if failed, 0 to success. + * @see + */ + const void* MSPAPI QTTSAudioGet(const char* sessionID, unsigned int* audioLen, int* synthStatus, int* errorCode); + """ + + def QTTSAudioGet(self, sessionID: bytes) -> Tuple[bytes, int]: + audioLen = c_uint() + synthStatus = c_int() + errorCode = c_int() + audioData: c_void_p = self.msc.QTTSAudioGet( + sessionID, byref(audioLen), byref(synthStatus), byref(errorCode) + ) + MSPAssert(errorCode.value, "QTTSAudioGet failed") + return string_at(audioData, audioLen.value), synthStatus.value + + """ + /** + * @fn QTTSSessionEnd + * @brief End a Recognizer Session + * + * End the recognizer session, release all resource. + * + * @return int MSPAPI - Return 0 in success, otherwise return error code. + * @param const char* session_id - [in] session id string to end + * @param const char* hints - [in] user hints to end session, hints will be logged to CallLog + * @see + */ + int MSPAPI QTTSSessionEnd(const char* sessionID, const char* hints); + """ + + def QTTSSessionEnd(self, sessionID: bytes, hints: bytes): + errorCode: int = self.msc.QTTSSessionEnd(sessionID, hints) + MSPAssert(errorCode, "QTTSSessionEnd failed") + + """ + /** + * @fn QTTSGetParam + * @brief get params related with msc + * + * the params could be local or server param, we only support netflow params "upflow" & "downflow" now + * + * @return int - Return 0 if success, otherwise return errcode. + * @param const char* sessionID - [in] session id of related param, set NULL to got global param + * @param const char* paramName - [in] param name,could pass more than one param split by ','';'or'\n' + * @param const char* paramValue - [in] param value buffer, malloced by user + * @param int *valueLen - [in, out] pass in length of value buffer, and return length of value string + * @see + */ + int MSPAPI QTTSGetParam(const char* sessionID, const char* paramName, char* paramValue, unsigned int* valueLen); + """ + + def QTTSGetParam( + self, sessionID: bytes, paramName: bytes, paramValue: bytes + ) -> bytes: + valueLen = c_uint(len(paramValue)) + errorCode: int = self.msc.QTTSGetParam( + sessionID, paramName, paramValue, byref(valueLen) + ) + MSPAssert(errorCode, "QTTSGetParam failed") + return string_at(paramValue, valueLen.value) + + +__all__ = ["QTTS"] diff --git a/setup.py b/setup.py index ab7793c..af437e9 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='msc', - version='0.1.0', + version='0.2.0', description='A package for IFLYTEK MSC.', long_description=open('README.md', encoding='UTF-8').read(), long_description_content_type='text/markdown',