CLAM-Development  1.4.0
AudioBufferSource.cxx
Go to the documentation of this file.
1 #include "AudioBufferSource.hxx"
2 #include "ProcessingFactory.hxx"
3 #include "Audio.hxx"
4 
5 namespace CLAM
6 {
7 
8 namespace
9 {
10  static const char* metadata[] = {
11  "key", "AudioBufferSource",
12  "category", "Audio I/O",
13  "description", "AudioBufferSource",
14  "port_source_type", typeid(Audio).name(),
15  "icon", "source.svg",
16  "embedded_svg", "source.svg",
17  0
18  };
19  static FactoryRegistrator<ProcessingFactory, AudioBufferSource> reg = metadata;
20 }
21 
22 AudioBufferSource::Config::~Config() { }
23 
25 {
26  for (Ports::iterator it = _ports.begin(); it != _ports.end(); ++it)
27  {
28  Port& port = (*it);
29  OutPort<Audio>* out = port.mPort;
30 
31  CLAM::Audio& so=out->GetData();
32  CLAM_DEBUG_ASSERT(!port.mFloatBuffer || !port.mDoubleBuffer, "AudioBufferSource: Just one buffer should be set");
33  CLAM_DEBUG_ASSERT(port.mFloatBuffer || port.mDoubleBuffer, "AudioBufferSource: No external buffer set");
34  CLAM_DEBUG_ASSERT(port.mBufferSize>0, "AudioBufferSource: internal buffer size must be greater than 0");
35 
36  so.SetSize(port.mBufferSize);
37 
38  CLAM::TData * audioBuffer = so.GetBuffer().GetPtr();
39 
40  if (port.mFloatBuffer)
41  for (unsigned i=0; i<port.mBufferSize; i++)
42  audioBuffer[i] = port.mFloatBuffer[i];
43  else
44  for (unsigned i=0; i<port.mBufferSize; i++)
45  audioBuffer[i] = port.mDoubleBuffer[i];
46 
47  for (int i=port.mBufferSize; i<out->GetSize(); i++)
48  audioBuffer[i] = 0.;
49 
50  out->Produce();
51  }
52  return true;
53 }
54 
55 void AudioBufferSource::SetExternalBuffer(const float* buf, unsigned nframes, unsigned index)
56 {
57  CLAM_ASSERT(index < _ports.size(), "OutPort<Audio> index out of range");
58  Port& port = _ports[index];
59  port.mPort->SetSize(1);
60  port.mPort->SetHop(1);
61  port.mFloatBuffer = buf;
62  port.mBufferSize = nframes;
63  port.mDoubleBuffer = 0;
64 }
65 
66 void AudioBufferSource::SetExternalBuffer(const double* buf, unsigned nframes, unsigned index)
67 {
68  CLAM_ASSERT(index < _ports.size(), "OutPort<Audio> index out of range");
69  Port& port = _ports[index];
70  port.mPort->SetSize(1);
71  port.mPort->SetHop(1);
72  port.mDoubleBuffer = buf;
73  port.mBufferSize = nframes;
74  port.mFloatBuffer = 0;
75 }
76 
77 } //namespace CLAM
78