File: generateFilesToCompile.py

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 (293 lines) | stat: -rw-r--r-- 9,317 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/python

from xml.sax import make_parser
from xml.sax.handler import ContentHandler
import sys

class ControlPort():
	def __init__ (self):
		self.name     = ""
		self.units    = ""
		self.minimum  =	""
		self.maximum  =	""
		self.default  =	""
	def setMaximum(self,theMaximum):
		self.maximum = theMaximum
	def setMinimum(self,theMinimum):
		self.minimum = theMinimum
	def setDefault(self,theDefault):
		self.default = theDefault
	def setName(self,theName):
		self.name = theName
	def setUnits(self,theUnits):
		self.units = theUnits

class AudioPort():
	
	def __init__ (self):
		self.name     = ""
		self.numPorts = 0

	def getName(self):
		return self.name
	def setName(self,theName):
		self.name = theName
	def setNumPorts(self,theNumPorts):
		self.numPorts = int(theNumPorts)

class ExporterHandler(ContentHandler):
 	
 	def __init__ (self):
		self.inputAudio = []
		self.outputAudio = []
		self.inputControl = []
		self.outputControl = []
		self.flagInAudio = 0;
		self.flagOutAudio = 0;
		self.flagInControl = 0;
		self.flagOutControl = 0;
		self.label = ""
		self.audioAux = None	
		self.controlAux = None

	def startElement(self, name, attrs):
		self.label = name
		if name == "processing":
			if attrs.get('type') == 'AudioSink':
				self.flagOutAudio = 1
				self.audioAux = AudioPort()
				self.audioAux.setName(attrs.get('id'))				
			elif attrs.get('type') == 'AudioSource':
				self.flagInAudio = 1
				self.audioAux = AudioPort()
				self.audioAux.setName(attrs.get('id'))
			elif attrs.get('type') == 'ControlSink':
				self.flagOutControl = 1
				self.controlAux = ControlPort()
				self.controlAux.setName(attrs.get('id'))
			elif attrs.get('type') == 'ControlSource':
				self.flagInControl = 1
				self.controlAux = ControlPort()
				self.controlAux.setName(attrs.get('id'))
	
	def characters (self, ch):
		if self.label == "NSinks" or self.label == "NSources":
			self.audioAux.setNumPorts( self.audioAux.numPorts + int(ch) )
		elif self.label == "MinValue":
			self.controlAux.setMinimum( self.controlAux.minimum + ch )
		elif self.label == "MaxValue":
			self.controlAux.setMaximum( self.controlAux.maximum + ch )
		elif self.label == "DefaultValue":
			self.controlAux.setDefault( self.controlAux.default + ch )
		elif self.label == "UnitName":
			self.controlAux.setUnits( self.controlAux.units + ch )
		

	def endElement(self, name):
		self.label = ""
		if name == "processing":
			if self.flagOutAudio:
				self.outputAudio.append(self.audioAux)
				self.flagOutAudio = 0
				self.audioAux = None
			elif self.flagInAudio:
				self.inputAudio.append(self.audioAux)
				self.flagInAudio = 0
				self.audioAux = None
			elif self.flagOutControl:
				if self.controlAux.default == "":
					self.controlAux.setDefault(repr(((float(self.controlAux.maximum)-float(self.controlAux.minimum))/2)+float(self.controlAux.minimum)))
				self.outputControl.append(self.controlAux)
				self.flagOutControl = 0
				self.controlAux = None
			elif self.flagInControl:
				if self.controlAux.default == "":
					self.controlAux.setDefault(repr(((float(self.controlAux.maximum)-float(self.controlAux.minimum))/2)+float(self.controlAux.minimum)))
				self.inputControl.append(self.controlAux)
				self.flagInControl = 0
				self.controlAux = None
	
	def numTotalPorts(self):
		numTotalPorts = 0
		for inAudio in self.inputAudio:
			numTotalPorts += int(inAudio.numPorts)
		for outAudio in self.outputAudio:
			numTotalPorts += int(outAudio.numPorts)			
		numTotalPorts +=len(self.inputControl)+len(self.outputControl)
		return numTotalPorts
	
	def printTTL(self,clamnetwork,uri,name):

