如何提高cmuSphinx的准确性?

时间:2012-03-23 15:23:04

标签: c speech-recognition speech-to-text alsa

我想使用pocketShpinx来做一些语音到文字。我安装了sphinxbase和pocketSphinx。并下载声学模型/语言模型/字典。然后我测试example code,如下所示:

#include <pocketsphinx/pocketsphinx.h>
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"

int main(int argc, char *argv[])
{
  ps_decoder_t  *ps;
  cmd_ln_t      *config;
  FILE *fh;
  int rv;
  char const *hyp, *uttid;
  int32 score;

  config = cmd_ln_init(NULL, ps_args(), TRUE,
                       "-hmm", "/home/madper/speech/hub4opensrc.cd_continuous_8gau",
                       "-lm",  "/home/madper/speech/language_model.arpaformat.DMP",
                       "-dict", "/home/madper/speech/cmudict/cmudict/sphinxdict/cmudict_SPHINX_40",
                       NULL);
  if (config == NULL)
    {
      DBG (("cmd_ln_init() failed.\n"));
      exit(1);
    }
  if ((ps = ps_init (config)) == NULL) /* init decoder */
    {
      DBG (("ps_init() failed.\n"));
      exit(1 );
    }
  if ((fh = fopen("test.raw", "rb")) == NULL) /* open raw file */
    {
      DBG (("fopen() failed.\n"));
      exit (1);
    }
  if ((rv = ps_decode_raw (ps, fh, "test", -1)) < 0 )
    {
      DBG (("ps_decode_raw() error!\n"));
      exit (1);
    }
  if ((hyp = ps_get_hyp(ps, &score, &uttid)) == NULL)
    {
      DBG (("ps_get_hyp() failed!\n"));
      exit (1);
    }
  printf ("Recognized: %s\n", hyp); /* this is what you say */

  fclose(fh);
  ps_free(ps);
  return 0;
}
如果定义了DEBUG,

DBG只是一个打印错误消息的宏。


然后我写了一些代码来记录mic使用alsa。如下:

#define ALSA_PCM_NEW_HW_PARAMS_API

#include <alsa/asoundlib.h>

int main() {
  long loops;
  int rc;
  int size;
  snd_pcm_t *handle;
  snd_pcm_hw_params_t *params;
  unsigned int val;
  int dir;
  snd_pcm_uframes_t frames;
  char *buffer;

  /* Open PCM device for recording (capture). */
  rc = snd_pcm_open(&handle, "default",
                    SND_PCM_STREAM_CAPTURE, 0);
  if (rc < 0) {
    fprintf(stderr,
            "unable to open pcm device: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Allocate a hardware parameters object. */
  snd_pcm_hw_params_alloca(&params);

  /* Fill it in with default values. */
  snd_pcm_hw_params_any(handle, params);

  /* Set the desired hardware parameters. */

  /* Interleaved mode */
  snd_pcm_hw_params_set_access(handle, params,
                      SND_PCM_ACCESS_RW_INTERLEAVED);

  /* Signed 16-bit little-endian format */
  snd_pcm_hw_params_set_format(handle, params,
                              SND_PCM_FORMAT_S16_LE);

  /* Two channels (stereo) */
  snd_pcm_hw_params_set_channels(handle, params, 1);

  /* 44100 bits/second sampling rate (CD quality) */
  val = 16000;
  snd_pcm_hw_params_set_rate_near(handle, params,
                                  &val, &dir);

  /* Set period size to 32 frames. */
  frames = 16;
  snd_pcm_hw_params_set_period_size_near(handle,
                              params, &frames, &dir);

  /* Write the parameters to the driver */
  rc = snd_pcm_hw_params(handle, params);
  if (rc < 0) {
    fprintf(stderr,
            "unable to set hw parameters: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Use a buffer large enough to hold one period */
  snd_pcm_hw_params_get_period_size(params,
                                      &frames, &dir);
  size = frames * 2; /* 2 bytes/sample, 2 channels */
  buffer = (char *) malloc(size);

  /* We want to loop for 5 seconds */
  snd_pcm_hw_params_get_period_time(params,
                                         &val, &dir);
  loops = 2000000 / val;

  while (loops > 0) {
    loops--;
    rc = snd_pcm_readi(handle, buffer, frames);
    if (rc == -EPIPE) {
      /* EPIPE means overrun */
      fprintf(stderr, "overrun occurred\n");
      snd_pcm_prepare(handle);
    } else if (rc < 0) {
      fprintf(stderr,
              "error from read: %s\n",
              snd_strerror(rc));
    } else if (rc != (int)frames) {
      fprintf(stderr, "short read, read %d frames\n", rc);
    }
    rc = write(1, buffer, size);
    if (rc != size)
      fprintf(stderr,
              "short write: wrote %d bytes\n", rc);
  }

  snd_pcm_drain(handle);
  snd_pcm_close(handle);
  free(buffer);

  return 0;
}

所以,我录制了一个raw文件。然后对该文件进行语音测试。但准确性非常差。就像hellogo home会给我hotelMHM MHM等等。那么这些代码有什么问题呢? 我已经阅读了faqs,我应该使用声学模型适应来提高准确度吗?

PS。我将立体声改为单声道。声音很奇怪。我无法理解我说的话。那么,它有什么问题?这是原始文件test.raw

1 个答案:

答案 0 :(得分:2)

如果您查看http://cmusphinx.sourceforge.net/wiki/faq中的第一个Q和A,您会发现该库假设是单声道数据。

以立体声录制。