PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C++ | 119 lines | 71 code | 18 blank | 30 comment | 15 complexity | 40950d1548ee86d681d22e55b1272fc6 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, LGPL-2.1
  1. // Copyright (C) 1999 Jean-Marc Valin
  2. #include "BufferedNode.h"
  3. #include "Buffer.h"
  4. #include "Vector.h"
  5. #include <stdlib.h>
  6. class SampleDelay;
  7. DECLARE_NODE(SampleDelay)
  8. /*Node
  9. *
  10. * @name SampleDelay
  11. * @category DSP:Base
  12. * @description No description available
  13. *
  14. * @input_name INPUT
  15. * @input_description No description available
  16. *
  17. * @input_name DELAY
  18. * @input_description No description available
  19. *
  20. * @output_name OUTPUT
  21. * @output_description No description available
  22. *
  23. * @parameter_name LENGTH
  24. * @parameter_description No description available
  25. *
  26. * @parameter_name DELAY
  27. * @parameter_description No description available
  28. *
  29. * @parameter_name LOOKBACK
  30. * @parameter_description No description available
  31. *
  32. * @parameter_name LOOKAHEAD
  33. * @parameter_description No description available
  34. *
  35. END*/
  36. class SampleDelay : public BufferedNode {
  37. int inputID;
  38. int outputID;
  39. int delayID;
  40. int delay;
  41. int constantDelay;
  42. int length;
  43. public:
  44. SampleDelay(string nodeName, ParameterSet params)
  45. : BufferedNode(nodeName, params)
  46. {
  47. inputID = addInput("INPUT");
  48. outputID = addOutput("OUTPUT");
  49. delay = 0;
  50. length = dereference_cast<int> (parameters.get("LENGTH"));
  51. if (parameters.exist("DELAY"))
  52. {
  53. delay = dereference_cast<int> (parameters.get("DELAY"));
  54. constantDelay = true;
  55. } else {
  56. if (parameters.exist("LOOKBACK"))
  57. inputsCache[inputID].lookBack=dereference_cast<int> (parameters.get("LOOKBACK"));
  58. if (parameters.exist("LOOKAHEAD"))
  59. inputsCache[inputID].lookAhead=dereference_cast<int> (parameters.get("LOOKAHEAD"));
  60. delayID = addInput("DELAY");
  61. constantDelay=false;
  62. }
  63. }
  64. void calculate(int output_id, int count, Buffer &out)
  65. {
  66. //FIXME: Should remove all references to Object->status
  67. if (!constantDelay)
  68. {
  69. ObjectRef delayValue = getInput(delayID, count);
  70. delay = int((object_cast<Vector<float> > (delayValue))[0]);
  71. }
  72. int frameDelay = delay/length;
  73. int sampleDelay = delay-frameDelay*length;
  74. ObjectRef firstValue=nilObject;
  75. ObjectRef secondValue=nilObject;
  76. if (count-frameDelay-1 >=0 )
  77. firstValue = getInput(inputID, count-frameDelay-1);
  78. if (count-frameDelay >=0 )
  79. secondValue = getInput(inputID, count-frameDelay);
  80. Vector<float> &output = *Vector<float>::alloc(length);
  81. out[count] = &output;
  82. if (!firstValue->isNil())
  83. {
  84. const Vector<float> &in1 = object_cast<Vector<float> > (firstValue);
  85. for (int i=0;i<sampleDelay;i++)
  86. output[i] = in1[length-sampleDelay+i];
  87. } else {
  88. for (int i=0;i<sampleDelay;i++)
  89. output[i] = 0;
  90. }
  91. if (!secondValue->isNil())
  92. {
  93. const Vector<float> &in2 = object_cast<Vector<float> > (secondValue);
  94. for (int i=sampleDelay;i<length;i++)
  95. output[i] = in2[i-sampleDelay];
  96. } else {
  97. for (int i=sampleDelay;i<length;i++)
  98. output[i] = 0;
  99. }
  100. }
  101. };