Avaya Logo

Previous Topic

Next Topic

Book Contents

Book Index

Sample IRAPI script with speech recognition

#include <stdio.h>
#include
"irapi.h"
#include
<unistd.h>
#include "shmemtab.h"

#include "s51.h"

#include "lspsipGlobals.h"

#include
"busDiag.h"
#include
"mtc_cmd.h"
#include "lspsmsg.h"

#include "lsps_asr.h"

#include "irlsps.h"

#include "recog_dip.h"


#define T_LVL
IRD_TRACE_LEVEL_16
#define T_AREA
IRD_TRACE_AREA_16

void myError(char
* string);
void
myQTrace(char * string);
int
iri_tdmlisten(int chan);
int
iri_tdmtalk(int chan);
void
speak_return(char * text, int chan, ir_key_t qkey);
char *
basename(char * str);

union recv_data
{
struct
recv_script script;
struct
VARLETTER(MAX_LEN) letrptr[1];
struct recv_dcdip
dcdip;
};

union
send_data {
struct send_tsm
tsm;
struct
send_dcdip dcdip;
};

struct
recv_msg {
struct mbhdr mhdr;
union
recv_data data;
};

struct
send_msg {
struct mbhdr
mhdr;
union send_data
data;
};

/*
* The following structure
helps in sending a message back to TSM
* only after all secondary
choices have been obtained and processed.
*/

struct wait_struct
{
struct recv_msg recv_mesg;
int
choice_wait_flag[MAX_NUM_CHOICES];
struct choice
choices[MAX_NUM_CHOICES];
int num_of_frames; /* number of
frames used in recognition */
int time_stamp;
};

/*
*
Structure used for servicing the requests from dcdip
*/

int
CHAN;
channel_id cid;

struct
recv_script recv_script;
struct recv_msg recv_msg;
struct
send_msg * send_msg;

