awaken.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include "msp_cmn.h"
  6. #include "msp_errors.h"
  7. #include "linuxrec.h"
  8. #include "formats.h"
  9. #include "qivw.h"
  10. #include "asr.h"
  11. #define SAMPLE_RATE_16K (16000)
  12. #define DEFAULT_FORMAT \
  13. {\
  14. WAVE_FORMAT_PCM,\
  15. 1, \
  16. 16000, \
  17. 32000, \
  18. 2, \
  19. 16, \
  20. sizeof(WAVEFORMATEX)\
  21. }
  22. struct recorder *recorder = NULL;
  23. static int flag = 1;
  24. void sleep_ms(int ms)
  25. {
  26. usleep(ms * 1000);
  27. }
  28. /* the record call back */
  29. void record_data_cb(char *data, unsigned long len, void *user_para)
  30. {
  31. int errcode = 0;
  32. const char *session_id = (const char *)user_para;
  33. if(len == 0 || data == NULL)
  34. return;
  35. errcode = QIVWAudioWrite(session_id, (const void *)data, len, MSP_AUDIO_SAMPLE_CONTINUE);
  36. if (MSP_SUCCESS != errcode)
  37. {
  38. printf("QIVWAudioWrite failed! error code:%d\n",errcode);
  39. int ret = stop_record(recorder);
  40. if (ret != 0)
  41. {
  42. printf("Stop failed! \n");
  43. }
  44. QIVWAudioWrite(session_id, NULL, 0, MSP_AUDIO_SAMPLE_LAST);
  45. }
  46. }
  47. int cb_ivw_msg_proc( const char *sessionID, int msg, int param1, int param2, const void *info, void *userData )
  48. {
  49. if(MSP_IVW_MSG_WAKEUP == msg) //唤醒成功消息
  50. {
  51. //printf("\n\nMSP_IVW_MSG_WAKEUP result = %s\n\n", (char*)info);
  52. system("play -q --multi-threaded ./msc/ding.wav");
  53. flag = 0;
  54. }
  55. else if(MSP_IVW_MSG_ERROR == msg) //唤醒出错消息
  56. {
  57. printf("\nMSP_IVW_MSG_ERROR errCode = %d\n\n", param1);
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. void run_ivw(const char* session_begin_params)
  63. {
  64. const char *session_id = NULL;
  65. int err_code = MSP_SUCCESS;
  66. char sse_hints[128];
  67. flag = 1;
  68. WAVEFORMATEX wavfmt = DEFAULT_FORMAT;
  69. wavfmt.nSamplesPerSec = SAMPLE_RATE_16K;
  70. wavfmt.nAvgBytesPerSec = wavfmt.nBlockAlign * wavfmt.nSamplesPerSec;
  71. //start QIVW
  72. session_id=QIVWSessionBegin(NULL, session_begin_params, &err_code);
  73. if (err_code != MSP_SUCCESS)
  74. {
  75. printf("QIVWSessionBegin failed! error code:%d\n",err_code);
  76. goto exit;
  77. }
  78. err_code = QIVWRegisterNotify(session_id, cb_ivw_msg_proc, NULL);
  79. if (err_code != MSP_SUCCESS)
  80. {
  81. snprintf(sse_hints, sizeof(sse_hints), "QIVWRegisterNotify errorCode=%d", err_code);
  82. printf("QIVWRegisterNotify failed! error code:%d\n",err_code);
  83. goto exit;
  84. }
  85. //1.create recorder
  86. err_code = create_recorder(&recorder, record_data_cb, (void*)session_id);
  87. if (recorder == NULL || err_code != 0)
  88. {
  89. printf("create recorder failed: %d\n", err_code);
  90. err_code = MSP_ERROR_FAIL;
  91. goto exit;
  92. }
  93. //2.open_recorder
  94. err_code = open_recorder(recorder, get_default_input_dev(), &wavfmt);
  95. if (err_code != 0)
  96. {
  97. printf("recorder open failed: %d\n", err_code);
  98. err_code = MSP_ERROR_FAIL;
  99. goto exit;
  100. }
  101. //3.start record
  102. err_code = start_record(recorder);
  103. if (err_code != 0)
  104. {
  105. printf("start record failed: %d\n", err_code);
  106. err_code = MSP_ERROR_FAIL;
  107. goto exit;
  108. }
  109. while(flag)
  110. {
  111. sleep_ms(1000); //模拟人说话时间间隙,10帧的音频时长为200ms
  112. printf("Listening wakeup trigger... \n");
  113. }
  114. snprintf(sse_hints, sizeof(sse_hints), "success");
  115. exit:
  116. if (recorder)
  117. {
  118. if(!is_record_stopped(recorder))
  119. stop_record(recorder);
  120. close_recorder(recorder);
  121. destroy_recorder(recorder);
  122. recorder = NULL;
  123. }
  124. if (NULL != session_id)
  125. {
  126. QIVWSessionEnd(session_id, sse_hints);
  127. }
  128. }
  129. int waitAwaken()
  130. {
  131. int ret = MSP_SUCCESS;
  132. const char *lgi_param = "appid = 5d5b9efd, work_dir = .";
  133. const char *ssb_param = "ivw_threshold=0:2000, sst=wakeup, ivw_res_path=fo|res/ivw/wakeupresource.jet";
  134. ret = MSPLogin(NULL, NULL, lgi_param);
  135. if(MSP_SUCCESS != ret)
  136. {
  137. printf("Awaken MSPLogin failed, error code: %d.\n", ret);
  138. MSPLogout(); //登录失败,退出登录
  139. return -1;
  140. }
  141. run_ivw(ssb_param);
  142. MSPLogout(); //退出登录
  143. //start ASR
  144. startASR();
  145. return 0;
  146. }