extends Node



#scene changing
var _scenelist = ["res://mainScene/Main.tscn", "res://oscScene/oscScene.tscn"]
var _sceneidx = 0

func nextScene():
	_sceneidx = (_sceneidx + 1) % _scenelist.size()
	get_tree().change_scene(_scenelist[_sceneidx])




#gdpd (pd interface with godot)
var _gdpd
var _patches = []

func _ready():
	_gdpd = load("res://addons/gdpd/bin/gdpd.gdns").new()
	var inps = _gdpd.get_available_input_devices()
	var outs = _gdpd.get_available_output_devices()
	_gdpd.init_devices(inps[0], outs[0])
	_gdpd.subscribe("toGodot")

func _exit_tree():
	if _patches.size() != 0:
		print()
		print("! ======== * purging leftover opened patches ... * ======== !")
		print()
		for id in _patches:
			_gdpd.closePatch(id)
		_patches.clear()
	_gdpd.stop()

func load_patch(pd_patch) -> int:
	#the patch path should be the absolute one
	#separate file name from directory
	var patch_name = pd_patch.split("/")[-1]
	var patch_dir = pd_patch.trim_suffix(patch_name)

	#load patch
	var id = _gdpd.openPatch(patch_name, patch_dir)
	_patches.append(id)

	return id

func close_patch(id):
	if id in _patches:
		_gdpd.closePatch(id)
		_patches.erase(id)

func _process(_delta) :
	while _gdpd.has_message():
		var msg = _gdpd.get_next()
		print(msg)
#		if msg[0] == "random":
#			print("r")
