Skip to content

Commit

Permalink
Config delays
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Dempsey committed Oct 17, 2023
1 parent db48232 commit 673674c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 46 deletions.
16 changes: 8 additions & 8 deletions src/HC-1/HC-1-midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ void Hc1Module::onChannel16CC(uint8_t cc, uint8_t value)
}
DebugLog("End User presets as %s", InitStateName(user_preset_state));
//sendControlChange(EM_SettingsChannel, EMCC_Download, EM_DownloadItem::midiTxFull);
heart_time = post_user_delay;
break;

case EM_DownloadItem::beginSysNames:
Expand All @@ -432,7 +433,7 @@ void Hc1Module::onChannel16CC(uint8_t cc, uint8_t value)
}
DebugLog("End System presets as %s", InitStateName(system_preset_state));
//sendControlChange(EM_SettingsChannel, EMCC_Download, EM_DownloadItem::midiTxFull);
heart_time = 5.f;
heart_time = post_system_delay;
break;
}
break;
Expand Down Expand Up @@ -505,9 +506,8 @@ void Hc1Module::onChannel16Message(const midi::Message& msg)
log_midi = false;
#endif
device_hello_state = InitState::Complete;
if (!cache_system_presets || (hardware == EM_Hardware::Unknown)) {
heart_time = 4.f;
}
notifyDeviceChanged();
notifyPresetChanged();
} else
if (configPending()) {
#ifdef VERBOSE_LOG
Expand Down Expand Up @@ -630,11 +630,11 @@ void Hc1Module::onChannelOneCC(uint8_t cc, uint8_t value)
}

switch (cc) {
// case EMCC_Pedal1:
// DEBUG("EMCC_Pedal1 %d", value);
// case EMCC_Jack1:
// DEBUG("EMCC_Jack1 %d", value);
// break;
// case EMCC_Pedal2:
// DEBUG("EMCC_Pedal2 %d", value);
// case EMCC_Jack2:
// DEBUG("EMCC_Jack2 %d", value);
// break;
// case MidiCC_Hold:
// DEBUG("MidiCC_Hold %d", value);
Expand Down
135 changes: 113 additions & 22 deletions src/HC-1/HC-1-presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
#include "../he_group.hpp"
namespace pachde {

void Hc1Module::tryCachedPresets() {
void Hc1Module::tryCachedPresets()
{
cached_preset_state = InitState::Complete;

if (cache_system_presets && hardware != EM_Hardware::Unknown) {
loadSystemPresets();
if (system_presets.empty()) {
system_preset_state = InitState::Uninitialized;
} else {
heart_time = .05f; // don't need delay after loading a file
}
}

if (cache_user_presets && connection) {
loadUserPresets();
if (user_presets.empty()) {
user_preset_state = InitState::Uninitialized;
} else {
heart_time = .05f; // don't need delay after loading a file
}
}
if (system_presets.empty()) {
system_preset_state = InitState::Uninitialized;
}
if (user_presets.empty()) {
user_preset_state = InitState::Uninitialized;
}

if (InitState::Complete == system_preset_state
Expand All @@ -38,6 +36,10 @@ void Hc1Module::tryCachedPresets() {
}
}
}
if ((InitState::Complete == system_preset_state)
&& (InitState::Complete == user_preset_state)) {
heart_time = .05;
}
}

void Hc1Module::setPresetOrder(PresetOrder order)
Expand All @@ -49,6 +51,10 @@ void Hc1Module::setPresetOrder(PresetOrder order)
}
}

std::string Hc1Module::startupConfigPath() {
return asset::user(format_string("%s/startup.json", pluginInstance->slug.c_str()));
}

std::string Hc1Module::userPresetsPath()
{
if (!connection) return "";
Expand All @@ -62,6 +68,89 @@ std::string Hc1Module::systemPresetsPath()
return asset::user(format_string("%s/%s-system.json", pluginInstance->slug.c_str(), ShortHardwareName(hardware)));
}

void Hc1Module::saveStartupConfig()
{
auto path = startupConfigPath();
if (system::exists(path)) return;

auto dir = system::getDirectory(path);
system::createDirectories(dir);

std::string tmpPath = system::join(dir, TempName(".tmp.json"));
FILE* file = std::fopen(tmpPath.c_str(), "w");
if (!file) {
system::remove(tmpPath);
return;
}

auto root = json_object();
if (!root) return;
DEFER({json_decref(root);});
json_object_set_new(root, "hearbeat-period", json_real(hearbeat_period));
json_object_set_new(root, "post-output-delay", json_real(post_output_delay));
json_object_set_new(root, "post-input-delay", json_real(post_input_delay));
json_object_set_new(root, "post-hello-delay", json_real(post_hello_delay));
json_object_set_new(root, "post-system-delay", json_real(post_system_delay));
json_object_set_new(root, "post-user-delay", json_real(post_user_delay));
json_object_set_new(root, "post-config-delay", json_real(post_config_delay));

json_dumpf(root, file, JSON_INDENT(2));
std::fclose(file);
system::sleep(0.0005);
system::remove(path);
system::sleep(0.0005);
system::rename(tmpPath, path);
}

