Audio track velocity editing in the sequencer.

This commit is contained in:
Johannes Findeisen 2026-11-06 21:10:48 +01:00
commit 896a7907d3
10 changed files with 292 additions and 6 deletions

View file

@ -153,6 +153,58 @@ bool SequencerWidget::velocity_note(const xtc_note_info &note, uint64_t track, i
return true;
}
bool SequencerWidget::audio_velocity_lane(uint64_t track) const
{
size_t tracks=0, clips=0; xtc_project_get_counts(project_,&tracks,&clips);
bool midi=false;
for(size_t i=0;i<clips;++i) {
xtc_clip_info c{}; xtc_project_get_clip(project_,i,&c);
if(c.track_id!=track) continue;
if(!c.midi) return true;
midi=true;
}
return !midi;
}
float SequencerWidget::audio_velocity_value(int py) const
{
return std::clamp(static_cast<float>(velocity_top_+velocity_height_-8-py)*4.0f/(velocity_height_-28),0.0f,4.0f);
}
void SequencerWidget::draw_audio_velocity(const xtc_track_info &track, int top, int left, int right)
{
std::array<xtc_automation_point,XTC_MAX_AUTOMATION_POINTS+1> points{};
size_t count=0,total=0; xtc_project_automation_count(project_,&total);
for(size_t i=0;i<total;++i) {
xtc_automation_point a{}; xtc_project_get_automation(project_,i,&a);
if(a.track_id!=track.id || a.parameter!=XTC_AUTOMATION_GAIN) continue;
if(mode_==7 && track_==track.id && a.tick==audio_velocity_preview_.tick) continue;
points[count++]=a;
}
if(mode_==7 && track_==track.id) points[count++]=audio_velocity_preview_;
std::sort(points.begin(),points.begin()+count,[](const auto &a,const auto &b){return a.tick<b.tick;});
auto px=[&](uint64_t t){return x()+header_width_+static_cast<int>(t*static_cast<unsigned int>(scale_)/960U);};
auto py=[&](float v){return top+velocity_height_-8-static_cast<int>(v*(velocity_height_-28)/4.0f);};
fl_color(xtc_ui::track(track.id));
if(!count) fl_line(left,py(track.gain),right,py(track.gain));
else {
fl_line(left,py(points[0].value),px(points[0].tick),py(points[0].value));
for(size_t i=1;i<count;++i) {
const auto &a=points[i-1]; const auto &b=points[i];
if(a.step) {
fl_line(px(a.tick),py(a.value),px(b.tick),py(a.value));
fl_line(px(b.tick),py(a.value),px(b.tick),py(b.value));
} else fl_line(px(a.tick),py(a.value),px(b.tick),py(b.value));
}
fl_line(px(points[count-1].tick),py(points[count-1].value),right,py(points[count-1].value));
for(size_t i=0;i<count;++i) {
int point_x=px(points[i].tick);
if(point_x<left-4 || point_x>right+4) continue;
fl_rectf(point_x-4,py(points[i].value)-3,8,6);
}
}
}
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));
@ -391,8 +443,11 @@ void SequencerWidget::draw()
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) {
bool audio=audio_velocity_lane(t.id);
int lane_left=std::max(cx,header_left()+header_width_);
fl_draw(audio ? "Click anywhere to add a level point; drag vertically; right-click a point to remove" : "Drag a note bar to change velocity",lane_left+8,top+14);
if(audio) draw_audio_velocity(t,top,lane_left,cx+cw);
for(size_t n=0;!audio && 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;
@ -453,9 +508,14 @@ void SequencerWidget::draw()
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);
bool audio=audio_velocity_lane(t.id);
fl_draw(audio ? "AUDIO VELOCITY" : "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);
fl_draw(audio ? "Level envelope 0..400%" : "Drag bars to edit notes",pinned+12,py+94);
if(mode_==7 && t.id==track_) {
char value[32]; std::snprintf(value,sizeof(value),"Level: %.0f%%",audio_velocity_preview_.value*100);
fl_draw(value,pinned+12,py+114);
}
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);
@ -477,6 +537,25 @@ void SequencerWidget::draw()
*/
int SequencerWidget::handle(int event)
{
if(mode_==7) {
if(event==FL_KEYDOWN && Fl::event_key()==FL_Escape) { mode_=0; redraw(); return 1; }
if(event==FL_DRAG) { audio_velocity_preview_.value=audio_velocity_value(Fl::event_y()); redraw(); return 1; }
if(event==FL_RELEASE) {
mode_=0;
auto point=audio_velocity_preview_;
point.value=audio_velocity_value(Fl::event_y());
if(audio_velocity_existing_) {
size_t count=0; xtc_project_automation_count(project_,&count);
for(size_t i=0;i<count;++i) {
xtc_automation_point a{}; xtc_project_get_automation(project_,i,&a);
if(a.track_id==point.track_id && a.parameter==point.parameter && a.tick==point.tick && a.value==point.value) { redraw(); return 1; }
}
}
xtc_result r=before_edit ? before_edit(context_) : XTC_OK;
if(r==XTC_OK) r=xtc_project_set_automation(project_,&point);
changed(r); return 1;
}
}
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; }
@ -534,7 +613,33 @@ int SequencerWidget::handle(int event)
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;
if(Fl::event_x()<header_left()+header_width_) return 1;
velocity_top_=track_top(static_cast<size_t>(row))+56;
if(audio_velocity_lane(t.id)) {
track_=t.id; clip_=UINT64_MAX; selection_.clear();
uint64_t position=std::min(XTC_PROJECT_MAX_TICK,static_cast<uint64_t>(std::max(0,Fl::event_x()-x()-header_width_))*960/static_cast<unsigned int>(scale_));
audio_velocity_preview_={t.id,position,XTC_AUTOMATION_GAIN,audio_velocity_value(Fl::event_y()),0};
audio_velocity_existing_=false;
size_t count=0; xtc_project_automation_count(project_,&count); int distance=12;
for(size_t i=0;i<count;++i) {
xtc_automation_point a{}; xtc_project_get_automation(project_,i,&a);
if(a.track_id!=t.id || a.parameter!=XTC_AUTOMATION_GAIN) continue;
int px=x()+header_width_+static_cast<int>(a.tick*static_cast<unsigned int>(scale_)/960U);
int py=velocity_top_+velocity_height_-8-static_cast<int>(a.value*(velocity_height_-28)/4.0f);
int d=std::abs(Fl::event_x()-px)+std::abs(Fl::event_y()-py);
if(d<distance) { distance=d; audio_velocity_preview_=a; audio_velocity_existing_=true; }
}
if(Fl::event_button()==FL_RIGHT_MOUSE) {
if(audio_velocity_existing_) {
auto point=audio_velocity_preview_;
xtc_result r=before_edit ? before_edit(context_) : XTC_OK;
if(r==XTC_OK) r=xtc_project_remove_automation(project_,point.track_id,point.parameter,point.tick);
changed(r);
}
} else if(Fl::event_button()==FL_LEFT_MOUSE) mode_=7;
redraw(); return 1;
}
if(Fl::event_button()!=FL_LEFT_MOUSE) 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;

View file

@ -4,6 +4,7 @@
#include <FL/Fl_Choice.H>
#include "xtc/xtc_plugin.h"
#include "xtc/xtc_editing.h"
#include "xtc/xtc_mixer.h"
#include <array>
#include "xtc/xtc_audio.h"
#include <memory>
@ -206,6 +207,11 @@ class SequencerWidget : public Fl_Group {
xtc_note_info velocity_original_{};
unsigned int velocity_preview_ = 100;
int velocity_top_ = 0;
bool audio_velocity_lane(uint64_t track) const;
void draw_audio_velocity(const xtc_track_info &track, int top, int left, int right);
float audio_velocity_value(int py) const;
xtc_automation_point audio_velocity_preview_{};
bool audio_velocity_existing_ = false;
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_;