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
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python





3 hours 11 min ago
6 hours 23 min ago
8 hours 38 min ago
9 hours 7 min ago
10 hours 5 min ago
11 hours 34 min ago
12 hours 42 min ago
13 hours 29 min ago
20 hours 4 min ago
1 day 1 hour ago