void Hc1Module::loadStartupConfig()
{
auto path = startupConfigPath();
if (path.empty()) return;

FILE* file = std::fopen(path.c_str(), "r");
if (!file) {
return;
}
DEFER({std::fclose(file);});

json_error_t error;
json_t* root = json_loadf(file, 0, &error);
if (!root) {
DebugLog("Invalid JSON at %d:%d %s in %s", error.line, error.column, error.text, path.c_str());
return;
}
DEFER({json_decref(root);});

json_t* j = json_object_get(root, "hearbeat-period");
if (j) {
hearbeat_period = json_number_value(j);
}
j = json_object_get(root, "post-output-delay");
if (j) {
post_output_delay = json_number_value(j);
}
j = json_object_get(root, "post-input-delay");
if (j) {
post_input_delay = json_number_value(j);
}
j = json_object_get(root, "post-hello-delay");
if (j) {
post_hello_delay = json_number_value(j);
}
j = json_object_get(root, "post-system-delay");
if (j) {
post_system_delay = json_number_value(j);
}
j = json_object_get(root, "post-user-delay");
if (j) {
post_user_delay = json_number_value(j);
}
j = json_object_get(root, "post-config-delay");
if (j) {
post_config_delay = json_number_value(j);
}
}

void Hc1Module::saveUserPresets()
{
if (user_presets.empty()) return;
Expand All @@ -74,20 +163,21 @@ void Hc1Module::saveUserPresets()

auto root = json_object();
if (!root) return;
DEFER({json_decref(root);});
DEFER({json_decref(root);});
userPresetsToJson(root);

std::string tmpPath = system::join(dir, TempName(".tmp.json"));
FILE* file = std::fopen(tmpPath.c_str(), "w");
if (!file) {
system::remove(tmpPath);
return;
std::string tmpPath = system::join(dir, TempName(".tmp.json"));
FILE* file = std::fopen(tmpPath.c_str(), "w");
if (!file) {
system::remove(tmpPath);
return;
}

json_dumpf(root, file, JSON_INDENT(2));
std::fclose(file);
system::sleep(0.0005);
system::remove(path);
system::sleep(0.0001);
system::sleep(0.0005);
system::rename(tmpPath, path);
}

Expand Down Expand Up @@ -116,8 +206,9 @@ void Hc1Module::saveSystemPresets()

json_dumpf(root, file, JSON_INDENT(2));
std::fclose(file);
system::sleep(0.0005);
system::remove(path);
system::sleep(0.0001);
system::sleep(0.0005);
system::rename(tmpPath, path);
}

Expand Down Expand Up @@ -230,7 +321,6 @@ void Hc1Module::systemPresetsToJson(json_t* root)
json_object_set_new(root, "system", jars);
}


std::string Hc1Module::moduleFavoritesPath()
{
if (!connection) return "";
Expand Down Expand Up @@ -295,8 +385,9 @@ void Hc1Module::writeFavoritesFile(const std::string& path)

json_dumpf(root, file, JSON_INDENT(2));
std::fclose(file);
system::sleep(0.0005);
system::remove(path);
system::sleep(0.0001);
system::sleep(0.0005);
system::rename(tmpPath, path);
}

Expand Down
28 changes: 14 additions & 14 deletions src/HC-1/HC-1-process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void Hc1Module::process(const ProcessArgs& args)
heart_phase += args.sampleTime;
if (heart_phase >= heart_time) {
heart_phase -= heart_time;
heart_time = 2.f;
heart_time = hearbeat_period;

if (!anyPending()
&& (notesOn <= 0)
Expand All @@ -303,7 +303,7 @@ void Hc1Module::process(const ProcessArgs& args)
if (InitState::Uninitialized == device_output_state) {
initOutputDevice();
if (InitState::Complete == device_output_state) {
heart_time = 5.f;
heart_time = post_output_delay;
}
return;
}
Expand All @@ -314,7 +314,7 @@ void Hc1Module::process(const ProcessArgs& args)
assert((connection->input_device_id == midi::Input::getDeviceId())); // subscribing failed: should handle it?
midi::Input::setChannel(-1);
device_input_state = InitState::Complete;
heart_time = 4.f;
heart_time = post_input_delay;
}
return;
}
Expand All @@ -324,21 +324,21 @@ void Hc1Module::process(const ProcessArgs& args)
return;
}