main(int argc, char *
argv[])
{
ir_key_t myKey, qkey,
recog_qkey;
ir_event_t ev;
union LSPSIPmsg *
dataptr;
struct mbhdr *
hdptr;
int
timeslot, chan, rc, value;
struct LSPSasr
asr;
struct
LSPSstarttimer stimer;
struct LSPSplay
play;
struct
asrStat * stat;
FILE *
fp;
int PLAY =
0;
char *
baseptr;
char
buf[21];

baseptr =
basename(argv[0]);
if((myKey = irRegister(baseptr)) ==
IRR_FAIL) {
myError("irRegister
failed. EXITING");
exit(-
1);
}
irTrace(CHAN, T_LVL,
T_AREA, "%s has registered (%d)", baseptr, myKey);

if((qkey =
irGetQKey(LSPSIP_PROC)) == IRR_FAIL) {
myError("irGetQKey for LSPSIP failed.
EXITING");
exit(-
1);
}
irTrace(CHAN, T_LVL, T_AREA,
"Qkey for LSPSIP is %d", qkey);

while(1) {
if(irWait() ==
IRR_FAIL)
myError("IrWait
failed");

rc = irCheck(&ev);
irTrace(CHAN,
T_LVL, T_AREA,
"irCheck returns %d,
event_id = %d", rc, ev.event_id);
if(rc == IRR_FAIL)
myError("irCheck
failed.");

switch(ev.event_id) {
case
IRE_EXEC:
myQTrace("In
IRE_EXEC");
CHAN = ev.event_mod1;
if(irInit(CHAN,
&cid, IRD_IMMEDIATE, 0) != IRR_OK) {
myError("irInit
failed.");
exit(1);
}

if(irAnswer(cid) <
0) {
myError("irInit
failed.");
exit(1);
}
break;

case
IRE_CHAN_GRANT:
myQTrace("Got
IRE_CHAN_GRANT");
break;
case
IRE_CHAN_DENY:
myQTrace("Got
IRE_CHAN_DENY");
break;
case
IRE_DEINIT_DONE:
myQTrace("Got
IRE_DEINIT_DONE");
exit(0);
case
IRE_ANSWER_DONE:
myQTrace("Got
IRE_ANSWER_DONE");
/*
* Play a phrase: "Begin
Testing"
*/
irFPlay(cid,
0,
"/home/sdoliver/lsps/speech/begn_tst.mu8");
irEnd(cid, 0, 0);
break;
case
IRE_PLAY_DONE:
myQTrace("Got
IRE_PLAY_DONE");
if(PLAY != 0)
{
break;
}
PLAY++;
/*
* setup for ASR and talk
to lspsip
*/
irGetParam(cid,
IRP_RECOG_PRETIME, &value);
irTrace(CHAN, T_LVL,
T_AREA,
"IRP_RECOG_PRETIME = %d\n",
value);

irGetParam(cid, IRP_RECOG_INTERTIME,
&value);
irTrace(CHAN, T_LVL,
T_AREA,
"IRP_RECOG_INTERTIME = %d\n",
value);

if(irSetParam(cid,
IRP_RECOG_GAPTIME, 100) == IRR_FAIL)
myError("irSetParam(IRP_RECOG_GAPTIME)
failed\n");

if(irSetParam(cid,
IRP_RECOG_EFLOOR, 0) == IRR_FAIL)
myError("irSetParam
of IRP_RECOG_TYPE failed\n");

if(irSetParam(cid, IRP_RECOG_TYPE,
IRD_WHOLE_WORD)
==
IRR_FAIL)
myError("irSetParam of IRP_RECOG_TYPE
failed\n");

if(irSetParamStr(cid,
IRP_RECOG_GRAMNAME, "IW_YESNO")
==
IRR_FAIL)
myError("irSetParam of IRP_RECOG_GRAMNAME
failed\n");

if(irStartRecog(cid, 0) ==
IRR_FAIL)
myError("irStartRecog
failed\n");

value =
irCheckRecog(cid);
irTrace(CHAN, T_LVL,
T_AREA,
"irCheckRecog returns = %d\n",
value);

if(irStartRecogTimer(cid)
== IRR_FAIL) {
myError("irStartRecogTimer
failed\n");
}
break;
case
IRE_INPUT_DONE:
myQTrace("Got
IRE_INPUT_DONE");
if((rc = irGetInput(cid, buf, 20)) ==
IRR_FAIL) {
myError("irGetInput failed\n");
}
else
{
irTrace(CHAN, T_LVL,
T_AREA,
"DATA: %s\n", buf);
}
break;
case
IRE_DISCONNECT:
case
IRE_DISCONNECT_DONE:
myQTrace("Got
IRE_DISCONNECT[_DONE]");
if(irSetEvent(cid,
IRE_DEINIT_DONE, IRF_NOTIFY)
==
IRR_FAIL)
myError("irSetEvent(IRE_DEINIT_DONE)
failed.");
if(irDeinit(cid) <
0)
myError("irDeinit failed.");
break;
case
IRE_EXTERNAL:
myQTrace("Got
IRE_EXTERNAL");
hdptr = (struct mbhdr *)
ev.event_text;
switch(hdptr->mcont)
{
case 100:
send_msg =
(struct send_msg *) ev.event_text;
irTrace(CHAN, T_LVL,
T_AREA,"best choice: %s\n",
send_msg-
>data.tsm.best_choice);
break;
case LSPS_ASR:
case
LSPS_RECOGCOMP:
stat = (struct asrStat *)
ev.event_text;
irTrace(CHAN, T_LVL, T_AREA,
"ASR
returns: %s\n",stat->results[0].text);
irTrace(CHAN, T_LVL,
T_AREA,
"timeslot: %d\n",stat-
>timeSlot);
irTrace(CHAN, T_LVL,
T_AREA,
"asrResource: %d\n",stat-
>asrResource);
speak_return(stat-
>results[0].text,
stat->hd.irChan,
qkey);

strcpy(recv_script.sr_type,
"CD_4");
recv_script.sr_sp_brdidx = stat-
>asrResource;
recv_script.sr_min =
4;
recv_script.sr_max =
4;
strcpy(recv_script.sr_input,
stat-
>results[0].text);
recv_script.sr_recogz =
2;
strcpy(recv_script.sr_expect,
stat-
>results[0].text);
recv_msg.mhdr.mcont =
100;
recv_msg.mhdr.mchan = CHAN;
recv_msg.mhdr.irChan =
CHAN;
memcpy(&(recv_msg.data),
&recv_script,
sizeof(struct
recv_script));
if((recog_qkey = irGetQKey(SP_RECOG_DIP))
==
IRR_FAIL)
{
myError("irGetQKey(recog_dip)
failed");
}
rc = irPostEventQ(recog_qkey,
&recv_msg,
sizeof(struct
recv_msg));
if(rc == IRR_FAIL)
irTrace(CHAN, T_LVL,
T_AREA,
"irPostEventQ
failed\n");
break;
default:
irTrace(CHAN, T_LVL,
T_AREA,
"unexpected mcont =
%d\n",hdptr->mcont);
break;
}
break;
default:
irTrace(CHA
N, T_LVL, T_AREA, "Unexpected event %d\n",
ev.event_id);
break;

}

}
>}

/****************************************
**********************
* NAME: myError(string)
*
*
FUNCTION:
* An error has occured.
Create a trace message using the string
*
send and the error number retrieved from the extern int irError.

***************************************************************
*/
void myError(string)
char *
string;
{
irTrace(CHAN, T_LVL,
T_AREA, "%s: %s (%s:%d)",
string, irErrorStr(irError),
irErrorName(irError), irError);
return;

}
void myQTrace(string)
char
* string;
{
irTrace(CHAN, T_LVL,
T_AREA, "%s", string);
return;
}
void
speak_return(char * text,
int chan, ir_key_t qkey)
{
char
string[80];
int i;

for(i = 0; i
< (int) strlen(text); i++) {
/*
Setup the files to play */
sprintf(string, "%s%c%s",
"/home/sdoliver/lsps/speech/alph_",
text[i],
".mu8");
irFPlay(cid, 0,
string);
irTrace(CHAN, T_LVL, T_AREA, "playing
%s\n", string);
}
irEnd(cid,
0, 0);
}

© 2004 Avaya Inc. All Rights Reserved.