CLAM-Development  1.4.0
AudioSource.hxx
Go to the documentation of this file.
1 #ifndef AudioSource_hxx
2 #define AudioSource_hxx
3 
4 #include "Processing.hxx"
5 #include "AudioOutPort.hxx"
6 
7 #include <sstream>
8 
9 namespace CLAM
10 {
11 
12  class AudioSource : public Processing
13  {
14  public:
15  struct Port
16  {
17  const float* mFloatBuffer;
18  const double* mDoubleBuffer;
19  unsigned mBufferSize;
21 
22  Port()
24  {
25  }
26 
27  explicit Port(AudioOutPort* p)
29  {
30  }
31  };
32  typedef std::vector<Port> Ports;
33 
34  private:
35 
36  class Config : public ProcessingConfig
37  {
39  DYN_ATTRIBUTE( 0, public, int, NSources);
40  ~Config();
41  protected:
42  void DefaultInit()
43  {
44  AddAll();
45  UpdateData();
46  SetNSources(1);
47  };
48  void LoadFrom(Storage & storage)
49  {
51  if (not HasNSources())
52  {
53  AddNSources();
54  UpdateData();
55  SetNSources(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 
71  // default constructed with 1 port
72  ResizePorts(1);
73  }
74 
76  {
77  for (unsigned port = 0; port < _ports.size(); ++port)
78  delete _ports[port].mPort;
79  }
80 
81  void SetFrameAndHopSize(const int val, unsigned index)
82  {
83  CLAM_ASSERT(index < _ports.size(), "AudioOutPort index out of range");
84  Port& port = _ports[index];
85  port.mPort->SetSize(val);
86  port.mPort->SetHop(val);
87  }
88 
89  void SetExternalBuffer(const float* buf, unsigned nframes, unsigned index);
90  void SetExternalBuffer(const double* buf, unsigned nframes, unsigned index);
91 
92  bool Do();
93 
94  const char* GetClassName() const { return "AudioSource";}
95 
96  const ProcessingConfig & GetConfig() const
97  {
98  return _config;
99  }
100 
102  {
103  CopyAsConcreteConfig(_config, config);
104  unsigned sources = _config.GetNSources();
105 
106  ResizePorts(sources);
107 
108  return true;
109  }
110 
111  Ports& GetPorts() { return _ports; }
112  const Ports & GetPorts() const { 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 sources)
123  {
124  if (sources == _ports.size())
125  return;
126 
127  for (unsigned port = sources; port < _ports.size(); ++port)
128  delete _ports[port].mPort;
129 
130  unsigned oldSize = _ports.size();
131  _ports.resize(sources);
132 
133  for (unsigned port = oldSize; port < sources; ++port)
134  _ports[port] = Port(new AudioOutPort(Portname(port), this));
135  }
136 
137 
138  };
139 
140 } //namespace CLAM
141 
142 #endif