Skip to content

Commit

Permalink
Change the Reset mechanism (#1309)
Browse files Browse the repository at this point in the history
The reset mechanism used to try to remove everything bit by
bit. error prone.

Instead, do the obbious thing of unstream a blank engine state.

Unfortunatley we can't with current APIs do a Multi from a memory
stream only a file, so rather than just embed a multi make a little
helper program to just make raw binary of engine state and put it
in resources etc...

Closes #1301
Closes #1162
Closes #1123
  • Loading branch information
baconpaul authored Sep 11, 2024
1 parent 8b729d3 commit bab2c50
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 10 deletions.
1 change: 1 addition & 0 deletions clients/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ project(scxt-clients LANGUAGES CXX)

add_subdirectory(clap-first)
add_subdirectory(sfz-token-dump)
add_subdirectory(init-maker)
5 changes: 5 additions & 0 deletions clients/init-maker/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(init-maker)

message(STATUS "Making ${PROJECT_NAME} executable")
add_executable(${PROJECT_NAME} init-maker.cpp)
target_link_libraries(${PROJECT_NAME} scxt-core)
63 changes: 63 additions & 0 deletions clients/init-maker/init-maker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Shortcircuit XT - a Surge Synth Team product
*
* A fully featured creative sampler, available as a standalone
* and plugin for multiple platforms.
*
* Copyright 2019 - 2024, Various authors, as described in the github
* transaction log.
*
* ShortcircuitXT is released under the Gnu General Public Licence
* V3 or later (GPL-3.0-or-later). The license is found in the file
* "LICENSE" in the root of this repository or at
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* Individual sections of code which comprises ShortcircuitXT in this
* repository may also be used under an MIT license. Please see the
* section "Licensing" in "README.md" for details.
*
* ShortcircuitXT is inspired by, and shares code with, the
* commercial product Shortcircuit 1 and 2, released by VemberTech
* in the mid 2000s. The code for Shortcircuit 2 was opensourced in
* 2020 at the outset of this project.
*
* All source for ShortcircuitXT is available at
* https://github.com/surge-synthesizer/shortcircuit-xt
*/

#include <fstream>

#include "engine/engine.h"
#include "json/scxt_traits.h"
#include "json/engine_traits.h"

#include "tao/json/msgpack/to_string.hpp"

int main(int argc, char **argv)
{
if (argc != 2)
{
SCLOG("Usage: " << argv[0] << " out-path");
exit(1);
}
auto e = std::make_unique<scxt::engine::Engine>();
auto sg = scxt::engine::Engine::StreamGuard(scxt::engine::Engine::FOR_MULTI);
auto msg = tao::json::msgpack::to_string(scxt::json::scxt_value(*e));

SCLOG("Message is of size " << msg.size());

auto p = fs::path(argv[1]);

SCLOG("Writing to file " << p.u8string());

std::ofstream f(p, std::ios::binary);
if (!f.is_open())
{
SCLOG("Unable to open file");
exit(2);
}

f.write(msg.c_str(), msg.size());

SCLOG("Output complete");
}
7 changes: 7 additions & 0 deletions resources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ cmrc_add_resource_library(${PROJECT_NAME}
"opensource-credits.txt"
)
set_target_properties(scxt-resources PROPERTIES UNITY_BUILD FALSE)


cmrc_add_resource_library(${PROJECT_NAME}-core
NAMESPACE scxt_resources_core
InitSettings.dat
)
set_target_properties(scxt-resources-core PROPERTIES UNITY_BUILD FALSE)
Binary file added resources/InitSettings.dat
Binary file not shown.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
minimp3
md5sum::md5

scxt-resources-core

sc-compiler-options
)
13 changes: 3 additions & 10 deletions src/messaging/client/interaction_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "selection/selection_manager.h"
#include "engine/engine.h"
#include "client_macros.h"
#include "patch_io/patch_io.h"

namespace scxt::messaging::client
{
Expand Down Expand Up @@ -83,17 +84,9 @@ inline void doHostCallback(uint64_t pl, MessageController &cont)
CLIENT_TO_SERIAL(RequestHostCallback, c2s_request_host_callback, uint64_t,
doHostCallback(payload, cont));

inline void doResetEngine(bool pl, const engine::Engine &e, MessageController &cont)
inline void doResetEngine(bool pl, engine::Engine &e, MessageController &cont)
{
cont.scheduleAudioThreadCallback(
[](auto &engine) {
engine.stopAllSounds();
engine.getPatch()->resetToBlankPatch();
},
[](auto &engine) {
engine.getSelectionManager()->clearAllSelections();
engine.sendFullRefreshToClient();
});
scxt::patch_io::initFromResourceBundle(e);
}
CLIENT_TO_SERIAL(ResetEngine, c2s_reset_engine, bool, doResetEngine(payload, engine, cont));

Expand Down
54 changes: 54 additions & 0 deletions src/patch_io/patch_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@

#include "json/engine_traits.h"

#include "cmrc/cmrc.hpp"

CMRC_DECLARE(scxt_resources_core);

namespace scxt::patch_io
{
void addSCManifest(const std::unique_ptr<RIFF::File> &f, const std::string &type)
Expand Down Expand Up @@ -108,6 +112,56 @@ bool saveMulti(const fs::path &p, const scxt::engine::Engine &e)
return true;
}

bool initFromResourceBundle(scxt::engine::Engine &engine)
{
SCLOG("Init From Resource Bundle");

std::string payload;

try
{
auto fs = cmrc::scxt_resources_core::get_filesystem();
auto fntf = fs.open("InitSettings.dat");
payload = std::string(fntf.begin(), fntf.end());
}
catch (std::exception &e)
{
SCLOG("Exception opening payload");
return false;
}
SCLOG("Init payload has size " << payload.size());
auto &cont = engine.getMessageController();
if (cont->isAudioRunning)
{
cont->stopAudioThreadThenRunOnSerial([payload, &nonconste = engine](auto &e) {
try
{
nonconste.stopAllSounds();
scxt::json::unstreamEngineState(nonconste, payload, true);
auto &cont = *e.getMessageController();
cont.restartAudioThreadFromSerial();
}
catch (std::exception &err)
{
SCLOG("Unable to load [" << err.what() << "]");
}
});
}
else
{
try
{
engine.stopAllSounds();
scxt::json::unstreamEngineState(engine, payload, true);
}
catch (std::exception &err)
{
SCLOG("Unable to load [" << err.what() << "]");
}
}
return true;
}

bool loadMulti(const fs::path &p, scxt::engine::Engine &engine)
{
SCLOG("loadMulti " << p.u8string());
Expand Down
2 changes: 2 additions & 0 deletions src/patch_io/patch_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ bool saveMulti(const fs::path &toFile, const scxt::engine::Engine &);
bool loadMulti(const fs::path &fromFile, scxt::engine::Engine &);
bool streamPart(const fs::path &toFile, const scxt::engine::Part &);
bool unstreamPart(const fs::path &fromFile, scxt::engine::Part &);

bool initFromResourceBundle(scxt::engine::Engine &e);
} // namespace scxt::patch_io

#endif // SHORTCIRCUITXT_PATCH_IO_H

0 comments on commit bab2c50

Please sign in to comment.