#TODO change the static PATH for Dynamic PATH
		f = open(name.lower()+'.ttl', 'w')
		f.write('@prefix lv2:  <http://lv2plug.in/ns/lv2core#>.\n')
		f.write('@prefix doap: <http://usefulinc.com/ns/doap#>.\n\n')
		f.write('<'+uri+'>\n')
		f.write('\ta lv2:Plugin;\n')
		f.write('\tlv2:binary <'+name.capitalize()+'.so>;\n')
  		f.write('\tdoap:name "'+name.capitalize()+'";\n')
  		f.write('\tdoap:license <http://usefulinc.com/doap/licenses/gpl>;\n\n')
  		
		f.write('\tlv2:port\n')
		
				
		index =0
		for inControl in self.inputControl:
			f.write('\t[\n')
			f.write('\t\ta lv2:ControlPort, lv2:InputPort;\n')
			f.write('\t\tlv2:index '+repr(index)+';\n')
			f.write('\t\tlv2:symbol "'+inControl.name.strip().replace(" ","_")+'";\n')
			f.write('\t\tlv2:name "'+inControl.name.strip().capitalize()+'";\n')
			f.write('\t\tlv2:default '+inControl.default+';\n')
			f.write('\t\tlv2:minimum '+inControl.minimum+';\n')
			f.write('\t\tlv2:maximum '+inControl.maximum+';\n')
			index = index+1
			if index == self.numTotalPorts():
				f.write('\t].\n')
			else:	
				f.write('\t],\n')		
		for outControl in self.outputControl:
			f.write('\t[\n')
			f.write('\t\ta lv2:ControlPort, lv2:OutputPort;\n')
			f.write('\t\tlv2:index '+repr(index)+';\n')
			f.write('\t\tlv2:symbol "'+outControl.name.strip().replace(" ","_")+'";\n')
			f.write('\t\tlv2:name "'+outControl.name.strip().capitalize()+'";\n')
			f.write('\t\tlv2:default '+outControl.default+';\n')
			f.write('\t\tlv2:minimum '+outControl.minimum+';\n')
			f.write('\t\tlv2:maximum '+outControl.maximum+';\n')
			index = index+1
			if index == self.numTotalPorts():
				f.write('\t].\n')
			else:	
				f.write('\t],\n')		
		for inAudio in self.inputAudio:
			for i in range(1,int(inAudio.numPorts)+1):
				f.write('\t[\n')
				f.write('\t\ta lv2:AudioPort, lv2:InputPort;\n')
				f.write('\t\tlv2:index '+repr(index)+';\n')
				f.write('\t\tlv2:symbol "'+(inAudio.name+"_"+repr(i)).strip().replace(" ","_")+'";\n')
				f.write('\t\tlv2:name "'+(inAudio.name+"_"+repr(i)).strip().capitalize()+'";\n')
				index = index+1
				if index == self.numTotalPorts():
					f.write('\t].\n')
				else:	
					f.write('\t],\n')	
				
		for outAudio in self.outputAudio:
			for i in range(1,int(outAudio.numPorts)+1):
				f.write('\t[\n')
				f.write('\t\ta lv2:AudioPort, lv2:OutputPort;\n')
				f.write('\t\tlv2:index '+repr(index)+';\n')
				f.write('\t\tlv2:symbol "'+(outAudio.name+"_"+repr(i)).strip().replace(" ","_")+'";\n')
				f.write('\t\tlv2:name "'+(outAudio.name+"_"+repr(i)).strip().capitalize()+'";\n')
				index = index+1
				if index == self.numTotalPorts():
					f.write('\t].\n')
				else:	
					f.write('\t],\n')	
		f.close()


