feat(preferences): choose MIDI devices and persistent channel defaults (phase 102)

This commit is contained in:
Johannes Findeisen 2026-11-06 02:25:28 +01:00
commit e9d0c62fd6
9 changed files with 101 additions and 17 deletions

View file

@ -905,9 +905,9 @@ void MainWindow::start_engine()
xtc_result result = configure_loop(project_.get(),settings_);
if(result==XTC_OK) result = xtc_backend_prepare_project(backend_, project_.get());
if (result == XTC_OK)
result = xtc_backend_start(backend_);
result = start_configured_backend();
if (result != XTC_OK)
report_error("Start engine failed", result);
report_error("Start failed; check audio and saved MIDI devices in Preferences", result);
else {
set_label(error_, "");
const char *issue = xtc_backend_asset_issue(backend_);
@ -1659,7 +1659,7 @@ xtc_result MainWindow::record_audio(const char *path)
recording_track_ = sequencer_->selected_track();
r = xtc_backend_set_recorder(backend_, recorder_);
if (r == XTC_OK)
r = xtc_backend_start(backend_);
r = start_configured_backend();
if (r != XTC_OK) {
(void)xtc_backend_set_recorder(backend_, nullptr);
xtc_recorder_destroy(recorder_);
@ -2462,7 +2462,7 @@ xtc_result MainWindow::set_loop(uint64_t start,uint64_t end,bool enabled)
bool resume=running_;xtc_result r=ready_ ? xtc_backend_stop(backend_) : XTC_OK;
if(r==XTC_OK)r=configure_loop(project_.get(),candidate);
if(r==XTC_OK) {settings_=candidate;sequencer_->loop_region(start,end,enabled);}
if(ready_ && resume && r==XTC_OK) {xtc_result restarted=xtc_backend_start(backend_);if(r==XTC_OK)r=restarted;}
if(ready_ && resume && r==XTC_OK) {xtc_result restarted=start_configured_backend();if(r==XTC_OK)r=restarted;}
refresh_status();return r;
}
void MainWindow::collect_preferences()
@ -2524,10 +2524,27 @@ xtc_result MainWindow::save_preferences()
if(configuration_.empty() || !configuration_valid_)return XTC_ERR_INVALID_STATE;
collect_preferences();return xtc_settings_save(&settings_,configuration_.c_str());
}
std::vector<std::string> MainWindow::midi_devices(bool input)
{
std::vector<std::string> ports;
if(ready_)(void)xtc_backend_visit_midi_ports(backend_,input ? XTC_PORT_OUTPUT : XTC_PORT_INPUT,
[](const char *name,void *c){static_cast<std::vector<std::string> *>(c)->emplace_back(name);return 0;},&ports);
return ports;
}
xtc_result MainWindow::start_configured_backend()
{
xtc_result r=xtc_backend_set_midi_options(backend_,static_cast<int>(settings_.midi_input_channel),static_cast<int>(settings_.midi_output_channel),settings_.midi_thru!=0);
if(r==XTC_OK)r=xtc_backend_start(backend_);
if(r!=XTC_OK)return r;
if(settings_.midi_input_port[0])r=xtc_backend_route_midi(backend_,XTC_PORT_INPUT,settings_.midi_input_port,0);
if(settings_.midi_output_port[0]){auto out=xtc_backend_route_midi(backend_,XTC_PORT_OUTPUT,settings_.midi_output_port,0);if(r==XTC_OK)r=out;}
if(r!=XTC_OK){(void)xtc_backend_stop(backend_);return r;}
return XTC_OK;
}
void MainWindow::show_preferences()
{
if(recorder_) {report_error("Stop recording before preferences",XTC_ERR_INVALID_STATE);return;}
collect_preferences();PreferencesDialog dialog(settings_,configuration_,[this](const xtc_settings &s){return apply_preferences(s);});dialog.run();
collect_preferences();PreferencesDialog dialog(settings_,configuration_,[this](const xtc_settings &s){return apply_preferences(s);},[this](bool input){return midi_devices(input);});dialog.run();
}
xtc_result MainWindow::set_tempo(unsigned int milli_bpm)

View file

