00001 #include "PitchEvent.h"
00002 #include <sstream>
00003 #include <libxml/tree.h>
00004
00005
00006 using namespace std;
00007
00008 std::string
00009 PitchEvent::getDescription(bool showTypeSpecific, unsigned int verbosity) 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;
00022
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
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
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
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);
00107 } else
00108 cur = skipToElement(cur->next);
00109 }
00110
00111
00112
00113 SAVE_PARAM(freq);
00114 SAVE_PARAM(amplitude);
00115 SAVE_PARAM(confidence);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130