This commit is contained in:
Dooho Yi 2023-09-30 19:46:43 +09:00
parent defc59a5d2
commit 0c2fc7d192
11 changed files with 369 additions and 321 deletions

View file

@ -5,47 +5,46 @@ script/source = "extends Control
var _gdpd var _gdpd
var _patch = 0
func _ready():
_gdpd = load(\"res://addons/gdpd/bin/gdpd.gdns\").new()
pass
func _process(delta):
while _gdpd.has_message() :
print(\"got msg\")
var msg = _gdpd.get_next()
print(msg)
func _load_patch(pd_patch) : func _load_patch(pd_patch) :
#separate file name from directory #separate file name from directory
var patch_name = pd_patch.split(\"/\")[-1] var patch_name = pd_patch.split(\"/\")[-1]
var patch_dir = pd_patch.trim_suffix(patch_name) var patch_dir = pd_patch.trim_suffix(patch_name)
#load patch #load patch
_gdpd.openfile(patch_name, patch_dir) _gdpd.openPatch(patch_name, patch_dir)
func _ready() :
_gdpd = load(\"res://addons/gdpd/bin/gdpd.gdns\").new()
func _process(delta) :
while _gdpd.has_message() :
var msg = _gdpd.get_next()
print(msg)
if msg[0] == \"random\" :
# print(\"r\")
pass
func _on_Start_pressed() : func _on_Start_pressed() :
var inps = _gdpd.get_available_input_devices() var inps = _gdpd.get_available_input_devices()
var outs = _gdpd.get_available_output_devices() var outs = _gdpd.get_available_output_devices()
# _gdpd.init(0, 2, 48000, 64)
_gdpd.init_devices(inps[0], outs[0]) _gdpd.init_devices(inps[0], outs[0])
#the patch path should be the absolute one #the patch path should be the absolute one
_load_patch(ProjectSettings.globalize_path(\"res://patch1.pd\")) _load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_load_patch(ProjectSettings.globalize_path(\"res://patch2.pd\")) _load_patch(ProjectSettings.globalize_path(\"res://patch.pd\"))
_gdpd.start_message(1) _gdpd.subscribe(\"toGodot\")
_gdpd.add_symbol(\"from godot\")
_gdpd.finish_list(\"blup\")
_gdpd.subscribe(\"toBfWeb\")
func _on_Stop_pressed() : func _on_Stop_pressed() :
_gdpd.closefile(\"patch1.pd\") _gdpd.closeAllPatches()
_gdpd.closefile(\"patch2.pd\")
_gdpd.stop() _gdpd.stop()
func _on_Send_pressed() :
_gdpd.start_message(1)
_gdpd.add_float(300)
_gdpd.finish_list(\"fromGodot\")
" "
@ -58,28 +57,38 @@ __meta__ = {
} }
[node name="Stop" type="Button" parent="."] [node name="Stop" type="Button" parent="."]
margin_left = 73.0 anchor_left = 0.5
margin_top = 198.0 anchor_top = 0.5
margin_right = 176.0 anchor_right = 0.5
margin_bottom = 244.0 anchor_bottom = 0.5
margin_left = -50.0
margin_top = 75.0
margin_right = 50.0
margin_bottom = 125.0
text = "Stop" text = "Stop"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Start" type="Button" parent="."] [node name="Start" type="Button" parent="."]
margin_left = 83.0 anchor_left = 0.5
margin_top = 93.0 anchor_top = 0.5
margin_right = 187.0 anchor_right = 0.5
margin_bottom = 144.0 anchor_bottom = 0.5
margin_left = -50.0
margin_top = -125.0
margin_right = 50.0
margin_bottom = -75.0
text = "Start" text = "Start"
[node name="Load" type="Button" parent="."] [node name="Send" type="Button" parent="."]
margin_left = 103.0 anchor_left = 0.5
margin_top = 159.0 anchor_top = 0.5
margin_right = 145.0 anchor_right = 0.5
margin_bottom = 179.0 anchor_bottom = 0.5
text = "Load" margin_left = -50.0
margin_top = -25.0
margin_right = 50.0
margin_bottom = 25.0
text = "send"
[connection signal="pressed" from="Stop" to="." method="_on_Stop_pressed"] [connection signal="pressed" from="Stop" to="." method="_on_Stop_pressed"]
[connection signal="pressed" from="Start" to="." method="_on_Start_pressed"] [connection signal="pressed" from="Start" to="." method="_on_Start_pressed"]
[connection signal="pressed" from="Load" to="." method="_on_Load_pressed"] [connection signal="pressed" from="Send" to="." method="_on_Send_pressed"]

View file

@ -6,4 +6,3 @@
resource_name = "gdpd" resource_name = "gdpd"
class_name = "Gdpd" class_name = "Gdpd"
library = ExtResource( 1 ) library = ExtResource( 1 )
_sections_unfolded = [ "Resource" ]

Binary file not shown.

View file

@ -28,6 +28,7 @@ process/fix_alpha_border=true
process/premult_alpha=false process/premult_alpha=false
process/HDR_as_SRGB=false process/HDR_as_SRGB=false
process/invert_color=false process/invert_color=false
process/normal_map_invert_y=false
stream=false stream=false
size_limit=0 size_limit=0
detect_3d=true detect_3d=true

28
GdpdExample/patch.pd Normal file
View file

@ -0,0 +1,28 @@
#N canvas 427 209 450 357 12;
#X obj 292 61 loadbang;
#X msg 292 91 \; pd dsp 1;
#X obj 92 86 t b;
#X obj 61 38 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 300 299 dac~;
#X obj 300 239 osc~ 440;
#X obj 300 269 *~ 0.1;
#X obj 300 142 random 900;
#X obj 300 166 + 500;
#X floatatom 300 190 5 0 0 0 - - - 0;
#X obj 187 39 r fromGodot;
#X obj 117 308 s toGodot;
#X obj 117 278 list;
#X msg 117 248 random \$1;
#X connect 0 0 1 0;
#X connect 2 0 7 0;
#X connect 3 0 2 0;
#X connect 5 0 6 0;
#X connect 6 0 4 0;
#X connect 6 0 4 1;
#X connect 7 0 8 0;
#X connect 8 0 9 0;
#X connect 9 0 5 0;
#X connect 9 0 13 0;
#X connect 10 0 2 0;
#X connect 12 0 11 0;
#X connect 13 0 12 0;

View file

@ -0,0 +1,15 @@
#N canvas 436 326 450 300 12;
#X obj 116 235 dac~;
#X obj 116 175 osc~ 440;
#X obj 116 205 *~ 0.1;
#X obj 116 78 random 900;
#X obj 116 102 + 500;
#X floatatom 116 126 5 0 0 0 - - - 0;
#X obj 99 29 inlet;
#X connect 1 0 2 0;
#X connect 2 0 0 0;
#X connect 2 0 0 1;
#X connect 3 0 4 0;
#X connect 4 0 5 0;
#X connect 5 0 1 0;
#X connect 6 0 3 0;

View file

@ -1,29 +1,17 @@
#N canvas 2 268 738 330 12; #N canvas 203 306 402 347 12;
#X obj 344 82 s pd; #X obj 292 61 loadbang;
#X obj 344 31 loadbang; #X obj 187 39 r blup;
#X msg 344 55 dsp 1; #X msg 292 91 \; pd dsp 1;
#X obj 152 134 dac~; #X obj 145 207 clone patch1-node 2;
#X obj 152 86 osc~ 440; #X floatatom 41 256 5 0 0 0 - - - 0;
#X obj 152 110 *~ 0.1; #X msg 41 279 vis \$1 1;
#X obj 240 91 loadbang; #X msg 180 128 all bang;
#X obj 241 141 metro 1000; #X obj 92 86 t b;
#X msg 240 168 patch1 0; #X obj 61 38 bng 19 250 50 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000;
#X obj 564 191 print patch1; #X connect 0 0 2 0;
#X obj 491 27 r blup; #X connect 1 0 7 0;
#X obj 506 91 print patch1;
#X obj 239 255 s toBfWeb;
#X obj 559 148 r toBfWeb;
#X obj 240 193 list append;
#X obj 491 51 t b b;
#X connect 1 0 2 0;
#X connect 2 0 0 0;
#X connect 4 0 5 0; #X connect 4 0 5 0;
#X connect 5 0 3 0; #X connect 5 0 3 0;
#X connect 5 0 3 1; #X connect 6 0 3 0;
#X connect 6 0 7 0; #X connect 7 0 6 0;
#X connect 7 0 8 0; #X connect 8 0 7 0;
#X connect 8 0 14 0;
#X connect 10 0 15 0;
#X connect 13 0 9 0;
#X connect 14 0 12 0;
#X connect 15 1 11 0;

View file

@ -10,7 +10,6 @@ config_version=4
_global_script_classes=[ ] _global_script_classes=[ ]
_global_script_class_icons={ _global_script_class_icons={
} }
[application] [application]
@ -20,9 +19,15 @@ run/main_scene="res://Main.tscn"
run/low_processor_mode=true run/low_processor_mode=true
config/icon="res://icon.png" config/icon="res://icon.png"
[display]
window/size/width=480
window/size/height=640
window/stretch/mode="2d"
window/stretch/aspect="keep"
[rendering] [rendering]
quality/driver/driver_name="GLES2"
vram_compression/import_etc=true vram_compression/import_etc=true
vram_compression/import_etc2=false vram_compression/import_etc2=false
environment/default_environment="res://default_env.tres" environment/default_environment="res://default_env.tres"

View file

@ -3,15 +3,13 @@
using namespace godot; using namespace godot;
void Gdpd::_register_methods() { void Gdpd::_register_methods() {
register_method("get_available_input_devices", register_method("get_available_input_devices", &Gdpd::get_available_input_devices);
&Gdpd::get_available_input_devices); register_method("get_available_output_devices", &Gdpd::get_available_output_devices);
register_method("get_available_output_devices",
&Gdpd::get_available_output_devices);
register_method("init_devices", &Gdpd::init_devices); register_method("init_devices", &Gdpd::init_devices);
register_method("init", &Gdpd::init); register_method("init", &Gdpd::init);
register_method("stop", &Gdpd::stop); register_method("stop", &Gdpd::stop);
register_method("openfile", &Gdpd::openfile); register_method("openPatch", &Gdpd::openPatch);
register_method("closefile", &Gdpd::closefile); register_method("closeAllPatches", &Gdpd::closeAllPatches);
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);
@ -42,6 +40,7 @@ void Gdpd::_init() {
} }
Gdpd::~Gdpd() { Gdpd::~Gdpd() {
} }
Array Gdpd::get_available_input_devices() { Array Gdpd::get_available_input_devices() {
@ -111,7 +110,6 @@ int Gdpd::start() {
print("Output channels = "+std::to_string(outParams.nChannels)); print("Output channels = "+std::to_string(outParams.nChannels));
print("Input channels = "+std::to_string(inpParams.nChannels)); print("Input channels = "+std::to_string(inpParams.nChannels));
if(!m_pd.init(m_nbInputs, m_nbOutputs, m_sampleRate, true)) { if(!m_pd.init(m_nbInputs, m_nbOutputs, m_sampleRate, true)) {
print("GDPD : Error starting libpd"); print("GDPD : Error starting libpd");
return 1; return 1;
@ -133,6 +131,18 @@ int Gdpd::start() {
if(m_audio.getCurrentApi() != RtAudio::MACOSX_CORE) { if(m_audio.getCurrentApi() != RtAudio::MACOSX_CORE) {
options.flags |= RTAUDIO_MINIMIZE_LATENCY; options.flags |= RTAUDIO_MINIMIZE_LATENCY;
} }
if (m_nbInputs == 0) {
try {
m_audio.openStream(&outParams, NULL, RTAUDIO_FLOAT32,
m_sampleRate, &m_bufferFrames, &audioCallback,
this, &options);
m_audio.startStream();
print("Stream started");
}
catch(RtAudioError& e) {
Godot::print(e.getMessage().c_str());
}
} else {
try { try {
m_audio.openStream(&outParams, &inpParams, RTAUDIO_FLOAT32, m_audio.openStream(&outParams, &inpParams, RTAUDIO_FLOAT32,
m_sampleRate, &m_bufferFrames, &audioCallback, m_sampleRate, &m_bufferFrames, &audioCallback,
@ -143,6 +153,7 @@ int Gdpd::start() {
catch(RtAudioError& e) { catch(RtAudioError& e) {
Godot::print(e.getMessage().c_str()); Godot::print(e.getMessage().c_str());
} }
}
//create message hook //create message hook
m_pd.subscribe("to_gdpd"); m_pd.subscribe("to_gdpd");
@ -174,17 +185,12 @@ void Gdpd::processAudio(void *outputBuffer, void *inputBuffer,
} }
} }
void Gdpd::openfile(godot::String baseStr, godot::String dirStr) { void 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());
if(m_patchsMap.find(baseS)!=m_patchsMap.end()) {
print("Patch "+baseS+" already opened");
return;
}
//libpd_openfile(baseS.c_str(), dirS.c_str()); //libpd_openfile(baseS.c_str(), dirS.c_str());
//m_patch = m_pd.openPatch(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());
@ -193,7 +199,7 @@ void Gdpd::openfile(godot::String baseStr, godot::String dirStr) {
} }
else { else {
print("Opened patch "+baseS); print("Opened patch "+baseS);
m_patchsMap[baseS] = p1; m_patches.push_back(p1);
} }
//m_pd.subscribe("to_gdpd"); //m_pd.subscribe("to_gdpd");
@ -207,15 +213,11 @@ void Gdpd::openfile(godot::String baseStr, godot::String dirStr) {
*/ */
} }
void Gdpd::closefile(godot::String baseStr) { void Gdpd::closeAllPatches() {
std::wstring baseWs = baseStr.unicode_str(); for (int idx = 0; idx < m_patches.size(); idx++) {
std::string baseS(baseWs.begin(), baseWs.end()); m_pd.closePatch(m_patches[idx]);
if(m_patchsMap.find(baseS)!=m_patchsMap.end()) {
m_pd.closePatch(m_patchsMap[baseS]);
m_patchsMap.erase(baseS);
print("Closed patch "+baseS);
} }
//m_pd.closePatch(baseS.c_str()); m_patches.clear();
} }
void Gdpd::subscribe(String symbStr) { void Gdpd::subscribe(String symbStr) {
@ -288,4 +290,3 @@ void Gdpd::receiveList(const std::string& dest, const pd::List& list) {
void Gdpd::set_volume(float vol) { void Gdpd::set_volume(float vol) {
m_vol=vol; m_vol=vol;
} }

View file

@ -32,7 +32,7 @@ private:
int m_sampleRate; int m_sampleRate;
int m_inputDevice; int m_inputDevice;
int m_outputDevice; int m_outputDevice;
std::map<std::string, pd::Patch> m_patchsMap; std::vector<pd::Patch> m_patches;
bool m_init; bool m_init;
@ -51,8 +51,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 openfile(String basename, String dirname); void openPatch(String basename, String dirname);
void closefile(String basename); void closeAllPatches();
bool has_message(); bool has_message();
Array get_next(); Array get_next();
int blocksize(); int blocksize();
@ -68,7 +68,9 @@ public:
//godot functions //godot functions
void set_volume(float vol); void set_volume(float vol);
inline const float& get_volume(){return m_vol;} inline const float& get_volume(){
return m_vol;
}
//rtaudio //rtaudio

Binary file not shown.