@ -66,6 +66,8 @@ class MainWindow : public Fl_Double_Window {
const xtc_settings &settings() const { return settings_; }
xtc_result apply_preferences(const xtc_settings &settings);
void show_preferences();
std::vector<std::string> midi_devices(bool input);
xtc_result start_configured_backend();
xtc_result save_preferences();
xtc_result set_loop(uint64_t start,uint64_t end,bool enabled);
Fl_Widget *velocity_lane() const { return velocity_lane_; }

View file

@ -4,6 +4,7 @@
#include "theme.hpp"
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Input_Choice.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <functional>
@ -15,6 +16,18 @@ class PreferencesDialog : public Fl_Double_Window {
std::function<xtc_result(const xtc_settings &)> apply_;
std::vector<Fl_Input *> inputs_;
Fl_Box *message_;
std::function<std::vector<std::string>(bool)> devices_;
Fl_Input_Choice *midi_input_=nullptr,*midi_output_=nullptr;
void refresh_devices() {
for(auto pair:{std::pair<Fl_Input_Choice *,bool>{midi_input_,true},{midi_output_,false}}){
if(!pair.first)continue;
std::string value=pair.first->value();std::vector<std::string> names=devices_ ? devices_(pair.second) : std::vector<std::string>{};
names.insert(names.begin(),"");std::vector<Fl_Menu_Item> items(names.size()+1);
for(size_t i=0;i<names.size();++i)items[i].text=i ? names[i].c_str() : "(None)";
pair.first->menubutton()->copy(items.data());pair.first->value(value.c_str());
}
}
static void apply_callback(Fl_Widget *,void *context) {
auto *d=static_cast<PreferencesDialog *>(context);
xtc_settings candidate=d->original_; size_t count=0;
@ -36,8 +49,9 @@ class PreferencesDialog : public Fl_Double_Window {
}
public:
PreferencesDialog(const xtc_settings &settings,const std::string &path,
std::function<xtc_result(const xtc_settings &)> apply)
: Fl_Double_Window(1050,660,"xtc — Preferences"),original_(settings),apply_(std::move(apply)) {
std::function<xtc_result(const xtc_settings &)> apply,
std::function<std::vector<std::string>(bool)> devices={})
: Fl_Double_Window(1050,660,"xtc — Preferences"),original_(settings),apply_(std::move(apply)),devices_(std::move(devices)) {
set_modal(); begin();
auto *location=new Fl_Box(16,12,1018,30); location->copy_label(path.c_str()); location->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
auto *tabs=new Fl_Tabs(12,50,1026,510); tabs->begin();
@ -50,12 +64,23 @@ class PreferencesDialog : public Fl_Double_Window {
group=new Fl_Group(12,82,1026,478); group->copy_label(section.c_str()); group->begin();
scroll=new Fl_Scroll(22,94,1000,450); scroll->begin(); row=0;
}
auto *input=new Fl_Input(510,108+row*48,480,30,schema[i].label);
Fl_Input *input;
if(!strcmp(schema[i].key,"midi_input_port") || !strcmp(schema[i].key,"midi_output_port")){
auto *choice=new Fl_Input_Choice(510,108+row*48,480,30,schema[i].label);
choice->callback([](Fl_Widget *w,void *){auto *c=static_cast<Fl_Input_Choice *>(w);auto *item=c->menubutton()->mvalue();if(item && !strcmp(item->label(),"(None)"))c->value("");});
input=choice->input();if(!strcmp(schema[i].key,"midi_input_port"))midi_input_=choice;else midi_output_=choice;
}else input=new Fl_Input(510,108+row*48,480,30,schema[i].label);
input->align(FL_ALIGN_LEFT); input->maximum_size(schema[i].capacity ? static_cast<int>(schema[i].capacity-1) : 22);
char value[4096]; xtc_settings_get(&settings,i,value,sizeof(value)); input->value(value);
inputs_[i]=input; ++row;
}
if(scroll) { scroll->end(); group->end(); } tabs->end();
tabs->begin();auto *midi_info=new Fl_Group(12,82,1026,478,"MIDI help");
auto *help=new Fl_Box(42,110,950,190,"Choose JACK/PipeWire MIDI ports on the midi tab. Refresh to see newly connected devices.\nYou can also type a remembered port name while it is offline; blank means no automatic route.\nRouting is restored on Play/Record; missing ports are reported without deleting preferences.\nInput/output channels use 0..15 for MIDI channels 1..16. MIDI thru defaults off.\nNote length/velocity/channel defaults are on editing; CC defaults are on project_defaults.\nHardware MIDI devices must be exposed by your host JACK/PipeWire MIDI bridge.");
help->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_WRAP);midi_info->end();tabs->end();
refresh_devices();
auto *refresh=new Fl_Button(16,614,200,30,"Refresh MIDI devices");
refresh->callback([](Fl_Widget *,void *c){static_cast<PreferencesDialog *>(c)->refresh_devices();},this);
message_=new Fl_Box(16,568,1018,36,"Apply saves every tab. Existing project contents are not overwritten by defaults.");
message_->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
auto *apply_button=new Fl_Button(812,614,100,30,"Apply"); apply_button->callback(apply_callback,this);

View file

@ -21,6 +21,11 @@ static const xtc_setting_info schema[] = {
STR("plugins",vst2_paths,"VST2 search paths (colon-separated)"),
STR("plugins",vst3_paths,"VST3 search paths (colon-separated)"),
STR("plugins",ladspa_paths,"LADSPA search paths (colon-separated)"),
STR("midi",midi_input_port,"Default MIDI input source (next Play)"),
STR("midi",midi_output_port,"Default MIDI output destination (next Play)"),
NUM("midi",midi_input_channel,"Live input channel (-1 all, 0..15)",-1,15),
NUM("midi",midi_output_channel,"Output channel (-1 original, 0..15)",-1,15),
NUM("midi",midi_thru,"Forward live MIDI to output: 0 or 1",0,1),
NUM("interface",main_x,"Main window X",-32768,32768), NUM("interface",main_y,"Main window Y",-32768,32768),
NUM("interface",inspector_visible,"Track inspector visible: 0 or 1",0,1),
NUM("interface",width,"Main window width",1220,3840), NUM("interface",height,"Main window height",720,2160),
@ -77,6 +82,7 @@ void xtc_settings_defaults(xtc_settings *s)
s->mixer_x=s->mixer_y=100;s->mixer_width=1056;s->mixer_height=600;
s->sequencer_snap=s->piano_snap=240; s->sequencer_zoom=48; s->clip_ticks=3840; s->note_ticks=960;
s->note_velocity=100; s->filter_channel=-1; s->filter_high=127; s->tempo=120000;
s->midi_input_channel=s->midi_output_channel=-1;
s->volume_cc=7; s->pan_cc=10; s->sustain_cc=64; s->loop_end=3840;s->tail_ms=3000;
}
static int snap(int64_t v) { return v==1 || v==120 || v==160 || v==240 || v==320 || v==480 || v==960 || v==1920 || v==3840; }