CLAM-Development  1.4.0
BinaryAudioOp.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _BINARY_AUDIO_OP_H_
23 #define _BINARY_AUDIO_OP_H_
24 
25 #include "Processing.hxx"
26 #include "DynamicType.hxx"
27 #include "Audio.hxx"
28 #include <typeinfo> // std::bad_cast
29 #include "AudioInPort.hxx"
30 #include "AudioOutPort.hxx"
31 
32 #include <iostream>
33 
34 namespace CLAM
35 {
36 
38  {
39  public:
41  };
42 
43  template < typename BinOp >
45  : public Processing
46  {
47  BinaryAudioOpConfig mConfig;
48  BinOp mOperation;
49 
53  bool ConcreteConfigure(const ProcessingConfig& c)
54  {
55  try {
56  mConfig = dynamic_cast<const BinaryAudioOpConfig&>(c);
57  }
58  catch (std::bad_cast)
59  {
60  CLAM_ASSERT(false,"Config should be a BynariaAudioOpConfig");
61  }
62  return true;
63 
64  }
65 
66  AudioInPort mFirstInput;
67  AudioInPort mSecondInput;
68  AudioOutPort mOutput;
69 
70 
71  public:
72 
74  :mFirstInput("First Audio Input",this),
75  mSecondInput("Second Audio Input",this),
76  mOutput("Audio Output",this)
77  {
79  }
80 
82  :mFirstInput("First Audio Input",this),
83  mSecondInput("Second Audio Input",this),
84  mOutput("Audio Output",this)
85 
86  {
87  Configure( c );
88  }
89 
91  {
92  }
93 
94  const ProcessingConfig &GetConfig() const { return mConfig;}
95 
96  const char *GetClassName() const {return "BinaryAudioOperation";}
97  void Check(const Audio& in1, const Audio& in2, const Audio& out)
98  {
99  CLAM_ASSERT(in1.GetSize() <= in2.GetSize(),
100  "BinaryAudioOperation::Do(): Incompatible Input Audio Data Sizes");
101  CLAM_ASSERT(in1.GetSize() <= out.GetSize(),
102  "BinaryAudioOperation::Do(): Incompatible Output Audio Data Size");
103 
104  }
105 
106  bool Do(void)
107  {
108  bool res = Do(mFirstInput.GetAudio(),
109  mSecondInput.GetAudio(),
110  mOutput.GetAudio());
111  mFirstInput.Consume();
112  mSecondInput.Consume();
113  mOutput.Produce();
114  return res;
115  }
116 
117  bool Do(const Audio& in1, const Audio& in2, Audio& out)
118  {
119 
120 
121  int size = in1.GetSize();
122  int i;
123 
124  Check(in1,in2,out);
125 
126  TData* inb1 = in1.GetBuffer().GetPtr();
127  TData* inb2 = in2.GetBuffer().GetPtr();
128  TData* outb = out.GetBuffer().GetPtr();
129 
130  for (i=0;i<size;i++)
131  {
132  *outb++ = mOperation( *inb1++ , *inb2++ );
133  }
134 
135  return true;
136  }
137 
138  // Port interfaces.
139 
140  bool SetPrototypes(const Audio& in1, const Audio& in2, const Audio& out)
141  {
142  return false;
143  }
144 
146  {
147  return false;
148  }
149 
151  {
152  return true;
153  }
154 
155  bool MayDisableExecution() const {return true;}
156 
157  private:
158  };
159 
160 }
161 
162 #endif // _BINARY_AUDIO_OP_H_
163