File: LV2NetworkPlayer.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (262 lines) | stat: -rw-r--r-- 7,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "LV2NetworkPlayer.hxx"
#include "LV2Library.hxx"

// LV2 Callbacks
extern "C"
{
	static CLAM::LV2NetworkPlayer * exporter(LV2_Handle handle)
	{
		return (CLAM::LV2NetworkPlayer*)handle;
	}

	// Construct a new plugin instance.
	static LV2_Handle Instantiate(const LV2_Descriptor * descriptor, double sampleRate, const char *path, const LV2_Feature * const* features)
	{
//		std::cout << "INSTANTIATE"<<std::endl;
		return new CLAM::LV2NetworkPlayer(descriptor);
	}

	// Destruct plugin instance
	static void CleanUp(LV2_Handle handle)
	{
//		std::cout << "CLEANUP"<<std::endl;
		delete exporter(handle);
	}

	// Run the plugin
	static void Run(LV2_Handle handle,uint32_t sampleCount)
	{
//		std::cout << "RUN" << std::endl;
		exporter(handle)->RunExporter( sampleCount );
	}
	// Activate Plugin
	static void Activate(LV2_Handle handle)
	{
//		std::cout << "ACTIVATE"<<std::endl;
		exporter(handle)->ActivateExporter();
	}
	
	static void Deactivate(LV2_Handle handle)
	{
//		std::cout << "DEACTIVATE"<<std::endl;
		exporter(handle)->DeactivateExporter();
	}

	// Connect a port to a data location.
	static void ConnectTo(LV2_Handle handle, uint32_t port,void* data)
	{
//		std::cout << "CONNECT_2"<<std::endl;
		exporter(handle)->ConnectPortExporter( port, data );
	}

/*	This function is necessary when creates the plugin without library
	LV2_SYMBOL_EXPORT
	const LV2_Descriptor *lv2_descriptor( uint32_t index)
	{
		std::cout << "CREATE_LV2_DESCRIPTOR"<<std::endl;
		
		if(index==0) {
			return LV2NetworkPlayer::CreateLV2Descriptor();		
		}
		else
			return NULL;
	}
*/
}

