Speech I/O for Embedded Applications
The eSpeak TTS system originally was developed for the Acorn RISC Machine (can you say “full circle”?), comes with Ubuntu and is included in the version for Genesi, so we get a pass there. That may not be true for your embedded system, but the installation procedure for eSpeak is straightforward and is given in the README of the download on the eSpeak site (see Resources). Of course, you'll need to do the install in the context of Scratchbox, or whatever native build environment you're using for your embedded Linux.
To install PocketSphinx, you first need to install sphinxbase, which also is available on the PocketSphinx site. Both are tarballs, and the installation instructions are given in the READMEs. On systems like the Genesi, you can download and use the target to build the package. I did have to set LD_LIBRARY_PATH, so ld could find the libraries:
export LD_LIBRARY_PATH=/usr/local/lib
On smaller embedded systems, you might have to use a cross-compiler or Scratchbox.
We want to create a general-purpose spoken checklist program along the lines of the checklists discussed in Dr Gawande's book. As an example, let's use part of the World Health Organization's Surgical Safety Checklist.
Let's create a speech checklist program that reads a checklist and listens to a reply for each item. We'll just match the reply with some valid ones now and record it in a file, but this could be a springboard for your own innovative speech user-interface ideas.
PocketSphinx comes with an application called pocketsphinx_continuous that will do basic continuous speech recognition and print the results to stdout, along with a lot of information about how it performed the recognition. We'll create a small C program, atul.c, that uses the libespeak library to speak the checklist items. We will have piped pocketsphinx_continuous to atul, so atul can listen to the replies on its stdin.
The compilation command for atul will vary depending on your development environment. The invocation is:
pocketsphinx_continuous | ./atul SafeSurgery.ckl
Let's keep the application simple by reading checklist items and commands from a text file, whose name we'll pass as an argument to the program. Let's mark commands with a # at the beginning of a line. If the # is followed by a number, let's pause that number of seconds (up to 9). We will record each item and the replies as text to stdout.
The espeak library depends on two development packages you'll need to load into your target development environment. Both are readily available as rpm or deb packages: portaudio-devel and espeak-devel.
The Safe Surgery Checklist file is shown in Listing 1, and Listing 2 shows the source code for atul.c.
Listing 1. SafeSurgery.ckl
This is the Surgical Safety Checklist. # Before induction of anesthesia. # Has the patient confirmed his or her identity, site, procedure and consent? # yes | no Is the site marked? # yes | notapplicable Is the anesthesia machine and medication check complete? # yes Is the pulse oximeter on the patient and functioning? # yes Does the patient have a known allergy? # yes | no Does the patient have a difficult airway or aspiration risk? # no | yes Is there a risk of more than 500 milliliters of blood loss? # no | yes Thank you, that completes this portion of the checklist.
Listing 2. atul.c
/*
atul - a simple speech checklist for embedded systems
*/
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <speak_lib.h>
espeak_POSITION_TYPE position_type;
espeak_AUDIO_OUTPUT output;
char *path=NULL;
int BuffLen=500, Options=0;
void* user_data;
t_espeak_callback *SynthCallback;
espeak_PARAMETER Parm;
FILE *ckfp; /* Checklist file pointer */
char *ckBuf; /* Checklist item buffer */
char *mtchBuf; /* Checklist expected response buffer */
char *srBuf; /* Speech rec buffer */
char *reply; /* Trimmed reply */
int bsize=100; /* buffer length for all buffers */
int next; /* flag - true if should go to next prompt */
char Voice[] = {"default"};
unsigned int size, position=0, end_position=0,
flags=espeakCHARS_AUTO|espeakENDPAUSE, *unique_identifier;
void recordreply(){
/* read lines from stdin, which are piped in
* from pocketsphinx_continuous.
* Valid responses look like:
* <9 digits>: reply (7 or 8 digits)
* Returns a trimmed reply as char *reply
* no spaces in return
*/
int i, j;
while (!feof(stdin)) {
getline (&srBuf, &bsize, stdin);
if (srBuf[9]!= ':') continue;
j=0;
for (i=0; i<strlen(srBuf); i++) {
if (isdigit(srBuf[i])) continue;
if (srBuf[i]=='-') continue;
if (srBuf[i]==':') continue;
if (isspace(srBuf[i])) continue;
if (srBuf[i]=='(') continue;
if (srBuf[i]==')') continue;
reply[j++] = srBuf[i];
}
reply[j] = '\0';
break;
}
}
int checkreply(){
/* returns true if reply matches
* false if no match (try again)
*/
char *tryagain="Please answer ";
char *ans, *spBuf;
/* if template blank, just sleep 2 sec */
if (strlen(mtchBuf)==2) {
sleep(2);
return(1);
}
/* see if reply matches template */
recordreply();
printf("reply: '%s'\n", reply);
if (strstr(mtchBuf, reply)==NULL){
/* no match - tell user what we're looking for */
spBuf = (char *) malloc (bsize+1);
strcpy(spBuf, tryagain);
if (ans=strtok(mtchBuf+2, "|")){
strcat(spBuf, ans);
}
ans = strtok(NULL, "|");
while (ans!=NULL){
strcat(spBuf, " or ");
strcat(spBuf, ans);
ans = strtok(NULL, "|");
}
espeak_Synth( spBuf, size, position,
position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
free(spBuf);
return(0); /* repeat last prompt */
}else return(1); /* go to next prompt */
}
int main(int argc, char* argv[] )
{
printf("atul started.\n");
/* allocate needed buffers */
ckBuf = (char *) malloc (bsize+1);
srBuf = (char *) malloc (bsize+1);
mtchBuf = (char *) malloc (bsize+1);
reply = (char *) malloc (bsize+1);
/* open the checklist file */
if (argc < 2) {
printf("Usage: atul <checklist filename>\n",
argc);
return 0;
}
ckfp = fopen(argv[1], "r");
if (ckfp == NULL) {
printf("Unable to open checklist file: %s\n",
argv[1]);
return 0;
}
/* Initialize the TTS subsystem */
output = AUDIO_OUTPUT_PLAYBACK;
espeak_Initialize(output, BuffLen, path, Options);
espeak_SetVoiceByName(Voice);
/* Initialize speech recognition
* piped in from pocketsphinx_continuous */
while (!feof(stdin)) {
getline (&srBuf, &bsize, stdin);
if (strncmp(srBuf, "READY...", 8)==0) break;
}
/* Go through the checklist */
next = 1; /* advance to next prompt */
while (!feof(ckfp)) {
if (next) {
getline (&ckBuf, &bsize, ckfp);
getline (&mtchBuf, &bsize, ckfp);
}
size = strlen(ckBuf)+1;
espeak_Synth( ckBuf, size, position,
position_type, end_position, flags,
unique_identifier, user_data );
espeak_Synchronize( );
fputs(ckBuf, stdout);
next = checkreply();
}
fclose(ckfp);
free(ckBuf);
free(srBuf);
free(mtchBuf);
return 0;
}
The code isn't very complex, although in retrospect, it might have been clearer in Python or some other language that is better than C at string manipulation. The main routine initializes the TTS subsystem and makes sure that phoenix_continuous is ready to catch replies and forward them to us. It then just cycles through the checklist file, reading prompts and comparing the replies with the acceptable ones it finds in the checklist file. If it doesn't find a match, it tells the user what it's looking for and asks again. One thing to note, the string trimming routine in recordreply() throws out all spaces, so if your checklist is looking for a multiword response, be sure to concatenate the words in the list (like “notapplicable” in our checklist above). Everything of note is recorded in stdout, which you might want to redirect to a log file.
Rick Rogers has been a professional embedded developer for more than 30 years. Now specializing in mobile application software, when Rick isn't writing software for a living, he's writing books and magazine articles like this one.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Validate an E-Mail Address with PHP, the Right Way
- Technical Support Rep
- Senior Perl Developer
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Introduction to MapReduce with Hadoop on Linux





1 hour 3 min ago
3 hours 57 min ago
4 hours 22 min ago
6 hours 51 min ago
7 hours 24 min ago
7 hours 25 min ago
7 hours 26 min ago
7 hours 28 min ago
7 hours 29 min ago
7 hours 31 min ago