如何找到espeak是否结束了演讲?

时间:2018-04-27 12:04:18

标签: c++ pocketsphinx espeak

我想在我的程序中使用espeak。我想知道espeak何时停止发言。有没有要检查的标志或功能?

让我们考虑这是我的计划:

Line 1
espeak
Line 2

当我执行这段代码时,espeak开始说“你好,这是espeak”,但在它结束之前,代码的第2行被执行了,我不喜欢这个。我正在寻找一种pause程序的方法,直到espeak结束发言!

编辑: 这是我的完整代码,我使用pocketsphinx来识别用户说的内容,然后将其保存在char* hyp内,并通过speech函数将其传递给espeak。

static ps_decoder_t *ps;
static cmd_ln_t *config;
static FILE *rawfd;

espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int Buflength = 1000, Options=0;
void* user_data;
char Voice[] = {"English"};
char text2[30] = {"this is a english test"};
unsigned int Size,position=0, end_position=0, flags=espeakCHARS_AUTO, *unique_identifier;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
//char* text;

static void initFuncs()
{

    output = AUDIO_OUTPUT_PLAYBACK;
    espeak_Initialize(output, Buflength, path, Options ); 
    espeak_SetVoiceByName(Voice);
    const char *langNativeString = "en";
    espeak_VOICE voice;
    memset(&voice, 0, sizeof(espeak_VOICE));
        voice.languages = langNativeString;
        voice.name = "US";
        voice.variant = 2;
        voice.gender = 1;
        espeak_SetVoiceByProperties(&voice);

}

static void sleep_msec(int32 ms)
{

    struct timeval tmo;

    tmo.tv_sec = 0;
    tmo.tv_usec = ms * 1000;

    select(0, NULL, NULL, NULL, &tmo);

}

static void speech(char* hyp)
{

    Size = strlen(hyp)+1;
    espeak_Synth( hyp, Size, position, position_type, end_position, flags,unique_identifier, user_data );
    espeak_Synchronize( );

}

static void recognize_from_microphone()
{
    ad_rec_t *ad;
    int16 adbuf[2048];
    uint8 utt_started, in_speech;
    int32 k;
    char  *hyp;

    if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),(int) cmd_ln_float32_r(config,"-samprate"))) == NULL)
        E_FATAL("Failed to open audio device\n");
    if (ad_start_rec(ad) < 0)
        E_FATAL("Failed to start recording\n");

    if (ps_start_utt(ps) < 0)
        E_FATAL("Failed to start utterance\n");

    utt_started = FALSE;
    E_INFO("Ready....\n");

    for (;;) {

        ad_start_rec(ad);

        if ((k = ad_read(ad, adbuf, 2048)) < 0)
            E_FATAL("Failed to read audio\n");
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);
        in_speech = ps_get_in_speech(ps);
        if (in_speech && !utt_started) {
            utt_started = TRUE;
            E_INFO("Listening...\n");
        }
        if (!in_speech && utt_started) {

            ps_end_utt(ps);
            hyp = (char*)ps_get_hyp(ps, NULL );
            if (hyp != NULL) {

                ad_stop_rec(ad);
                speech(hyp);
                printf("%s\n", hyp); 
                fflush(stdout);
            }

            if (ps_start_utt(ps) < 0)
                E_FATAL("Failed to start utterance\n");
            utt_started = FALSE;
            E_INFO("Ready....\n");

        }

    }//for loop
    ad_close(ad);
}

int main(int argc, char *argv[])
{
    initFuncs();

    config = cmd_ln_init(NULL, ps_args(), TRUE,
                 "-hmm", MODELDIR "/en-us/en-us",
                     "-lm", MODELDIR "/en-us/en-us.lm.bin",
                     "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                     NULL);
    ps = ps_init(config);
        recognize_from_microphone();

    ps_free(ps);
    cmd_ln_free_r(config);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

我修改了代码的espeak部分。在此代码中,espeakLine 2开始之前完成。此外,还实现了回调功能。您正在按名称和属性设置语音。也许这是一个问题。您正在使用c样式字符串而不使用std :: string。也许你正在计算错误的字符串长度。我不知道代码中的问题在哪里,但以下代码修复了它:

#include <string>
#include <iostream>
#include <espeak/speak_lib.h>

espeak_POSITION_TYPE position_type(POS_CHARACTER);
espeak_AUDIO_OUTPUT output(AUDIO_OUTPUT_PLAYBACK);
void* user_data;
std::string voice("English");
std::string text("this is a english test");
unsigned int Size(0);
unsigned int position(0);
unsigned int end_position(0);
unsigned int flags(espeakCHARS_AUTO);
unsigned int* unique_identifier;

static void initFuncs() {
  espeak_Initialize(output, 0, 0, 0);
  espeak_SetVoiceByName(voice.c_str());
}

int SynthCallback(short *wav, int numsamples, espeak_EVENT *events) {
  std::cout << "Callback: ";
  for (unsigned int i(0); events[i].type != espeakEVENT_LIST_TERMINATED; i++) {
    if (i != 0) {
      std::cout << ", ";
    }
    switch (events[i].type) {
      case espeakEVENT_LIST_TERMINATED:
        std::cout << "espeakEVENT_LIST_TERMINATED";
        break;
      case espeakEVENT_WORD:
        std::cout << "espeakEVENT_WORD";
        break;
      case espeakEVENT_SENTENCE:
        std::cout << "espeakEVENT_SENTENCE";
        break;
      case espeakEVENT_MARK:
        std::cout << "espeakEVENT_MARK";
        break;
      case espeakEVENT_PLAY:
        std::cout << "espeakEVENT_PLAY";
        break;
      case espeakEVENT_END:
        std::cout << "espeakEVENT_END";
        break;
      case espeakEVENT_MSG_TERMINATED:
        std::cout << "espeakEVENT_MSG_TERMINATED";
        break;
      case espeakEVENT_PHONEME:
        std::cout << "espeakEVENT_PHONEME";
        break;
      case espeakEVENT_SAMPLERATE:
        std::cout << "espeakEVENT_SAMPLERATE";
        break;
      default:
        break;
    }
  }
  std::cout << std::endl;
  return 0;
}

static void speech(std::string hyp) {
    Size = hyp.length();
    espeak_SetSynthCallback(SynthCallback);
    espeak_Synth(hyp.c_str(), Size, position, position_type, end_position, flags,unique_identifier, user_data );
    espeak_Synchronize( );
}

int main() {
  initFuncs();
  std::cout << "Start" << std::endl;
  speech(text.c_str());
  std::cout << "End" << std::endl;
  return 0;
}

输出是

Start
Callback: espeakEVENT_SENTENCE
Callback: espeakEVENT_WORD
Callback: espeakEVENT_WORD
Callback: espeakEVENT_WORD
Callback: espeakEVENT_WORD
Callback: espeakEVENT_WORD
Callback: espeakEVENT_END
Callback: espeakEVENT_MSG_TERMINATED
End

控制台输出的时间适合音频输出。当你使用C ++时,你应该使用它的工具和功能,如字符串,cout而不是printf和智能指针,以避免这样的问题。