updated ...

This commit is contained in:
Dooho Yi 2023-10-11 18:39:12 +09:00
parent 0c2fc7d192
commit 38de570162
10 changed files with 99 additions and 28 deletions

View file

@ -4,6 +4,7 @@
script/source = "extends Control script/source = "extends Control
var _gdpd var _gdpd
var _patches = []
func _load_patch(pd_patch) : func _load_patch(pd_patch) :
#separate file name from directory #separate file name from directory
@ -11,7 +12,9 @@ func _load_patch(pd_patch) :
var patch_dir = pd_patch.trim_suffix(patch_name) var patch_dir = pd_patch.trim_suffix(patch_name)
#load patch #load patch
_gdpd.openPatch(patch_name, patch_dir) var id = _gdpd.openPatch(patch_name, patch_dir)
_patches.append(id)
# print(\"loading:\" + str(id))
func _ready() : func _ready() :
_gdpd = load(\"res://addons/gdpd/bin/gdpd.gdns\").new() _gdpd = load(\"res://addons/gdpd/bin/gdpd.gdns\").new()
@ -33,12 +36,25 @@ func _on_Start_pressed() :
#the patch path should be the absolute one #the patch path should be the absolute one
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\")) _load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\")) _load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_gdpd.subscribe(\"toGodot\") _gdpd.subscribe(\"toGodot\")
func _on_Stop_pressed() : func _on_Stop_pressed() :
print(_patches)
_gdpd.closeAllPatches() for id in _patches:
# print(\"closing:\" + str(id))
_gdpd.closePatch(id)
# _patches.erase(id) # <= BAD!
# Do not erase entries while iterating over the array !!
_patches.clear()
_gdpd.stop() _gdpd.stop()
func _on_Send_pressed() : func _on_Send_pressed() :

View file

@ -0,0 +1,8 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://addons/gdpd/bin/libgdpd.gdnlib" type="GDNativeLibrary" id=1]
[resource]
resource_name = "gdpd"
class_name = "Gdpd"
library = ExtResource( 1 )

View file

@ -0,0 +1,18 @@
[general]
singleton=false
load_once=false
symbol_prefix="godot_"
reloadable=true
[entry]
X11.64="res://addons/gdpd/bin/x11/libgdpd.so"
Windows.64="res://addons/gdpd/bin/win/libgdpd.dll"
OSX.64="res://addons/gdpd/bin/osx/libgdpd.dylib"
[dependencies]
X11.64=[ ]
Windows.64=[ ]
OSX.64=[ ]

Binary file not shown.

View file

@ -0,0 +1,8 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://addons/gdpd/bin/libgdpd.gdnlib" type="GDNativeLibrary" id=1]
[resource]
resource_name = "gdpd"
class_name = "Gdpd"
library = ExtResource( 1 )

View file

@ -0,0 +1,18 @@
[general]
singleton=false
load_once=false
symbol_prefix="godot_"
reloadable=true
[entry]
X11.64="res://addons/gdpd/bin/x11/libgdpd.so"
Windows.64="res://addons/gdpd/bin/win/libgdpd.dll"
OSX.64="res://addons/gdpd/bin/osx/libgdpd.dylib"
[dependencies]
X11.64=[ ]
Windows.64=[ ]
OSX.64=[ ]

Binary file not shown.

View file

@ -9,7 +9,7 @@ void Gdpd::_register_methods() {
register_method("init", &Gdpd::init); register_method("init", &Gdpd::init);
register_method("stop", &Gdpd::stop); register_method("stop", &Gdpd::stop);
register_method("openPatch", &Gdpd::openPatch); register_method("openPatch", &Gdpd::openPatch);
register_method("closeAllPatches", &Gdpd::closeAllPatches); register_method("closePatch", &Gdpd::closePatch);
register_method("subscribe", &Gdpd::subscribe); register_method("subscribe", &Gdpd::subscribe);
register_method("has_message", &Gdpd::has_message); register_method("has_message", &Gdpd::has_message);
register_method("get_next", &Gdpd::get_next); register_method("get_next", &Gdpd::get_next);
@ -117,6 +117,10 @@ int Gdpd::start() {
//libpd_set_verbose(1); //libpd_set_verbose(1);
#if defined(WITH_GUI)
libpd_start_gui((char*)"/Applications/Pd-0.53-1.app/Contents/Resources");
#endif
//start dsp //start dsp
m_pd.computeAudio(true); m_pd.computeAudio(true);
@ -170,6 +174,10 @@ void Gdpd::stop() {
m_audio.closeStream(); m_audio.closeStream();
m_pd.computeAudio(false); m_pd.computeAudio(false);
print("Stopped"); print("Stopped");
#if defined(WITH_GUI)
libpd_stop_gui();
#endif
} }
void Gdpd::processAudio(void *outputBuffer, void *inputBuffer, void Gdpd::processAudio(void *outputBuffer, void *inputBuffer,
@ -185,39 +193,32 @@ void Gdpd::processAudio(void *outputBuffer, void *inputBuffer,
} }
} }
void Gdpd::openPatch(godot::String baseStr, godot::String dirStr) { int Gdpd::openPatch(godot::String baseStr, godot::String dirStr) {
std::wstring baseWs = baseStr.unicode_str(); std::wstring baseWs = baseStr.unicode_str();
std::string baseS(baseWs.begin(), baseWs.end()); std::string baseS(baseWs.begin(), baseWs.end());
std::wstring dirWs = dirStr.unicode_str(); std::wstring dirWs = dirStr.unicode_str();
std::string dirS(dirWs.begin(), dirWs.end()); std::string dirS(dirWs.begin(), dirWs.end());
//libpd_openfile(baseS.c_str(), dirS.c_str());
//m_patch = m_pd.openPatch(baseS.c_str(), dirS.c_str());
pd::Patch p1 = m_pd.openPatch(baseS.c_str(), dirS.c_str()); pd::Patch p1 = m_pd.openPatch(baseS.c_str(), dirS.c_str());
if(!p1.isValid()) { if(!p1.isValid()) {
print("Could not open patch "+baseS); print("Could not open patch "+baseS);
} }
else { else {
print("Opened patch "+baseS); print("Opened patch "+baseS+ " [" + std::to_string(p1.dollarZero()) + "]");
m_patches.push_back(p1); m_patchsMap[p1.dollarZero()] = p1;
} }
//m_pd.subscribe("to_gdpd"); return p1.dollarZero();
/*
if(!m_pd.init(m_nbInputs, m_nbOutputs, m_sampleRate, true)) {
Godot::print("GDPD : Error starting libpd");
}
m_pd.setReceiver(this);
m_pd.computeAudio(true);
*/
} }
void Gdpd::closeAllPatches() { void Gdpd::closePatch(int id) {
for (int idx = 0; idx < m_patches.size(); idx++) { if(m_patchsMap.find(id) != m_patchsMap.end()) {
m_pd.closePatch(m_patches[idx]); // print("Closing patch, id: "+ std::to_string(id));
std::string baseS = m_patchsMap[id].filename();
m_pd.closePatch(m_patchsMap[id]);
m_patchsMap.erase(id);
print("Closed patch "+baseS+ " ["+ std::to_string(id) + "]");
} }
m_patches.clear();
} }
void Gdpd::subscribe(String symbStr) { void Gdpd::subscribe(String symbStr) {

View file

@ -13,6 +13,8 @@
#include "PdReceiver.hpp" #include "PdReceiver.hpp"
#include "RtAudio.h" #include "RtAudio.h"
// #define WITH_GUI
namespace godot { namespace godot {
class Gdpd : public godot::AudioStreamPlayer, public pd::PdReceiver { class Gdpd : public godot::AudioStreamPlayer, public pd::PdReceiver {
@ -32,7 +34,7 @@ private:
int m_sampleRate; int m_sampleRate;
int m_inputDevice; int m_inputDevice;
int m_outputDevice; int m_outputDevice;
std::vector<pd::Patch> m_patches; std::map<int, pd::Patch> m_patchsMap;
bool m_init; bool m_init;
@ -51,8 +53,8 @@ public:
int init(int nbInputs, int nbOutputs, int sampleRate, int bufferSize); int init(int nbInputs, int nbOutputs, int sampleRate, int bufferSize);
int start(); int start();
void stop(); void stop();
void openPatch(String basename, String dirname); int openPatch(String basename, String dirname);
void closeAllPatches(); void closePatch(int id);
bool has_message(); bool has_message();
Array get_next(); Array get_next();
int blocksize(); int blocksize();