namespace CLAM
{

LV2NetworkPlayer::LV2NetworkPlayer(const LV2_Descriptor * descriptor)
{
	mClamBufferSize=512;
	mExternBufferSize=mClamBufferSize;

	std::istringstream xmlfile(LV2Library::getNetwork(descriptor->URI));

	try
	{
		CLAM::XmlStorage::Restore( _network, xmlfile);
	}
	catch ( CLAM::XmlStorageErr err)
	{
		std::cerr << "CLAM LV2: Error while loading CLAM network based plugin '" << descriptor->URI<< "'." <<std::endl;
		std::cerr << err.what() << std::endl;
		std::cerr << "Plugin not loaded." << std::endl;
		return;
	}

	if (_network.HasMisconfiguredProcessings())
	{
		std::cerr << "CLAM LV2: Error while configuring CLAM network based plugin '"<< descriptor->URI<< "'." <<std::endl;
		std::cerr << _network.GetConfigurationErrors() << std::endl;
		std::cerr << "Plugin not loaded." << std::endl;
		return;
	}

	if (_network.HasUnconnectedInPorts())
	{
		std::cerr << "CLAM LV2: Error loading CLAM network based plugin '" <<descriptor->URI<< "'." <<std::endl;
		std::cerr << "Plugin not loaded because internal network inports were unconnected." <<std::endl;
		std::cerr << _network.GetUnconnectedInPorts() << std::flush;
		return;
	}
	// TOFIX: Should be a full link for the network to obtain parameters 
	// but the network adquires the ownership of the player and, in this case,
	// the player owns the network.
	SetNetworkBackLink(_network);
	LocateConnections();
}

void LV2NetworkPlayer::LocateConnections()
{

//	CHECK THAT ALL VECTORS AREN'T EMPTY
//	CLAM_ASSERT( mReceiverList.empty(), "NetworkLADSPAPlugin::LocateConnections() : there are already registered input ports");
//	CLAM_ASSERT( mSenderList.empty(), "NetworkLADSPAPlugin::LocateConnections() : there are already registered output ports");
//	CLAM_ASSERT( mInControlList.empty(), "NetworkLADSPAPlugin::LocateConnections() : there are already registered controls");
//	CLAM_ASSERT( mOutControlList.empty(), "NetworkLADSPAPlugin::LocateConnections() : there are already registered controls");
	CacheSourcesAndSinks();

	_sourceBuffers.resize(GetNSources());
	for (unsigned i=0; i<GetNSources(); i++)
	{
		SetSourceFrameSize(i, mExternBufferSize);
	}
	_sinkBuffers.resize(GetNSinks());
	for (unsigned i=0; i<GetNSinks(); i++)
	{
		SetSinkFrameSize(i, mExternBufferSize);
	}

	InControlList controlSources = _network.getOrderedControlSources();
	for (CLAM::Network::ControlSources::const_iterator it=controlSources.begin(); it!=controlSources.end(); it++)
	{	//std::cout<<"INSERT CONTROL SOURCE"<<std::endl;
		LV2Info<CLAM::ControlSource> info;
		info.name = _network.GetNetworkId(*it).c_str();
		info.processing=*it;
		mInControlList.push_back(info);
	}
	_inControlBuffers.resize(mInControlList.size());

	OutControlList controlSinks  = _network.getOrderedControlSinks();
	for (CLAM::Network::ControlSinks::const_iterator it=controlSinks.begin(); it!=controlSinks.end(); it++)
	{	//std::cout<<"INSERT CONTROL SINK"<<std::endl;
		LV2Info<CLAM::ControlSink> info;
		info.name = _network.GetNetworkId( *it ).c_str();
		info.processing =*it;
		mOutControlList.push_back(info);	
	}
	_outControlBuffers.resize(mOutControlList.size());
}

LV2NetworkPlayer::~LV2NetworkPlayer(){}
void LV2NetworkPlayer::ActivateExporter()
{
	_network.Start();
}
void LV2NetworkPlayer::DeactivateExporter()
{
	_network.Stop();
}
void LV2NetworkPlayer::CleanupExporter(){}
void LV2NetworkPlayer::InstantiateExporter(){}

void LV2NetworkPlayer::ConnectPortExporter(uint32_t port, void *data)
{
	if ( port < _inControlBuffers.size() )
	{
		_inControlBuffers[port]=(float*)data;
		return;
	}
	port-=_inControlBuffers.size();
	if ( port < _outControlBuffers.size())
	{
		_outControlBuffers[port]=(float*)data;
		return;
	}
	port-=_outControlBuffers.size();
	if ( port < _sourceBuffers.size() )
	{
		_sourceBuffers[port]=(float*)data;
		return;
	}
	port-=_sourceBuffers.size();
	if ( port < _sinkBuffers.size() )
	{
		_sinkBuffers[port]=(float*)data;
		return;
	}
	CLAM_ASSERT(true,"Accessing a non-existing port");
}
	
void LV2NetworkPlayer::UpdatePortFrameAndHopSize()
{	
	for (unsigned i=0; i<GetNSources(); i++)
		SetSourceFrameSize(i, mExternBufferSize);
	for (unsigned i=0; i<GetNSinks(); i++)
		SetSinkFrameSize(i, mExternBufferSize);
}

void LV2NetworkPlayer::SetAudioSourceBuffers(const unsigned long nframes)
{
	for (unsigned i=0; i<GetNSources(); i++)
		SetSourceBuffer(i,_sourceBuffers[i], nframes);
}

void LV2NetworkPlayer::SetAudioSinkBuffers(const unsigned long nframes)
{
	for (unsigned i=0; i<GetNSinks(); i++)
		SetSinkBuffer(i,_sinkBuffers[i], nframes);
}

void LV2NetworkPlayer::ProcessInControlValues()
{
	for (LV2InControlList::iterator it=mInControlList.begin(); it!=mInControlList.end(); it++)
		it->processing->Do( (float) *(it->dataBuffer) );
}

void LV2NetworkPlayer::ProcessOutControlValues()
{

}	


void LV2NetworkPlayer::RunExporter(uint32_t nframes)
{
	if(nframes != mExternBufferSize)
	{
		mExternBufferSize=nframes;
		if(nframes == 0) return;		
		UpdatePortFrameAndHopSize();
	}
	ProcessInControlValues();
	SetAudioSourceBuffers(nframes);
	SetAudioSinkBuffers(nframes);  		
	_network.Do();
	ProcessOutControlValues();
}

//TODO Delete the networkXmlContent
LV2_Descriptor * LV2NetworkPlayer::CreateLV2Descriptor(
	const std::string & networkXmlContent,
	const std::string & uri
	)
{
	// HERE WRITE THE RDF	

	LV2_Descriptor * descriptor = new LV2_Descriptor;
	
	descriptor->URI 		= LV2Library::dupstr(uri.c_str());
	descriptor->extension_data	= NULL;
	descriptor->instantiate = ::Instantiate;
	descriptor->connect_port = ::ConnectTo;
	descriptor->activate = ::Activate;
	descriptor->run = ::Run;
	descriptor->deactivate = Deactivate;
	descriptor->cleanup = ::CleanUp;

	return descriptor;
}

}