run_thread.gd 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. extends Node
  2. var control_script
  3. var progress_label
  4. var progress_bar
  5. var graph_edit
  6. var console_output
  7. var progress_window
  8. var console_window
  9. var process_successful #tracks if the last run process was successful
  10. var process_info = {} #tracks the data of the currently running process
  11. var process_running := false #tracks if a process is currently running
  12. var process_cancelled = false #checks if the currently running process has been cancelled
  13. var final_output_dir
  14. # Called when the node enters the scene tree for the first time.
  15. func _ready() -> void:
  16. pass
  17. func init(main_node: Node, progresswindow: Window, progresslabel: Label, progressbar: ProgressBar, graphedit: GraphEdit, consolewindow: Window, consoleoutput: RichTextLabel) -> void:
  18. control_script = main_node
  19. progress_window = progresswindow
  20. progress_label = progresslabel
  21. progress_bar = progressbar
  22. graph_edit = graphedit
  23. console_window = consolewindow
  24. console_output = consoleoutput
  25. func run_thread_with_branches():
  26. process_cancelled = false
  27. process_successful = true
  28. progress_bar.value = 0
  29. progress_label.text = "Initialising Inputs"
  30. # Detect platform: Determine if the OS is Windows
  31. var is_windows := OS.get_name() == "Windows"
  32. # Choose appropriate commands based on OS
  33. var delete_cmd = "del" if is_windows else "rm"
  34. var rename_cmd = "ren" if is_windows else "mv"
  35. var path_sep := "/" # Always use forward slash for paths
  36. # Get all node connections in the GraphEdit
  37. var connections = graph_edit.get_connection_list()
  38. # Prepare data structures for graph traversal
  39. var graph = {} # forward adjacency list
  40. var reverse_graph = {} # reverse adjacency list (for input lookup)
  41. var indegree = {} # used for topological sort
  42. var all_nodes = {} # map of node name -> GraphNode reference
  43. #store input nodes for sample rate and stereo matching
  44. var input_nodes = []
  45. var nodes_with_sample_rates = []
  46. var processing_sample_rate = 0 #sample rate that processing is being done at after input file is normalised, if this stays at 0 only synthesis exists in thread and highest value from that should be used
  47. var processing_bit_depth = 1 #stores the file type and bit-depth in the format used by the copysfx cdp function 1: 16-bit 2: 32-bit int 3: 32-bit float 4: 24-bit
  48. var intermediate_files = [] # Files to delete later
  49. var breakfiles = [] #breakfiles to delete later
  50. log_console("Mapping thread.", true)
  51. await get_tree().process_frame # Let UI update
  52. #Step 0: check thread is valid
  53. var is_valid = path_exists_through_all_nodes()
  54. if is_valid == false:
  55. log_console("[color=#9c2828][b]Error: Valid Thread not found[/b][/color]", true)
  56. log_console("Threads must contain at least one processing node and a valid path from the Input File or Synthesis node to the Output File.", true)
  57. await get_tree().process_frame # Let UI update
  58. if progress_window.visible:
  59. progress_window.hide()
  60. if !console_window.visible:
  61. console_window.popup_centered()
  62. return
  63. else:
  64. log_console("[color=#638382][b]Valid Thread found[/b][/color]", true)
  65. await get_tree().process_frame # Let UI update
  66. # Step 1: Gather nodes from the GraphEdit
  67. var inputcount = 0 # used for tracking the number of input nodes and trims on input files for progress bar
  68. for child in graph_edit.get_children():
  69. if child is GraphNode:
  70. var includenode = true
  71. var name = str(child.name)
  72. all_nodes[name] = child
  73. if child.has_meta("utility"):
  74. includenode = false
  75. else:
  76. #check if node has inputs
  77. if child.get_input_port_count() > 0:
  78. #if it does scan through those inputs
  79. for i in range(child.get_input_port_count()):
  80. #check if it can find any valid connections
  81. var connected = false
  82. for conn in connections:
  83. if conn["to_node"] == name and conn["to_port"] == i:
  84. connected = true
  85. break
  86. #if no valid connections are found break the for loop to skip checking other inputs and set include to false
  87. if connected == false:
  88. log_console(name + " input is not connected, skipping node.", true)
  89. includenode = false
  90. break
  91. #check if node has outputs
  92. if child.get_output_port_count() > 0:
  93. #if it does scan through those outputs
  94. for i in range(child.get_output_port_count()):
  95. #check if it can find any valid connections
  96. var connected = false
  97. for conn in connections:
  98. if conn["from_node"] == name and conn["from_port"] == i:
  99. connected = true
  100. break
  101. #if no valid connections are found break the for loop to skip checking other inputs and set include to false
  102. if connected == false:
  103. log_console(name + " output is not connected, skipping node.", true)
  104. includenode = false
  105. break
  106. if includenode == true:
  107. graph[name] = []
  108. reverse_graph[name] = []
  109. indegree[name] = 0 # Start with zero incoming edges
  110. if child.get_meta("command") == "inputfile":
  111. inputcount -= 1
  112. input_nodes.append(child)
  113. if child.get_node("AudioPlayer").get_meta("trimfile"):
  114. inputcount += 1
  115. #check if node has internal sample rate, e.g. synthesis nodes and add to array for checking if this is set correctly
  116. if child.has_meta("node_sets_sample_rate") and child.get_meta("node_sets_sample_rate") == true:
  117. nodes_with_sample_rates.append(child)
  118. #do calculations for progress bar
  119. var progress_step
  120. progress_step = 100 / (graph.size() + 3 + inputcount)
  121. #check if input file sample rates and bit depths match
  122. if input_nodes.size() > 1:
  123. var match_input_files = await match_input_file_sample_rates_and_bit_depths(input_nodes)
  124. var stereo = []
  125. if control_script.delete_intermediate_outputs:
  126. for f in match_input_files[0]:
  127. intermediate_files.append(f)
  128. processing_sample_rate = match_input_files[1]
  129. processing_bit_depth = match_input_files[2]
  130. elif input_nodes.size() == 1:
  131. #reset upsampled if it has previously been set on this node
  132. input_nodes[0].get_node("AudioPlayer").set_meta("upsampled", false)
  133. #get sample rate and bit-depth so that any synthesis nodes can have the correct sample rate set
  134. processing_sample_rate = input_nodes[0].get_node("AudioPlayer").get_meta("sample_rate")
  135. var soundfile_properties = get_soundfile_properties(input_nodes[0].get_node("AudioPlayer").get_meta("inputfile"))
  136. processing_bit_depth = classify_format(soundfile_properties["format"], soundfile_properties["bitdepth"])
  137. #check if the sample rate of synthesis nodes match and if they match any files in the input file nodes
  138. if (nodes_with_sample_rates.size() > 0 and input_nodes.size() > 0) or nodes_with_sample_rates.size() > 1:
  139. var sythesis_sample_rates = []
  140. var highest_synthesis_sample_rate
  141. var final_synthesis_sample_rate
  142. var change_synthesis_sample_rate
  143. for node in nodes_with_sample_rates:
  144. #get the sample rate from the meta and add to an array
  145. var sample_rate_option_button = node.get_node("samplerate")
  146. sythesis_sample_rates.append(int(sample_rate_option_button.get_item_text(sample_rate_option_button.selected)))
  147. #Check if all sample rates are the same
  148. if sythesis_sample_rates.all(func(v): return v == sythesis_sample_rates[0]):
  149. highest_synthesis_sample_rate = sythesis_sample_rates[0]
  150. if processing_sample_rate != 0 and processing_sample_rate != highest_synthesis_sample_rate:
  151. change_synthesis_sample_rate = true
  152. final_synthesis_sample_rate = processing_sample_rate
  153. else:
  154. #if not find the highest sample rate
  155. change_synthesis_sample_rate = true
  156. highest_synthesis_sample_rate = sythesis_sample_rates.max()
  157. if processing_sample_rate != 0 and processing_sample_rate != highest_synthesis_sample_rate:
  158. final_synthesis_sample_rate = processing_sample_rate
  159. else:
  160. final_synthesis_sample_rate = highest_synthesis_sample_rate
  161. if change_synthesis_sample_rate:
  162. log_console("Sample rate in synthesis nodes do not match the rest of the thread. Adjusting values to " + str(final_synthesis_sample_rate) + "Hz", true)
  163. for node in nodes_with_sample_rates:
  164. #get the sample rate from the meta and add to an array
  165. node.get_node("samplerate").set_meta("adjusted_sample_rate", true)
  166. node.get_node("samplerate").set_meta("new_sample_rate", final_synthesis_sample_rate)
  167. # Step 2: Build graph relationships from connections
  168. if process_cancelled:
  169. progress_label.text = "Thread Stopped"
  170. log_console("[b]Thread Stopped[/b]", true)
  171. return
  172. else:
  173. progress_label.text = "Building Thread"
  174. for conn in connections:
  175. var from = str(conn["from_node"])
  176. var to = str(conn["to_node"])
  177. if graph.has(from) and graph.has(to):
  178. graph[from].append(to)
  179. reverse_graph[to].append(from)
  180. indegree[to] += 1 # Count incoming edges
  181. # check for loops
  182. var has_cycle := detect_cycles(graph, {}) # pass loop_nodes list later
  183. if has_cycle:
  184. log_console("[color=#9c2828][b]Error: Thread not valid, Threads cannot contain loops.[/b][/color]", true)
  185. if progress_window.visible:
  186. progress_window.hide()
  187. if !console_window.visible:
  188. console_window.popup_centered()
  189. return
  190. # Step 3: Topological sort to get execution order
  191. var sorted = [] # Sorted list of node names
  192. var queue = [] # Queue of nodes with 0 indegree
  193. for node in graph.keys():
  194. if indegree[node] == 0:
  195. queue.append(node)
  196. while not queue.is_empty():
  197. var current = queue.pop_front()
  198. sorted.append(current)
  199. for neighbor in graph[current]:
  200. indegree[neighbor] -= 1
  201. if indegree[neighbor] == 0:
  202. queue.append(neighbor)
  203. # If not all nodes were processed, there's a cycle
  204. #if sorted.size() != graph.size():
  205. #log_console("[color=#9c2828][b]Error: Thread not valid[/b][/color]", true)
  206. #log_console("Threads cannot contain loops.", true)
  207. #return
  208. progress_bar.value = progress_step
  209. # Step 4: Start processing audio
  210. # Dictionary to keep track of each node's output file
  211. var output_files = {}
  212. var process_count = 0
  213. #var current_infile
  214. # Iterate over the processing nodes in topological order
  215. for node_name in sorted:
  216. var node = all_nodes[node_name]
  217. if process_cancelled:
  218. progress_label.text = "Thread Stopped"
  219. log_console("[b]Thread Stopped[/b]", true)
  220. break
  221. else:
  222. progress_label.text = "Running process: " + node.get_title()
  223. # Find upstream nodes connected to the current node
  224. # Build an array of all inlet connections
  225. var input_connections := []
  226. for conn in connections:
  227. if conn["to_node"] == node_name:
  228. input_connections.append(conn)
  229. input_connections.sort_custom(func(a, b): return a["to_port"] < b["to_port"])
  230. #build a dictionary with all inputs sorted by inlet number
  231. var inlet_inputs = {}
  232. for conn in input_connections:
  233. var inlet_idx = conn["to_port"]
  234. var upstream_node = conn["from_node"]
  235. if output_files.has(upstream_node):
  236. if not inlet_inputs.has(inlet_idx):
  237. inlet_inputs[inlet_idx] = []
  238. inlet_inputs[inlet_idx].append(output_files[upstream_node])
  239. # Merge inputs if inlet has more than one input and build infile dictionary
  240. var current_infiles = {} #dictionary to store input files by inlet number
  241. for inlet_idx in inlet_inputs.keys():
  242. var files = inlet_inputs[inlet_idx]
  243. if files.size() > 1: #if more than one file mix them together
  244. var runmerge = await merge_many_files(inlet_idx, process_count, files)
  245. var merge_output = runmerge[0] #mixed output file name
  246. var converted_files = runmerge[1] #intermediate files created from merge
  247. current_infiles[inlet_idx] = merge_output #input filename added to dictionary sorted by inlet number
  248. #add intermediate files to delete list if toggled
  249. if control_script.delete_intermediate_outputs:
  250. intermediate_files.append(merge_output)
  251. for f in converted_files:
  252. intermediate_files.append(f)
  253. elif files.size() == 1:
  254. current_infiles[inlet_idx] = files[0] #only one file, do not merge add to dictionary
  255. #if the dictionary has more than one entry there is more than one inlet and files need to be matched
  256. #however this should only be done to nodes with audio files rather than pvoc nodes
  257. if current_infiles.size() > 1 and node.get_slot_type_left(0) == 0:
  258. #check all files in dictionary have the same sample rate and channel count and fix if not
  259. var all_files = current_infiles.values()
  260. var match_channels = await match_file_channels(0, process_count, all_files)
  261. var matched_files = match_channels[0]
  262. #add intermediate files
  263. if control_script.delete_intermediate_outputs:
  264. for f in match_channels[1]:
  265. intermediate_files.append(f)
  266. #replace files in dictionary with matched files
  267. var idx = 0
  268. for key in current_infiles.keys():
  269. current_infiles[key] = matched_files[idx]
  270. idx += 1
  271. #check if node is some form of input node
  272. if node.get_input_port_count() == 0:
  273. if node.get_meta("command") == "inputfile":
  274. var loadedfile
  275. #get the inputfile from the nodes meta
  276. if node.get_node("AudioPlayer").get_meta("upsampled"):
  277. loadedfile = node.get_node("AudioPlayer").get_meta("upsampled_file")
  278. else:
  279. loadedfile = node.get_node("AudioPlayer").get_meta("inputfile")
  280. #get wether trim has been enabled
  281. var trimfile = node.get_node("AudioPlayer").get_meta("trimfile")
  282. #if trim is enabled trim the file
  283. if trimfile == true:
  284. #get the start and end points
  285. var start = node.get_node("AudioPlayer").get_meta("trimpoints")[0]
  286. var end = node.get_node("AudioPlayer").get_meta("trimpoints")[1]
  287. if process_cancelled:
  288. #exit out of process if cancelled
  289. progress_label.text = "Thread Stopped"
  290. log_console("[b]Thread Stopped[/b]", true)
  291. return
  292. else:
  293. progress_label.text = "Trimming input audio"
  294. await run_command(control_script.cdpprogs_location + "/sfedit", ["cut", "1", loadedfile, "%s_%d_input_trim.wav" % [Global.outfile, process_count], str(start), str(end)])
  295. output_files[node_name] = "%s_%d_input_trim.wav" % [Global.outfile, process_count]
  296. # Mark trimmed file for cleanup if needed
  297. if control_script.delete_intermediate_outputs:
  298. intermediate_files.append("%s_%d_input_trim.wav" % [Global.outfile, process_count])
  299. progress_bar.value += progress_step
  300. else:
  301. #if trim not enabled pass the loaded file
  302. output_files[node_name] = loadedfile
  303. process_count += 1
  304. else: #not an audio file must be synthesis
  305. var slider_data = _get_slider_values_ordered(node)
  306. var makeprocess = await make_process(node, process_count, [], slider_data)
  307. # run the command
  308. await run_command(makeprocess[0], makeprocess[3])
  309. await get_tree().process_frame
  310. var output_file = makeprocess[1]
  311. #check if bitdepth matches other files in thread and convert if needed
  312. var soundfile_properties = get_soundfile_properties(output_file)
  313. if processing_bit_depth != classify_format(soundfile_properties["format"], soundfile_properties["bitdepth"]):
  314. var bit_convert_output = output_file.get_basename() + "_bit_depth_convert.wav"
  315. await run_command(control_script.cdpprogs_location + "/copysfx", ["-h0", "-s" + str(processing_bit_depth), output_file, bit_convert_output])
  316. #store converted output file path for this node
  317. output_files[node_name] = bit_convert_output
  318. #mark for cleanup if needed
  319. if control_script.delete_intermediate_outputs:
  320. intermediate_files.append(bit_convert_output)
  321. else:
  322. # Store original output file path for this node
  323. output_files[node_name] = output_file
  324. # Mark file for cleanup if needed
  325. if control_script.delete_intermediate_outputs:
  326. for file in makeprocess[2]:
  327. breakfiles.append(file)
  328. intermediate_files.append(output_file)
  329. process_count += 1
  330. else:
  331. # Build the command for the current node's audio processing
  332. var slider_data = _get_slider_values_ordered(node)
  333. if node.get_slot_type_right(0) == 1: #detect if process outputs pvoc data
  334. if is_pvoc_stereo(current_infiles): #check if infiles contain an array meaning at least one input pvoc process has be processed in dual mono mode
  335. var split_files = await process_dual_mono_pvoc(current_infiles, node, process_count, slider_data)
  336. var pvoc_stereo_files = split_files[0]
  337. # Mark file for cleanup if needed
  338. if control_script.delete_intermediate_outputs:
  339. for file in split_files[1]:
  340. breakfiles.append(file)
  341. for file in pvoc_stereo_files:
  342. intermediate_files.append(file)
  343. process_count += 1
  344. output_files[node_name] = pvoc_stereo_files
  345. else:
  346. var input_stereo = await is_stereo(current_infiles.values()[0])
  347. if input_stereo == true:
  348. #audio file is stereo and needs to be split for pvoc processing
  349. var pvoc_stereo_files = []
  350. ##Split stereo to c1/c2 and process
  351. var split_files = await stereo_split_and_process(current_infiles.values(), node, process_count, slider_data)
  352. pvoc_stereo_files = split_files[0]
  353. # Mark file for cleanup if needed
  354. if control_script.delete_intermediate_outputs:
  355. for file in split_files[1]:
  356. breakfiles.append(file)
  357. for file in pvoc_stereo_files:
  358. intermediate_files.append(file)
  359. #Delete c1 and c2 because they can be in the wrong folder and if the same infile is used more than once
  360. #with this stereo process CDP will throw errors in the console even though its fine
  361. var files_to_delete = split_files[2] + split_files[3]
  362. for file in files_to_delete:
  363. if is_windows:
  364. file = file.replace("/", "\\")
  365. await run_command(delete_cmd, [file])
  366. #advance process count to match the advancement in the stereo_split_and_process function
  367. process_count += 1
  368. # Store output file path for this node
  369. output_files[node_name] = pvoc_stereo_files
  370. else:
  371. #input file is mono run through process
  372. var makeprocess = await make_process(node, process_count, current_infiles.values(), slider_data)
  373. # run the command
  374. await run_command(makeprocess[0], makeprocess[3])
  375. await get_tree().process_frame
  376. var output_file = makeprocess[1]
  377. # Store output file path for this node
  378. output_files[node_name] = output_file
  379. # Mark file for cleanup if needed
  380. if control_script.delete_intermediate_outputs:
  381. for file in makeprocess[2]:
  382. breakfiles.append(file)
  383. intermediate_files.append(output_file)
  384. # Increase the process step count
  385. process_count += 1
  386. else:
  387. #Process outputs audio
  388. #check if this is the last pvoc process in a stereo processing chain and check if infile is an array meaning that the last pvoc process was run in dual mono mode
  389. if node.get_meta("command") == "pvoc_synth" and is_pvoc_stereo(current_infiles):
  390. var split_files = await process_dual_mono_pvoc(current_infiles, node, process_count, slider_data)
  391. var pvoc_stereo_files = split_files[0]
  392. # Mark file for cleanup if needed
  393. if control_script.delete_intermediate_outputs:
  394. for file in split_files[1]:
  395. breakfiles.append(file)
  396. for file in pvoc_stereo_files:
  397. intermediate_files.append(file)
  398. process_count += 1
  399. #interleave left and right
  400. var output_file = Global.outfile.get_basename() + str(process_count) + "_interleaved.wav"
  401. await run_command(control_script.cdpprogs_location + "/submix", ["interleave", pvoc_stereo_files[0], pvoc_stereo_files[1], output_file])
  402. # Store output file path for this node
  403. output_files[node_name] = output_file
  404. # Mark file for cleanup if needed
  405. if control_script.delete_intermediate_outputs:
  406. intermediate_files.append(output_file)
  407. elif node.get_meta("command") == "preview":
  408. var preview_audioplayer = node.get_child(1)
  409. var preview_file = current_infiles.values()[0]
  410. preview_audioplayer._on_file_selected(preview_file)
  411. if preview_file in intermediate_files:
  412. intermediate_files.erase(preview_file)
  413. else:
  414. #Detect if input file is mono or stereo
  415. var input_stereo = await is_stereo(current_infiles.values()[0])
  416. #var input_stereo = true #bypassing stereo check just for testing need to reimplement
  417. if input_stereo == true:
  418. if node.get_meta("stereo_input") == true: #audio file is stereo and process is stereo, run file through process
  419. #current_infile = current_infiles.values()
  420. var makeprocess = await make_process(node, process_count, current_infiles.values(), slider_data)
  421. # run the command
  422. await run_command(makeprocess[0], makeprocess[3])
  423. await get_tree().process_frame
  424. var output_file = makeprocess[1]
  425. # Store output file path for this node
  426. output_files[node_name] = output_file
  427. # Mark file for cleanup if needed
  428. if control_script.delete_intermediate_outputs:
  429. for file in makeprocess[2]:
  430. breakfiles.append(file)
  431. intermediate_files.append(output_file)
  432. else: #audio file is stereo and process is mono, split stereo, process and recombine
  433. ##Split stereo to c1/c2 and process
  434. var split_files = await stereo_split_and_process(current_infiles.values(), node, process_count, slider_data)
  435. var dual_mono_output = split_files[0]
  436. # Mark file for cleanup if needed
  437. if control_script.delete_intermediate_outputs:
  438. for file in split_files[1]:
  439. breakfiles.append(file)
  440. for file in dual_mono_output:
  441. intermediate_files.append(file)
  442. #Delete c1 and c2 because they can be in the wrong folder and if the same infile is used more than once
  443. #with this stereo process CDP will throw errors in the console even though its fine
  444. var files_to_delete = split_files[2] + split_files[3]
  445. for file in files_to_delete:
  446. if is_windows:
  447. file = file.replace("/", "\\")
  448. await run_command(delete_cmd, [file])
  449. #advance process count to match the advancement in the stereo_split_and_process function
  450. process_count += 1
  451. var output_file = Global.outfile.get_basename() + str(process_count) + "_interleaved.wav"
  452. await run_command(control_script.cdpprogs_location + "/submix", ["interleave", dual_mono_output[0], dual_mono_output[1], output_file])
  453. # Store output file path for this node
  454. output_files[node_name] = output_file
  455. # Mark file for cleanup if needed
  456. if control_script.delete_intermediate_outputs:
  457. intermediate_files.append(output_file)
  458. else: #audio file is mono, run through the process
  459. var makeprocess = await make_process(node, process_count, current_infiles.values(), slider_data)
  460. # run the command
  461. await run_command(makeprocess[0], makeprocess[3])
  462. await get_tree().process_frame
  463. var output_file = makeprocess[1]
  464. # Store output file path for this node
  465. output_files[node_name] = output_file
  466. # Mark file for cleanup if needed
  467. if control_script.delete_intermediate_outputs:
  468. for file in makeprocess[2]:
  469. breakfiles.append(file)
  470. intermediate_files.append(output_file)
  471. # Increase the process step count
  472. process_count += 1
  473. progress_bar.value += progress_step
  474. # FINAL OUTPUT STAGE
  475. # Collect all nodes that are connected to the outputfile node
  476. if process_cancelled:
  477. progress_label.text = "Thread Stopped"
  478. log_console("[b]Thread Stopped[/b]", true)
  479. return
  480. else:
  481. progress_label.text = "Finalising output"
  482. var output_inputs := []
  483. for conn in connections:
  484. var to_node = str(conn["to_node"])
  485. if all_nodes.has(to_node) and all_nodes[to_node].get_meta("command") == "outputfile":
  486. output_inputs.append(str(conn["from_node"]))
  487. # List to hold the final output files to be merged (if needed)
  488. var final_outputs := []
  489. for node_name in output_inputs:
  490. if output_files.has(node_name):
  491. final_outputs.append(output_files[node_name])
  492. # If multiple outputs go to the outputfile node, merge them
  493. if final_outputs.size() > 1:
  494. var runmerge = await merge_many_files(0, process_count, final_outputs)
  495. final_output_dir = runmerge[0]
  496. var converted_files = runmerge[1]
  497. if control_script.delete_intermediate_outputs:
  498. for f in converted_files:
  499. intermediate_files.append(f)
  500. # Only one output, no merge needed
  501. elif final_outputs.size() == 1:
  502. var single_output = final_outputs[0]
  503. final_output_dir = single_output
  504. intermediate_files.erase(single_output)
  505. progress_bar.value += progress_step
  506. # CLEANUP: Delete intermediate files after processing, rename final output and reset upsampling meta
  507. if process_cancelled:
  508. progress_label.text = "Thread Stopped"
  509. log_console("[b]Thread Stopped[/b]", true)
  510. return
  511. else:
  512. log_console("Cleaning up intermediate files.", true)
  513. progress_label.text = "Cleaning up"
  514. for file_path in intermediate_files:
  515. # Adjust file path format for Windows if needed
  516. var fixed_path = file_path
  517. if is_windows:
  518. fixed_path = fixed_path.replace("/", "\\")
  519. await run_command(delete_cmd, [fixed_path])
  520. await get_tree().process_frame
  521. #delete break files
  522. for file_path in breakfiles:
  523. # Adjust file path format for Windows if needed
  524. var fixed_path = file_path
  525. if is_windows:
  526. fixed_path = fixed_path.replace("/", "\\")
  527. await run_command(delete_cmd, [fixed_path])
  528. await get_tree().process_frame
  529. var final_filename = "%s.wav" % Global.outfile
  530. var final_output_dir_fixed_path = final_output_dir
  531. if is_windows:
  532. final_output_dir_fixed_path = final_output_dir_fixed_path.replace("/", "\\")
  533. await run_command(rename_cmd, [final_output_dir_fixed_path, final_filename.get_file()])
  534. else:
  535. await run_command(rename_cmd, [final_output_dir_fixed_path, "%s.wav" % Global.outfile])
  536. final_output_dir = Global.outfile + ".wav"
  537. control_script.output_audio_player.play_outfile(final_output_dir)
  538. Global.cdpoutput = final_output_dir
  539. progress_bar.value = 100.0
  540. var interface_settings = ConfigHandler.load_interface_settings() #checks if close console is enabled and closes console on a success
  541. progress_window.hide()
  542. progress_bar.value = 0
  543. progress_label.text = ""
  544. if interface_settings.auto_close_console and process_successful == true:
  545. console_window.hide()
  546. func stereo_split_and_process(files: Array, node: Node, process_count: int, slider_data: Array) -> Array:
  547. var dual_mono_output:= []
  548. var left:= []
  549. var right:= []
  550. var intermediate_files:= []
  551. for file in files:
  552. await run_command(control_script.cdpprogs_location + "/housekeep",["chans", "2", file])
  553. left.append(file.get_basename() + "_%s.%s" % ["c1", file.get_extension()])
  554. right.append(file.get_basename() + "_%s.%s" % ["c2", file.get_extension()])
  555. #loop through the left and right arrays and make and run the process for each of them
  556. for channel in [left, right]:
  557. var makeprocess = await make_process(node, process_count, channel, slider_data)
  558. # run the command
  559. await run_command(makeprocess[0], makeprocess[3])
  560. await get_tree().process_frame
  561. var output_file = makeprocess[1]
  562. dual_mono_output.append(output_file)
  563. for file in makeprocess[2]:
  564. intermediate_files.append(file)
  565. #advance process count to maintain unique file names
  566. process_count += 1
  567. #return the two output files, any breakfiles generated and the split files for deletion
  568. return [dual_mono_output, intermediate_files, left, right]
  569. func process_dual_mono_pvoc(current_infiles: Dictionary, node: Node, process_count: int, slider_data: Array) -> Array:
  570. match_pvoc_channels(current_infiles) #normalise dictionary to ensure that all entries are dual mono (any mono only processes are duplicated to both left and right)
  571. var infiles_left = []
  572. var infiles_right = []
  573. var pvoc_stereo_files = []
  574. var intermediate_files = []
  575. # extract left and right infiles from dictionary
  576. for value in current_infiles.values():
  577. infiles_left.append(value[0])
  578. infiles_right.append(value[1])
  579. for infiles in [infiles_left, infiles_right]:
  580. var makeprocess = await make_process(node, process_count, infiles, slider_data)
  581. # run the command
  582. await run_command(makeprocess[0], makeprocess[3])
  583. await get_tree().process_frame
  584. var output_file = makeprocess[1]
  585. pvoc_stereo_files.append(output_file)
  586. for file in makeprocess[2]:
  587. intermediate_files.append(file)
  588. #advance process count to maintain unique file names
  589. process_count += 1
  590. return [pvoc_stereo_files, intermediate_files]
  591. func is_stereo(file: String) -> bool:
  592. var soundfile_properties = get_soundfile_properties(file)
  593. if soundfile_properties["channels"] == 2:
  594. return true
  595. else:
  596. return false
  597. func is_pvoc_stereo(current_infiles: Dictionary) -> bool:
  598. for value in current_infiles.values():
  599. if value is Array:
  600. return true
  601. return false
  602. ## Returns properties of a WAV file as a Dictionary:
  603. ## {
  604. ## "format": 1 or 3,
  605. ## "channels": number of channels,
  606. ## "samplerate": sample rate in Hz,
  607. ## "bitdepth": bits per sample,
  608. ## "duration": length in seconds
  609. ## }
  610. func get_soundfile_properties(file: String) -> Dictionary:
  611. var soundfile_properties:= {
  612. "format": 0,
  613. "channels": 0,
  614. "samplerate": 0,
  615. "bitdepth": 0,
  616. "duration": 0.0
  617. }
  618. #open the audio file
  619. var f = FileAccess.open(file, FileAccess.READ)
  620. if f == null:
  621. log_console("Could not find file: " + file, true)
  622. return soundfile_properties # couldn't open
  623. #Skip the RIFF header (12 bytes: "RIFF", file size, "WAVE")
  624. f.seek(12)
  625. var audio_chunk_size = 0
  626. #read through file until end of file if needed
  627. while f.get_position() + 8 <= f.get_length():
  628. #read the 4 byte chunk id to identify what this chunk is
  629. var chunk_id = f.get_buffer(4).get_string_from_ascii()
  630. #read how big this chunk is
  631. var chunk_size = f.get_32()
  632. if chunk_id == "fmt ":
  633. #found the format chunk
  634. #fmt chunk layout:
  635. #2 bytes: Audio format (1 = PCM, 3 = IEEE float, etc.)
  636. #2 bytes: Number of channels (1 = mono, 2 = stereo, ...)
  637. #4 bytes: Sample rate
  638. #4 bytes: Byte rate
  639. #2 bytes: Block align
  640. #2 bytes: Bits per sample
  641. #potentially misc other stuff depending on format
  642. soundfile_properties["format"] = f.get_16() #format 2 bytes: 1 = int PCM, 3 = float
  643. soundfile_properties["channels"] = f.get_16() #num of channels 2 bytes
  644. soundfile_properties["samplerate"] = f.get_32() #sample rate 4 bytes
  645. f.seek(f.get_position() + 6)
  646. soundfile_properties["bitdepth"] = f.get_16() #bitdepth 2 bytes
  647. #check if we have already found the data chunk (not likely) and break the loop
  648. if audio_chunk_size > 0:
  649. f.close()
  650. break
  651. #skip to the end of the fmt chunk - max protects against skipping weirdly if wav is malformed and we have already moved too far into the file
  652. f.seek(f.get_position() + (max(chunk_size - 16, 0)))
  653. elif chunk_id == "data":
  654. #this is where the audio is stored
  655. audio_chunk_size = chunk_size
  656. #check if we have already found the fmt chunk and break loop
  657. if soundfile_properties["format"] > 0:
  658. f.close()
  659. break
  660. #skip the rest of the chunk
  661. f.seek(f.get_position() + chunk_size)
  662. else:
  663. #don't care about any other data in the file skip it
  664. f.seek(f.get_position() + chunk_size)
  665. #close the file
  666. f.close()
  667. if audio_chunk_size > 0 and soundfile_properties["channels"] > 0 and soundfile_properties["bitdepth"] > 0 and soundfile_properties["samplerate"] > 0:
  668. #(channels * bitdepth) / 8 - div 8 to convet bits to bytes
  669. var block_align = int((soundfile_properties["channels"] * soundfile_properties["bitdepth"]) / 8)
  670. #number of frames = size of audio chunk / block size in bytes
  671. var num_frames = int(audio_chunk_size / block_align)
  672. #length in seconds = number of frames / sample rate
  673. soundfile_properties.duration = (num_frames) / soundfile_properties["samplerate"]
  674. else:
  675. #something = 0 and something has gone wrong
  676. log_console("No valid fmt chunk found in wav file, unable to establish, format, channel count, samplerate or bit-depth", true)
  677. for key in soundfile_properties:
  678. #normalise dictionary to 0 so code can detect errors later even if some values have ended up in the dictionary
  679. soundfile_properties[key] = 0
  680. return soundfile_properties #no fmt chunk found, invalid wav file
  681. return soundfile_properties
  682. func merge_many_files(inlet_id: int, process_count: int, input_files: Array) -> Array:
  683. var merge_output = "%s_merge_%d_%d.wav" % [Global.outfile.get_basename(), inlet_id, process_count]
  684. var converted_files := [] # Track any mono->stereo converted files or upsampled files
  685. #check if there are a mix of mono and stereo files and interleave mono files if required
  686. var match_channels = await match_file_channels(inlet_id, process_count, input_files)
  687. input_files = match_channels[0]
  688. converted_files += match_channels[1]
  689. # Merge all input files (converted or original)
  690. log_console("Mixing files to combined input.", true)
  691. var command := ["mergemany"]
  692. command += input_files
  693. command.append(merge_output)
  694. await run_command(control_script.cdpprogs_location + "/submix", command)
  695. if process_successful == false:
  696. log_console("Failed to to merge files to" + merge_output, true)
  697. return [merge_output, converted_files]
  698. func match_input_file_sample_rates_and_bit_depths(input_nodes: Array) -> Array:
  699. var sample_rates := []
  700. var input_files := [] #used to track input files so that the same file is not upsampled more than once should it be loaded into more than one input node
  701. var converted_files := []
  702. var highest_sample_rate
  703. var bit_depths:= []
  704. var file_types:= []
  705. var highest_bit_depth
  706. var int_float
  707. var final_format
  708. #get the sample rate, bit depth and file type (int/float) for each file and add to arrays
  709. for node in input_nodes:
  710. var soundfile_props = get_soundfile_properties(node.get_node("AudioPlayer").get_meta("inputfile"))
  711. file_types.append(soundfile_props["format"])
  712. sample_rates.append(soundfile_props["samplerate"])
  713. bit_depths.append(soundfile_props["bitdepth"])
  714. #set upsampled meta to false to allow for repeat runs of thread
  715. node.get_node("AudioPlayer").set_meta("upsampled", false)
  716. #Check if all sample rates are the same
  717. if sample_rates.all(func(v): return v == sample_rates[0]):
  718. highest_sample_rate = sample_rates[0]
  719. pass
  720. else:
  721. #if not find the highest sample rate
  722. highest_sample_rate = sample_rates.max()
  723. log_console("Different sample rates found in input files, upsampling files to match highest sample rate (" + str(highest_sample_rate) + "Hz) before processing.", true)
  724. #move through all input files and compare match their index to the sample_rate array
  725. for node in input_nodes:
  726. #check if sample rate of current node is less than the highest sample rate
  727. if node.get_node("AudioPlayer").get_meta("sample_rate") < highest_sample_rate:
  728. var input_file = node.get_node("AudioPlayer").get_meta("inputfile")
  729. #up sample it to the highest sample rate if so
  730. var upsample_output = Global.outfile + "_" + input_file.get_file().get_slice(".wav", 0) + "_" + str(highest_sample_rate) + ".wav"
  731. #check if file has previously been upsampled and if not upsample it
  732. if !input_files.has(input_file):
  733. input_files.append(input_file)
  734. await run_command(control_script.cdpprogs_location + "/housekeep", ["respec", "1", input_file, upsample_output, str(highest_sample_rate)])
  735. #add to converted files for cleanup if needed
  736. converted_files.append(upsample_output)
  737. node.get_node("AudioPlayer").set_meta("upsampled", true)
  738. node.get_node("AudioPlayer").set_meta("upsampled_file", upsample_output)
  739. input_files = [] #clear input files array for reuse with bitdepths
  740. #check if all file types and bit-depths are the same
  741. if file_types.all(func(v): return v == sample_rates[0]) and bit_depths.all(func(v): return v == sample_rates[0]):
  742. highest_bit_depth = bit_depths[0]
  743. int_float = file_types[0]
  744. #convert this to the value cdp uses in copysfx for potential use with synthesis nodes later
  745. final_format = classify_format(int_float, highest_bit_depth)
  746. else:
  747. highest_bit_depth = bit_depths.max()
  748. int_float = file_types.max()
  749. #convert this to the value cdp needs to convert file types using copysfx
  750. final_format = classify_format(int_float, highest_bit_depth)
  751. log_console("Different bit-depths found in input files, converting files to match highest bit-depth (" + str(highest_bit_depth) + "-bit) before processing.", true)
  752. #move through all input file nodes and compare them to the highest bit depth and file type
  753. var index = 0
  754. for node in input_nodes:
  755. if classify_format(file_types[index], bit_depths[index]) != final_format:
  756. var input_file
  757. #check if input file has already been upsampled and respec that file instead
  758. if node.get_node("AudioPlayer").get_meta("upsampled") == true:
  759. input_file = node.get_node("AudioPlayer").get_meta("upsampled_file")
  760. else:
  761. input_file = node.get_node("AudioPlayer").get_meta("inputfile")
  762. #build unique output name
  763. var bit_convert_output = Global.outfile + "_" + input_file.get_file().get_slice(".wav", 0) + "_" + str(highest_bit_depth) + "-bit" + ".wav"
  764. #check if this file has already been respeced (two input nodes with the same file loaded for some reason)
  765. if !input_files.has(input_file):
  766. input_files.append(input_file)
  767. await run_command(control_script.cdpprogs_location + "/copysfx", ["-h0", "-s" + str(final_format), input_file, bit_convert_output])
  768. #add to converted files for cleanup if needed
  769. converted_files.append(bit_convert_output)
  770. node.get_node("AudioPlayer").set_meta("upsampled", true)
  771. node.get_node("AudioPlayer").set_meta("upsampled_file", bit_convert_output)
  772. index += 1
  773. return [converted_files, highest_sample_rate, final_format]
  774. func classify_format(file_type: int, bit_depth: int) -> int:
  775. #takes the bitdepth and file type (int/float) of a wav file and outputs a number that can be used by the cdp process copysfx to respec a files bit-depth
  776. match [file_type, bit_depth]:
  777. [1, 16]:
  778. return 1
  779. [1, 32]:
  780. return 2
  781. [3, 32]:
  782. return 3
  783. [1, 24]:
  784. return 4
  785. _:
  786. return -1
  787. #need to remove this function as not needed
  788. #func match_file_sample_rates(inlet_id: int, process_count: int, input_files: Array) -> Array:
  789. #var sample_rates := []
  790. #var converted_files := []
  791. #
  792. ##Get all sample rates
  793. #for f in input_files:
  794. #var samplerate = await get_samplerate(f)
  795. #sample_rates.append(samplerate)
  796. #
  797. ##Check if all sample rates are the same
  798. #if sample_rates.all(func(v): return v == sample_rates[0]):
  799. #pass
  800. #else:
  801. #log_console("Different sample rates found, upsampling files to match highest current sample rate before processing.", true)
  802. ##if not find the highest sample rate
  803. #var highest_sample_rate = sample_rates.max()
  804. #var index = 0
  805. ##move through all input files and compare match their index to the sample_rate array
  806. #for f in input_files:
  807. ##check if sample rate of current file is less than the highest sample rate
  808. #if sample_rates[index] < highest_sample_rate:
  809. ##up sample it to the highest sample rate if so
  810. #var upsample_output = Global.outfile + "_" + str(inlet_id) + "_" + str(process_count) + f.get_file().get_slice(".wav", 0) + "_" + str(highest_sample_rate) + ".wav"
  811. #await run_command(control_script.cdpprogs_location + "/housekeep", ["respec", "1", f, upsample_output, str(highest_sample_rate)])
  812. ##replace the file in the input_file index with the new upsampled file
  813. #input_files[index] = upsample_output
  814. #converted_files.append(upsample_output)
  815. #
  816. #index += 1
  817. #return [input_files, converted_files]
  818. func match_file_channels(inlet_id: int, process_count: int, input_files: Array) -> Array:
  819. var converted_files := []
  820. var channel_counts := []
  821. # Check each file's channel count and build channel count array
  822. for f in input_files:
  823. var stereo = await is_stereo(f)
  824. channel_counts.append(stereo)
  825. # Check if there is a mix of mono and stereo files
  826. if channel_counts.has(true) and channel_counts.has(false):
  827. log_console("Mix of mono and stereo files found, interleaving mono files to stereo before mixing.", true)
  828. var index = 0
  829. for f in input_files:
  830. if channel_counts[index] == false: #file is mono
  831. var stereo_file = Global.outfile + "_" + str(inlet_id) + "_" + str(process_count) + f.get_file().get_slice(".wav", 0) + "_stereo.wav"
  832. await run_command(control_script.cdpprogs_location + "/submix", ["interleave", f, f, stereo_file])
  833. if process_successful == false:
  834. log_console("Failed to interleave mono file: %s" % f, true)
  835. else:
  836. converted_files.append(stereo_file)
  837. input_files[index] = stereo_file
  838. index += 1
  839. return [input_files, converted_files]
  840. func match_pvoc_channels(dict: Dictionary) -> void:
  841. #work through dictionary of files and make all entries dual arrays for stereo pvoc processing
  842. for key in dict.keys():
  843. var value = dict[key]
  844. if value is String:
  845. dict[key] = [value, value]
  846. func _get_slider_values_ordered(node: Node) -> Array:
  847. var results := []
  848. for child in node.get_children():
  849. if child is Range:
  850. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  851. var time = child.get_meta("time")
  852. var brk_data = []
  853. var min_slider = child.min_value
  854. var max_slider = child.max_value
  855. var exp = child.exp_edit
  856. if child.has_meta("brk_data"):
  857. brk_data = child.get_meta("brk_data")
  858. results.append(["slider", flag, child.value, time, brk_data, min_slider, max_slider, exp])
  859. elif child is CheckButton:
  860. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  861. results.append(["checkbutton", flag, child.button_pressed])
  862. elif child is OptionButton:
  863. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  864. var value = child.get_item_text(child.selected)
  865. #check if there has been a sample rate mismatch in the thread and adjust the this parameter to match the threads sample rate
  866. if child.has_meta("adjusted_sample_rate") and child.get_meta("adjusted_sample_rate"):
  867. value = str(child.get_meta("new_sample_rate"))
  868. child.set_meta("adjusted_sample_rate", false)
  869. results.append(["optionbutton", flag, value])
  870. #call this function recursively to find any nested sliders in scenes
  871. if child.get_child_count() > 0:
  872. var nested := _get_slider_values_ordered(child)
  873. results.append_array(nested)
  874. return results
  875. func make_process(node: Node, process_count: int, current_infile: Array, slider_data: Array) -> Array:
  876. var args:= []
  877. var command
  878. # Determine output extension: .wav or .ana based on the node's slot type
  879. var extension = ".wav" if node.get_slot_type_right(0) == 0 else ".ana"
  880. # Construct output filename for this step
  881. var output_file = "%s_%d%s" % [Global.outfile.get_basename(), process_count, extension]
  882. # Get the command name from metadata or default to node name
  883. var command_name = str(node.get_meta("command"))
  884. if command_name.find("_") != -1:
  885. command_name = command_name.split("_", true, 1)
  886. command = "%s/%s" %[control_script.cdpprogs_location, command_name[0]]
  887. args = command_name[1].split("_", true, 1)
  888. else:
  889. command = "%s/%s" %[control_script.cdpprogs_location, command_name]
  890. if current_infile.size() > 0:
  891. #check if input is empty, e.g. synthesis nodes, otherwise append input file to arguments
  892. for file in current_infile:
  893. args.append(file)
  894. args.append(output_file)
  895. # Start building the command line windows i dont think this is used anymore
  896. #var line = "%s/%s \"%s\" \"%s\" " % [control_script.cdpprogs_location, command_name, current_infile, output_file]
  897. #mac
  898. var cleanup = []
  899. # Append parameter values from the sliders, include flags if present
  900. var slider_count = 0
  901. for entry in slider_data:
  902. if entry[0] == "slider":
  903. var flag = entry[1]
  904. var value = entry[2]
  905. #if value == int(value):
  906. #value = int(value)
  907. var time = entry[3] #checks if slider is a time percentage slider
  908. var brk_data = entry[4]
  909. var min_slider = entry[5]
  910. var max_slider = entry[6]
  911. var exp = entry[7]
  912. if brk_data.size() > 0: #if breakpoint data is present on slider
  913. #Sort all points by time
  914. var sorted_brk_data = []
  915. sorted_brk_data = brk_data.duplicate()
  916. sorted_brk_data.sort_custom(sort_points)
  917. var calculated_brk = []
  918. #get length of input file in seconds
  919. var infile_length = 1 #set infile length to dummy value just incase it does get used where it shouldn't to avoid crashes
  920. if current_infile.size() > 0:
  921. var soundfile_props = get_soundfile_properties(current_infile[0])
  922. infile_length = soundfile_props["duration"]
  923. #scale values from automation window to the right length for file and correct slider values
  924. #if node has an output duration then breakpoint files should be x = outputduration y= slider value else x=input duration, y=value
  925. if node.has_meta("outputduration"):
  926. for i in range(sorted_brk_data.size()):
  927. var point = sorted_brk_data[i]
  928. var new_x = float(node.get_meta("outputduration")) * (point.x / 700) #output time
  929. if i == sorted_brk_data.size() - 1: #check if this is last automation point
  930. new_x = float(node.get_meta("outputduration")) + 0.1 # force last point's x to infile_length + 100ms to make sure the file is defo over
  931. var new_y
  932. #check if slider is exponential and scale automation
  933. if exp:
  934. new_y = remap_y_to_log_scale(point.y, 0.0, 255.0, min_slider, max_slider)
  935. else:
  936. new_y = remap(point.y, 255, 0, min_slider, max_slider) #slider value
  937. if time: #check if this is a time slider and convert to percentage of input file
  938. new_y = infile_length * (new_y / 100)
  939. calculated_brk.append(Vector2(new_x, new_y))
  940. else:
  941. for i in range(sorted_brk_data.size()):
  942. var point = sorted_brk_data[i]
  943. var new_x = infile_length * (point.x / 700) #time
  944. if i == sorted_brk_data.size() - 1: #check if this is last automation point
  945. new_x = infile_length + 0.1 # force last point's x to infile_length + 100ms to make sure the file is defo over
  946. var new_y
  947. #check if slider is exponential and scale automation
  948. if exp:
  949. new_y = remap_y_to_log_scale(point.y, 0.0, 255.0, min_slider, max_slider)
  950. else:
  951. new_y = remap(point.y, 255, 0, min_slider, max_slider) #slider value
  952. calculated_brk.append(Vector2(new_x, new_y))
  953. #make text file
  954. var brk_file_path = output_file.get_basename() + "_" + str(slider_count) + ".txt"
  955. write_breakfile(calculated_brk, brk_file_path)
  956. #add breakfile to cleanup before adding flag
  957. cleanup.append(brk_file_path)
  958. #append text file in place of value
  959. #include flag if this param has a flag
  960. if flag.begins_with("-"):
  961. brk_file_path = flag + brk_file_path
  962. args.append(brk_file_path)
  963. else: #no break file append slider value
  964. if time == true:
  965. var soundfile_props = get_soundfile_properties(current_infile[0])
  966. var infile_length = soundfile_props["duration"]
  967. if value == 100:
  968. #if slider is set to 100% default to a millisecond before the end of the file to stop cdp moaning about rounding errors
  969. value = infile_length - 0.01
  970. else:
  971. value = infile_length * (value / 100) #calculate percentage time of the input file
  972. #line += ("%s%.2f " % [flag, value]) if flag.begins_with("-") else ("%.2f " % value)
  973. args.append(("%s%.2f " % [flag, value]) if flag.begins_with("-") else str(value))
  974. elif entry[0] == "checkbutton":
  975. var flag = entry[1]
  976. var value = entry[2]
  977. #if button is pressed add the flag to the arguments list
  978. if value == true:
  979. args.append(flag)
  980. elif entry[0] == "optionbutton":
  981. var flag = entry[1]
  982. var value = entry[2]
  983. args.append(("%s%.2f " % [flag, value]) if flag.begins_with("-") else str(value))
  984. slider_count += 1
  985. return [command, output_file, cleanup, args]
  986. #return [line.strip_edges(), output_file, cleanup]
  987. func remap_y_to_log_scale(y: float, min_y: float, max_y: float, min_val: float, max_val: float) -> float:
  988. var t = clamp((y - min_y) / (max_y - min_y), 0.0, 1.0)
  989. # Since y goes top-down (0 = top, 255 = bottom), we invert t
  990. t = 1.0 - t
  991. var log_min = log(min_val) / log(10)
  992. var log_max = log(max_val) / log(10)
  993. var log_val = lerp(log_min, log_max, t)
  994. return pow(10.0, log_val)
  995. func sort_points(a, b):
  996. return a.x < b.x
  997. func write_breakfile(points: Array, path: String):
  998. var file = FileAccess.open(path, FileAccess.WRITE)
  999. if file:
  1000. for point in points:
  1001. var line = str(point.x) + " " + str(point.y) + "\n"
  1002. file.store_string(line)
  1003. file.close()
  1004. else:
  1005. log_console("Failed to open file to write breakfile", true)
  1006. func _on_kill_process_button_down() -> void:
  1007. if process_running and process_info.has("pid"):
  1008. progress_window.hide()
  1009. # Terminate the process by PID
  1010. OS.kill(process_info["pid"])
  1011. process_running = false
  1012. process_cancelled = true
  1013. func path_exists_through_all_nodes() -> bool:
  1014. var graph = {}
  1015. var input_node_names = []
  1016. var output_node_name = ""
  1017. # Gather nodes and initialize adjacency list
  1018. for child in graph_edit.get_children():
  1019. if child is GraphNode:
  1020. var name = str(child.name)
  1021. var command = child.get_meta("command")
  1022. var input = child.get_meta("input")
  1023. if input:
  1024. input_node_names.append(name)
  1025. elif command == "outputfile":
  1026. output_node_name = name
  1027. graph[name] = []
  1028. # Add edges
  1029. for conn in graph_edit.get_connection_list():
  1030. var from_node = str(conn["from_node"])
  1031. var to_node = str(conn["to_node"])
  1032. if graph.has(from_node):
  1033. graph[from_node].append(to_node)
  1034. # BFS from each input node
  1035. for input_node in input_node_names:
  1036. var queue = [[input_node]] # store paths, not just nodes
  1037. while queue.size() > 0:
  1038. var path = queue.pop_front()
  1039. var current = path[-1]
  1040. if current == output_node_name:
  1041. # Candidate path found; validate multi-inlets
  1042. if validate_path_inlets(path, graph, input_node_names):
  1043. return true # fully valid path found
  1044. for neighbor in graph.get(current, []):
  1045. if neighbor in path:
  1046. continue # avoid cycles
  1047. var new_path = path.duplicate()
  1048. new_path.append(neighbor)
  1049. queue.append(new_path)
  1050. return false
  1051. # Validate all nodes along a candidate path for multi-inlets
  1052. func validate_path_inlets(path: Array, graph: Dictionary, input_node_names: Array) -> bool:
  1053. for node_name in path:
  1054. var child = graph_edit.get_node(node_name)
  1055. var input_count = child.get_input_port_count()
  1056. if input_count <= 1:
  1057. continue # single-inlet nodes are trivially valid
  1058. # Check each inlet
  1059. for i in range(input_count):
  1060. var inlet_valid = false
  1061. for conn in graph_edit.get_connection_list():
  1062. if str(conn["to_node"]) == node_name and conn["to_port"] == i:
  1063. var src_node = str(conn["from_node"])
  1064. if path_has_input(src_node, graph, input_node_names):
  1065. inlet_valid = true
  1066. break
  1067. if not inlet_valid:
  1068. return false # this inlet cannot reach any input
  1069. return true
  1070. # Step backwards from a node to see if a path exists to any input node
  1071. func path_has_input(current: String, graph: Dictionary, input_node_names: Array, visited: Dictionary = {}) -> bool:
  1072. if current in input_node_names:
  1073. return true
  1074. if current in visited:
  1075. return false
  1076. visited[current] = true
  1077. # Check all nodes that lead to current
  1078. for conn in graph_edit.get_connection_list():
  1079. if str(conn["to_node"]) == current:
  1080. var src_node = str(conn["from_node"])
  1081. if path_has_input(src_node, graph, input_node_names, visited.duplicate()):
  1082. return true
  1083. return false
  1084. #func path_exists_through_all_nodes() -> bool:
  1085. #var graph = {}
  1086. #var input_node_names = []
  1087. #var output_node_name = ""
  1088. #
  1089. ## Gather nodes and build empty graph
  1090. #for child in graph_edit.get_children():
  1091. #if child is GraphNode:
  1092. #var name = str(child.name)
  1093. #var command = child.get_meta("command")
  1094. #var input = child.get_meta("input")
  1095. #
  1096. #if input:
  1097. #input_node_names.append(name)
  1098. #elif command == "outputfile":
  1099. #output_node_name = name
  1100. #
  1101. #graph[name] = [] # Initialize adjacency list
  1102. #
  1103. ## Add connections (edges)
  1104. #for conn in graph_edit.get_connection_list():
  1105. #var from = str(conn["from_node"])
  1106. #var to = str(conn["to_node"])
  1107. #if graph.has(from):
  1108. #graph[from].append(to)
  1109. #
  1110. ## BFS to check if any input node reaches the output
  1111. #for input_node in input_node_names:
  1112. #var visited = {}
  1113. #var queue = [input_node]
  1114. #
  1115. #while queue.size() > 0:
  1116. #var current = queue.pop_front()
  1117. #
  1118. #if current == output_node_name:
  1119. #return true # Path found
  1120. #
  1121. #if current in visited:
  1122. #continue
  1123. #visited[current] = true
  1124. #
  1125. #for neighbor in graph.get(current, []):
  1126. #queue.append(neighbor)
  1127. #
  1128. ## No path from any input node to output
  1129. #return false
  1130. func log_console(text: String, update: bool) -> void:
  1131. console_output.append_text(text + "\n \n")
  1132. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1133. if update == true:
  1134. await get_tree().process_frame # Optional: ensure UI updates
  1135. func run_command(command: String, args: Array) -> String:
  1136. var is_windows = OS.get_name() == "Windows"
  1137. console_output.append_text(command + " " + " ".join(args) + "\n")
  1138. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1139. await get_tree().process_frame
  1140. if is_windows and (command == "del" or command == "ren"): #checks if the command is a windows system command and runs it through cmd.exe
  1141. args.insert(0, command)
  1142. args.insert(0, "/C")
  1143. process_info = OS.execute_with_pipe("cmd.exe", args, false)
  1144. else:
  1145. process_info = OS.execute_with_pipe(command, args, false)
  1146. # Check if the process was successfully started
  1147. if !process_info.has("pid"):
  1148. log_console("Failed to start process]", true)
  1149. return ""
  1150. process_running = true
  1151. # Start monitoring the process output and status
  1152. return await monitor_process(process_info["pid"], process_info["stdio"], process_info["stderr"])
  1153. func monitor_process(pid: int, stdout: FileAccess, stderr: FileAccess) -> String:
  1154. var output := ""
  1155. while OS.is_process_running(pid):
  1156. await get_tree().process_frame
  1157. while stdout.get_position() < stdout.get_length():
  1158. var line = stdout.get_line()
  1159. output += line
  1160. console_output.append_text(line + "\n")
  1161. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1162. while stderr.get_position() < stderr.get_length():
  1163. var line = stderr.get_line()
  1164. output += line
  1165. console_output.append_text(line + "\n")
  1166. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1167. var exit_code = OS.get_process_exit_code(pid)
  1168. if exit_code == 0:
  1169. if output.contains("ERROR:"): #checks if CDP reported an error but passed exit code 0 anyway
  1170. console_output.append_text("[color=#9c2828][b]Processes failed[/b][/color]\n\n")
  1171. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1172. process_successful = false
  1173. if process_cancelled == false:
  1174. progress_window.hide()
  1175. if !console_window.visible:
  1176. console_window.popup_centered()
  1177. else:
  1178. console_output.append_text("[color=#638382]Processes ran successfully[/color]\n\n")
  1179. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1180. else:
  1181. console_output.append_text("[color=#9c2828][b]Processes failed with exit code: %d[/b][/color]\n" % exit_code + "\n")
  1182. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1183. process_successful = false
  1184. if process_cancelled == false:
  1185. progress_window.hide()
  1186. if !console_window.visible:
  1187. console_window.popup_centered()
  1188. if output.contains("as an internal or external command"): #check for cdprogs location error on windows
  1189. console_output.append_text("[color=#9c2828][b]Please make sure your cdprogs folder is set to the correct location in the Settings menu. The default location is C:\\CDPR8\\_cdp\\_cdprogs[/b][/color]\n\n")
  1190. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1191. if output.contains("command not found"): #check for cdprogs location error on unix systems
  1192. console_output.append_text("[color=#9c2828][b]Please make sure your cdprogs folder is set to the correct location in the Settings menu. The default location is ~/cdpr8/_cdp/_cdprogs[/b][/color]\n\n")
  1193. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1194. process_running = false
  1195. return output
  1196. # Main cycle detection
  1197. func detect_cycles(graph: Dictionary, loop_nodes: Dictionary) -> bool:
  1198. var visited := {}
  1199. var stack := {}
  1200. for node in graph.keys():
  1201. if _dfs_cycle(node, graph, visited, stack, loop_nodes):
  1202. return true
  1203. return false
  1204. func _dfs_cycle(node: String, graph: Dictionary, visited: Dictionary, stack: Dictionary, loop_nodes: Dictionary) -> bool:
  1205. if not visited.has(node):
  1206. visited[node] = true
  1207. stack[node] = true
  1208. for neighbor in graph[node]:
  1209. # If neighbor hasn't been visited, recurse
  1210. if not visited.has(neighbor):
  1211. if _dfs_cycle(neighbor, graph, visited, stack, loop_nodes):
  1212. # Cycle found down this path
  1213. if not (loop_nodes.has(node) or loop_nodes.has(neighbor)):
  1214. return true
  1215. elif stack.has(neighbor):
  1216. # Back edge found → cycle
  1217. if not (loop_nodes.has(node) or loop_nodes.has(neighbor)):
  1218. return true
  1219. # Done exploring this node
  1220. stack.erase(node)
  1221. return false