Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

PitchEvent.cc

Go to the documentation of this file.
00001 #include "PitchEvent.h"
00002 #include <sstream>
00003 #include <libxml/tree.h>
00004 
00005 using namespace std; 
00006 
00007 const EventBase::classTypeID_t PitchEvent::autoRegisterPitchEvent=getTypeRegistry().registerType<PitchEvent>(makeClassTypeID("PITC"));
00008 
00009 std::string
00010 PitchEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00011   if(!showTypeSpecific)
00012     return EventBase::getDescription(showTypeSpecific,verbosity);
00013   std::ostringstream logdata;
00014   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t' << freq << '\t' << amplitude << '\t' << confidence;
00015   return logdata.str();
00016 }
00017 
00018 unsigned int
00019 PitchEvent::getBinSize() const {
00020   unsigned int used=EventBase::getBinSize();
00021   if(saveFormat==XML)
00022     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00023   //otherwise need to add our own fields
00024   used+=creatorSize("EventBase::PitchEvent");
00025   used+=getSerializedSize(freq);
00026   used+=getSerializedSize(amplitude);
00027   used+=getSerializedSize(confidence);
00028   return used;
00029 }
00030 
00031 unsigned int
00032 PitchEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00033   unsigned int origlen=len;
00034   if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00035   if(!checkCreatorInc("EventBase::PitchEvent",buf,len,true)) return 0;
00036   if(!decodeInc(freq,buf,len)) return 0;
00037   if(!decodeInc(amplitude,buf,len)) return 0;
00038   if(!decodeInc(confidence,buf,len)) return 0;
00039   return origlen-len; 
00040 }
00041 
00042 unsigned int
00043 PitchEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00044   unsigned int origlen=len;
00045   if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00046   if(!saveCreatorInc("EventBase::PitchEvent",buf,len)) return 0;
00047   if(!encodeInc(freq,buf,len)) return 0;
00048   if(!encodeInc(amplitude,buf,len)) return 0;
00049   if(!encodeInc(confidence,buf,len)) return 0;
00050   return origlen-len;
00051 }
00052 
00053 void
00054 PitchEvent::loadXML(xmlNode* node) {
00055   if(node==NULL)
00056     return;
00057   
00058   EventBase::loadXML(node);
00059   
00060   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00061     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00062       continue;
00063     
00064     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00065     if(name==NULL)
00066       throw bad_format(cur,"property missing name");
00067     
00068     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00069     if(val==NULL)
00070       throw bad_format(cur,"property missing value");
00071     
00072     //cout << "loadXML: " << name << "=" << val << endl;
00073     
00074     if(xmlStrcmp(name, (const xmlChar *)"freq")==0)
00075       freq=(float)atof((const char*)val);
00076     else if(xmlStrcmp(name, (const xmlChar *)"amplitude")==0)
00077       amplitude=(float)atof((const char*)val);
00078     else if(xmlStrcmp(name, (const xmlChar *)"confidence")==0)
00079       confidence=(float)atof((const char*)val);
00080     
00081     xmlFree(val);
00082     xmlFree(name);
00083   }
00084 }
00085 
00086 //! a little local macro to make saving fields easier
00087 #define SAVE_PARAM(name) { \
00088   xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL); \
00089   if(cur==NULL) \
00090     throw bad_format(node,"Error: PitchEvent xml error on saving param"); \
00091   xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)#name); \
00092   char valbuf[20]; \
00093   snprintf(valbuf,20,"%g",name); \
00094   xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)valbuf); }
00095 
00096 void
00097 PitchEvent::saveXML(xmlNode * node) const {
00098   if(node==NULL)
00099     return;
00100   EventBase::saveXML(node);
00101   
00102   //clear old params first
00103   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00104     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00105       xmlUnlinkNode(cur);
00106       xmlFreeNode(cur);
00107       cur = skipToElement(node->children); //restart the search (boo)
00108     } else
00109       cur = skipToElement(cur->next);
00110   }
00111   
00112   //cout << "saveXML: " << x << ' ' << y << ' ' << a << endl;
00113   
00114   SAVE_PARAM(freq);
00115   SAVE_PARAM(amplitude);
00116   SAVE_PARAM(confidence);
00117 }
00118 
00119 /*! @file
00120  * @brief Implements PitchEvent, which provides information about a tone detected from the microphone(s)
00121  * @author Matus Telgarsky and Jonah Sherman (Creators)
00122  * @author Ethan Tira-Thompson (imported into framework)
00123  *
00124  * Originally written as a part of a final project at Carnegie Mellon (15-494 Cognitive Robotics, Spring 2006)
00125  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:46 2016 by Doxygen 1.6.3