awaken_offline_sample.c 3.9 KB

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