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