CLAM-Development  1.4.0
CircularShift.cxx
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 #include "Audio.hxx"
23 #include "Spectrum.hxx"
24 #include "Complex.hxx"
25 #include "CircularShift.hxx"
26 
27 namespace CLAM {
28 
29  /* Processing object Method implementations */
30 
32  : mInput( "Input samples", this ),
33  mOutput( "Shifted samples", this ),
34  mSteps( "Shift Steps", this )
35  {
37  }
38 
40  : mInput( "Input samples", this ),
41  mOutput( "Shifted samples", this ),
42  mSteps("Shift Steps",this)
43  {
44  Configure(c);
45  }
46 
48  {}
49 
50 
51  /* Configure the Processing Object according to the Config object */
52 
54  {
56  mSteps.DoControl(TData(mConfig.GetAmount()));
57 
58  return true;
59  }
60 
61  /* Setting Prototypes for faster processing */
62 
64  {
65  return false;
66  }
67 
69  {
70  return false;
71  }
72 
74  {
75  return false;
76  }
77 
78  /* The supervised Do() function */
79 
80  bool CircularShift::Do(void)
81  {
82  return Do( mInput.GetAudio(), mOutput.GetAudio() );
83  mInput.Consume();
84  mOutput.Produce();
85 
86  }
87 
88  /* The unsupervised Do() function */
89 
90  bool CircularShift::Do( const DataArray& in, DataArray& out)
91  {
92 
93  int i;
94  TData amount = mSteps.GetLastValue();
95  int size = in.Size();
96  const TData* inp = in.GetPtr();
97  TData* outp = out.GetPtr();
98  TData* tmp;
99 
100  CLAM_ASSERT(size == out.Size(),
101  "CircularShift::Do(): input and output vectors do not match");
102 
103  if (amount > 0) {
104  int ia = (int)amount;
105  tmp = new TData[ia];
106  for (i=0;i<ia;i++)
107  tmp[i] = inp[size - ia + i];
108  for (i=size-ia-1;i>= 0;i--)
109  outp[i + ia] = inp[i];
110  for (i=0;i< ia;i++)
111  outp[i] = tmp[i];
112  }
113  else {
114  int ia = (int)-amount;
115  tmp = new TData[ia];
116  for (i=0;i<ia;i++)
117  tmp[i] = inp[i];
118  for (i=0;i< (size - ia);i++)
119  outp[i] = inp[i+ia];
120  for (i=0;i< ia;i++)
121  outp[i+size-ia] = tmp[i];
122  }
123  delete[] tmp;
124  return true;
125  }
126 
128  {
129  CLAM_ASSERT(!in.HasMagBuffer(),
130  "CircularShift::Do(): only implemented for Spectrums with MagBuffer");
131 
132  return Do(in.GetMagBuffer(),out.GetMagBuffer());
133  }
134 
135  bool CircularShift::Do( const Audio& in, Audio& out)
136  {
137  Do(in.GetBuffer(),out.GetBuffer());
138  return true;
139  }
140 
141 
142 }
143