|
libGDF
|
00001 // 00002 // This file is part of libGDF. 00003 // 00004 // libGDF is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation, either version 3 of 00007 // the License, or (at your option) any later version. 00008 // 00009 // libGDF is distributed in the hope that it will be useful, 00010 // but WITHout ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with libGDF. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // Copyright 2010 Christoph Eibel 00018 00019 #include "GDF/EventConverter.h" 00020 #include "GDF/Types.h" 00021 00022 #include <map> 00023 #include <vector> 00024 #include <algorithm> 00025 00026 using namespace std; 00027 00028 namespace gdf 00029 { 00030 typedef map<uint16, vector<Mode1Event> > TypeEventMap; 00031 00032 uint16 const STOP_FLAG = 0x8000; 00033 uint16 const NOT_STOP_FLAG = 0x7FFF; 00034 00035 //------------------------------------------------------------------------- 00036 vector<Mode3Event> convertMode1EventsIntoMode3Events (vector<Mode1Event> const& mode_1_events) 00037 throw (exception::general) 00038 { 00039 vector<Mode3Event> mode_3_events; 00040 00041 TypeEventMap type_event_map; 00042 00043 for (size_t index = 0; index < mode_1_events.size (); index++) 00044 { 00045 Mode1Event const& event_1 = mode_1_events[index]; 00046 type_event_map[event_1.type & NOT_STOP_FLAG].push_back (event_1); 00047 } 00048 00049 for (TypeEventMap::iterator iter = type_event_map.begin (); 00050 iter != type_event_map.end (); ++iter) 00051 { 00052 sort (iter->second.begin (), iter->second.end ()); 00053 for (unsigned index = 0; index < iter->second.size (); index++) 00054 { 00055 if (iter->second[index].type & STOP_FLAG) 00056 throw exception::general ("events could not be converted from mode 1 to mode 3"); 00057 // throw exception::general ("events of same type are overlapping, not supported for mode 1 events"); 00058 00059 Mode3Event event_3; 00060 event_3.channel = 0; // ALL CHANNELS 00061 event_3.type = iter->first; 00062 event_3.position = iter->second[index].position; 00063 00064 if (iter->second[index+1].type == (iter->first | STOP_FLAG)) 00065 { 00066 event_3.duration = iter->second[index + 1].position - event_3.position; 00067 index++; 00068 } 00069 else 00070 event_3.duration = 0; 00071 00072 mode_3_events.push_back (event_3); 00073 } 00074 } 00075 00076 return mode_3_events; 00077 } 00078 }