CLAM-Development  1.4.0
AudioBufferSink.hxx
Go to the documentation of this file.
1 #ifndef AudioBufferSink_hxx
2 #define AudioBufferSink_hxx
3 
4 #include "Processing.hxx"
5 #include "InPort.hxx"
6 #include "Audio.hxx"
7 
8 #include <sstream>
9 
10 namespace CLAM
11 {
12  class AudioBufferSink : public Processing
13  {
14  public:
15  struct Port
16  {
17  float* mFloatBuffer;
18  double* mDoubleBuffer;
19  unsigned mBufferSize;
20  //AudioInPort* mPort;
22 
23  //resize needs a default constructor
24  Port()
26  {
27  }
28 
29  explicit Port(InPort<Audio>* p)
31  {
32  }
33  };
34  typedef std::vector<Port> Ports;
35 
36  private:
37  class Config : public ProcessingConfig
38  {
40  DYN_ATTRIBUTE( 0, public, int, NSinks);
41  protected:
42  void DefaultInit()
43  {
44  AddAll();
45  UpdateData();
46  SetNSinks(1);
47  };
48  void LoadFrom(Storage & storage)
49  {
51  if (not HasNSinks())
52  {
53  AddNSinks();
54  UpdateData();
55  SetNSinks(1);
56  }
57  }
58  };
59 
60  private:
61  Config _config;
62  Ports _ports;
63 
64  public:
66  {
67  //After being dropped it is ready to run as it does not need any configuration at all
68  //SetExecState(Ready);
69  Configure( config );
70  ResizePorts(1);
71  }
72 
74  {
75  for (unsigned port = 0; port < _ports.size(); ++port)
76  delete _ports[port].mPort;
77  }
78 
80  void SetFrameAndHopSize(const int val, unsigned index)
81  {
82  CLAM_ASSERT(index < _ports.size(), "AudioInPort index out of range");
83  Port& port = _ports[index];
84  port.mPort->SetSize(1);
85  port.mPort->SetHop(1);
86  }
87 
88  void SetExternalBuffer(float* buf, unsigned nframes, unsigned index);
89  void SetExternalBuffer(double* buf, unsigned nframes, unsigned index);
90 
91  bool Do();
92 
93  virtual bool SupportsVariableAudioSize() const {return true;}
94 
95  const char* GetClassName() const { return "AudioBufferSink";}
96 
97  const ProcessingConfig & GetConfig() const
98  {
99  return _config;
100  }
101 
103  {
104  CopyAsConcreteConfig(_config, config);
105  unsigned sinks = _config.GetNSinks();
106 
107  ResizePorts(sinks);
108 
109  return true;
110  }
111 
112  Ports& GetPorts() { return _ports; }
113 
114  private:
115  std::string const Portname(unsigned port) const
116  {
117  std::ostringstream os;
118  os << port + 1; //to make ports one based (when viewed in jack)
119  return os.str();
120  }
121 
122  void ResizePorts(unsigned sinks)
123  {
124  if (sinks == _ports.size())
125  return;
126 
127  for (unsigned port = sinks; port < _ports.size(); ++port)
128  delete _ports[port].mPort;
129 
130  unsigned oldSize = _ports.size();
131  _ports.resize(sinks);
132 
133  for (unsigned port = oldSize; port < sinks; ++port)
134  _ports[port] = Port(new InPort<Audio>(Portname(port), this));
135  }
136  };
137 
138 } //namespace CLAM
139 
140 #endif
141