freespeech /tags/rel-0-4-1/FreeSpeech/audio_blocks/src/SampleDelay.cc

Language C++ Lines 137
MD5 Hash 8baec3f27341bf3db2ceb846a074d1e5 Estimated Cost $1,755 (why?)
Repository https://freespeech.svn.sourceforge.net/svnroot/freespeech View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 1999 Jean-Marc Valin
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file.  If not, write to the Free Software Foundation,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "BufferedNode.h"
#include "Buffer.h"
#include "Vector.h"
#include <stdlib.h>

class SampleDelay;

DECLARE_NODE(SampleDelay)
/*Node
 *
 * @name SampleDelay
 * @category Signal:Base
 * @description No description available
 *
 * @input_name INPUT
 * @input_description No description available
 *
 * @input_name DELAY
 * @input_description No description available
 *
 * @output_name OUTPUT
 * @output_description No description available
 *
 * @parameter_name LENGTH
 * @parameter_description No description available
 *
 * @parameter_name DELAY
 * @parameter_description No description available
 *
 * @parameter_name LOOKBACK
 * @parameter_description No description available
 *
 * @parameter_name LOOKAHEAD
 * @parameter_description No description available
 *
END*/


class SampleDelay : public BufferedNode {
   
   int inputID;
   int outputID;
   int delayID;
   int delay;
   int constantDelay;
   int length;

public:
   SampleDelay(string nodeName, ParameterSet params)
   : BufferedNode(nodeName, params)
   {
      inputID = addInput("INPUT");
      outputID = addOutput("OUTPUT");

      delay = 0;
      length = dereference_cast<int> (parameters.get("LENGTH"));
      if (parameters.exist("DELAY"))
      {
	 delay = dereference_cast<int> (parameters.get("DELAY"));
	 constantDelay = true;
      } else {
	 if (parameters.exist("LOOKBACK"))
	    inputsCache[inputID].lookBack=dereference_cast<int> (parameters.get("LOOKBACK"));
	 if (parameters.exist("LOOKAHEAD"))
	    inputsCache[inputID].lookAhead=dereference_cast<int> (parameters.get("LOOKAHEAD"));
	 delayID = addInput("DELAY");
	 constantDelay=false;
      } 
   }


   void calculate(int output_id, int count, Buffer &out)
   {
      if (!constantDelay)
      {
	 ObjectRef delayValue = getInput(delayID, count);
	 if (delayValue->status == Object::valid)
	 {
	    delay = (object_cast<Vector<float> > (delayValue))[0];
	    //delay = dereference_cast<float> (delayValue);
	 }
      }
      
      int frameDelay = delay/length;
      int sampleDelay = delay-frameDelay*length;

      ObjectRef firstValue=Object::nilObject;
      ObjectRef secondValue=Object::nilObject;
      if (count-frameDelay-1 >=0 )
	 firstValue = getInput(inputID, count-frameDelay-1);
      if (count-frameDelay >=0 )
	 secondValue = getInput(inputID, count-frameDelay);

      Vector<float> &output = *Vector<float>::alloc(length);
      out[count] = &output;

      if (firstValue->status == Object::valid)
      {
	 const Vector<float> &in1 = object_cast<Vector<float> > (firstValue);
	 for (int i=0;i<sampleDelay;i++)
	    output[i] = in1[length-sampleDelay+i];
      } else {
	 for (int i=0;i<sampleDelay;i++)
	    output[i] = 0;
	 
      }

      if (secondValue->status == Object::valid)
      {
	 const Vector<float> &in2 = object_cast<Vector<float> > (secondValue);
	 for (int i=sampleDelay;i<length;i++)
	    output[i] = in2[i-sampleDelay];
      } else {
	 for (int i=sampleDelay;i<length;i++)
	    output[i] = 0;
      }

   }

};
Back to Top