Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/Selector.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program 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 General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_SELECTOR_H
00020 #define MARSYAS_SELECTOR_H
00021 
00022 #include <marsyas/system/MarSystem.h>
00023 #include <algorithm>
00024 
00025 namespace Marsyas
00026 {
00055 class marsyas_EXPORT Selector: public MarSystem
00056 {
00057 private:
00058   void addControls();
00059   void myUpdate(MarControlPtr sender);
00060 
00061   MarControlPtr ctrl_enabled_;
00062 
00063   bool is_enabled(const realvec & mask, int index)
00064   {
00065     return index >= mask.getSize() ||
00066         (index >= 0 && mask(index) > 0);
00067   }
00068 
00069   void set_enabled(realvec & mask, int index, bool enabled)
00070   {
00071     if (index < 0)
00072       return;
00073     fit_mask(mask, index);
00074     mask(index) = enabled ? 1.0 : 0.0;
00075   }
00076 
00077   void set_enabled_range(realvec & mask, int begin, int end, bool enabled)
00078   {
00079     if (end < begin || end < 0)
00080       return;
00081     fit_mask(mask, end);
00082     begin = std::max(0, begin);
00083     for (int i = begin; i <= end; ++i)
00084       mask(i) = enabled ? 1.0 : 0.0;
00085   }
00086 
00087   void fit_mask(realvec & mask, int max_index)
00088   {
00089     if (max_index < mask.getSize())
00090       return;
00091     int current_size = mask.getSize();
00092     int new_size = max_index+1;
00093     mask.stretch(new_size);
00094     // initialize as enabled:
00095     for (int i = current_size; i < new_size; ++i)
00096         mask(i) = 1.0;
00097   }
00098 
00099   int enabled_count(const realvec & mask, int total)
00100   {
00101     int count = 0;
00102     for (int i = 0; i < total; i++)
00103       if (is_enabled(mask, i))
00104         count++;
00105     return count;
00106   }
00107 
00108 public:
00109   Selector(std::string name);
00110   Selector(const Selector& a);
00111   ~Selector();
00112   MarSystem* clone() const;
00113 
00114   void myProcess(realvec& in, realvec& out);
00115 };
00116 
00117 }
00118 
00119 #endif
00120