Speech-Recognizer-ViaVoice

 view release on metacpan or  search on metacpan

ViaVoice.xs  view on Meta::CPAN

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <smapi.h>


#define VOCWORD_MAXLEN 1024
int msgsock = -1;
int bListening = 0;
SV* recognizedWord = &PL_sv_undef;
IV recognitionScore = -1;

/* forward decls */
int NotifierCallback(int so, int (*rcv)(), void* data, void* cinfo);


/*------------------------------------*/
/* setup API */

/*	return value of 0 indicates success
 */
int connectEngine()
{
	SM_MSG msg;
	SmArg args[3];
	int rc;
	char buf[1024];

	setenv("SPCH_BIN", VVDIR "/bin", 1);
	sprintf(buf, "%s/viavoice/temp", getenv("HOME"));
	setenv("SPCH_TRN", buf, 1);
	setenv("SPCH_RO", VVDIR "/vocabs", 1);
	setenv("SPCH_LOC", "vl1", 1);
	setenv("SPCH_PATH", VVDIR "/vocabs/langs/" LOCALE "/pools", 1);
	sprintf(buf, "%s/viavoice", getenv("HOME"));
	setenv("SPCH_RW", buf, 1);
	sprintf(buf, "%s/viavoice/temp", getenv("HOME"));
	setenv("SPCH_RUN", buf, 1);
	sprintf(buf, "%s/viavoice/temp/tmp", getenv("HOME"));
	setenv("SPCH_DIR", buf, 1);

	rc = SmOpen(0, NULL);
	if (rc == SM_RC_OK) {
		SmSetArg(args[0], SmNrecognize, 1);
		SmSetArg(args[1], SmNexternalNotifier, NotifierCallback);
		rc = SmConnect(2, args, &msg);
	}

	return rc;
}

/*	return value of 0 indicates success
 */
int defineVocab(char* vocabName, SV* sv_words)
{
	SM_MSG msg;
	SM_VOCWORD** words;
	AV* av_words;
	int i, rc, alen;

	if (!SvROK(sv_words)) {
		if (SvTYPE(SvRV(sv_words)) != SVt_PVAV) {
			return -1;
		}
	}
	av_words = (AV*) SvRV(sv_words);
	alen = av_len(av_words) + 1;
	words = (SM_VOCWORD**) malloc(sizeof(SM_VOCWORD*) * (av_len(av_words) + 1));

	for (i = 0; i < alen; i++) {
		SV** svp = av_fetch(av_words, i, 0);
		if (NULL != svp) {
			char* word = sv_pv(*svp);
			if (NULL != word) {
				words[i] = (SM_VOCWORD*) malloc(sizeof(SM_VOCWORD));
				words[i]->flags = 0;
				words[i]->spelling_size = strnlen(word, VOCWORD_MAXLEN);
				words[i]->spelling = word;
			}
		}
	}
	rc = SmDefineVocab(vocabName, alen, words, &msg);
	if (rc == SM_RC_OK) {
		rc = SmEnableVocab(vocabName, &msg);
	}

	for (i = 0; i < alen; i++) {
		free(words[i]);
	}
	free(words);

	return rc;
}


/*	return value of 0 indicates success
 */
int startListening()
{
	SM_MSG msg;



( run in 0.763 second using v1.01-cache-2.11-cpan-6aa56a78535 )