PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C++ | 136 lines | 74 code | 18 blank | 44 comment | 19 complexity | 8baec3f27341bf3db2ceb846a074d1e5 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, LGPL-2.1
  1. // Copyright (C) 1999 Jean-Marc Valin
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #include "BufferedNode.h"
  17. #include "Buffer.h"
  18. #include "Vector.h"
  19. #include <stdlib.h>
  20. class SampleDelay;
  21. DECLARE_NODE(SampleDelay)
  22. /*Node
  23. *
  24. * @name SampleDelay
  25. * @category Signal:Base
  26. * @description No description available
  27. *
  28. * @input_name INPUT
  29. * @input_description No description available
  30. *
  31. * @input_name DELAY
  32. * @input_description No description available
  33. *
  34. * @output_name OUTPUT
  35. * @output_description No description available
  36. *
  37. * @parameter_name LENGTH
  38. * @parameter_description No description available
  39. *
  40. * @parameter_name DELAY
  41. * @parameter_description No description available
  42. *
  43. * @parameter_name LOOKBACK
  44. * @parameter_description No description available
  45. *
  46. * @parameter_name LOOKAHEAD
  47. * @parameter_description No description available
  48. *
  49. END*/
  50. class SampleDelay : public BufferedNode {
  51. int inputID;
  52. int outputID;
  53. int delayID;
  54. int delay;
  55. int constantDelay;
  56. int length;
  57. public:
  58. SampleDelay(string nodeName, ParameterSet params)
  59. : BufferedNode(nodeName, params)
  60. {
  61. inputID = addInput("INPUT");
  62. outputID = addOutput("OUTPUT");
  63. delay = 0;
  64. length = dereference_cast<int> (parameters.get("LENGTH"));
  65. if (parameters.exist("DELAY"))
  66. {
  67. delay = dereference_cast<int> (parameters.get("DELAY"));
  68. constantDelay = true;
  69. } else {
  70. if (parameters.exist("LOOKBACK"))
  71. inputsCache[inputID].lookBack=dereference_cast<int> (parameters.get("LOOKBACK"));
  72. if (parameters.exist("LOOKAHEAD"))
  73. inputsCache[inputID].lookAhead=dereference_cast<int> (parameters.get("LOOKAHEAD"));
  74. delayID = addInput("DELAY");
  75. constantDelay=false;
  76. }
  77. }
  78. void calculate(int output_id, int count, Buffer &out)
  79. {
  80. if (!constantDelay)
  81. {
  82. ObjectRef delayValue = getInput(delayID, count);
  83. if (delayValue->status == Object::valid)
  84. {
  85. delay = (object_cast<Vector<float> > (delayValue))[0];
  86. //delay = dereference_cast<float> (delayValue);
  87. }
  88. }
  89. int frameDelay = delay/length;
  90. int sampleDelay = delay-frameDelay*length;
  91. ObjectRef firstValue=Object::nilObject;
  92. ObjectRef secondValue=Object::nilObject;
  93. if (count-frameDelay-1 >=0 )
  94. firstValue = getInput(inputID, count-frameDelay-1);
  95. if (count-frameDelay >=0 )
  96. secondValue = getInput(inputID, count-frameDelay);
  97. Vector<float> &output = *Vector<float>::alloc(length);
  98. out[count] = &output;
  99. if (firstValue->status == Object::valid)
  100. {
  101. const Vector<float> &in1 = object_cast<Vector<float> > (firstValue);
  102. for (int i=0;i<sampleDelay;i++)
  103. output[i] = in1[length-sampleDelay+i];
  104. } else {
  105. for (int i=0;i<sampleDelay;i++)
  106. output[i] = 0;
  107. }
  108. if (secondValue->status == Object::valid)
  109. {
  110. const Vector<float> &in2 = object_cast<Vector<float> > (secondValue);
  111. for (int i=sampleDelay;i<length;i++)
  112. output[i] = in2[i-sampleDelay];
  113. } else {
  114. for (int i=sampleDelay;i<length;i++)
  115. output[i] = 0;
  116. }
  117. }
  118. };