run_thread.gd 62 KB

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