/tags/rel-0-0-1/FreeSpeech/audio_blocks/src/GCMS.cc

# · C++ · 107 lines · 69 code · 13 blank · 25 comment · 9 complexity · be096eb2cee8761a89989de94447e30b MD5 · raw file

  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 "GCMS.h"
  17. #include "FrameOperation.h"
  18. #include "Vector.h"
  19. #include "multithread.h"
  20. #include "Buffer.h"
  21. #include "RotatingBuffer.h"
  22. GCMS::GCMS(string nodeName, const ParameterSet &params)
  23. : FrameOperation(nodeName, params)
  24. , sum(outputLength, float ())
  25. , accumCount(0)
  26. {
  27. inputID = addInput("INPUT");
  28. if (parameters.exist("INPUTLENGTH"))
  29. inputLength = dereference_cast<int> (parameters.get("INPUTLENGTH"));
  30. else inputLength = dereference_cast<int> (parameters.get("LENGTH"));
  31. }
  32. void GCMS::specificInitialize()
  33. {
  34. //cerr << "GCMS initialize...\n";
  35. this->FrameOperation::specificInitialize();
  36. //perform requests for input node
  37. ParameterSet req;
  38. req.add("LOOKAHEAD", ObjectRef(new Int(outputLookAhead)));
  39. req.add("LOOKBACK", ObjectRef(new Int(outputLookBack)));
  40. //cerr << "Request to node type: " << typeid(inputs[0].node).name() << " name: " << inputs[0].node->getName() << endl;
  41. //req.print(cerr);
  42. inputs[0].node->request(req);
  43. for (int i=0;i<outputLength;i++)
  44. sum[i]=0;
  45. accumCount=0;
  46. }
  47. void GCMS::reset()
  48. {
  49. this->FrameOperation::reset();
  50. for (int i=0;i<outputLength;i++)
  51. sum[i]=0;
  52. accumCount=0;
  53. }
  54. ObjectRef GCMS::getOutput(int output_id, int count)
  55. {
  56. lock();
  57. try {
  58. Buffer &out = object_cast<Buffer> (output);
  59. if (count != processCount)
  60. {
  61. int i;
  62. ObjectRef inputValue;
  63. processCount = count;
  64. NodeInput input = inputs[inputID];
  65. inputValue = input.node->getOutput(input.outputID, count);
  66. if (inputValue->status != Object::valid)
  67. {
  68. return inputValue;
  69. }
  70. //cerr << "input invalid? " << invalid<< endl;
  71. //cerr << "computing\n";
  72. //this->computeFrame(inputBuffer, count);
  73. Vector<float> &cms = object_cast<Vector<float> > (out[count]);
  74. Vector<float> &in = object_cast<Vector<float> > (inputValue);
  75. accumCount++;
  76. float inv_accum=1.0/accumCount;
  77. for (i=0;i<outputLength;i++)
  78. {
  79. sum[i] = (1-inv_accum)*sum[i] + inv_accum*in[i];
  80. cms[i] = in[i]-sum[i];
  81. }
  82. out[count]->status = Object::valid;
  83. }
  84. //unlock();
  85. //cerr << "leaving GCMS::getOutput for " << name << " count: " << count << endl;
  86. //cerr << "returning status " << out[count]->status << endl;
  87. return unlock_and_return(out[count]);
  88. } catch (BaseException &e)
  89. {
  90. e.print();
  91. unlock();
  92. throw NodeException (this, "Exception in GCMS::getOutput", __FILE__, __LINE__);
  93. }
  94. }