Skip to content

Instantly share code, notes, and snippets.

@cash
Last active September 25, 2024 03:15
Show Gist options
  • Select an option

  • Save cash/3fbc6ba9beab38678d8ee81b1d3ab5d5 to your computer and use it in GitHub Desktop.

Select an option

Save cash/3fbc6ba9beab38678d8ee81b1d3ab5d5 to your computer and use it in GitHub Desktop.
Instructions here: http://cmusphinx.sourceforge.net/wiki/tutorialpocketsphinx
sudo apt-get install python-dev bison autoconf libtool-bin swig libpulse-dev
@mmartin-glitch
Copy link

#include <stdio.h>
#include <stdlib.h>
#include <pocketsphinx.h>
#include <sphinxbase/ad.h>
#include <sphinxbase/err.h>

void recognize_from_microphone() {
ps_decoder_t *ps;
cmd_ln_t *config;
ad_rec_t *ad;
int16 adbuf[4096];
uint8 utt_started, in_speech;
int32 k;
char const *hyp;

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);

ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"), (int)cmd_ln_float32_r(config, "-samprate"));
ad_start_rec(ad);
ps_start_utt(ps);

utt_started = FALSE;
printf("Listening...\n");

while (1) {
    if ((k = ad_read(ad, adbuf, 4096)) < 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;
        printf("Listening...\n");
    }
    if (!in_speech && utt_started) {
        ps_end_utt(ps);
        hyp = ps_get_hyp(ps, NULL);
        if (hyp != NULL) {
            printf("Recognized: %s\n", hyp);
            // Aquí puedes agregar la lógica para controlar el juego de rompecabezas
        }
        ps_start_utt(ps);
        utt_started = FALSE;
        printf("Listening...\n");
    }
}

ad_close(ad);
ps_free(ps);
cmd_ln_free_r(config);

}

int main(int argc, char *argv[]) {
recognize_from_microphone();
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment