PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C++ | 122 lines | 74 code | 18 blank | 30 comment | 19 complexity | ce80e387f67dd6a68563a5ed01d528e8 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 Signal: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. if (!constantDelay)
  67. {
  68. ObjectRef delayValue = getInput(delayID, count);
  69. if (delayValue->status == Object::valid)
  70. {
  71. delay = int((object_cast<Vector<float> > (delayValue))[0]);
  72. //delay = dereference_cast<float> (delayValue);
  73. }
  74. }
  75. int frameDelay = delay/length;
  76. int sampleDelay = delay-frameDelay*length;
  77. ObjectRef firstValue=Object::nilObject;
  78. ObjectRef secondValue=Object::nilObject;
  79. if (count-frameDelay-1 >=0 )
  80. firstValue = getInput(inputID, count-frameDelay-1);
  81. if (count-frameDelay >=0 )
  82. secondValue = getInput(inputID, count-frameDelay);
  83. Vector<float> &output = *Vector<float>::alloc(length);
  84. out[count] = &output;
  85. if (firstValue->status == Object::valid)
  86. {
  87. const Vector<float> &in1 = object_cast<Vector<float> > (firstValue);
  88. for (int i=0;i<sampleDelay;i++)
  89. output[i] = in1[length-sampleDelay+i];
  90. } else {
  91. for (int i=0;i<sampleDelay;i++)
  92. output[i] = 0;
  93. }
  94. if (secondValue->status == Object::valid)
  95. {
  96. const Vector<float> &in2 = object_cast<Vector<float> > (secondValue);
  97. for (int i=sampleDelay;i<length;i++)
  98. output[i] = in2[i-sampleDelay];
  99. } else {
  100. for (int i=sampleDelay;i<length;i++)
  101. output[i] = 0;
  102. }
  103. }
  104. };