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

Tekkotsu v3.0
Generated Wed Oct 4 00:03:45 2006 by Doxygen 1.4.7