Skip to content

Commit

Permalink
ysfx: add strcpy api
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Jul 7, 2024
1 parent a19e02b commit b7e62de
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake.tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ add_executable(ysfx_tests
"tests/ysfx_test_audio_flac.cpp"
"tests/ysfx_test_filesystem.cpp"
"tests/ysfx_test_preset.cpp"
"tests/ysfx_test_integration.cpp"
"tests/ysfx_test_c_api.c"
"tests/ysfx_test_utils.hpp"
"tests/ysfx_test_utils.cpp"
Expand Down
2 changes: 2 additions & 0 deletions include/ysfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ YSFX_API bool ysfx_slider_is_enum(ysfx_t *fx, uint32_t index);
YSFX_API uint32_t ysfx_slider_get_enum_names(ysfx_t *fx, uint32_t index, const char **dest, uint32_t destsize);
// get a single label for the enumeration slider
YSFX_API const char *ysfx_slider_get_enum_name(ysfx_t *fx, uint32_t slider_index, uint32_t enum_index);
// get slider base path
const char *ysfx_slider_path(ysfx_t *fx, uint32_t slider_index);
// get whether the slider is a path (implies enumeration)
YSFX_API bool ysfx_slider_is_path(ysfx_t *fx, uint32_t index);
// get whether the slider is initially visible
Expand Down
13 changes: 13 additions & 0 deletions sources/ysfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,19 @@ const char *ysfx_slider_get_enum_name(ysfx_t *fx, uint32_t slider_index, uint32_
return slider.enum_names[enum_index].c_str();
}

const char *ysfx_slider_path(ysfx_t *fx, uint32_t slider_index) {
ysfx_source_unit_t *main = fx->source.main.get();
if (slider_index >= ysfx_max_sliders || !main)
return 0;

ysfx_slider_t &slider = main->header.sliders[slider_index];
if (slider.path.empty()) {
return 0;
} else {
return slider.path.c_str();
}
}

bool ysfx_slider_is_path(ysfx_t *fx, uint32_t index)
{
ysfx_source_unit_t *main = fx->source.main.get();
Expand Down
37 changes: 37 additions & 0 deletions sources/ysfx_api_reaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,41 @@ static EEL_F NSEEL_CGEN_CALL ysfx_api_midirecv_str(void *opaque, EEL_F *offset_,
return event.size;
}


static EEL_F NSEEL_CGEN_CALL ysfx_api_strcpy_from_slider(void *opaque, EEL_F *str_, EEL_F *slider_)
{
int32_t handle = ysfx_eel_round<int32_t>(*slider_);
if (handle < 0)
return 0;

ysfx_t *fx = REAPER_GET_INTERFACE(opaque);
uint32_t slider_idx = ysfx_get_slider_of_var(fx, slider_);
if (slider_idx >= ysfx_max_sliders) {
return 0;
}

if (!ysfx_slider_is_enum(fx, slider_idx)) {
return 0;
}

uint32_t enum_idx = static_cast<uint32_t>(ysfx_slider_get_value(fx, slider_idx));
std::string root{ysfx_slider_path(fx, slider_idx)};
root.erase(0, 1);
std::string name{ysfx_slider_get_enum_name(fx, slider_idx, enum_idx)};
std::string full_name = root + "/" + name;

auto process_str = [](void *userdata, WDL_FastString &str) {
std::string *value = (std::string *)userdata;
str.SetRaw(value->c_str(), (int32_t)value->size());
};

if (!ysfx_string_access(fx, *str_, true, +process_str, &full_name))
return 0;

return 1;
}


//------------------------------------------------------------------------------
void ysfx_api_init_reaper()
{
Expand All @@ -436,4 +471,6 @@ void ysfx_api_init_reaper()
NSEEL_addfunc_retval("midirecv_buf", 3, NSEEL_PProc_THIS, &ysfx_api_midirecv_buf);
NSEEL_addfunc_retval("midirecv_str", 2, NSEEL_PProc_THIS, &ysfx_api_midirecv_str);
NSEEL_addfunc_retval("midisyx", 3, NSEEL_PProc_THIS, &ysfx_api_midisyx);

NSEEL_addfunc_retval("strcpy_fromslider", 2, NSEEL_PProc_THIS, &ysfx_api_strcpy_from_slider);
}
73 changes: 73 additions & 0 deletions tests/ysfx_test_integration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2024 Joep Vanlier
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//

#include "ysfx.h"
#include "ysfx_test_utils.hpp"
#include "ysfx_api_eel.hpp"
#include <catch.hpp>

#include <iostream>

TEST_CASE("integration", "[integration]")
{
SECTION("strcpy_from_slider")
{
const char *text =
"desc:example" "\n"
"out_pin:output" "\n"
"slider43:/filedir:blip.txt:Directory test" "\n"
"@init" "\n"
"slider43 = 1;" "\n"
"x = 5;" "\n"
"strcpy_fromslider(x, slider43);" "\n"
"slider43 = 2;" "\n"
"x = 6;" "\n"
"strcpy_fromslider(x, slider43);" "\n"
"slider43 = 0;" "\n"
"x = 7;" "\n"
"strcpy_fromslider(x, slider43);" "\n"
"@sample" "\n"
"spl0=0.0;" "\n";

scoped_new_dir dir_fx("${root}/Effects");
scoped_new_txt file_main("${root}/Effects/example.jsfx", text);

scoped_new_dir dir_data("${root}/Data");
scoped_new_dir dir_data2("${root}/Data/filedir");
scoped_new_txt f1("${root}/Data/filedir/blip.txt", "blah");
scoped_new_txt f2("${root}/Data/filedir/blap.txt", "bloo");
scoped_new_txt f3("${root}/Data/filedir/blop.txt", "bloo");

ysfx_config_u config{ysfx_config_new()};
ysfx_set_data_root(config.get(), dir_data.m_path.c_str());
ysfx_u fx{ysfx_new(config.get())};

REQUIRE(ysfx_load_file(fx.get(), file_main.m_path.c_str(), 0));
REQUIRE(ysfx_compile(fx.get(), 0));
ysfx_init(fx.get());

std::string txt;
ysfx_string_get(fx.get(), 5, txt);
REQUIRE((txt == "filedir/blip.txt"));

ysfx_string_get(fx.get(), 6, txt);
REQUIRE((txt == "filedir/blop.txt"));

ysfx_string_get(fx.get(), 7, txt);
REQUIRE((txt == "filedir/blap.txt"));
};
}

0 comments on commit b7e62de

Please sign in to comment.