void cTumkwsaSink::ATKresultThread()
{
  Boolean term;
  smileMutexLock(terminatedMtx);
  term = terminated;
  smileMutexUnlock(terminatedMtx);

  while (term==FALSE) {
    APacket p = ansChan->GetPacket();
    if (p.GetKind() == StringPacket){
      AStringData * sd = (AStringData *)p.GetData();
      if (sd->data.find("TERMINATED") != string::npos) {
        smileMutexLock(terminatedMtx);
        terminated = TRUE;
        smileMutexUnlock(terminatedMtx);
      }
    }

    // TODO: string output:  (call printResult function...)
    processResult(0, 0, 0.0, &p);

    smileMutexLock(terminatedMtx);
    term = terminated;
    smileMutexUnlock(terminatedMtx);
  }
}
Example #2
0
std::vector<std::string> recognize() {

	ABuffer auChan("auChan");		// waveform data 
	ABuffer feChan("feChan");		// features 
	ABuffer ansChan("ansChan");		// recognition output 

	ASource ain("AIn",&auChan);			// auChan connects source to coder 
	ACode acode("ACode",&auChan,&feChan); //feChan connects coder to reco 
	
	AHmms hset("HmmSet");	// create HMM set called HmmSet 
	ADict dict("ADict");	// create a dictionary called ADict 
	AGram gram("AGram");	// create a grammar called AGram 
	ARMan rman;				// create a resource manager 
	rman.StoreHMMs(&hset);	// store the resources in rman 
	rman.StoreDict(&dict); 
	rman.StoreGram(&gram); 
	
	ResourceGroup *group = rman.NewGroup("directions"); 
	group->AddHMMs(&hset); 
	group->AddDict(&dict); 
	group->AddGram(&gram); 
	ARec arec("ARec",&feChan,&ansChan,&rman); // create recogniser 
	
	ain.Start(); acode.Start(); arec.Start();
	
	arec.SendMessage("start()");
	ain.SendMessage("start()");

	// Waiting for processing to finish
	do
	{
		usleep(10000);
	}while(!auChan.IsEmpty() || !feChan.IsEmpty());
	
	// Get the results
	std::vector<std::string> result;
	while(!ansChan.IsEmpty())
	{
		APacket p = ansChan.GetPacket();
		if(p.GetKind() == PhrasePacket)
		{
			APhraseData *pd = (APhraseData *)p.GetData();
			result.push_back(pd->word);
		}
	}
	
	ain.SendMessage("stop()");
	acode.SendMessage("terminate()");
	arec.SendMessage("terminate()");
	ain.SendMessage("terminate()");
	
	// Wait for threads to finish
	acode.Join();
	arec.Join();
	ain.Join();
	
	return result;
}
Example #3
0
// ChkAckBuf: check acks coming from sink and update host on status
void ASyn::ChkAckBuf()
{
   if (!ackbuf->IsEmpty()){
      // first unpack the incoming packet which has the form
      //   cmd(ss,playedSamps,totalSamps)
      APacket p = ackbuf->GetPacket();
      if (p.GetKind() != CommandPacket) {
         HRError(12002,"ASyn:ChkAckBuf - non-command pkt in ackbuf\n");
         throw ATK_Error(12002);
      }
      ACommandData * cd = (ACommandData *) p.GetData();
      string ack = cd->GetCommand();
      int ss = cd->GetInt(0);
      int played = cd->GetInt(1);
      int total = cd->GetInt(2);
      // next construct reply command
      cd = new ACommandData(ack);
      // if intermediate state, then figure out what has been played
      string word;
      if (ack=="aborted" || ack=="muted" || ack=="unmuted"){
         int idx;
         float percent;
         if (ss==seqnum) {  // still current output utterance
               //printf("********* p=%d,t=%d,nw=%d,b1=%d,b2=%d\n",
               //played,total,syn->GetNumWords(),
               //syn->GetBoundary(1),syn->GetBoundary(2));
            idx = syn->GetNumWords();
            while (idx>0 && syn->GetBoundary(idx)>played) --idx;
            word = "";
            if (idx>0) word = syn->GetWord(idx);
            percent = 100.0*played/total;
         }else{
            idx=0; percent=100.0; word=".";
         }
         cd->AddArg(ss); cd->AddArg(idx);
         cd->AddArg(word); cd->AddArg(percent);
         if (ack=="aborted") syn->EndUtterance();
         if (trace&T_ABT)
            printf("ASyn: syn interrupted: ss=%d,played=%d,total=%d,idx=%d[%s]\n",
                    ss,played,total,idx,word.c_str());
      }else if (ack == "started") {
         cd->AddArg(ss); cd->AddArg(total);
      }else if (ack == "error" || ack == "finished") {
         cd->AddArg(ss); syn->EndUtterance();
      }
      APacket reppkt(cd);
      reppkt.SetStartTime(GetTimeNow());
      reppkt.SetEndTime(GetTimeNow());
      repbuf->PutPacket(reppkt);
      if (trace&T_TOP){
         printf("ASyn: reply sent - "); cd->Show(); printf("\n");
      }
   }
}
Example #4
0
// Peek at kind of first item in buffer
PacketKind  ABuffer::GetFirstKind()
{
  PacketKind pk = AnyPacket;
  HEnterSection(lock);
  if (pktList.size()>0){
    APacket p = pktList.front();
    pk = p.GetKind();
  }
  HLeaveSection(lock);
  return pk;
}
Example #5
0
// Push packet p onto the end of the pktList.  Wait for nonFull
// if the buffer is full
void ABuffer::PutPacket(APacket p)
{
  assert(filter == AnyPacket || p.GetKind() == filter);
  HEnterSection(lock);
  while (bsize!=0 && int(pktList.size())>= bsize) {
    HWaitSignal(notFull, lock);
  }
  pktList.push_back(p);
  HLeaveSection(lock);
  HSendSignal(notEmpty);
  SendBufferEvents();
}
Example #6
0
// Flush  a single observation
Boolean ARec::FlushObservation()
{
   APacket pkt;
   AStringData *sd;
   AObsData *od;
   PacketKind kind;
   Boolean flushing = TRUE;

   stTime = -1.0;  // ensure -ve before switching to runstate
   if (runmode&RUN_IMMED) // no flushing
      return TRUE;

   while (in->NumPackets()>0){
      pkt = in->PeekPacket(); kind = pkt.GetKind();

      if (kind==StringPacket) {
         in->PopPacket(); StoreMarker(pkt);
         sd = (AStringData *)pkt.GetData();
         if (sd->data.find("TERMINATED")!=string::npos) terminated=TRUE;
         if (runmode&FLUSH_TOMARK) {
            // if start or endoflist marker found, flushing complete
            string::size_type posn = sd->data.find("ENDOFLIST");
            if (posn != string::npos ) {
               OutPacket(Start_PT, "","ENDOFLIST",0,0,0.0,0.0,0.0,-1,0.0,0.0,0.0);
               terminated = TRUE;
               return TRUE;
            }
            posn = sd->data.find("START");
            if (posn != string::npos) {
               string::size_type pos1 = sd->data.find("(");
               string::size_type pos2 = sd->data.find(")");
               if (pos1<1 || pos2<pos1) return TRUE;
               fname = string(sd->data,pos1+1,pos2-pos1-1);
               return TRUE;
            }
         }
         OutMarkers(GetTimeNow());
      } else
         if (kind==ObservationPacket){
            // if speech flagged observation, flushing complete
            if (runmode&FLUSH_TOSPEECH) {
               od = (AObsData *)pkt.GetData();
               if (od->data.vq[0]) return TRUE;
            }
            in->PopPacket();
         } else {
            in->PopPacket();	// ignore non-string or -obs packets
         }
   }
   return FALSE;
}
Example #7
0
// Push packet p onto the end of the pktList.  Wait for nonFull
// if the buffer is full
void ABuffer::PutPacket(APacket p)
{
  assert(filter == AnyPacket || p.GetKind() == filter);
  
  for (ABufferListenerList::iterator l = listenerList.begin(); l != listenerList.end(); l++) {
    ABufferListener *listener = *l;
    listener->ABufferReceivedPacket(*this, p);
  }
  
  HEnterSection(lock);
  while (bsize!=0 && int(pktList.size())>= bsize) {
    HWaitSignal(notFull, lock);
  }
  pktList.push_back(p);
  HLeaveSection(lock);
  HSendSignal(notEmpty);
  SendBufferEvents();
}
Example #8
0
// listen to rec packets and construct slot value
void QA::Listen(string rgroup)
{
   APacket p;
   APhraseData *pd;
   string s="";
   float conf=0.0;
   int numwords=0;

   if(terminated) return;
   curtag=""; curwords="";
   // set the resource group to use and start recognising
   arec->SendMessage("usegrp("+rgroup+")");
   arec->SendMessage("start()");
   // listen to response of form  "tag(w1 w2 ...)"
   do {
      p = ansChan->GetPacket();
      pd = (APhraseData *)p.GetData();
      if (p.GetKind() == StringPacket){
         AStringData * sd = (AStringData *)p.GetData();
         if (sd->data.find("TERMINATED") != string::npos) {
            terminated = TRUE;
         }
      }
      else {
         if (pd->ptype == OpenTag_PT){
            assert(s == "" && numwords == 0);
            do {
               p = ansChan->GetPacket(); pd = (APhraseData *)p.GetData();
               if (pd->ptype == Word_PT) {
                  if (s=="") s = pd->word; else s = s + " " + pd->word;
                  // printf("{%s[%.2f]}",pd->word.c_str(),pd->confidence);
                  conf += pd->confidence; ++numwords;
               }
            } while(pd->ptype != CloseTag_PT && pd->ptype != End_PT);
            curtag = pd->tag;
            curwords = s;
         }
      }
   } while ((pd->ptype != End_PT) && (!terminated));
   arec->SendMessage("stop()");
   curconf = numwords==0?0.0:conf/numwords;
   printf("\n  Response:  %s(%s)[%.1f]\n",curtag.c_str(),curwords.c_str(),curconf);
}
Example #9
0
// Recognise a single observation
Boolean ARec::RecObservation()
{
   APacket pkt;
   AStringData *sd;
   AObsData *od;
   PacketKind kind;
   Boolean stopDetected = FALSE;

   if (runmode&STOP_IMMED) // stop immediately
      return TRUE;

   while (in->NumPackets()>0){
      pkt = in->GetPacket(); kind = pkt.GetKind();
      if (kind==StringPacket) {
         // check if stop marker
         sd = (AStringData *)pkt.GetData();
         if ((runmode&STOP_ATMARK) && (sd->data.find("STOP") != string::npos))
            stopDetected = TRUE;
         StoreMarker(pkt);
      }
      if (kind != StringPacket || stopDetected){
         if (stTime<0) {  // just entered runmode
            stTime = pkt.GetStartTime(); enTime = stTime;
            score = -1e6;
            trbak = "";  trbakCount = 0;
            if (runmode&RESULT_ALL)
               OutPacket(Start_PT, "", fname, 0,0,0.0,0.0,0.0,-1.0,0.0,stTime,stTime);
         }
         if (stopDetected) return TRUE;
         if (kind==ObservationPacket){
            od = (AObsData *)pkt.GetData();
            if ((runmode&STOP_ATSIL) && !od->data.vq[0])  // stop if silence
               return TRUE;
            if (trace&T_OBS) PrintObservation(frameCount,&(od->data),13);
            // recognise observation
            ProcessObservation(pri,&(od->data),-1,hset->curXForm);

            if (trace & T_FRS) {
               char *p;  MLink m;
               NetNode *node = pri->genMaxNode;
               printf("Frame %-4d ",pri->frame);
               if ( node == NULL){
                  printf("null\n");
               }else{
                  char *qm="?";
                  p = (node->wordset==NULL)?qm:node->wordset->name;
                  m=FindMacroStruct(hset,'h',node->info.hmm);
                  printf("HMM: %s (%s)  %d %5.3f\n",m->id->name,p,
                     pri->nact,pri->genMaxTok.like/pri->frame);
               }
            }
            ++frameCount; tact+=pri->nact;
            enTime += sampPeriod;
            if ((showRD || (runmode&(RESULT_IMMED|RESULT_ASAP)))
               && (++trbakCount == trbakFreq)) {
                  TraceBackRecogniser(); trbakCount = 0;
               }
         }
      }
   }
   return FALSE;
}
Example #10
0
int main(int argc, char *argv[])
{
   APacket p;

   try {

      // if (NCInitHTK("TRec.cfg",version)<SUCCESS){
      if (InitHTK(argc,argv,version)<SUCCESS){
         ReportErrors("Main",0); exit(-1);
      }
      printf("TRec: Basic Recogniser Test\n");
      ConfParam *cParm[MAXGLOBS];       /* config parameters */
      int numParm,i;
      char ngramFN[100],buf[100];
      ngramFN[0] = '\0';
      // Read configuration parms for ANGRAM to see if NGram used
      numParm = GetConfig("ANGRAM", TRUE, cParm, MAXGLOBS);
      if (numParm>0){
         if (GetConfStr(cParm,numParm,"NGRAMFILE",buf)) strcpy(ngramFN,buf);
      }
      printf("TRec: HTK initialised: %s\n",ngramFN);
      // Create Buffers
      ABuffer auChan("auChan");
      ABuffer feChan("feChan");
      ABuffer ansChan("ansChan");
      printf("TRec: Buffers initialised\n");
      // create a resource manager
      ARMan rman;

      // Create Audio Source and Coder
      ASource ain("AIn",&auChan);
      ACode acode("ACode",&auChan,&feChan);
      ARec  arec("ARec",&feChan,&ansChan,&rman,0);
      printf("TRec: Components initialised\n");

      // create global resources
      AHmms hset("HmmSet"); // load info in config
      ADict dict("ADict");
      AGram gram("AGram");
      rman.StoreHMMs(&hset);
      rman.StoreDict(&dict);
      rman.StoreGram(&gram);

      ResourceGroup *main = rman.NewGroup("main");
      main->AddHMMs(&hset);
      main->AddDict(&dict);
      main->AddGram(&gram);

      if (strlen(ngramFN)>0){
         ANGram * ngram  = new ANGram("ANGram");
         rman.StoreNGram(ngram);
         main->AddNGram(ngram);
      }

#ifdef WITHMON
      // Create Monitor and Start it
      AMonitor amon;
      amon.AddComponent(&ain);
      amon.AddComponent(&acode);
      amon.AddComponent(&arec);
      amon.Start();
#endif

      // Start components executing
      ain.Start();
      acode.Start();
      arec.Start();
      arec.SendMessage("usegrp(main)");
      arec.SendMessage("start()");

      Boolean terminated = FALSE;
      while (!terminated) {
         APacket p = ansChan.GetPacket();
         if (p.GetKind() == StringPacket){
            AStringData * sd = (AStringData *)p.GetData();
            if (sd->data.find("TERMINATED") != string::npos) {
               terminated = TRUE;
            }
         }
         p.Show();
      }
      // Shutdown
      printf("Waiting for ain\n");
      ain.Join();
      printf("Waiting for acode\n");
      acode.Join();
      printf("Waiting for arec\n");
      arec.Join();
#ifdef WITHMON
      printf("Waiting for monitor\n");
      amon.Terminate();
      HJoinMonitor();
#endif
      return 0;
   }
   catch (ATK_Error e){ ReportErrors("ATK",e.i); return 0;}
   catch (HTK_Error e){ ReportErrors("HTK",e.i); return 0;}
}