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