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 4a12d6c commit ccc585a
Show file tree
Hide file tree
Showing 3 changed files with 108 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
34 changes: 34 additions & 0 deletions sources/ysfx_api_reaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,38 @@ 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 name{ysfx_slider_get_enum_name(fx, slider_idx, enum_idx)};

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, &name))
return 0;

return 1;
}


//------------------------------------------------------------------------------
void ysfx_api_init_reaper()
{
Expand All @@ -436,4 +468,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 == "blip.txt"));

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

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

0 comments on commit ccc585a

Please sign in to comment.