-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAudioCurve.py
executable file
·348 lines (283 loc) · 10.8 KB
/
AudioCurve.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -*- coding: utf-8 -*-
#
# AudioCurve example Python plugin for Natron.
#
# Based on work done by rcspam (https://github.com/rcspam/audio2ascii).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import NatronEngine
# extra lib added
import os, time, tempfile
from os import path
def getPluginID():
return "AudioCurve"
def getLabel():
return "AudioCurve"
def getVersion():
return 1
def getIconPath():
return "AudioCurve.png"
def getGrouping():
return "Read"
def getDescription():
return "Audio curve generator for Natron.\n\nWritten by @olear and @rcspam.\nPowered by SoX."
# extra defs added
def audioCurve(audioFileATA, asciiFileATA, dimATA, fpsATA, durationATA, xHeightATA, yHeightATA):
ret_a2a = os.system(str(os.path.dirname(os.path.realpath(__file__)))+"/AudioCurve -input \""+str(audioFileATA)+"\" -output \""+str(asciiFileATA)+"\" -"+str(dimATA)+" -fps "+str(fpsATA)+" -frames "+str(durationATA)+" -cX "+str(xHeightATA)+" -cY "+str(yHeightATA));
return ret_a2a
def animCurves(thisParam, fileAC, dimAC, durationAC ,frameStartAC):
# ascii file
asciiAC = open(fileAC, "r")
# end frame
lineAC = int(durationAC) + int(frameStartAC)
# anim x
if dimAC == 0:
# reset x before recalculate
thisParam.removeAnimation(0)
for frameC in range(int(frameStartAC),lineAC + int(frameStartAC)):
x = asciiAC.readline()
thisParam.setValueAtTime(float(x), frameC, 0)
# anim y
elif dimAC == 1:
# reset y before recalculate
thisParam.removeAnimation(1)
for frameC in range(int(frameStartAC),lineAC):
y = asciiAC.readline()
thisParam.setValueAtTime(float(y), frameC, 1)
# anim yx
else:
# reset x and y before recalculate
thisParam.removeAnimation(0)
thisParam.removeAnimation(1)
for frameC in range(int(frameStartAC),lineAC):
x, y = asciiAC.readline().split ("_")
thisParam.setValueAtTime(float(x), frameC, 0)
thisParam.setValueAtTime(float(y), frameC, 1)
def paramHasChanged(thisParam, thisNode, thisGroup, app, userEdited):
# audio input file
audio_file = thisNode.inputFile.get()
# ascii output file
ascii_file = thisNode.curveFile.get()
# convert dimension in comprehensive thing for audio2ascii script
dim = thisNode.dimEnsion.get()
if dim == 0:
dimension = "x"
elif dim == 1:
dimension = "y"
elif dim == 2:
dimension = "xy"
# Import Curve
if ascii_file is not None and audio_file is not None and thisParam == thisNode.importCurve:
ret_exec = audioCurve(audio_file, ascii_file, dimension, thisNode.framesPerSec.get(), thisNode.duraTion.get(), thisNode.xHeight.get(), thisNode.yHeight.get())
# test and wait end of audio2ascii
if ret_exec == 0:
# calculate animation
animCurves(thisNode.curveIn, ascii_file, thisNode.dimEnsion.get(), thisNode.duraTion.get(), thisNode.atFrameNum.get())
## / extra defs
def createInstance(app,group):
#Create all nodes in the group
lastNode = app.createNode("fr.inria.built-in.Output", 1, group)
lastNode.setScriptName("Output1")
lastNode.setLabel("Output1")
lastNode.setPosition(758.75, 325.125)
lastNode.setSize(104, 44)
lastNode.setColor(0.699992, 0.699992, 0.699992)
groupOutput1 = lastNode
param = lastNode.getParam("Output_layer_name")
if param is not None:
param.setValue("RGBA")
param.setVisible(False)
del param
param = lastNode.getParam("highDefUpstream")
if param is not None:
param.setVisible(False)
del param
del lastNode
lastNode = app.createNode("fr.inria.built-in.Input", 1, group)
lastNode.setScriptName("Input1")
lastNode.setLabel("Input1")
lastNode.setPosition(758.75, 161.125)
lastNode.setSize(104, 44)
lastNode.setColor(0.300008, 0.500008, 0.2)
groupInput1 = lastNode
param = lastNode.getParam("Output_layer_name")
if param is not None:
param.setValue("RGBA")
param.setVisible(False)
del param
param = lastNode.getParam("highDefUpstream")
if param is not None:
param.setVisible(False)
del param
del lastNode
#Create the parameters of the group node the same way we did for all internal nodes
lastNode = group
param = lastNode.getParam("highDefUpstream")
if param is not None:
param.setVisible(False)
del param
param = lastNode.getParam("onParamChanged")
if param is not None:
param.setValue("AudioCurve.paramHasChanged")
del param
#Create the user-parameters
lastNode.userNatron = lastNode.createPageParam("userNatron", "Settings")
param = lastNode.createFileParam("inputFile", "Audio")
param.setSequenceEnabled(False)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("")
param.setAddNewLine(True)
param.setAnimationEnabled(False)
lastNode.inputFile = param
del param
param = lastNode.createFileParam("curveFile", "CurveFile")
param.setSequenceEnabled(False)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("")
param.setVisible(False)
param.setAddNewLine(True)
param.setAnimationEnabled(False)
param.setDefaultValue(os.path.join(tempfile.gettempdir(),"audiocurve"))
lastNode.curveFile = param
del param
param = lastNode.createChoiceParam("dimEnsion", "Dimension")
entries = [ ("x", ""),
("y", ""),
("xy", "")]
param.setOptions(entries)
del entries
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Dimension curve X/Y (left/right)")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
options = param.getOptions()
foundOption = False
for i in range(len(options)):
if options[i] == "x":
param.setValue(i)
foundOption = True
break
if not foundOption:
app.writeToScriptEditor("Could not set option for parameter dimension on node AudioCurve")
lastNode.dimEnsion = param
del param
param = lastNode.createDoubleParam("framesPerSec", "FPS")
param.setDisplayMinimum(0, 0)
param.setDisplayMaximum(100, 0)
param.setDefaultValue(24, 0)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Calculate curve with this frame rate")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.framesPerSec = param
del param
param = lastNode.createIntParam("duraTion", "Frames")
param.setDisplayMinimum(0, 0)
param.setDisplayMaximum(10000, 0)
param.setDefaultValue(240, 0)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Duration in frames")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.duraTion = param
del param
param = lastNode.createIntParam("xHeight", "Height X")
param.setDisplayMinimum(0, 0)
param.setDisplayMaximum(500, 0)
param.setDefaultValue(100, 0)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Height of X deviation in pixels")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.xHeight = param
del param
param = lastNode.createIntParam("yHeight", "Height Y")
param.setDisplayMinimum(0, 0)
param.setDisplayMaximum(500, 0)
param.setDefaultValue(100, 0)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Height of Y deviation in pixels")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.yHeight = param
del param
param = lastNode.createIntParam("atFrameNum", "Start at frame")
param.setDisplayMinimum(1, 0)
param.setDisplayMaximum(500, 0)
param.setDefaultValue(1, 0)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Start the curve at this frame")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.atFrameNum = param
del param
param = lastNode.createButtonParam("importCurve", "Generate curve(s)")
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Generate curve from parameters")
param.setAddNewLine(True)
param.setPersistant(False)
param.setEvaluateOnChange(False)
lastNode.importCurve = param
del param
param = lastNode.createDouble2DParam("curveIn", "Curve ")
param.setMinimum(-2.14748e+09, 0)
param.setMaximum(2.14748e+09, 0)
param.setDisplayMinimum(0, 0)
param.setDisplayMaximum(500, 0)
param.setMinimum(-2.14748e+09, 1)
param.setMaximum(2.14748e+09, 1)
param.setDisplayMinimum(0, 1)
param.setDisplayMaximum(500, 1)
#Add the param to the page
lastNode.userNatron.addParam(param)
#Set param properties
param.setHelp("Curve X/Y result")
param.setAddNewLine(True)
param.setAnimationEnabled(True)
lastNode.curveIn = param
del param
# extra callback added
app.AudioCurve1.onParamChanged.set("AudioCurve.paramHasChanged")
#Refresh the GUI with the newly created parameters
lastNode.refreshUserParamsGUI()
del lastNode
#Now that all nodes are created we can connect them together, restore expressions
groupOutput1.connectInput(0, groupInput1)