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. if sub_chunk_id == "note":
  732. var note_bytes = f.get_buffer(sub_chunk_size)
  733. var note_text = ""
  734. for b in note_bytes:
  735. note_text += char(b)
  736. var pvoc_header_data = note_text.split("\n", false)
  737. var i = 0
  738. for entry in pvoc_header_data:
  739. if entry == "analwinlen":
  740. analysis_file_properties["windowsize"] = hex_string_to_int_le(pvoc_header_data[i+1])
  741. elif entry == "decfactor":
  742. analysis_file_properties["decimationfactor"] = hex_string_to_int_le(pvoc_header_data[i+1])
  743. i += 1
  744. break
  745. #check if we have already found the data chunk (not likely) and break the loop
  746. if data_chunk_size > 0:
  747. f.close()
  748. break
  749. elif chunk_id == "data":
  750. #this is where the audio is stored
  751. data_chunk_size = chunk_size
  752. #check if we have already found the sfif chunk and break loop
  753. if analysis_file_properties["windowsize"] > 0:
  754. f.close()
  755. break
  756. #skip the rest of the chunk
  757. f.seek(f.get_position() + chunk_size)
  758. else:
  759. #don't care about any other data in the file skip it
  760. f.seek(f.get_position() + chunk_size)
  761. #close the file
  762. f.close()
  763. if analysis_file_properties["windowsize"] != 0 and data_chunk_size != 0:
  764. var bytes_per_frame = (analysis_file_properties["windowsize"] + 2) * 4
  765. analysis_file_properties["windowcount"] = int(data_chunk_size / bytes_per_frame)
  766. else:
  767. log_console("Error: Could not get information from analysis file", true)
  768. return analysis_file_properties
  769. func hex_string_to_int_le(hex_string: String) -> int:
  770. # Ensure the string is 8 characters (4 bytes)
  771. if hex_string.length() != 8:
  772. push_error("Invalid hex string length: " + hex_string)
  773. return 0
  774. var le_string = ""
  775. for i in [6, 4, 2, 0]: #flip the order of the bytes as ana format uses little endian
  776. le_string += hex_string.substr(i, 2)
  777. return le_string.hex_to_int()
  778. func merge_many_files(inlet_id: int, process_count: int, input_files: Array) -> Array:
  779. var merge_output = "%s_merge_%d_%d.wav" % [Global.outfile.get_basename(), inlet_id, process_count]
  780. var converted_files := [] # Track any mono->stereo converted files or upsampled files
  781. #check if there are a mix of mono and stereo files and interleave mono files if required
  782. var match_channels = await match_file_channels(inlet_id, process_count, input_files)
  783. input_files = match_channels[0]
  784. converted_files += match_channels[1]
  785. # Merge all input files (converted or original)
  786. log_console("Mixing files to combined input.", true)
  787. var command := ["mergemany"]
  788. command += input_files
  789. command.append(merge_output)
  790. await run_command(control_script.cdpprogs_location + "/submix", command)
  791. if process_successful == false:
  792. log_console("Failed to merge files to" + merge_output, true)
  793. return [merge_output, converted_files]
  794. func match_input_file_sample_rates_and_bit_depths(input_nodes: Array) -> Array:
  795. var sample_rates := []
  796. 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
  797. var converted_files := []
  798. var highest_sample_rate
  799. var bit_depths:= []
  800. var file_types:= []
  801. var highest_bit_depth
  802. var int_float
  803. var final_format
  804. #get the sample rate, bit depth and file type (int/float) for each file and add to arrays
  805. for node in input_nodes:
  806. var soundfile_props = get_soundfile_properties(node.get_node("AudioPlayer").get_meta("inputfile"))
  807. file_types.append(soundfile_props["format"])
  808. sample_rates.append(soundfile_props["samplerate"])
  809. bit_depths.append(soundfile_props["bitdepth"])
  810. #set upsampled meta to false to allow for repeat runs of thread
  811. node.get_node("AudioPlayer").set_meta("upsampled", false)
  812. #Check if all sample rates are the same
  813. if sample_rates.all(func(v): return v == sample_rates[0]):
  814. highest_sample_rate = sample_rates[0]
  815. pass
  816. else:
  817. #if not find the highest sample rate
  818. highest_sample_rate = sample_rates.max()
  819. log_console("Different sample rates found in input files, upsampling files to match highest sample rate (" + str(highest_sample_rate) + "Hz) before processing.", true)
  820. #move through all input files and compare match their index to the sample_rate array
  821. for node in input_nodes:
  822. #check if sample rate of current node is less than the highest sample rate
  823. if node.get_node("AudioPlayer").get_meta("sample_rate") < highest_sample_rate:
  824. var input_file = node.get_node("AudioPlayer").get_meta("inputfile")
  825. #up sample it to the highest sample rate if so
  826. var upsample_output = Global.outfile + "_" + input_file.get_file().get_slice(".wav", 0) + "_" + str(highest_sample_rate) + ".wav"
  827. #check if file has previously been upsampled and if not upsample it
  828. if !input_files.has(input_file):
  829. input_files.append(input_file)
  830. await run_command(control_script.cdpprogs_location + "/housekeep", ["respec", "1", input_file, upsample_output, str(highest_sample_rate)])
  831. #add to converted files for cleanup if needed
  832. converted_files.append(upsample_output)
  833. node.get_node("AudioPlayer").set_meta("upsampled", true)
  834. node.get_node("AudioPlayer").set_meta("upsampled_file", upsample_output)
  835. input_files = [] #clear input files array for reuse with bitdepths
  836. #check if all file types and bit-depths are the same
  837. if file_types.all(func(v): return v == file_types[0]) and bit_depths.all(func(v): return v == bit_depths[0]):
  838. highest_bit_depth = bit_depths[0]
  839. int_float = file_types[0]
  840. #convert this to the value cdp uses in copysfx for potential use with synthesis nodes later
  841. final_format = classify_format(int_float, highest_bit_depth)
  842. else:
  843. highest_bit_depth = bit_depths.max()
  844. int_float = file_types.max()
  845. #convert this to the value cdp needs to convert file types using copysfx
  846. final_format = classify_format(int_float, highest_bit_depth)
  847. log_console("Different bit-depths found in input files, converting files to match highest bit-depth (" + str(highest_bit_depth) + "-bit) before processing.", true)
  848. #move through all input file nodes and compare them to the highest bit depth and file type
  849. var index = 0
  850. for node in input_nodes:
  851. if classify_format(file_types[index], bit_depths[index]) != final_format:
  852. var input_file
  853. #check if input file has already been upsampled and respec that file instead
  854. if node.get_node("AudioPlayer").get_meta("upsampled") == true:
  855. input_file = node.get_node("AudioPlayer").get_meta("upsampled_file")
  856. else:
  857. input_file = node.get_node("AudioPlayer").get_meta("inputfile")
  858. #build unique output name
  859. var bit_convert_output = Global.outfile + "_" + input_file.get_file().get_slice(".wav", 0) + "_" + str(highest_bit_depth) + "-bit" + ".wav"
  860. #check if this file has already been respeced (two input nodes with the same file loaded for some reason)
  861. if !input_files.has(input_file):
  862. input_files.append(input_file)
  863. await run_command(control_script.cdpprogs_location + "/copysfx", ["-h0", "-s" + str(final_format), input_file, bit_convert_output])
  864. #add to converted files for cleanup if needed
  865. converted_files.append(bit_convert_output)
  866. node.get_node("AudioPlayer").set_meta("upsampled", true)
  867. node.get_node("AudioPlayer").set_meta("upsampled_file", bit_convert_output)
  868. index += 1
  869. return [converted_files, highest_sample_rate, final_format]
  870. func classify_format(file_type: int, bit_depth: int) -> int:
  871. #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
  872. match [file_type, bit_depth]:
  873. [1, 16]:
  874. return 1
  875. [1, 32]:
  876. return 2
  877. [3, 32]:
  878. return 3
  879. [1, 24]:
  880. return 4
  881. _:
  882. return -1
  883. func match_file_channels(inlet_id: int, process_count: int, input_files: Array) -> Array:
  884. var converted_files := []
  885. var channel_counts := []
  886. # Check each file's channel count and build channel count array
  887. for f in input_files:
  888. var stereo = is_stereo(f)
  889. channel_counts.append(stereo)
  890. # Check if there is a mix of mono and stereo files
  891. if channel_counts.has(true) and channel_counts.has(false):
  892. log_console("Mix of mono and stereo files found, interleaving mono files to stereo before mixing.", true)
  893. var index = 0
  894. for f in input_files:
  895. if channel_counts[index] == false: #file is mono
  896. var stereo_file = Global.outfile + "_" + str(inlet_id) + "_" + str(process_count) + f.get_file().get_slice(".wav", 0) + "_stereo.wav"
  897. await run_command(control_script.cdpprogs_location + "/submix", ["interleave", f, f, stereo_file])
  898. if process_successful == false:
  899. log_console("Failed to interleave mono file: %s" % f, true)
  900. else:
  901. converted_files.append(stereo_file)
  902. input_files[index] = stereo_file
  903. index += 1
  904. return [input_files, converted_files]
  905. func match_pvoc_channels(dict: Dictionary) -> void:
  906. #work through dictionary of files and make all entries dual arrays for stereo pvoc processing
  907. for key in dict.keys():
  908. var value = dict[key]
  909. if value is String:
  910. dict[key] = [value, value]
  911. func _get_slider_values_ordered(node: Node) -> Array:
  912. var results := []
  913. if node.has_meta("command") and node.get_meta("command") == "pvoc_anal_1":
  914. results.append(["slider", "-c", fft_size, false, [], 2, 16380, false, false])
  915. results.append(["slider", "-o", fft_overlap, false, [], 1, 4, false, false])
  916. return results
  917. for child in node.get_children():
  918. if child is Range:
  919. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  920. var time = child.get_meta("time")
  921. var brk_data = []
  922. var min_slider = child.min_value
  923. var max_slider = child.max_value
  924. var exponential = child.exp_edit
  925. var fftwindowsize = child.get_meta("fftwindowsize")
  926. var fftwindowcount = child.get_meta("fftwindowcount")
  927. var value = child.value
  928. if child.has_meta("brk_data"):
  929. brk_data = child.get_meta("brk_data")
  930. #if this slider is a percentage of the fft size just calulate this here as fft size is a global value
  931. if fftwindowsize == true:
  932. if value == 100:
  933. value = fft_size
  934. else:
  935. value = max(int(fft_size * (value/100)), 1)
  936. min_slider = max(int(fft_size * (min_slider/100)), 1)
  937. max_slider = int(fft_size * (max_slider/100))
  938. results.append(["slider", flag, value, time, brk_data, min_slider, max_slider, exponential, fftwindowcount])
  939. elif child is CheckButton:
  940. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  941. results.append(["checkbutton", flag, child.button_pressed])
  942. elif child is OptionButton:
  943. var flag = child.get_meta("flag") if child.has_meta("flag") else ""
  944. var value = child.get_item_text(child.selected)
  945. #check if there has been a sample rate mismatch in the thread and adjust the this parameter to match the threads sample rate
  946. if child.has_meta("adjusted_sample_rate") and child.get_meta("adjusted_sample_rate"):
  947. value = str(child.get_meta("new_sample_rate"))
  948. child.set_meta("adjusted_sample_rate", false)
  949. results.append(["optionbutton", flag, value])
  950. #call this function recursively to find any nested sliders in scenes
  951. if child.get_child_count() > 0:
  952. var nested := _get_slider_values_ordered(child)
  953. results.append_array(nested)
  954. return results
  955. func make_process(node: Node, process_count: int, current_infile: Array, slider_data: Array) -> Array:
  956. var args:= []
  957. var command
  958. var cleanup = []
  959. # Determine output extension: .wav or .ana based on the node's slot type
  960. var extension = ".wav" if node.get_slot_type_right(0) == 0 else ".ana"
  961. # Construct output filename for this step
  962. var output_file = "%s_%d%s" % [Global.outfile.get_basename(), process_count, extension]
  963. #special case for morph glide as it requires spec grab to have been run first
  964. if node.get_meta("command") == "morph_glide":
  965. #get slider values nothing else needed
  966. var window1 = slider_data[0][2]
  967. var window2 = slider_data[1][2]
  968. var duration = slider_data[2][2]
  969. #get length of the two input files
  970. var soundfile_1_props = get_soundfile_properties(current_infile[0])
  971. var infile_1_length = soundfile_1_props["duration"]
  972. var soundfile_2_props = get_soundfile_properties(current_infile[1])
  973. var infile_2_length = soundfile_2_props["duration"]
  974. if window1 == 100:
  975. #if slider is set to 100% default to 10 milliseconds before the end of the file to stop cdp moaning about rounding errors
  976. window1 = infile_1_length - 0.1
  977. else:
  978. window1 = infile_1_length * (window1 / 100) #calculate percentage time of the input file
  979. if window2 == 100:
  980. #if slider is set to 100% default to 10 milliseconds before the end of the file to stop cdp moaning about rounding errors
  981. window2 = infile_2_length - 0.1
  982. else:
  983. window2 = infile_2_length * (window2 / 100) #calculate percentage time of the input file
  984. #run spec grab to extract the chosen windows
  985. var window1_outfile = "%s_%d_%s%s" % [Global.outfile.get_basename(), process_count, "window1", extension]
  986. run_command("%s/%s" %[control_script.cdpprogs_location, "spec"], ["grab", current_infile[0], window1_outfile, str(window1)])
  987. cleanup.append(window1_outfile)
  988. var window2_outfile = "%s_%d_%s%s" % [Global.outfile.get_basename(), process_count, "window2", extension]
  989. run_command("%s/%s" %[control_script.cdpprogs_location, "spec"], ["grab", current_infile[1], window2_outfile, str(window2)])
  990. cleanup.append(window2_outfile)
  991. #build actual glide command
  992. command = "%s/%s" %[control_script.cdpprogs_location, "morph"]
  993. args = ["glide", window1_outfile, window2_outfile, output_file, duration]
  994. else:
  995. # Normal node process as usual
  996. # Get the command name from metadata
  997. var command_name = str(node.get_meta("command"))
  998. if command_name.find("_") != -1:
  999. command_name = command_name.split("_", true, 1)
  1000. command = "%s/%s" %[control_script.cdpprogs_location, command_name[0]]
  1001. args = command_name[1].split("_", true, 1)
  1002. else:
  1003. command = "%s/%s" %[control_script.cdpprogs_location, command_name]
  1004. if current_infile.size() > 0:
  1005. #check if input is empty, e.g. synthesis nodes, otherwise append input file to arguments
  1006. for file in current_infile:
  1007. args.append(file)
  1008. args.append(output_file)
  1009. # Append parameter values from the sliders, include flags if present
  1010. var slider_count = 0
  1011. for entry in slider_data:
  1012. if entry[0] == "slider":
  1013. var flag = entry[1]
  1014. var value = entry[2]
  1015. #if value == int(value):
  1016. #value = int(value)
  1017. var time = entry[3] #checks if slider is a time percentage slider
  1018. var brk_data = entry[4]
  1019. var min_slider = entry[5]
  1020. var max_slider = entry[6]
  1021. var exponential = entry[7]
  1022. var fftwindowcount = entry[8]
  1023. var window_count
  1024. if fftwindowcount == true:
  1025. var analysis_file_data = get_analysis_file_properties(current_infile[0])
  1026. window_count = analysis_file_data["windowcount"]
  1027. min_slider = int(max(window_count * (min_slider / 100), 1))
  1028. max_slider = int(window_count * (max_slider / 100))
  1029. if brk_data.size() > 0: #if breakpoint data is present on slider
  1030. #Sort all points by time
  1031. var sorted_brk_data = []
  1032. sorted_brk_data = brk_data.duplicate()
  1033. sorted_brk_data.sort_custom(sort_points)
  1034. var calculated_brk = []
  1035. #get length of input file in seconds
  1036. var infile_length = 1 #set infile length to dummy value just incase it does get used where it shouldn't to avoid crashes
  1037. if current_infile.size() > 0:
  1038. var soundfile_props = get_soundfile_properties(current_infile[0])
  1039. infile_length = soundfile_props["duration"]
  1040. #scale values from automation window to the right length for file and correct slider values
  1041. #if node has an output duration then breakpoint files should be x = outputduration y= slider value else x=input duration, y=value
  1042. if node.has_meta("outputduration"):
  1043. for i in range(sorted_brk_data.size()):
  1044. var point = sorted_brk_data[i]
  1045. var new_x = float(node.get_meta("outputduration")) * (point.x / 700) #output time
  1046. if i == sorted_brk_data.size() - 1: #check if this is last automation point
  1047. 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
  1048. var new_y
  1049. #check if slider is exponential and scale automation
  1050. if exponential:
  1051. new_y = remap_y_to_log_scale(point.y, 0.0, 255.0, min_slider, max_slider)
  1052. else:
  1053. new_y = remap(point.y, 255, 0, min_slider, max_slider) #slider value
  1054. if time: #check if this is a time slider and convert to percentage of input file
  1055. new_y = infile_length * (new_y / 100)
  1056. calculated_brk.append(Vector2(new_x, new_y))
  1057. else:
  1058. for i in range(sorted_brk_data.size()):
  1059. var point = sorted_brk_data[i]
  1060. var new_x = infile_length * (point.x / 700) #time
  1061. if i == sorted_brk_data.size() - 1: #check if this is last automation point
  1062. new_x = infile_length + 0.1 # force last point's x to infile_length + 100ms to make sure the file is defo over
  1063. var new_y
  1064. #check if slider is exponential and scale automation
  1065. if exponential:
  1066. new_y = remap_y_to_log_scale(point.y, 0.0, 255.0, min_slider, max_slider)
  1067. else:
  1068. new_y = remap(point.y, 255, 0, min_slider, max_slider) #slider value
  1069. calculated_brk.append(Vector2(new_x, new_y))
  1070. #make text file
  1071. var brk_file_path = output_file.get_basename() + "_" + str(slider_count) + ".txt"
  1072. write_breakfile(calculated_brk, brk_file_path)
  1073. #add breakfile to cleanup before adding flag
  1074. cleanup.append(brk_file_path)
  1075. #append text file in place of value
  1076. #include flag if this param has a flag
  1077. if flag.begins_with("-"):
  1078. brk_file_path = flag + brk_file_path
  1079. args.append(brk_file_path)
  1080. else: #no break file append slider value
  1081. if time == true:
  1082. var soundfile_props = get_soundfile_properties(current_infile[0])
  1083. var infile_length = soundfile_props["duration"]
  1084. if value == 100:
  1085. #if slider is set to 100% default to a millisecond before the end of the file to stop cdp moaning about rounding errors
  1086. value = infile_length - 0.1
  1087. else:
  1088. value = infile_length * (value / 100) #calculate percentage time of the input file
  1089. if fftwindowcount == true:
  1090. if value == 100:
  1091. value = window_count
  1092. else:
  1093. value = int(window_count * (value / 100))
  1094. args.append(("%s%.2f " % [flag, value]) if flag.begins_with("-") else str(value))
  1095. elif entry[0] == "checkbutton":
  1096. var flag = entry[1]
  1097. var value = entry[2]
  1098. #if button is pressed add the flag to the arguments list
  1099. if value == true:
  1100. args.append(flag)
  1101. elif entry[0] == "optionbutton":
  1102. var flag = entry[1]
  1103. var value = entry[2]
  1104. args.append(("%s%.2f " % [flag, value]) if flag.begins_with("-") else str(value))
  1105. slider_count += 1
  1106. return [command, output_file, cleanup, args]
  1107. #return [line.strip_edges(), output_file, cleanup]
  1108. func remap_y_to_log_scale(y: float, min_y: float, max_y: float, min_val: float, max_val: float) -> float:
  1109. var t = clamp((y - min_y) / (max_y - min_y), 0.0, 1.0)
  1110. # Since y goes top-down (0 = top, 255 = bottom), we invert t
  1111. t = 1.0 - t
  1112. var log_min = log(min_val) / log(10)
  1113. var log_max = log(max_val) / log(10)
  1114. var log_val = lerp(log_min, log_max, t)
  1115. return pow(10.0, log_val)
  1116. func sort_points(a, b):
  1117. return a.x < b.x
  1118. func write_breakfile(points: Array, path: String):
  1119. var file = FileAccess.open(path, FileAccess.WRITE)
  1120. if file:
  1121. for point in points:
  1122. var line = str(point.x) + " " + str(point.y) + "\n"
  1123. file.store_string(line)
  1124. file.close()
  1125. else:
  1126. log_console("Failed to open file to write breakfile", true)
  1127. func _on_kill_process_button_down() -> void:
  1128. if process_running and process_info.has("pid"):
  1129. progress_window.hide()
  1130. # Terminate the process by PID
  1131. OS.kill(process_info["pid"])
  1132. process_running = false
  1133. process_cancelled = true
  1134. func path_exists_through_all_nodes() -> bool:
  1135. var graph = {}
  1136. var input_node_names = []
  1137. var output_node_name = ""
  1138. # Gather nodes and initialize adjacency list
  1139. for child in graph_edit.get_children():
  1140. if child is GraphNode:
  1141. var name = str(child.name)
  1142. var command = child.get_meta("command")
  1143. var input = child.get_meta("input")
  1144. if input:
  1145. input_node_names.append(name)
  1146. elif command == "outputfile":
  1147. output_node_name = name
  1148. graph[name] = []
  1149. # Add edges
  1150. for conn in graph_edit.get_connection_list():
  1151. var from_node = str(conn["from_node"])
  1152. var to_node = str(conn["to_node"])
  1153. if graph.has(from_node):
  1154. graph[from_node].append(to_node)
  1155. # BFS from each input node
  1156. for input_node in input_node_names:
  1157. var queue = [[input_node]] # store paths, not just nodes
  1158. while queue.size() > 0:
  1159. var path = queue.pop_front()
  1160. var current = path[-1]
  1161. if current == output_node_name:
  1162. # Candidate path found; validate multi-inlets
  1163. if validate_path_inlets(path, graph, input_node_names):
  1164. return true # fully valid path found
  1165. for neighbor in graph.get(current, []):
  1166. if neighbor in path:
  1167. continue # avoid cycles
  1168. var new_path = path.duplicate()
  1169. new_path.append(neighbor)
  1170. queue.append(new_path)
  1171. return false
  1172. # Validate all nodes along a candidate path for multi-inlets
  1173. func validate_path_inlets(path: Array, graph: Dictionary, input_node_names: Array) -> bool:
  1174. for node_name in path:
  1175. var child = graph_edit.get_node(node_name)
  1176. var input_count = child.get_input_port_count()
  1177. if input_count <= 1:
  1178. continue # single-inlet nodes are trivially valid
  1179. # Check each inlet
  1180. for i in range(input_count):
  1181. var inlet_valid = false
  1182. for conn in graph_edit.get_connection_list():
  1183. if str(conn["to_node"]) == node_name and conn["to_port"] == i:
  1184. var src_node = str(conn["from_node"])
  1185. if path_has_input(src_node, graph, input_node_names):
  1186. inlet_valid = true
  1187. break
  1188. if not inlet_valid:
  1189. return false # this inlet cannot reach any input
  1190. return true
  1191. # Step backwards from a node to see if a path exists to any input node
  1192. func path_has_input(current: String, graph: Dictionary, input_node_names: Array, visited: Dictionary = {}) -> bool:
  1193. if current in input_node_names:
  1194. return true
  1195. if current in visited:
  1196. return false
  1197. visited[current] = true
  1198. # Check all nodes that lead to current
  1199. for conn in graph_edit.get_connection_list():
  1200. if str(conn["to_node"]) == current:
  1201. var src_node = str(conn["from_node"])
  1202. if path_has_input(src_node, graph, input_node_names, visited.duplicate()):
  1203. return true
  1204. return false
  1205. #func path_exists_through_all_nodes() -> bool:
  1206. #var graph = {}
  1207. #var input_node_names = []
  1208. #var output_node_name = ""
  1209. #
  1210. ## Gather nodes and build empty graph
  1211. #for child in graph_edit.get_children():
  1212. #if child is GraphNode:
  1213. #var name = str(child.name)
  1214. #var command = child.get_meta("command")
  1215. #var input = child.get_meta("input")
  1216. #
  1217. #if input:
  1218. #input_node_names.append(name)
  1219. #elif command == "outputfile":
  1220. #output_node_name = name
  1221. #
  1222. #graph[name] = [] # Initialize adjacency list
  1223. #
  1224. ## Add connections (edges)
  1225. #for conn in graph_edit.get_connection_list():
  1226. #var from = str(conn["from_node"])
  1227. #var to = str(conn["to_node"])
  1228. #if graph.has(from):
  1229. #graph[from].append(to)
  1230. #
  1231. ## BFS to check if any input node reaches the output
  1232. #for input_node in input_node_names:
  1233. #var visited = {}
  1234. #var queue = [input_node]
  1235. #
  1236. #while queue.size() > 0:
  1237. #var current = queue.pop_front()
  1238. #
  1239. #if current == output_node_name:
  1240. #return true # Path found
  1241. #
  1242. #if current in visited:
  1243. #continue
  1244. #visited[current] = true
  1245. #
  1246. #for neighbor in graph.get(current, []):
  1247. #queue.append(neighbor)
  1248. #
  1249. ## No path from any input node to output
  1250. #return false
  1251. func log_console(text: String, update: bool) -> void:
  1252. console_output.append_text(text + "\n \n")
  1253. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1254. if update == true:
  1255. await get_tree().process_frame # Optional: ensure UI updates
  1256. func run_command(command: String, args: Array) -> String:
  1257. var is_windows = OS.get_name() == "Windows"
  1258. console_output.append_text(command + " " + " ".join(args) + "\n")
  1259. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1260. await get_tree().process_frame
  1261. 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
  1262. args.insert(0, command)
  1263. args.insert(0, "/C")
  1264. process_info = OS.execute_with_pipe("cmd.exe", args, false)
  1265. else:
  1266. process_info = OS.execute_with_pipe(command, args, false)
  1267. # Check if the process was successfully started
  1268. if !process_info.has("pid"):
  1269. log_console("Failed to start process", true)
  1270. return ""
  1271. process_running = true
  1272. # Start monitoring the process output and status
  1273. return await monitor_process(process_info["pid"], process_info["stdio"], process_info["stderr"])
  1274. func monitor_process(pid: int, stdout: FileAccess, stderr: FileAccess) -> String:
  1275. var output := ""
  1276. while OS.is_process_running(pid):
  1277. await get_tree().process_frame
  1278. while stdout.get_position() < stdout.get_length():
  1279. var line = stdout.get_line()
  1280. output += line
  1281. console_output.append_text(line + "\n")
  1282. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1283. while stderr.get_position() < stderr.get_length():
  1284. var line = stderr.get_line()
  1285. output += line
  1286. console_output.append_text(line + "\n")
  1287. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1288. var exit_code = OS.get_process_exit_code(pid)
  1289. if exit_code == 0:
  1290. if output.contains("ERROR:"): #checks if CDP reported an error but passed exit code 0 anyway
  1291. console_output.append_text("[color=#9c2828][b]Processes failed[/b][/color]\n\n")
  1292. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1293. process_successful = false
  1294. if process_cancelled == false:
  1295. progress_window.hide()
  1296. if !console_window.visible:
  1297. console_window.popup_centered()
  1298. else:
  1299. console_output.append_text("[color=#638382]Processes ran successfully[/color]\n\n")
  1300. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1301. else:
  1302. console_output.append_text("[color=#9c2828][b]Processes failed with exit code: %d[/b][/color]\n" % exit_code + "\n")
  1303. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1304. process_successful = false
  1305. if process_cancelled == false:
  1306. progress_window.hide()
  1307. if !console_window.visible:
  1308. console_window.popup_centered()
  1309. if output.contains("as an internal or external command"): #check for cdprogs location error on windows
  1310. 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")
  1311. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1312. if output.contains("command not found"): #check for cdprogs location error on unix systems
  1313. 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")
  1314. console_output.scroll_to_line(console_output.get_line_count() - 1)
  1315. process_running = false
  1316. return output
  1317. # Main cycle detection
  1318. func detect_cycles(graph: Dictionary, loop_nodes: Dictionary) -> bool:
  1319. var visited := {}
  1320. var stack := {}
  1321. for node in graph.keys():
  1322. if _dfs_cycle(node, graph, visited, stack, loop_nodes):
  1323. return true
  1324. return false
  1325. func _dfs_cycle(node: String, graph: Dictionary, visited: Dictionary, stack: Dictionary, loop_nodes: Dictionary) -> bool:
  1326. if not visited.has(node):
  1327. visited[node] = true
  1328. stack[node] = true
  1329. for neighbor in graph[node]:
  1330. # If neighbor hasn't been visited, recurse
  1331. if not visited.has(neighbor):
  1332. if _dfs_cycle(neighbor, graph, visited, stack, loop_nodes):
  1333. # Cycle found down this path
  1334. if not (loop_nodes.has(node) or loop_nodes.has(neighbor)):
  1335. return true
  1336. elif stack.has(neighbor):
  1337. # Back edge found → cycle
  1338. if not (loop_nodes.has(node) or loop_nodes.has(neighbor)):
  1339. return true
  1340. # Done exploring this node
  1341. stack.erase(node)
  1342. return false