if (system_preset_state != InitState::Complete) {
if (InitState::Broken != system_preset_state) {
tryCachedPresets();
if (system_preset_state != InitState::Complete) {
heart_time = 4.f;
return;
}
}
if (system_preset_state != InitState::Complete) {
transmitRequestSystemPresets();
if (InitState::Uninitialized == cached_preset_state) {
tryCachedPresets();
if (system_preset_state != InitState::Complete
|| user_preset_state != InitState::Complete) {
heart_time = post_hello_delay;
return;
}
}

if (system_preset_state != InitState::Complete) {
transmitRequestSystemPresets();
return;
}

if (InitState::Uninitialized == user_preset_state || InitState::Broken == user_preset_state) {
if (user_preset_state != InitState::Complete) {
transmitRequestUserPresets();
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/HC-1/HC-1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Hc1Module::Hc1Module()

ModuleBroker::get()->registerHc1(this);
heart_phase = heart_time;
loadStartupConfig();
}

Hc1Module::~Hc1Module()
Expand Down Expand Up @@ -232,6 +233,8 @@ void Hc1Module::onRemove(const RemoveEvent& e)

json_t * Hc1Module::dataToJson()
{
saveStartupConfig();

auto root = json_object();

json_object_set_new(root, "midi-device-claim", json_string(device_claim.c_str()));
Expand Down Expand Up @@ -306,7 +309,6 @@ void Hc1Module::dataFromJson(json_t *root)
}
cache_system_presets = GetBool(root, "cache-presets", cache_system_presets);
cache_user_presets = GetBool(root, "cache-user-presets", cache_user_presets);
tryCachedPresets();
}

void Hc1Module::reboot()
Expand Down
14 changes: 13 additions & 1 deletion src/HC-1/HC-1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ struct Hc1Module : IPresetHolder, ISendMidi, ISetDevice, IMidiDeviceChange, midi
hc1->bulk_favoriting = false;
}
};

std::string startupConfigPath();
std::string userPresetsPath();
std::string systemPresetsPath();
void saveStartupConfig();
void loadStartupConfig();
void saveUserPresets();
void saveSystemPresets();
void savePresets();
Expand Down Expand Up @@ -149,6 +151,7 @@ struct Hc1Module : IPresetHolder, ISendMidi, ISetDevice, IMidiDeviceChange, midi
InitState device_output_state = InitState::Uninitialized;
InitState device_input_state = InitState::Uninitialized;
InitState device_hello_state = InitState::Uninitialized;
InitState cached_preset_state = InitState::Uninitialized;
InitState system_preset_state = InitState::Uninitialized;
InitState user_preset_state = InitState::Uninitialized;
InitState apply_favorite_state = InitState::Uninitialized;
Expand All @@ -157,6 +160,14 @@ struct Hc1Module : IPresetHolder, ISendMidi, ISetDevice, IMidiDeviceChange, midi
InitState request_updates_state = InitState::Uninitialized;
InitState handshake = InitState::Uninitialized;

float post_output_delay = 5.f;
float post_input_delay = 4.f;
float post_hello_delay = 2.f;
float post_system_delay = 5.f;
float post_user_delay = 3.f;
float post_config_delay = 2.f;
float hearbeat_period = 2.f;

bool hasSystemPresets() { return InitState::Complete == system_preset_state && !system_presets.empty(); }
bool hasUserPresets() { return InitState::Complete == user_preset_state && !user_presets.empty(); }
bool hasConfig() { return InitState::Complete == config_state; }
Expand Down Expand Up @@ -472,6 +483,7 @@ struct Hc1ModuleWidget : ModuleWidget, IPresetHolder, IHandleHcEvents
// HC-1.draw.cpp
void drawDSP(NVGcontext* vg);
void drawStatusDots(NVGcontext* vg);
void drawPedalAssignment(NVGcontext* vg, float x, float y, char ped_char, uint8_t ped, uint8_t ped_value);
void drawPedals(NVGcontext* vg, std::shared_ptr<rack::window::Font> font, bool stockPedals);

void onHoverScroll(const HoverScrollEvent& e) override;
Expand Down

0 comments on commit 673674c

Please sign in to comment.