Added velocity to tracks in the sequencer.

This commit is contained in:
Johannes Findeisen 2026-11-06 21:00:20 +01:00
commit b46d4f09bb
13 changed files with 873 additions and 19 deletions

View file

@ -1428,7 +1428,7 @@ xtc_result MainWindow::install_project(xtc_project *candidate, bool reset_histor
}
graph_widget_->reset_layout();
graph_widget_->set_graph(xtc_project_graph(candidate));
sequencer_->set_project(candidate);
sequencer_->set_project(candidate,true,!reset_history);
if (reset_history)
tabs_->value(tabs_->child(0));
dirty_ = false;

View file

@ -26,6 +26,7 @@
SequencerWidget::SequencerWidget(int left, int top, xtc_project *project, Seek seek, void *context)
: Fl_Group(left, top, 6300, 3700), project_(project), seek_(seek), context_(context)
{
expanded_tracks_.fill(UINT64_MAX);
resizable(nullptr);
extent();
end();
@ -56,10 +57,12 @@ bool SequencerWidget::select(uint64_t track, uint64_t clip)
return true;
}
void SequencerWidget::set_project(xtc_project *project, bool reset)
void SequencerWidget::set_project(xtc_project *project, bool reset, bool preserve_expansion)
{
if(project_ != project) mode_=0;
project_ = project;
if (reset) {
if(!preserve_expansion) expanded_tracks_.fill(UINT64_MAX);
waves_.clear();
selection_.clear();
track_ = clip_ = UINT64_MAX;
@ -93,6 +96,68 @@ uint64_t SequencerWidget::tick(int px) const
return std::min(XTC_PROJECT_MAX_TICK,static_cast<uint64_t>(xtc_ui::snap_position(raw,grid_)));
}
bool SequencerWidget::track_expanded(uint64_t track) const
{
return track != UINT64_MAX && std::find(expanded_tracks_.begin(),expanded_tracks_.end(),track) != expanded_tracks_.end();
}
bool SequencerWidget::expand_track(uint64_t track, bool expanded)
{
size_t tracks=0, clips=0;
xtc_project_get_counts(project_,&tracks,&clips);
bool found=false;
for(size_t i=0;i<tracks;++i) {
xtc_track_info t{}; xtc_project_get_track(project_,i,&t);
if(t.id==track) found=true;
}
if(!found) return false;
if(track_expanded(track)==expanded) return true;
for(auto &id:expanded_tracks_) if(id==(expanded ? UINT64_MAX : track)) {
id=expanded ? track : UINT64_MAX;
mode_=0; extent(); redraw(); return true;
}
return false;
}
int SequencerWidget::track_height(size_t row) const
{
xtc_track_info t{};
return 56+(xtc_project_get_track(project_,row,&t)==XTC_OK && track_expanded(t.id) ? velocity_height_ : 0);
}
int SequencerWidget::track_top(size_t row) const
{
int top=y()+28;
for(size_t i=0;i<row;++i) top+=track_height(i);
return top;
}
int SequencerWidget::track_row(int py) const
{
size_t tracks=0, clips=0; xtc_project_get_counts(project_,&tracks,&clips);
int top=y()+28;
for(size_t i=0;i<tracks;++i) {
int bottom=top+track_height(i);
if(py>=top && py<bottom) return static_cast<int>(i);
top=bottom;
}
return -1;
}
bool SequencerWidget::velocity_note(const xtc_note_info &note, uint64_t track, int &px) const
{
xtc_clip_info c{};
if(xtc_project_find_clip(project_,note.clip_id,&c)!=XTC_OK || c.track_id!=track || !c.midi ||
note.start_tick<c.offset_tick || note.start_tick-c.offset_tick>=c.length_tick) return false;
px=x()+header_width_+static_cast<int>((c.start_tick+note.start_tick-c.offset_tick)*static_cast<unsigned int>(scale_)/960U);
return true;
}
unsigned int SequencerWidget::velocity_value(int py) const
{
return static_cast<unsigned int>(std::clamp((velocity_top_+velocity_height_-8-py)*127/(velocity_height_-28),1,127));
}
/**
* @brief Resizes the timeline's scrollable area to fit its tracks and clip ends.
*/
@ -100,6 +165,15 @@ void SequencerWidget::extent()
{
size_t tracks, clips;
xtc_project_get_counts(project_, &tracks, &clips);
for(auto &id:expanded_tracks_) {
if(id==UINT64_MAX) continue;
bool found=false;
for(size_t i=0;i<tracks;++i) {
xtc_track_info t{}; xtc_project_get_track(project_,i,&t);
if(t.id==id) found=true;
}
if(!found) id=UINT64_MAX;
}
uint64_t end = 32U * 3840U;
for (size_t i = 0; i < clips; ++i) {
xtc_clip_info c{};
@ -113,7 +187,7 @@ void SequencerWidget::extent()
end=std::max(end,std::min(XTC_PROJECT_MAX_TICK,marker.tick)+UINT64_C(3840));
}
size(static_cast<int>(end * static_cast<unsigned int>(scale_) / 960U + static_cast<unsigned int>(header_width_+20)),
std::max(400, static_cast<int>(tracks) * 56 + 100));
std::max(400, track_top(tracks)-y()+72));
refresh_instruments();
if (parent())
parent()->redraw();
@ -245,10 +319,10 @@ void SequencerWidget::draw()
size_t tracks = 0, clips = 0, notes = 0;
xtc_project_get_counts(project_,&tracks,&clips); xtc_project_note_count(project_,&notes);
for (size_t i = 0; i < tracks; ++i) {
int py = y()+28+static_cast<int>(i)*56;
if (py+56 < cy || py > cy+ch) continue;
int py = track_top(i);
if (py+track_height(i) < cy || py > cy+ch) continue;
fl_color(i%2 ? fl_rgb_color(30,34,40) : fl_rgb_color(33,37,43));
fl_rectf(x()+header_width_,py,w()-header_width_,55);
fl_rectf(x()+header_width_,py,w()-header_width_,track_height(i)-1);
}
fl_font(FL_HELVETICA,11);
uint64_t visual = grid_ == 1 ? 960 : grid_;
@ -270,8 +344,8 @@ void SequencerWidget::draw()
}
for (size_t i = 0; i < tracks; ++i) {
xtc_track_info t{}; xtc_project_get_track(project_,i,&t);
int py = y()+28+static_cast<int>(i)*56;
if (py+56 < cy || py > cy+ch) continue;
int py = track_top(i);
if (py+track_height(i) < cy || py > cy+ch) continue;
Fl_Color color = xtc_ui::track(t.id);
for (size_t j = 0; j < clips; ++j) {
xtc_clip_info c{}; xtc_project_get_clip(project_,j,&c);
@ -310,6 +384,24 @@ void SequencerWidget::draw()
fl_pop_clip();
}
}
for(size_t i=0;i<tracks;++i) {
xtc_track_info t{}; xtc_project_get_track(project_,i,&t);
if(!track_expanded(t.id)) continue;
int top=track_top(i)+56, bottom=top+velocity_height_-8;
fl_push_clip(std::max(cx,header_left()+header_width_),top,cw,velocity_height_);
fl_color(xtc_ui::border()); fl_line(x()+header_width_,top,x()+w(),top);
fl_font(FL_HELVETICA,10); fl_color(xtc_ui::muted());
fl_draw("Drag a note bar to change velocity",std::max(x()+header_width_,header_left()+header_width_)+8,top+14);
for(size_t n=0;n<notes;++n) {
xtc_note_info note{}; xtc_project_get_note(project_,n,&note); int px=0;
if(!velocity_note(note,t.id,px)) continue;
unsigned int value=mode_==6 && velocity_original_.id==note.id ? velocity_preview_ : note.velocity;
int ny=bottom-static_cast<int>(value)*(velocity_height_-28)/127;
fl_color(mode_==6 && velocity_original_.id==note.id ? xtc_ui::text() : xtc_ui::track(t.id));
fl_rectf(px-2,ny,4,bottom-ny); fl_rectf(px-4,ny-2,8,4);
}
fl_pop_clip();
}
if (!tracks) {
fl_font(FL_HELVETICA_BOLD,18); fl_color(xtc_ui::text());
fl_draw("Your next track starts here",x()+220,y()+105);
@ -342,25 +434,37 @@ void SequencerWidget::draw()
fl_color(xtc_ui::background());fl_rectf(pinned,y(),header_width_,28);
fl_color(xtc_ui::muted());fl_draw("TRACKS / drag edge to resize",pinned+10,y()+19);
for(size_t i=0;i<tracks;++i) {
xtc_track_info t{};xtc_project_get_track(project_,i,&t);int py=y()+28+static_cast<int>(i)*56;
if(py+56<cy || py>cy+ch)continue;
xtc_track_info t{};xtc_project_get_track(project_,i,&t);int py=track_top(i);
if(py+track_height(i)<cy || py>cy+ch)continue;
Fl_Color color = xtc_ui::track(t.id);
fl_color(t.id == track_ ? fl_rgb_color(49,62,70) : xtc_ui::panel());
fl_rectf(pinned,py,header_width_-2,55);
fl_color(color); fl_rectf(pinned,py,3,55);
fl_rectf(pinned,py,header_width_-2,track_height(i)-1);
fl_color(color); fl_rectf(pinned,py,3,track_height(i)-1);
fl_push_clip(pinned+9,py,header_width_-18,55);
fl_color(xtc_ui::text()); fl_font(FL_HELVETICA_BOLD,12);
fl_draw(t.name,pinned+12,py+18);
if(track_expanded(t.id)) fl_polygon(pinned+10,py+10,pinned+20,py+10,pinned+15,py+16);
else fl_polygon(pinned+12,py+8,pinned+12,py+18,pinned+18,py+13);
fl_draw(t.name,pinned+26,py+18);
char detail[64];
std::snprintf(detail,sizeof(detail),"%02zu %s%s%s",i+1,t.is_bus ? "BUS" : "TRACK",
t.mute ? " MUTE" : "",t.solo ? " SOLO" : "");
if (t.is_bus) { fl_font(FL_HELVETICA,10); fl_color(xtc_ui::muted()); fl_draw(detail,pinned+12,py+42); }
if (t.mute || t.solo) { fl_font(FL_HELVETICA,10); fl_color(xtc_ui::muted()); fl_draw(t.mute ? "M" : "S",pinned+header_width_-16,py+18); }
fl_pop_clip();
if(track_expanded(t.id)) {
fl_color(xtc_ui::muted()); fl_font(FL_HELVETICA,11);
fl_draw("VELOCITY 1..127",pinned+12,py+76);
fl_font(FL_HELVETICA,10);
fl_draw(t.is_bus ? "No MIDI notes on buses" : "Drag bars to edit notes",pinned+12,py+94);
if(mode_==6 && t.id==track_) {
char value[24]; std::snprintf(value,sizeof(value),"Velocity: %u",velocity_preview_);
fl_draw(value,pinned+12,py+114);
}
}
}
fl_color(xtc_ui::muted());fl_rectf(pinned+header_width_-3,cy,3,ch);
for(size_t i=0;i<track_choices_.size();++i)if(track_choices_[i].widget)
track_choices_[i].widget->resize(pinned+10,y()+58+static_cast<int>(i)*56,header_width_-22,20);
track_choices_[i].widget->resize(pinned+10,track_top(i)+30,header_width_-22,20);
draw_children();
fl_pop_clip();
}
@ -373,6 +477,19 @@ void SequencerWidget::draw()
*/
int SequencerWidget::handle(int event)
{
if(mode_==6) {
if(event==FL_KEYDOWN && Fl::event_key()==FL_Escape) { mode_=0; redraw(); return 1; }
if(event==FL_DRAG) { velocity_preview_=velocity_value(Fl::event_y()); redraw(); return 1; }
if(event==FL_RELEASE) {
mode_=0;
unsigned int value=velocity_value(Fl::event_y());
if(value==velocity_original_.velocity) { redraw(); return 1; }
xtc_note_info note=velocity_original_;
xtc_result r=before_edit ? before_edit(context_) : XTC_OK;
if(r==XTC_OK) r=xtc_project_edit_note(project_,note.id,note.start_tick,note.length_tick,note.pitch,value,note.channel);
changed(r); return 1;
}
}
if(event==FL_PUSH && Fl::event_button()==FL_LEFT_MOUSE && Fl::event_x()>=header_left()+header_width_-5 && Fl::event_x()<header_left()+header_width_) {
mode_=5;header_drag_x_=Fl::event_x();header_drag_original_=header_width_;return 1;
}
@ -407,11 +524,30 @@ int SequencerWidget::handle(int event)
}
size_t tracks, clips;
xtc_project_get_counts(project_, &tracks, &clips);
int row = (Fl::event_y() - y() - 28) / 56;
int row = track_row(Fl::event_y());
if (row < 0 || static_cast<size_t>(row) >= tracks)
return 1;
xtc_track_info t{};
xtc_project_get_track(project_, static_cast<size_t>(row), &t);
if(Fl::event_button()==FL_LEFT_MOUSE && Fl::event_x()>=header_left()+7 &&
Fl::event_x()<header_left()+24 && Fl::event_y()<track_top(static_cast<size_t>(row))+26) {
expand_track(t.id,!track_expanded(t.id)); return 1;
}
if(Fl::event_y()>=track_top(static_cast<size_t>(row))+56) {
if(Fl::event_button()!=FL_LEFT_MOUSE || Fl::event_x()<header_left()+header_width_) return 1;
size_t notes=0; xtc_project_note_count(project_,&notes);
int distance=10; bool hit=false;
velocity_top_=track_top(static_cast<size_t>(row))+56;
for(size_t n=0;n<notes;++n) {
xtc_note_info note{}; xtc_project_get_note(project_,n,&note); int px=0;
if(!velocity_note(note,t.id,px)) continue;
int ny=velocity_top_+velocity_height_-8-static_cast<int>(note.velocity)*(velocity_height_-28)/127;
int d=std::abs(Fl::event_x()-px)+std::abs(Fl::event_y()-ny);
if(d<distance) { distance=d; velocity_original_=note; hit=true; }
}
if(hit) { track_=t.id; clip_=velocity_original_.clip_id; mode_=6; velocity_preview_=velocity_original_.velocity; }
redraw(); return 1;
}
uint64_t previous_clip=clip_;
track_ = t.id;
clip_ = UINT64_MAX;
@ -474,7 +610,7 @@ int SequencerWidget::handle(int event)
uint64_t target = original_.track_id;
if (mode == 1) {
start = xtc_ui::snap_position(start+delta,grid_);
int row = (Fl::event_y() - y() - 28) / 56;
int row = track_row(Fl::event_y());
xtc_track_info t{};
if (row >= 0 && xtc_project_get_track(project_, static_cast<size_t>(row), &t) == XTC_OK)
target = t.id;
@ -887,13 +1023,13 @@ void SequencerWidget::refresh_instruments() try
explicit GroupScope(Fl_Group *group) : previous(Fl_Group::current()) { Fl_Group::current(group); }
~GroupScope() { Fl_Group::current(previous); }
} scope(this);
entry.widget = new Fl_Choice(header_left()+10,y()+58+static_cast<int>(i)*56,header_width_-22,20);
entry.widget = new Fl_Choice(header_left()+10,track_top(i)+30,header_width_-22,20);
xtc_ui::style(entry.widget); entry.widget->textsize(10); entry.widget->textcolor(xtc_ui::text());
entry.widget->tooltip("Track instrument: built-in sine, an instrument used in this project, or Load plugin...");
entry.widget->callback(instrument_callback,this);
}
entry.track = t.id;
entry.widget->resize(header_left()+10,y()+58+static_cast<int>(i)*56,header_width_-22,20);
entry.widget->resize(header_left()+10,track_top(i)+30,header_width_-22,20);
entry.widget->clear(); entry.widget->add("Built-in sine");
xtc_plugin_spec current{}; int selected = 0;
bool assigned = xtc_project_get_instrument(project_,t.id,&current) == XTC_OK;

