tts.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "tts.h"
  2. #include "talk.h"
  3. #include "lsapi_tts.h"
  4. #include "lsapi_audio.h"
  5. #define TTS_MAX_LEN (20+4*200) //max 200 chinese
  6. typedef struct{
  7. T_UINT8 plyNow:1;
  8. T_UINT8 ttsType;
  9. T_UINT16 ttsLen;
  10. T_UINT8 ttsData[TTS_MAX_LEN+2];//should have 2 '"'
  11. }TTS_DEF;
  12. TTS_DEF tts={
  13. .plyNow=0,
  14. };
  15. static void prvLsTtsPlayerEvCb(LSAPI_AUD_PlayerEvent_t event){
  16. wlog_info("tts event:%d",event);
  17. //LSAPI_Log_Debug("*******prvLsTtsPlayerEvCb:%d",event);
  18. if(LSAPI_AUD_EVENT_FINISHED==event){//播放完毕
  19. outterInfo("+LSHTTS: END\r\n",14);
  20. wlog_warn("tts end1");
  21. talking.media=MEDIA_IDLE;
  22. }
  23. }
  24. static int tts_speak(void){
  25. int result=-1;
  26. LSAPI_AUD_SetPlayerEvCb(prvLsTtsPlayerEvCb);
  27. result=LSAPI_TTS_Play(tts.ttsData, tts.ttsLen, tts.ttsType);
  28. wlog_info("TTS:%d,%d", tts.ttsType,tts.ttsLen);
  29. return result;
  30. }
  31. void tts_stop(void){
  32. if(LSAPI_AUD_GetPlayerState()==LSAPI_PLAYER_PLAYING){//播放完毕
  33. outterInfo("+LSHTTS: END\r\n",14);
  34. wlog_warn("tts end2");
  35. talking.media=MEDIA_IDLE;
  36. }
  37. LSAPI_TTS_Stop();
  38. }
  39. PT_THREAD (ptTTSTask(pt_timer_t *ptPool, struct pt *pt)){
  40. static pt_timer_t ptTimer;
  41. bool status;
  42. PT_BEGIN(pt);
  43. while(1){
  44. if(tts.plyNow==1){
  45. if(MEDIA_POC==talking.media){
  46. //media is taken by poc, need to wait it stop
  47. talking.media=MEDIA_SHUT_POC;
  48. PTTimerStart(ptPool, &ptTimer,1000);//wait 200ms to shut poc
  49. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)||MEDIA_POC_PENDING==talking.media);
  50. talking.media=MEDIA_POC_PENDING;
  51. }
  52. status=LSAPI_TTS_IsPlaying();
  53. if(MEDIA_TTS==talking.media || true==status){
  54. //media is taken by tts, need to shut the current pro if it is on
  55. wlog_info("call tts stop");
  56. tts_stop();
  57. PTTimerStart(ptPool, &ptTimer,1000);//wait 1000ms to stop
  58. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)|| false==(status=LSAPI_TTS_IsPlaying()));
  59. }
  60. if(false==status) wlog_info("tts stop ok");
  61. else{
  62. wlog_info("tts stop failed, continue");
  63. continue;
  64. }
  65. //start new tts
  66. if(0==tts_speak()){
  67. wlog_info("tts play ok");
  68. talking.media=MEDIA_TTS;
  69. }else{
  70. msgToOutter("+LSHTTS: END\r\n");
  71. wlog_info("tts play failed");
  72. }
  73. tts.plyNow=0;
  74. }
  75. PTTimerStart(ptPool, &ptTimer,1);//can be fast
  76. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  77. }
  78. PT_END(pt);
  79. }
  80. char checkTtsData(unsigned char *data, int len){
  81. int i;
  82. unsigned char ch;
  83. for(i=0;i<len;i++){
  84. ch=data[i];
  85. if((ch>='0' && ch <= '9') ||
  86. (ch>='a' && ch <= 'f') ||
  87. (ch>='A' && ch <= 'F')) {}else return 1;
  88. }
  89. return 0;
  90. }
  91. void cmdTTSHandler(T_UINT8 *data, T_UINT16 len){
  92. //AT+LSHTTS=x,"vvvvvv"//没有回车换行了
  93. //x,"vvvvvv"
  94. T_UINT32 thisTime,i,j,k;
  95. T_UINT8 *ptr=data+10,type,tmp;
  96. T_BOOL ttsErr=FALSE;
  97. //如果是停止指令
  98. if(len==11 && ptr[0]=='0'){
  99. tts_stop();
  100. return;
  101. }
  102. //如果是播放指令
  103. if(len<15 || ptr[1] != ',' || (ptr[0] != '1' && ptr[0] != '2' && ptr[0] != '3') || ptr[2]!='"' || data[len-1] != '"'){
  104. wlog_warn("tts invalid1");
  105. goto TTS_ERROR;
  106. }
  107. type=ptr[0]-0x30;
  108. ptr += 3;//VVVVVVV"
  109. k=len-10-3-1;
  110. //检测数据合法性
  111. if(type==1 || type==2){//GBK/UNICODE的十六进制字符串
  112. if(type==1) tmp=k%2;
  113. else tmp=k%4;
  114. if(0!=checkTtsData(ptr,k) || tmp){
  115. wlog_warn("tts invalid2");
  116. goto TTS_ERROR;
  117. }
  118. if(0!=restoreDataFormatByHex(ptr,k)){
  119. wlog_warn("tts invalid3");
  120. goto TTS_ERROR;
  121. }
  122. k /= 2;
  123. type --;
  124. }else type=0;
  125. if(k>TTS_MAX_LEN){
  126. wlog_warn("tts len over");
  127. goto TTS_LENOV;
  128. }
  129. tts.ttsLen=k;
  130. tts.ttsType=type;
  131. memcpy(tts.ttsData,ptr,tts.ttsLen);
  132. tts.plyNow=1;
  133. return;
  134. TTS_ERROR:
  135. msgToOutter("+LSHTTS: ERROR\r\n");
  136. return;
  137. TTS_LENOV:
  138. msgToOutter("+LSHTTS: LENOV\r\n");
  139. }
  140. void ttsPlayNow(unsigned char encodetype, unsigned char *ttsData, int ttslen){
  141. if(0!=checkTtsData(ttsData,ttslen) || ttslen%4){
  142. wlog_warn("sms tts invalid1");
  143. return;
  144. }
  145. if(0!=restoreDataFormatByHex(ttsData,ttslen)){
  146. wlog_warn("sms tts invalid2");
  147. return;
  148. }
  149. tts.ttsLen=ttslen/2;
  150. tts.ttsType=encodetype;
  151. memcpy(tts.ttsData,ttsData,tts.ttsLen);
  152. tts.plyNow=1;
  153. }