tts.c 4.0 KB

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