View file

@ -32,6 +32,9 @@ class SequencerWidget : public Fl_Group {
InstrumentSelect instrument_select = nullptr;
Fl_Choice *instrument_choice(uint64_t track) const;
void refresh_instruments();
bool track_expanded(uint64_t track) const;
bool expand_track(uint64_t track, bool expanded);
int track_top(size_t row) const;
bool select(uint64_t track, uint64_t clip = UINT64_MAX);
xtc_result merge_midi();
xtc_result duplicate_track(const char *name);
@ -119,8 +122,9 @@ class SequencerWidget : public Fl_Group {
*
* @param project Borrowed project; must outlive the widget's use of it.
* @param reset Whether to clear selection, cursor, and drag state.
* @param preserve_expansion Keep disclosure state when installing history snapshots.
*/
void set_project(xtc_project *project, bool reset = true);
void set_project(xtc_project *project, bool reset = true, bool preserve_expansion = false);
/**
* @brief Returns the timeline cursor position in musical ticks.
@ -193,6 +197,15 @@ class SequencerWidget : public Fl_Group {
void draw() override;
private:
static constexpr int velocity_height_ = 88;
std::array<uint64_t,XTC_PROJECT_MAX_TRACKS> expanded_tracks_{};
int track_height(size_t row) const;
int track_row(int py) const;
bool velocity_note(const xtc_note_info &note, uint64_t track, int &px) const;
unsigned int velocity_value(int py) const;
xtc_note_info velocity_original_{};
unsigned int velocity_preview_ = 100;
int velocity_top_ = 0;
struct TrackChoice { Fl_Choice *widget = nullptr; uint64_t track = UINT64_MAX; };
std::array<TrackChoice,XTC_PROJECT_MAX_TRACKS> track_choices_{};
std::vector<xtc_plugin_spec> catalog_;