def printCLAM_PLUGIN(clamnetworks,uris):
	f = open('clam_plugin.cxx', 'w')
	f.write('#include "LV2NetworkExporter.hxx"\n')
	f.write('#include "LV2Library.hxx"\n')
	f.write('#include <CLAM/EmbeddedFile.hxx>\n')	
	f.write('#include <iostream>\n\n')
	
	i=0
	for clamnetwork in clamnetworks:
		f.write('CLAM_EMBEDDED_FILE( network'+repr(i)+' ,"'+clamnetwork+'");\n')
		i=i+1
	
	f.write('\nextern "C" const LV2_Descriptor * lv2_descriptor(uint32_t index)\n')
	f.write('{\n')
	f.write('\tstatic LV2Library library;\n')

	i=0
	for uri in uris:
		f.write('\tstatic LV2NetworkExporter n'+repr(i)+'(library,network'+repr(i)+',"'+uri+'");\n')
		i=i+1
	
	f.write('\treturn library.pluginAt(index);\n')
	f.write('}\n')
	f.close()

def openConfigFile():
	networks = []
	uris = []
	names = []
	f = open('network_and_URI.config',"r")
	lines = f.readlines()
	bundle = lines[0].strip()
	for line in lines[1:]:
		network,uri,name = line.split("\t")
		network = network.strip()
		uri = uri.strip()
		name = name.strip()
		uris.append(uri)
		networks.append(network)
		names.append(name)

	f.close()
	return bundle,networks,uris,names


def printMakeFile(bundle, names):
	f = open('Makefile', 'w')
	f.write("BUNDLE = %s.lv2\n"%bundle)
	f.write("INSTALL_DIR = /usr/local/lib/lv2\n\n\n")
	f.write("$(BUNDLE): manifest.ttl ")
	for name in names:
		f.write(name.lower()+".ttl ")
	f.write(name.lower().capitalize()+".so\n")
	f.write("\trm -rf $(BUNDLE)\n")
	f.write("\tmkdir $(BUNDLE)\n")
	f.write("\tcp manifest.ttl ")
	for name in names:
		f.write(name.lower()+".ttl ")
	f.write(name.lower().capitalize()+".so $(BUNDLE)\n\n\n")

	f.write(name.lower().capitalize()+".so: LV2NetworkPlayer.cxx clam_plugin.cxx LV2NetworkExporter.cxx\n")
	f.write("\tg++ -shared -fPIC -DPIC clam_plugin.cxx LV2NetworkExporter.cxx LV2NetworkPlayer.cxx `pkg-config --cflags --libs lv2core clam_core clam_processing clam_audioio` -o "+name.lower().capitalize()+".so  -l asound\n\n")
	f.write("install: $(BUNDLE)\n\tmkdir -p $(INSTALL_DIR)\n\trm -rf $(INSTALL_DIR)/$(BUNDLE)\n\tcp -R $(BUNDLE) $(INSTALL_DIR)\n\n")
	f.write("clean:\n\trm -rf $(BUNDLE) "+ name.lower().capitalize()+".so ")
	for name in names:
		f.write(name.lower()+".ttl ")
	f.write(" clam_plugin.cxx Makefile\n")

	f.close()


def printManifest(uris,names):
	f = open('manifest.ttl', 'w')
	f.write('@prefix lv2:  <http://lv2plug.in/ns/lv2core#>.\n')
	f.write('@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n\n')

	for i in range(0,len(uris)):
		f.write("<"+uris[i]+">\n\t a lv2:Plugin;\n")
		f.write("\trdfs:seeAlso <"+names[i]+".ttl>.\n\n")
		
	f.close()

def main():
	

	bundle,clamnetworks,uris,names = openConfigFile()

	printCLAM_PLUGIN(clamnetworks,uris)

	for i in range(0,len(clamnetworks)):
		parser = make_parser()   
		curHandler = ExporterHandler()
		parser.setContentHandler(curHandler)
		parser.parse(open(clamnetworks[i]))
		curHandler.printTTL(clamnetworks[i],uris[i],names[i])
	
	printManifest(uris,names)
	printMakeFile(bundle, names)



if __name__ == '__main__' :
	main()