xtc/src/gui/sequencer_instruments.cpp

63 lines
2.9 KiB
C++
Raw Normal View History

#include "sequencer_widget.hpp"
#include "sequencer_track_widget.hpp"
#include <cstring>
#include <new>
Fl_Choice *SequencerWidget::instrument_choice(uint64_t track) const
{
auto *view=track_widget(track);
return view && !view->is_bus() ? view->instrument_choice() : nullptr;
}
void SequencerWidget::refresh_instruments() try
{
sync_views();
size_t tracks = 0, clips = 0;
xtc_project_get_counts(project_,&tracks,&clips);
catalog_.clear();
for (unsigned int i=0;i<2;++i) { xtc_plugin_spec s{}; if (xtc_plugin_probe(XTC_BUILTIN_PATH,i,48000,&s)==XTC_OK) catalog_.push_back(s); }
for (size_t i = 0; i < tracks; ++i) {
xtc_track_info t{}; xtc_plugin_spec spec{};
xtc_project_get_track(project_,i,&t);
if (xtc_project_get_instrument(project_,t.id,&spec) != XTC_OK) continue;
bool present = false;
for (const auto &item : catalog_) if (!std::strcmp(item.path,spec.path) && item.index == spec.index) present = true;
if (!present) catalog_.push_back(spec);
}
for(auto *view:track_widgets_) {
auto *choice=view->instrument_choice();
xtc_track_info t{};
for(size_t i=0;i<tracks;++i) {xtc_project_get_track(project_,i,&t); if(t.id==view->track_id()) break;}
if(t.is_bus) {choice->hide(); continue;}
choice->clear(); choice->add("Built-in sine");
xtc_plugin_spec current{}; int selected = 0;
bool assigned = xtc_project_get_instrument(project_,t.id,&current) == XTC_OK;
for (size_t j = 0; j < catalog_.size(); ++j) {
std::string name = std::string(xtc_plugin_format_name(catalog_[j].format))+": "+catalog_[j].label;
int item = choice->add("Instrument"); choice->replace(item,name.c_str());
if (assigned && !std::strcmp(current.path,catalog_[j].path) && current.index == catalog_[j].index) selected = static_cast<int>(j)+1;
}
choice->add("Load plugin..."); choice->value(selected); choice->show();
}
} catch (const std::bad_alloc &) {
catalog_.clear();
for(auto *view:track_widgets_) view->instrument_choice()->hide();
}
void SequencerWidget::instrument_callback(Fl_Widget *widget, void *context)
{
auto *owner = static_cast<SequencerWidget *>(context);
auto *choice = static_cast<Fl_Choice *>(widget);
for (auto *view : owner->track_widgets_) if (view->instrument_choice() == choice) {
uint64_t track = view->track_id();
int index = choice->value();
bool browse = index == static_cast<int>(owner->catalog_.size())+1;
xtc_plugin_spec spec{};
if (index > 0 && !browse) {
if (static_cast<size_t>(index) > owner->catalog_.size()) return;
spec = owner->catalog_[static_cast<size_t>(index)-1];
}
if (owner->instrument_select) owner->instrument_select(owner->context_,track,index > 0 && !browse ? &spec : nullptr,browse);
owner->refresh_instruments(); owner->redraw(); return;
}
}