1
0
Fork 0
forked from hanez/xtc

Phase 22: add non-destructive audio splitting, clip gain and fades, and cached waveforms

This commit is contained in:
Johannes Findeisen 2026-10-27 15:46:41 +01:00
commit 01d8c1d661
23 changed files with 650 additions and 18 deletions

View file

@ -8,7 +8,8 @@
#include <string.h>
typedef struct {
uint64_t start, end, offset;
uint64_t start, end, offset, fade_in, fade_out;
float gain;
size_t asset, track;
} region;
@ -185,6 +186,9 @@ xtc_result xtc_audio_plan_create(const xtc_project *project, unsigned int rate,
result = xtc_project_tick_to_sample(project, clip->start_tick + clip->length_tick, rate, &r->end);
if (result == XTC_OK)
result = xtc_project_tick_to_sample(project, clip->offset_tick, rate, &r->offset);
r->gain = clip->gain;
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->fade_in_tick, rate, &r->fade_in);
if (result == XTC_OK) result = xtc_project_tick_to_sample(project, clip->fade_out_tick, rate, &r->fade_out);
if (result != XTC_OK)
break;
}
@ -219,7 +223,7 @@ xtc_result xtc_audio_plan_set_fade_frames(xtc_audio_plan *plan, size_t frames)
}
/**
* @brief Renders a block of the plan at unity gain with linear rate conversion.
* @brief Renders a block with clip/track gain, boundary fades and linear rate conversion.
*
* Both buffers are replaced, not mixed into. Rendering performs no allocation,
* file access, or locking. Non-finite output samples are replaced with zero.
@ -247,7 +251,7 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
uint64_t audible_end = r->end;
uint64_t begin = position > r->start ? position : r->start;
uint64_t end;
if (plan->fade_frames && wave->samples) {
if (wave->samples) {
uint64_t duration = ((uint64_t)wave->frames * plan->rate + wave->rate - 1U) / wave->rate;
if (duration <= r->offset) continue;
if (duration - r->offset < audible_end - r->start)
@ -274,6 +278,16 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
if (distance < plan->fade_frames)
sample *= (float)distance / (float)plan->fade_frames;
}
{
uint64_t age = position + j - r->start, tail = audible_end - 1U - (position + j);
float envelope = 1.0f;
if (r->fade_in && age < r->fade_in) envelope = (float)age / (float)r->fade_in;
if (r->fade_out && tail < r->fade_out) {
float fade = (float)tail / (float)r->fade_out;
if (fade < envelope) envelope = fade;
}
sample *= r->gain * envelope;
}
output[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, right ? 0U : 2U);
if (right)
right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);

53
src/audio/waveform.c Normal file
View file

@ -0,0 +1,53 @@
#include "wave_internal.h"
#include <stdlib.h>
struct xtc_waveform {
unsigned int rate;
size_t frames, bins;
float minimum[1024], maximum[1024];
};
xtc_result xtc_waveform_create(const char *path, xtc_waveform **out)
{
xtc_wave wave = {0};
xtc_waveform *summary;
xtc_result result;
size_t i, j;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!path) return XTC_ERR_INVALID_ARGUMENT;
result = xtc_wave_load(path, &wave);
if (result != XTC_OK) return result;
summary = calloc(1, sizeof(*summary));
if (!summary) { free(wave.samples); return XTC_ERR_OUT_OF_MEMORY; }
summary->rate = wave.rate;
summary->frames = wave.frames;
summary->bins = wave.frames < 1024 ? wave.frames : 1024;
for (i = 0; i < summary->bins; ++i) {
size_t first = (size_t)((uint64_t)i * wave.frames / summary->bins);
size_t end = (size_t)((uint64_t)(i+1) * wave.frames / summary->bins);
float low = wave.samples[first], high = low;
for (j = first+1; j < end; ++j) {
if (wave.samples[j] < low) low = wave.samples[j];
if (wave.samples[j] > high) high = wave.samples[j];
}
summary->minimum[i] = low;
summary->maximum[i] = high;
}
free(wave.samples);
*out = summary;
return XTC_OK;
}
void xtc_waveform_destroy(xtc_waveform *p) { free(p); }
xtc_result xtc_waveform_info(const xtc_waveform *p, unsigned int *rate, size_t *frames, size_t *bins)
{
if (!p || !rate || !frames || !bins) return XTC_ERR_INVALID_ARGUMENT;
*rate = p->rate; *frames = p->frames; *bins = p->bins;
return XTC_OK;
}
xtc_result xtc_waveform_peak(const xtc_waveform *p, size_t bin, float *low, float *high)
{
if (!p || !low || !high || bin >= p->bins) return XTC_ERR_INVALID_ARGUMENT;
*low = p->minimum[bin]; *high = p->maximum[bin];
return XTC_OK;
}

View file

@ -147,6 +147,8 @@ MainWindow::MainWindow(bool prompt_unsaved)
timeline_scroll->end();
sequencer_->before_edit = before_arrangement_edit;
menu->add("Edit/Duplicate clip",FL_CTRL+'d',[](Fl_Widget *, void *c) { (void)static_cast<SequencerWidget *>(c)->duplicate_selection(); },sequencer_);
menu->add("Edit/Split audio at cursor", 0, [](Fl_Widget *, void *c) { (void)static_cast<SequencerWidget *>(c)->split_selection(); }, sequencer_);
menu->add("Edit/Audio clip gain and fades", 0, [](Fl_Widget *, void *c) { static_cast<SequencerWidget *>(c)->audio_properties_dialog(); }, sequencer_);
menu->add("Edit/Rename clip or track",FL_F+2,[](Fl_Widget *, void *c) {
const char *name = fl_input("Name (1..63 UTF-8 bytes)",""); if (name) (void)static_cast<SequencerWidget *>(c)->rename_selection(name);
},sequencer_);

View file

@ -7,6 +7,8 @@
#include <FL/Fl_Menu_Item.H>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <new>
/**
* @brief Constructs the timeline widget with a borrowed project and seek callback.
@ -33,10 +35,12 @@ void SequencerWidget::set_project(xtc_project *project, bool reset)
{
project_ = project;
if (reset) {
waves_.clear();
track_ = clip_ = UINT64_MAX;
cursor_ = 0;
mode_ = 0;
}
refresh_waveforms();
extent();
redraw();
}
@ -82,6 +86,7 @@ void SequencerWidget::changed(xtc_result result)
if (result != XTC_OK)
fl_alert("%s", xtc_result_string(result));
else {
refresh_waveforms();
extent();
do_callback();
}
@ -244,6 +249,7 @@ void SequencerWidget::draw()
fl_color(c.id == clip_ ? fl_rgb_color(100, 170, 205) : fl_rgb_color(65, 120, 150));
fl_rectf(a, py + 5, b - a, 44);
fl_push_clip(a, py + 5, b - a, 44);
draw_waveform(c, left, py);
fl_color(FL_WHITE);
char label[96];
std::snprintf(label,sizeof(label),"%s: %s",c.midi ? "MIDI" : c.asset_path[0] ? "Audio" : "Region",c.name);
@ -293,7 +299,7 @@ int SequencerWidget::handle(int event)
xtc_clip_info c{};
xtc_project_get_clip(project_, j - 1, &c);
int left = x() + 120 + static_cast<int>(c.start_tick * static_cast<unsigned int>(scale_) / 960U);
int right = left + static_cast<int>(c.length_tick * static_cast<unsigned int>(scale_) / 960U);
int right = left + std::max(4, static_cast<int>(c.length_tick * static_cast<unsigned int>(scale_) / 960U));
if (c.track_id == track_ && Fl::event_x() >= left && Fl::event_x() < right) {
clip_ = c.id;
original_ = c;
@ -304,12 +310,15 @@ int SequencerWidget::handle(int event)
}
if (Fl::event_button() == FL_RIGHT_MOUSE) {
mode_ = 0;
Fl_Menu_Item items[4]{};
Fl_Menu_Item items[6]{};
items[0].text = "Duplicate clip (Ctrl+D)"; items[1].text = "Rename (F2)"; items[2].text = "Delete (undoable)";
items[3].text = "Split audio at cursor (S)"; items[4].text = "Audio gain and fades (A)";
const Fl_Menu_Item *picked = items->popup(Fl::event_x(),Fl::event_y());
if (picked == items) duplicate_selection();
else if (picked == items+1) { const char *name = fl_input("Name (1..63 UTF-8 bytes)",""); if (name) rename_selection(name); }
else if (picked == items+2) delete_selection();
else if (picked == items+3) split_selection();
else if (picked == items+4) audio_properties_dialog();
}
redraw();
return 1;
@ -349,6 +358,10 @@ int SequencerWidget::handle(int event)
static_cast<uint64_t>(length), static_cast<uint64_t>(offset)));
return 1;
}
if (event == FL_KEYDOWN && !Fl::event_state(FL_CTRL | FL_ALT)) {
if (Fl::event_key() == 's') { split_selection(); return 1; }
if (Fl::event_key() == 'a') { audio_properties_dialog(); return 1; }
}
if (event == FL_KEYDOWN && Fl::event_state(FL_CTRL) && Fl::event_key() == 'd') { duplicate_selection(); return 1; }
if (event == FL_KEYDOWN && Fl::event_key() == FL_F+2) {
const char *name = fl_input("Name (1..63 UTF-8 bytes)",""); if (name) rename_selection(name); return 1;
@ -398,3 +411,97 @@ xtc_result SequencerWidget::move_track(int direction)
if (r == XTC_OK) r = xtc_project_move_track(project_,track_,direction < 0 ? i-1 : i+1);
changed(r); return r;
}
xtc_result SequencerWidget::split_selection()
{
uint64_t right;
const uint64_t split_tick = cursor_; // Stop/status refresh may move the playhead.
xtc_result result = before_edit ? before_edit(context_) : XTC_OK;
if (result == XTC_OK) result = xtc_project_split_audio(project_, clip_, split_tick, &right);
if (result == XTC_OK) clip_ = right;
changed(result);
return result;
}
xtc_result SequencerWidget::audio_properties(float gain, uint64_t in, uint64_t out)
{
xtc_result result = before_edit ? before_edit(context_) : XTC_OK;
if (result == XTC_OK) result = xtc_project_set_clip_audio(project_, clip_, gain, in, out);
changed(result);
return result;
}
void SequencerWidget::audio_properties_dialog()
{
xtc_clip_info clip{};
if (xtc_project_find_clip(project_, clip_, &clip) != XTC_OK || clip.midi) {
fl_alert("Select an audio clip or silent region first.");
return;
}
char defaults[128];
std::snprintf(defaults, sizeof(defaults), "%.9g %llu %llu", static_cast<double>(clip.gain),
static_cast<unsigned long long>(clip.fade_in_tick), static_cast<unsigned long long>(clip.fade_out_tick));
const char *text = fl_input("Linear gain (0..4), fade-in ticks, fade-out ticks (0 disables)", defaults);
if (!text) return;
std::istringstream input(text);
float gain; uint64_t in, out;
if (!(input >> gain >> in >> out) || !(input >> std::ws).eof()) {
fl_alert("Enter gain and two fade durations in ticks."); return;
}
audio_properties(gain, in, out);
}
void SequencerWidget::refresh_waveforms() try
{
// Control-thread loading only. Keep cached failures as well as successes;
// removed/relinked paths are evicted. Reopening rebuilds externally changed files.
size_t tracks, clips;
xtc_project_get_counts(project_, &tracks, &clips);
std::vector<Wave> next;
for (size_t i = 0; i < clips && next.size() < 64; ++i) {
xtc_clip_info c{};
xtc_project_get_clip(project_, i, &c);
if (!c.asset_path[0]) continue;
bool present = false;
for (const auto &wave : next) if (wave.path == c.asset_path) { present = true; break; }
if (present) continue;
Wave entry;
entry.path = c.asset_path;
for (auto &wave : waves_) if (wave.path == entry.path) {
entry.data = std::move(wave.data); present = true; break;
}
if (!present) {
xtc_waveform *summary = nullptr;
(void)xtc_waveform_create(c.asset_path, &summary);
entry.data.reset(summary);
}
next.push_back(std::move(entry));
}
waves_ = std::move(next);
} catch (const std::bad_alloc &) {
waves_.clear(); // Preview failure must not interrupt the model/history edit.
}
void SequencerWidget::draw_waveform(const xtc_clip_info &c, int left, int top)
{
const xtc_waveform *summary = nullptr;
for (const auto &wave : waves_) if (wave.path == c.asset_path) { summary = wave.data.get(); break; }
if (!summary) return;
unsigned int rate; size_t frames, bins;
if (xtc_waveform_info(summary, &rate, &frames, &bins) != XTC_OK) return;
double ticks_per_frame = static_cast<double>(xtc_project_tempo(project_)) * 960 / (rate * 60000.0);
fl_color(fl_rgb_color(30, 65, 80));
for (size_t bin = 0; bin < bins; ++bin) {
double start = static_cast<double>(bin * frames / bins) * ticks_per_frame;
double end = static_cast<double>((bin+1) * frames / bins) * ticks_per_frame;
double offset = static_cast<double>(c.offset_tick);
if (end <= offset || start >= offset + static_cast<double>(c.length_tick)) continue;
int px = left + static_cast<int>((std::max(start, offset) - offset) * scale_ / 960);
int width = std::max(1, static_cast<int>((end - std::max(start, offset)) * scale_ / 960));
float low, high;
if (xtc_waveform_peak(summary, bin, &low, &high) != XTC_OK) continue;
int y1 = top + 28 - static_cast<int>(std::clamp(high, -1.0f, 1.0f) * 19);
int y2 = top + 28 - static_cast<int>(std::clamp(low, -1.0f, 1.0f) * 19);
fl_rectf(px, y1, width, std::max(1, y2-y1+1));
}
}

View file

@ -1,7 +1,10 @@
#ifndef XTC_SEQUENCER_WIDGET_HPP
#define XTC_SEQUENCER_WIDGET_HPP
#include <FL/Fl_Widget.H>
#include "xtc/xtc_project.h"
#include "xtc/xtc_audio.h"
#include <memory>
#include <string>
#include <vector>
class SequencerWidget : public Fl_Widget {
public:
@ -11,6 +14,12 @@ class SequencerWidget : public Fl_Widget {
* @return XTC_OK to allow the edit, or an error to cancel it.
*/
xtc_result duplicate_selection();
xtc_result split_selection();
xtc_result audio_properties(float gain, uint64_t fade_in, uint64_t fade_out);
void audio_properties_dialog();
void refresh_waveforms();
size_t waveform_count() const { return waves_.size(); }
xtc_result rename_selection(const char *name);
xtc_result move_track(int direction);
xtc_result grid(uint64_t ticks);
@ -131,6 +140,12 @@ class SequencerWidget : public Fl_Widget {
void draw() override;
private:
struct Wave {
std::string path;
std::unique_ptr<xtc_waveform, decltype(&xtc_waveform_destroy)> data{nullptr, xtc_waveform_destroy};
};
std::vector<Wave> waves_;
void draw_waveform(const xtc_clip_info &clip, int left, int top);
xtc_project *project_;
Seek seek_;
void *context_;

View file

@ -351,6 +351,13 @@ static xtc_result read_project(reader *input, xtc_project *project, const char *
return XTC_ERR_FORMAT;
clip->midi = (int)value;
}
clip->gain = 1.0f;
if (format_version >= 8U) {
READ("AUDIO", 4U);
if (!decode_float(input->tokens[1], &clip->gain)) return XTC_ERR_FORMAT;
NUM(2U, clip->fade_in_tick);
NUM(3U, clip->fade_out_tick);
}
}
if (format_version >= 3U) {
READ("NOTES", 3U);
@ -513,6 +520,12 @@ static xtc_result write_project(FILE *file, const xtc_project *project, const ch
return XTC_ERR_IO;
if (fprintf(file, "MIDI %d\n", clip->midi) < 0)
return XTC_ERR_IO;
{
uint32_t bits;
memcpy(&bits, &clip->gain, sizeof(bits));
if (fprintf(file, "AUDIO %08" PRIx32 " %" PRIu64 " %" PRIu64 "\n", bits,
clip->fade_in_tick, clip->fade_out_tick) < 0) return XTC_ERR_IO;
}
}
if (fprintf(file, "NOTES %" PRIu64 " %zu\n", project->next_note_id, project->note_count) < 0)
return XTC_ERR_IO;

View file

@ -358,6 +358,7 @@ xtc_result xtc_project_add_clip(xtc_project *project, uint64_t track, const char
return XTC_ERR_NOT_FOUND;
if (project->clip_count == XTC_PROJECT_MAX_CLIPS || project->next_clip_id == UINT64_MAX)
return XTC_ERR_CAPACITY;
clip.gain = 1.0f;
clip.id = project->next_clip_id++;
clip.track_id = track;
clip.start_tick = start;
@ -527,6 +528,10 @@ xtc_result xtc_project_validate(const xtc_project *project)
}
if (path_length == XTC_ASSET_PATH_BYTES || (path_length && clip->asset_path[0] != '/'))
return XTC_ERR_FORMAT;
if (!isfinite(clip->gain) || clip->gain < 0 || clip->gain > 4 ||
clip->fade_in_tick > clip->source_length_tick || clip->fade_out_tick > clip->source_length_tick ||
(clip->midi && (clip->gain != 1 || clip->fade_in_tick || clip->fade_out_tick)))
return XTC_ERR_FORMAT;
if ((clip->midi != 0 && clip->midi != 1) || (clip->midi && path_length))
return XTC_ERR_FORMAT;
if (clip->id >= project->next_clip_id || track_index(project, clip->track_id) == SIZE_MAX ||
@ -646,3 +651,42 @@ xtc_result xtc_project_move_track(xtc_project *p, uint64_t id, size_t destinatio
else if (i > destination) memmove(p->tracks+destination+1,p->tracks+destination,(i-destination)*sizeof(track));
p->tracks[destination] = track; return XTC_OK;
}
xtc_result xtc_project_set_clip_audio(xtc_project *p, uint64_t id, float gain, uint64_t in, uint64_t out)
{
size_t i;
if (!p || !isfinite(gain) || gain < 0 || gain > 4) return XTC_ERR_INVALID_ARGUMENT;
i = clip_index(p, id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
if (p->clips[i].midi) return XTC_ERR_INVALID_STATE;
if (in > p->clips[i].source_length_tick || out > p->clips[i].source_length_tick) return XTC_ERR_INVALID_ARGUMENT;
p->clips[i].gain = gain;
p->clips[i].fade_in_tick = in;
p->clips[i].fade_out_tick = out;
return XTC_OK;
}
xtc_result xtc_project_split_audio(xtc_project *p, uint64_t id, uint64_t tick, uint64_t *out)
{
size_t i;
xtc_clip_info right;
uint64_t left_length;
if (!p || !out) return XTC_ERR_INVALID_ARGUMENT;
i = clip_index(p, id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
right = p->clips[i];
if (right.midi) return XTC_ERR_INVALID_STATE;
if (tick <= right.start_tick || tick >= right.start_tick + right.length_tick) return XTC_ERR_INVALID_ARGUMENT;
if (p->clip_count == XTC_PROJECT_MAX_CLIPS || p->next_clip_id == UINT64_MAX) return XTC_ERR_CAPACITY;
left_length = tick - right.start_tick;
right.id = p->next_clip_id++;
right.start_tick = tick;
right.offset_tick += left_length;
right.length_tick -= left_length;
right.fade_in_tick = 0;
p->clips[i].length_tick = left_length;
p->clips[i].fade_out_tick = 0;
p->clips[p->clip_count++] = right;
*out = right.id;
return XTC_OK;
}