1
0
Fork 0
forked from hanez/xtc

Phase 21: add filtered MIDI composition edits and triplet piano-roll grids

This commit is contained in:
Johannes Findeisen 2026-10-27 15:34:54 +01:00
commit 73fc5eea8d
12 changed files with 796 additions and 39 deletions

View file

@ -1238,11 +1238,16 @@ xtc_result MainWindow::open_piano_roll()
if (!piano_window_) {
piano_window_ = std::make_unique<Fl_Double_Window>(900, 620, "xtc — Piano roll");
piano_window_->begin();
auto *hint = new Fl_Box(12, 8, 876, 32,
"Source ticks: click draws; drag body moves/pitches, right edge resizes; "
"right-click/Delete removes. Snap 1/16.");
hint->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
hint->labelsize(12);
auto *menu = new Fl_Menu_Bar(12, 8, 876, 30);
const char *commands[] = {"Notes/Grid (G)", "Notes/Filter (F)", "Notes/Velocity and channel (V)",
"Notes/Quantize (Q)", "Notes/Transpose (T)", "Notes/Repeat (Ctrl+D)",
"Notes/Select filtered (Ctrl+A)", "Notes/Delete"};
for (int i = 0; i < 8; ++i)
menu->add(commands[i], 0, [](Fl_Widget *widget, void *data) {
auto *owner = static_cast<MainWindow *>(data);
auto *bar = static_cast<Fl_Menu_Bar *>(widget);
owner->piano_->command(static_cast<int>(bar->mvalue() - bar->menu()) - 1);
}, this);
auto *scroll = new Fl_Scroll(12, 48, 876, 560);
piano_ = new PianoRollWidget(12, 48, project_.get(), clip.id, before_arrangement_edit, this);
piano_->callback(

View file

@ -4,9 +4,16 @@
#include <FL/fl_ask.H>
#include <algorithm>
#include <cstdio>
#include <sstream>
namespace {
constexpr int key_width = 64, row_height = 16, scale = 96, header = 28;
template <typename... Values> bool parse(const char *text, Values &...values)
{
std::istringstream input(text);
(input >> ... >> values);
return !input.fail() && (input >> std::ws).eof();
}
}
/**
@ -34,15 +41,18 @@ PianoRollWidget::PianoRollWidget(int left, int top, xtc_project *project, uint64
* @brief Points the editor at a different project or clip.
*
* Clears any in-progress drag when the project or clip changes and resizes the
* widget to fit the clip's source length. The current selection is not changed.
* widget to fit the clip's source length. Selection is cleared on a new model or clip.
*
* @param project Borrowed project that must outlive the widget.
* @param clip ID of the MIDI clip to display.
*/
void PianoRollWidget::set_project(xtc_project *project, uint64_t clip)
{
if (project != project_ || clip != clip_)
if (project != project_ || clip != clip_) {
mode_ = 0;
selected_ = UINT64_MAX;
all_ = false;
}
project_ = project;
clip_ = clip;
xtc_clip_info c{};
@ -57,7 +67,7 @@ void PianoRollWidget::set_project(xtc_project *project, uint64_t clip)
* @brief Converts a horizontal pixel position to a snapped tick.
*
* The position is measured in absolute window coordinates and snapped down to
* the nearest sixteenth note (240 ticks).
* the current configured grid.
*
* @param px Horizontal position in window coordinates.
* @return Tick offset within the clip source, or 0 for positions on or left of
@ -65,7 +75,7 @@ void PianoRollWidget::set_project(xtc_project *project, uint64_t clip)
*/
uint64_t PianoRollWidget::tick(int px) const
{
return static_cast<uint64_t>(std::max(0, px - x() - key_width)) * 960U / scale / 240U * 240U;
return static_cast<uint64_t>(std::max(0, px - x() - key_width)) * 960U / scale / grid_ * grid_;
}
/**
@ -106,8 +116,10 @@ xtc_result PianoRollWidget::add_note(uint64_t start, uint64_t length, unsigned i
uint64_t id;
if (r == XTC_OK)
r = xtc_project_add_note(project_, clip_, start, length, pitch, velocity, channel, &id);
if (r == XTC_OK)
if (r == XTC_OK) {
selected_ = id;
all_ = false;
}
return change(r);
}
@ -121,12 +133,9 @@ xtc_result PianoRollWidget::add_note(uint64_t start, uint64_t length, unsigned i
*/
xtc_result PianoRollWidget::delete_selected()
{
xtc_result r = before_ ? before_(context_) : XTC_OK;
if (r == XTC_OK)
r = xtc_project_remove_note(project_, selected_);
if (r == XTC_OK)
selected_ = UINT64_MAX;
return change(r);
xtc_note_edit edit{};
edit.action = XTC_NOTES_DELETE;
return apply(edit);
}
/**
@ -164,11 +173,14 @@ void PianoRollWidget::draw()
fl_draw(text, x() + 6, py + 12);
}
}
int first = std::max(0, (cx - x() - key_width) / (scale / 4));
int last = (cx + cw - x() - key_width) / (scale / 4) + 1;
// At 1-tick snap, show at least a 120-tick visual grid to avoid pixel overdraw.
uint64_t visual = std::max<uint64_t>(120, grid_);
int spacing = static_cast<int>(visual * scale / 960);
int first = std::max(0, (cx - x() - key_width) / spacing);
int last = (cx + cw - x() - key_width) / spacing + 1;
for (int step = first; step <= last; ++step) {
int px = x() + key_width + step * (scale / 4);
fl_color(step % 16 == 0 ? fl_rgb_color(95, 98, 105) : fl_rgb_color(52, 55, 62));
int px = x() + key_width + static_cast<int>(static_cast<uint64_t>(step) * visual * scale / 960);
fl_color((static_cast<uint64_t>(step) * visual) % 3840 == 0 ? fl_rgb_color(95, 98, 105) : fl_rgb_color(52, 55, 62));
fl_line(px, cy, px, cy + ch);
}
size_t count = 0;
@ -176,7 +188,7 @@ void PianoRollWidget::draw()
for (size_t i = 0; i < count; ++i) {
xtc_note_info n{};
xtc_project_get_note(project_, i, &n);
if (n.clip_id != clip_)
if (!visible(n))
continue;
int left = x() + key_width + static_cast<int>(n.start_tick * scale / 960);
int right = left + std::max(3, static_cast<int>(n.length_tick * scale / 960));
@ -184,9 +196,18 @@ void PianoRollWidget::draw()
int a = std::max(cx, left), b = std::min(cx + cw, right);
if (a >= b || py + row_height < cy || py > cy + ch)
continue;
fl_color(n.id == selected_ ? fl_rgb_color(230, 190, 80) : fl_rgb_color(90, 175, 130));
fl_color(all_ || n.id == selected_ ? fl_rgb_color(230, 190, 80)
: fl_rgb_color(50, static_cast<uchar>(70 + n.velocity), 110));
fl_rectf(a, py + 1, b - a, row_height - 3);
}
fl_color(fl_rgb_color(30, 33, 38));
fl_rectf(x(), y(), w(), header);
char status[160];
std::snprintf(status, sizeof(status), "Snap %llu ticks | Channel %d (-1 all) | Pitches %u..%u | %s",
static_cast<unsigned long long>(grid_), channel_filter_, low_, high_,
all_ ? "All filtered notes selected" : "Click selects; Ctrl+A selects filtered notes");
fl_color(FL_WHITE);
fl_draw(status, x() + 6, y() + 18);
fl_pop_clip();
}
@ -209,13 +230,14 @@ int PianoRollWidget::handle(int event)
size_t count = 0;
xtc_project_note_count(project_, &count);
selected_ = UINT64_MAX;
all_ = false;
for (size_t i = count; i > 0; --i) {
xtc_note_info n{};
xtc_project_get_note(project_, i - 1, &n);
int left = x() + key_width + static_cast<int>(n.start_tick * scale / 960);
int right = left + std::max(3, static_cast<int>(n.length_tick * scale / 960));
int top = y() + header + (127 - static_cast<int>(n.pitch)) * row_height;
if (n.clip_id == clip_ && Fl::event_x() >= left && Fl::event_x() < right && Fl::event_y() >= top &&
if (visible(n) && Fl::event_x() >= left && Fl::event_x() < right && Fl::event_y() >= top &&
Fl::event_y() < top + row_height) {
selected_ = n.id;
original_ = n;
@ -233,10 +255,10 @@ int PianoRollWidget::handle(int event)
int pitch = 127 - (Fl::event_y() - y() - header) / row_height;
xtc_clip_info c{};
uint64_t start = tick(Fl::event_x());
if (pitch >= 0 && pitch <= 127 && xtc_project_find_clip(project_, clip_, &c) == XTC_OK &&
if (pitch >= static_cast<int>(low_) && pitch <= static_cast<int>(high_) && xtc_project_find_clip(project_, clip_, &c) == XTC_OK &&
start < c.source_length_tick)
add_note(start, std::min<uint64_t>(960, c.source_length_tick - start),
static_cast<unsigned int>(pitch));
static_cast<unsigned int>(pitch), 100, channel_filter_ < 0 ? 0U : static_cast<unsigned int>(channel_filter_));
}
redraw();
return 1;
@ -246,7 +268,7 @@ int PianoRollWidget::handle(int event)
if (event == FL_RELEASE && mode_) {
int mode = mode_;
mode_ = 0;
int64_t delta = static_cast<int64_t>(Fl::event_x() - drag_x_) * 960 / scale / 240 * 240;
int64_t delta = static_cast<int64_t>(Fl::event_x() - drag_x_) * 960 / scale / static_cast<int64_t>(grid_) * static_cast<int64_t>(grid_);
xtc_clip_info c{};
if (xtc_project_find_clip(project_, clip_, &c) != XTC_OK)
return 1;
@ -259,7 +281,7 @@ int PianoRollWidget::handle(int event)
std::clamp(static_cast<int>(pitch) - (Fl::event_y() - drag_y_) / row_height, 0, 127));
} else {
int64_t maximum = static_cast<int64_t>(c.source_length_tick) - start;
length = std::clamp<int64_t>(length + delta, std::min<int64_t>(240, maximum), maximum);
length = std::clamp<int64_t>(length + delta, std::min<int64_t>(static_cast<int64_t>(grid_), maximum), maximum);
}
xtc_result r = before_ ? before_(context_) : XTC_OK;
if (r == XTC_OK)
@ -268,6 +290,23 @@ int PianoRollWidget::handle(int event)
change(r);
return 1;
}
if (event == FL_KEYDOWN) {
int key = Fl::event_key();
if (key == 'a' && (Fl::event_state() & FL_CTRL)) { select_filtered(); return 1; }
if (key == 'q') { command(3); return 1; }
if (key == 'v') { command(2); return 1; }
if (key == 'g') { command(0); return 1; }
if (key == 'f') { command(1); return 1; }
if (key == 't') { command(4); return 1; }
if (key == 'd' && (Fl::event_state() & FL_CTRL)) { command(5); return 1; }
if (key == FL_Up || key == FL_Down) {
xtc_note_edit edit{};
edit.velocity = edit.channel = -1;
edit.transpose = key == FL_Up ? 1 : -1;
apply(edit);
return 1;
}
}
if (event == FL_KEYDOWN && Fl::event_key() == FL_Delete) {
delete_selected();
return 1;
@ -280,3 +319,100 @@ int PianoRollWidget::handle(int event)
return 1;
return Fl_Widget::handle(event);
}
bool PianoRollWidget::visible(const xtc_note_info &n) const
{
return n.clip_id == clip_ && (channel_filter_ < 0 || n.channel == static_cast<unsigned int>(channel_filter_)) &&
n.pitch >= low_ && n.pitch <= high_;
}
xtc_result PianoRollWidget::set_grid(uint64_t ticks)
{
if (ticks != 1 && ticks != 120 && ticks != 160 && ticks != 240 && ticks != 320 && ticks != 480 && ticks != 960)
return XTC_ERR_INVALID_ARGUMENT;
grid_ = ticks;
redraw();
return XTC_OK;
}
xtc_result PianoRollWidget::set_filter(int channel, unsigned int low, unsigned int high)
{
if (channel < -1 || channel > 15 || low > high || high > 127) return XTC_ERR_INVALID_ARGUMENT;
channel_filter_ = channel;
low_ = low;
high_ = high;
selected_ = UINT64_MAX;
all_ = false;
mode_ = 0;
redraw();
return XTC_OK;
}
void PianoRollWidget::select_filtered()
{
selected_ = UINT64_MAX;
all_ = true;
mode_ = 0;
redraw();
}
xtc_result PianoRollWidget::apply(const xtc_note_edit &edit)
{
if (!all_ && selected_ == UINT64_MAX) return XTC_ERR_NOT_FOUND;
xtc_note_filter filter{clip_, all_ ? UINT64_MAX : selected_, channel_filter_, low_, high_};
size_t count;
xtc_result r = before_ ? before_(context_) : XTC_OK;
if (r == XTC_OK) r = xtc_project_edit_notes(project_, &filter, &edit, &count);
if (r == XTC_OK && edit.action == XTC_NOTES_DELETE) {
selected_ = UINT64_MAX;
all_ = false;
}
return change(r);
}
void PianoRollWidget::command(int action)
{
const char *text;
xtc_note_edit edit{};
edit.velocity = edit.channel = -1;
if (action == 0) {
text = fl_input("Snap ticks: 1, 120, 160 (1/16 triplet), 240, 320 (1/8 triplet), 480, 960", "240");
unsigned int value;
if (text && (!parse(text, value) || set_grid(value) != XTC_OK))
fl_alert("Choose one of the listed grids.");
return;
}
if (action == 1) {
text = fl_input("Channel (-1 all, 0..15), lowest pitch, highest pitch", "-1 0 127");
int channel; unsigned int low, high;
if (text && (!parse(text, channel, low, high) ||
set_filter(channel, low, high) != XTC_OK)) fl_alert("Invalid note filter.");
return;
}
if (action == 6) { select_filtered(); return; }
if (action == 7) { delete_selected(); return; }
if (action == 2) {
text = fl_input("Velocity (1..127, -1 preserve), channel (0..15, -1 preserve)", "100 -1");
if (!text) return;
if (!parse(text, edit.velocity, edit.channel)) {
fl_alert("Enter velocity and channel."); return;
}
} else if (action == 3) edit.grid_tick = grid_;
else if (action == 4) {
text = fl_input("Transpose semitones (-127..127)", "12");
if (!text) return;
if (!parse(text, edit.transpose)) {
fl_alert("Enter a semitone count."); return;
}
} else if (action == 5) {
text = fl_input("Additional copies (1..4096), spacing in source ticks (clip must have room)", "1 960");
unsigned long long interval;
if (!text) return;
if (!parse(text, edit.copies, interval)) {
fl_alert("Enter copy count and spacing."); return;
}
edit.action = XTC_NOTES_REPEAT;
edit.interval_tick = static_cast<uint64_t>(interval);
} else return;
apply(edit);
}

View file

@ -29,8 +29,8 @@ class PianoRollWidget : public Fl_Widget {
* @brief Points the editor at a different project or clip.
*
* Clears any in-progress drag when the project or clip changes and resizes
* the widget to fit the clip's source length. The current selection is not
* changed.
* the widget to fit the clip's source length. Selection is cleared on a new
* model or clip.
*
* @param project Borrowed project that must outlive the widget.
* @param clip ID of the MIDI clip to display.
@ -70,6 +70,15 @@ class PianoRollWidget : public Fl_Widget {
* @return XTC_OK on success, the guard's error, or the project error.
*/
xtc_result delete_selected();
/* UI preferences; independent of persisted project state. */
xtc_result set_grid(uint64_t ticks);
xtc_result set_filter(int channel, unsigned int low, unsigned int high);
void select_filtered();
xtc_result apply(const xtc_note_edit &edit);
/* Menu/keyboard commands: grid, filter, properties, quantize, transpose,
repeat, select all, delete. */
void command(int action);
/**
* @brief Handles mouse, keyboard, and focus events for the editor.
*
@ -98,6 +107,12 @@ class PianoRollWidget : public Fl_Widget {
void *context_;
xtc_note_info original_{};
int mode_ = 0, drag_x_ = 0, drag_y_ = 0;
uint64_t grid_ = 240;
int channel_filter_ = -1;
unsigned int low_ = 0, high_ = 127;
bool all_ = false;
bool visible(const xtc_note_info &n) const;
/**
* @brief Reports an edit result and notifies listeners on success.
*
@ -112,7 +127,7 @@ class PianoRollWidget : public Fl_Widget {
* @brief Converts a horizontal pixel position to a snapped tick.
*
* The position is measured in absolute window coordinates and snapped down
* to the nearest sixteenth note (240 ticks).
* to the current configured grid.
*
* @param px Horizontal position in window coordinates.
* @return Tick offset within the clip source, or 0 for positions on or left

View file

@ -204,3 +204,90 @@ xtc_result xtc_project_remove_note(xtc_project *p, uint64_t id)
--p->note_count;
return XTC_OK;
}
/* Selection is evaluated against original properties, never transformed ones. */
static int matches(const xtc_note_info *n, const xtc_note_filter *f)
{
return n->clip_id == f->clip_id && (f->note_id == UINT64_MAX || n->id == f->note_id) &&
(f->channel == -1 || n->channel == (unsigned int)f->channel) &&
n->pitch >= f->pitch_min && n->pitch <= f->pitch_max;
}
static xtc_result transformed(const xtc_project *p, xtc_note_info *n, const xtc_note_edit *e,
unsigned int copy)
{
int pitch = (int)n->pitch + e->transpose;
if (pitch < 0 || pitch > 127) return XTC_ERR_INVALID_ARGUMENT;
n->pitch = (unsigned int)pitch;
if (e->velocity != -1) n->velocity = (unsigned int)e->velocity;
if (e->channel != -1) n->channel = (unsigned int)e->channel;
if (e->grid_tick) {
uint64_t rem = n->start_tick % e->grid_tick;
n->start_tick -= rem;
if (rem >= e->grid_tick - rem) {
if (n->start_tick > UINT64_MAX - e->grid_tick) return XTC_ERR_INVALID_ARGUMENT;
n->start_tick += e->grid_tick;
}
}
if (copy) {
if (e->interval_tick > (UINT64_MAX - n->start_tick) / copy) return XTC_ERR_INVALID_ARGUMENT;
n->start_tick += e->interval_tick * copy;
}
return valid_note(p, n->clip_id, n->start_tick, n->length_tick, n->pitch, n->velocity, n->channel);
}
xtc_result xtc_project_edit_notes(xtc_project *p, const xtc_note_filter *f,
const xtc_note_edit *e, size_t *out)
{
size_t i, count = 0, original, write = 0;
unsigned int k;
xtc_clip_info clip;
xtc_result r;
if (!p || !f || !e || !out || f->channel < -1 || f->channel > 15 ||
f->pitch_min > f->pitch_max || f->pitch_max > 127 ||
e->action < XTC_NOTES_EDIT || e->action > XTC_NOTES_DELETE)
return XTC_ERR_INVALID_ARGUMENT;
r = xtc_project_find_clip(p, f->clip_id, &clip);
if (r != XTC_OK) return r;
if (!clip.midi) return XTC_ERR_INVALID_STATE;
if (e->action != XTC_NOTES_DELETE && (e->transpose < -127 || e->transpose > 127 ||
e->velocity < -1 || e->velocity == 0 || e->velocity > 127 || e->channel < -1 || e->channel > 15 ||
(e->action == XTC_NOTES_REPEAT && (!e->copies || e->copies > XTC_PROJECT_MAX_NOTES || !e->interval_tick))))
return XTC_ERR_INVALID_ARGUMENT;
original = p->note_count;
for (i = 0; i < original; ++i) if (matches(p->notes+i, f)) ++count;
if (!count) return XTC_ERR_NOT_FOUND;
if (e->action == XTC_NOTES_REPEAT &&
(count > (XTC_PROJECT_MAX_NOTES-original) / e->copies ||
(uint64_t)count > (UINT64_MAX-p->next_note_id) / e->copies)) return XTC_ERR_CAPACITY;
/* Validate every result before changing anything. Bounded by note capacity. */
if (e->action != XTC_NOTES_DELETE)
for (i = 0; i < original; ++i) if (matches(p->notes+i, f)) {
unsigned int copies = e->action == XTC_NOTES_REPEAT ? e->copies : 0;
for (k = copies ? 1U : 0U; k <= copies; ++k) {
xtc_note_info n = p->notes[i];
r = transformed(p, &n, e, k);
if (r != XTC_OK) return r;
}
}
for (i = 0; i < original; ++i) {
xtc_note_info n = p->notes[i];
if (!matches(&n, f)) {
if (e->action == XTC_NOTES_DELETE) p->notes[write++] = n;
continue;
}
if (e->action == XTC_NOTES_DELETE) continue;
if (e->action == XTC_NOTES_EDIT) {
(void)transformed(p, &n, e, 0);
p->notes[i] = n;
} else for (k = 1; k <= e->copies; ++k) {
n = p->notes[i];
(void)transformed(p, &n, e, k);
n.id = p->next_note_id++;
p->notes[p->note_count++] = n;
}
}
if (e->action == XTC_NOTES_DELETE) p->note_count = write;
*out = count;
return XTC_OK;
}