Phase 24: add mix buses, coherent peak meters and master clipping indicators
This commit is contained in:
parent
ba5c2503a7
commit
2fd7724065
32 changed files with 632 additions and 61 deletions
|
|
@ -14,7 +14,8 @@ typedef struct {
|
|||
} region;
|
||||
|
||||
struct xtc_audio_plan {
|
||||
size_t fade_frames;
|
||||
size_t fade_frames, monitor_stride;
|
||||
float *monitor;
|
||||
xtc_mix_snapshot mix;
|
||||
xtc_wave waves[64];
|
||||
char paths[64][XTC_ASSET_PATH_BYTES];
|
||||
|
|
@ -288,6 +289,7 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
|
|||
}
|
||||
sample *= r->gain * envelope;
|
||||
}
|
||||
if (plan->monitor) xtc_mix_monitor(&plan->mix, plan->monitor, plan->monitor_stride, r->track, position+j, j, sample, right != NULL);
|
||||
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);
|
||||
|
|
@ -315,3 +317,6 @@ xtc_result xtc_audio_plan_render(const xtc_audio_plan *p, uint64_t position, flo
|
|||
{
|
||||
return xtc_audio_plan_render_stereo(p, position, out, NULL, frames);
|
||||
}
|
||||
|
||||
void xtc_audio_plan_monitor(xtc_audio_plan *p, float *samples, size_t stride)
|
||||
{ if (p) { p->monitor = samples; p->monitor_stride = stride; } }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "xtc/xtc_backend.h"
|
||||
#include "xtc/xtc_realtime.h"
|
||||
#include "xtc/xtc_dsp.h"
|
||||
#include "../mixer/mix_internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
|
@ -18,7 +19,10 @@ struct xtc_backend {
|
|||
xtc_midi_plan *midi;
|
||||
unsigned int panic_requested, panic_seen, midi_dropped, peak_left, peak_right;
|
||||
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
|
||||
float *timeline, *instrument;
|
||||
float *timeline, *instrument, *monitor;
|
||||
size_t meter_count;
|
||||
uint64_t meter_ids[XTC_PROJECT_MAX_TRACKS];
|
||||
unsigned int meter_peaks[XTC_PROJECT_MAX_TRACKS][2], master_peaks[2];
|
||||
size_t max_frames;
|
||||
size_t transition_frames; /* Control owner while stopped. */
|
||||
xtc_gain_ramp ramp; /* Audio owner while active. */
|
||||
|
|
@ -31,6 +35,15 @@ struct xtc_backend {
|
|||
int ready, running; /* Control thread only. */
|
||||
};
|
||||
|
||||
/* One producer, one exchanging reader; at most one CAS, never a retry loop. */
|
||||
static void publish_peak(unsigned int *slot, float peak)
|
||||
{
|
||||
unsigned int value = (unsigned int)(fminf(isfinite(peak) ? peak : 0, 1000) * 1000000);
|
||||
unsigned int old = __atomic_load_n(slot, __ATOMIC_RELAXED);
|
||||
if (value > old && !__atomic_compare_exchange_n(slot, &old, value, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
__atomic_store_n(slot, value, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decodes queued JACK MIDI and renders the instrument for the current audio block.
|
||||
*
|
||||
|
|
@ -127,6 +140,11 @@ static int process(jack_nframes_t frames, void *context)
|
|||
(void)xtc_realtime_position(backend->engine,position);
|
||||
if (backend->recorder)
|
||||
xtc_recorder_push(backend->recorder, input, (size_t)frames);
|
||||
{
|
||||
size_t channel;
|
||||
for (channel = 0; channel < backend->meter_count*2; ++channel)
|
||||
memset(backend->monitor + channel*backend->max_frames, 0, (size_t)frames*sizeof(float));
|
||||
}
|
||||
if (backend->audio)
|
||||
result =
|
||||
xtc_audio_plan_render_stereo(backend->audio, position, backend->timeline,
|
||||
|
|
@ -175,6 +193,20 @@ static int process(jack_nframes_t frames, void *context)
|
|||
if (r > right_peak)
|
||||
right_peak = r;
|
||||
}
|
||||
publish_peak(backend->master_peaks, left_peak);
|
||||
publish_peak(backend->master_peaks+1, right_peak);
|
||||
if (result == XTC_OK) {
|
||||
size_t track, side, frame;
|
||||
for (track = 0; track < backend->meter_count; ++track)
|
||||
for (side = 0; side < 2; ++side) {
|
||||
float peak = 0;
|
||||
for (frame = 0; frame < frames; ++frame) {
|
||||
float value = fabsf(backend->monitor[(track*2+side)*backend->max_frames+frame]);
|
||||
if (isfinite(value) && value > peak) peak = value;
|
||||
}
|
||||
publish_peak(&backend->meter_peaks[track][side], peak);
|
||||
}
|
||||
}
|
||||
__atomic_store_n(&backend->peak_left, (unsigned int)(fminf(left_peak, 1000) * 1000000), __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&backend->peak_right, (unsigned int)(fminf(right_peak, 1000) * 1000000), __ATOMIC_RELAXED);
|
||||
}
|
||||
|
|
@ -284,14 +316,16 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
|
|||
backend->instrument = calloc(config->max_frames * config->channel_count, sizeof(float));
|
||||
if (!backend->timeline || !backend->instrument) {
|
||||
free(backend->timeline);
|
||||
free(backend->instrument);
|
||||
free(backend->monitor);
|
||||
free(backend->instrument);
|
||||
free(backend);
|
||||
return XTC_ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
backend->max_frames = config->max_frames;
|
||||
backend->client = jack_client_open(config->client_name, JackNoStartServer, &status);
|
||||
if (backend->client == NULL) {
|
||||
free(backend->instrument);
|
||||
free(backend->monitor);
|
||||
free(backend->instrument);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
return XTC_ERR_IO;
|
||||
|
|
@ -337,6 +371,7 @@ fail:
|
|||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
xtc_midi_plan_destroy(backend->midi);
|
||||
free(backend->monitor);
|
||||
free(backend->instrument);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
|
|
@ -359,6 +394,7 @@ xtc_result xtc_backend_close(xtc_backend *backend)
|
|||
xtc_realtime_destroy(backend->engine);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
xtc_midi_plan_destroy(backend->midi);
|
||||
free(backend->monitor);
|
||||
free(backend->instrument);
|
||||
free(backend->timeline);
|
||||
free(backend);
|
||||
|
|
@ -487,12 +523,19 @@ xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *
|
|||
{
|
||||
xtc_audio_plan *candidate = NULL;
|
||||
xtc_midi_plan *midi = NULL;
|
||||
float *monitor = NULL;
|
||||
size_t tracks = 0, clips = 0, i;
|
||||
xtc_result result;
|
||||
if (!backend || !project)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!backend->ready || backend->running)
|
||||
return XTC_ERR_INVALID_STATE;
|
||||
result = xtc_audio_plan_create(project, backend->expected_rate, &candidate);
|
||||
result = xtc_project_get_counts(project, &tracks, &clips);
|
||||
if (result == XTC_OK && tracks) {
|
||||
monitor = calloc(tracks*2*backend->max_frames, sizeof(float));
|
||||
if (!monitor) result = XTC_ERR_OUT_OF_MEMORY;
|
||||
}
|
||||
if (result == XTC_OK) result = xtc_audio_plan_create(project, backend->expected_rate, &candidate);
|
||||
if (result == XTC_OK)
|
||||
result = xtc_audio_plan_set_fade_frames(candidate, backend->transition_frames);
|
||||
if (result == XTC_OK)
|
||||
|
|
@ -504,8 +547,21 @@ xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *
|
|||
if (result != XTC_OK) {
|
||||
xtc_audio_plan_destroy(candidate);
|
||||
xtc_midi_plan_destroy(midi);
|
||||
free(monitor);
|
||||
return result;
|
||||
}
|
||||
free(backend->monitor);
|
||||
backend->monitor = monitor;
|
||||
backend->meter_count = tracks;
|
||||
memset(backend->meter_peaks, 0, sizeof(backend->meter_peaks));
|
||||
memset(backend->master_peaks, 0, sizeof(backend->master_peaks));
|
||||
for (i = 0; i < tracks; ++i) {
|
||||
xtc_track_info track;
|
||||
(void)xtc_project_get_track(project, i, &track);
|
||||
backend->meter_ids[i] = track.id;
|
||||
}
|
||||
xtc_audio_plan_monitor(candidate, monitor, backend->max_frames);
|
||||
xtc_midi_plan_monitor(midi, monitor, backend->max_frames);
|
||||
xtc_audio_plan_destroy(backend->audio);
|
||||
backend->audio = candidate;
|
||||
xtc_midi_plan_destroy(backend->midi);
|
||||
|
|
@ -773,6 +829,22 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
|
|||
return route(backend, direction, remote, 1);
|
||||
}
|
||||
|
||||
xtc_result xtc_backend_read_meters(xtc_backend *backend, xtc_meter_snapshot *out)
|
||||
{
|
||||
size_t i;
|
||||
if (!backend || !out) return XTC_ERR_INVALID_ARGUMENT;
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->count = backend->meter_count;
|
||||
for (i = 0; i < out->count; ++i) {
|
||||
out->tracks[i].id = backend->meter_ids[i];
|
||||
out->tracks[i].left = (float)__atomic_exchange_n(&backend->meter_peaks[i][0], 0U, __ATOMIC_RELAXED)/1000000;
|
||||
out->tracks[i].right = (float)__atomic_exchange_n(&backend->meter_peaks[i][1], 0U, __ATOMIC_RELAXED)/1000000;
|
||||
}
|
||||
out->master_left = (float)__atomic_exchange_n(backend->master_peaks, 0U, __ATOMIC_RELAXED)/1000000;
|
||||
out->master_right = (float)__atomic_exchange_n(backend->master_peaks+1, 0U, __ATOMIC_RELAXED)/1000000;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
#else
|
||||
/**
|
||||
* @brief Reports that output name is unavailable without JACK support.
|
||||
|
|
@ -1038,4 +1110,7 @@ xtc_result xtc_backend_request_stop(xtc_backend *backend)
|
|||
return XTC_ERR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
|
||||
xtc_result xtc_backend_read_meters(xtc_backend *backend, xtc_meter_snapshot *out)
|
||||
{ (void)backend; (void)out; return XTC_ERR_UNSUPPORTED; }
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -521,8 +521,7 @@ void MainWindow::reconnect_engine()
|
|||
*/
|
||||
void MainWindow::refresh_status()
|
||||
{
|
||||
if (mixer_)
|
||||
mixer_->peaks(0, 0);
|
||||
if (mixer_ && !ready_) { mixer_->peaks(0, 0); mixer_->track_peaks(0, 0); }
|
||||
bool can_play = false, can_record = false;
|
||||
if (!ready_) {
|
||||
set_label(indicator_, "Audio: disconnected");
|
||||
|
|
@ -558,7 +557,15 @@ void MainWindow::refresh_status()
|
|||
indicator_->labelcolor(fault ? fl_rgb_color(255, 155, 120)
|
||||
: running_ ? fl_rgb_color(125, 225, 160)
|
||||
: fl_rgb_color(215, 215, 215));
|
||||
mixer_->peaks(status.peak_left, status.peak_right);
|
||||
xtc_meter_snapshot meters{};
|
||||
float track_left = 0, track_right = 0;
|
||||
if (xtc_backend_read_meters(backend_, &meters) == XTC_OK) {
|
||||
mixer_->peaks(meters.master_left, meters.master_right);
|
||||
for (size_t i = 0; i < meters.count; ++i) if (meters.tracks[i].id == mixer_->selected_track()) {
|
||||
track_left = meters.tracks[i].left; track_right = meters.tracks[i].right; break;
|
||||
}
|
||||
} else mixer_->peaks(status.peak_left, status.peak_right);
|
||||
mixer_->track_peaks(track_left, track_right);
|
||||
char text[256];
|
||||
std::snprintf(
|
||||
text, sizeof(text),
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Check_Button.H>
|
||||
#include <FL/Fl_Button.H>
|
||||
#include <FL/Fl_Choice.H>
|
||||
#include <FL/Fl_Progress.H>
|
||||
#include <FL/Fl_Value_Input.H>
|
||||
|
|
@ -193,26 +194,39 @@ MixerWidget::MixerWidget(int x, int y, int w, int h, xtc_project *p, BeforeEdit
|
|||
solo_ = new Fl_Check_Button(x + 440, y + 8, 90, 26, "Solo");
|
||||
mute_->callback(control, this);
|
||||
solo_->callback(control, this);
|
||||
routes_ = new Fl_Choice(x + 600, y + 8, 180, 26, "Route");
|
||||
routes_->callback([](Fl_Widget *, void *c) {
|
||||
auto *m = static_cast<MixerWidget *>(c);
|
||||
int index = m->routes_->value();
|
||||
if (index >= 0 && static_cast<size_t>(index) < m->route_ids_.size()) m->route(m->route_ids_[static_cast<size_t>(index)]);
|
||||
}, this);
|
||||
auto *bus = new Fl_Button(x + 820, y + 8, 110, 26, "Add bus");
|
||||
bus->callback([](Fl_Widget *, void *c) { static_cast<MixerWidget *>(c)->add_bus(); }, this);
|
||||
left_ = new Fl_Progress(x + 70, y + 90, 320, 20, "Output L");
|
||||
right_ = new Fl_Progress(x + 480, y + 90, 320, 20, "Output R");
|
||||
for (auto *meter : {left_, right_}) {
|
||||
meter->minimum(0);
|
||||
meter->maximum(1);
|
||||
track_left_ = new Fl_Progress(x + 70, y + 116, 320, 20, "Track L");
|
||||
track_right_ = new Fl_Progress(x + 480, y + 116, 320, 20, "Track R");
|
||||
for (auto *meter : {left_, right_, track_left_, track_right_}) {
|
||||
meter->minimum(-90);
|
||||
meter->maximum(6);
|
||||
meter->selection_color(fl_rgb_color(80, 180, 120));
|
||||
}
|
||||
parameter_ = new Fl_Choice(x + 110, y + 126, 150, 26, "Automation");
|
||||
auto *reset = new Fl_Button(x + 820, y + 90, 110, 46, "Reset peaks");
|
||||
reset->callback([](Fl_Widget *, void *c) { static_cast<MixerWidget *>(c)->reset_meters(); }, this);
|
||||
reset_meters();
|
||||
parameter_ = new Fl_Choice(x + 110, y + 146, 150, 26, "Automation");
|
||||
parameter_->add("Gain|Pan");
|
||||
parameter_->value(0);
|
||||
parameter_->callback(select, this);
|
||||
start_ = new Fl_Value_Input(x + 420, y + 126, 140, 26, "First tick");
|
||||
start_ = new Fl_Value_Input(x + 420, y + 146, 140, 26, "First tick");
|
||||
start_->bounds(0, XTC_PROJECT_MAX_TICK - lane_ticks);
|
||||
start_->step(3840);
|
||||
start_->callback(select, this);
|
||||
auto *hint = new Fl_Box(x + 10, y + 160, w - 20, 24,
|
||||
auto *hint = new Fl_Box(x + 10, y + 180, w - 20, 24,
|
||||
"Gain 0..4; pan -1 left / +1 right. M/S toggles; arrows change gain, "
|
||||
"Shift+arrows pan; Up/Down selects track.");
|
||||
hint->labelsize(12);
|
||||
lane_ = new AutomationLane(x + 10, y + 192, w - 20, h - 222, *this);
|
||||
lane_ = new AutomationLane(x + 10, y + 212, w - 20, h - 242, *this);
|
||||
end();
|
||||
set_project(p);
|
||||
}
|
||||
|
|
@ -267,6 +281,8 @@ void MixerWidget::set_project(xtc_project *p)
|
|||
uint64_t selected = project_ == p ? selected_track() : UINT64_MAX;
|
||||
project_ = p;
|
||||
tracks_->clear();
|
||||
routes_->clear(); routes_->add("Master");
|
||||
route_ids_.clear(); route_ids_.push_back(UINT64_MAX);
|
||||
size_t n = 0, c = 0;
|
||||
xtc_project_get_counts(p, &n, &c);
|
||||
int index = 0;
|
||||
|
|
@ -279,7 +295,8 @@ void MixerWidget::set_project(xtc_project *p)
|
|||
label += '\\';
|
||||
label += ch;
|
||||
}
|
||||
tracks_->add(label.c_str());
|
||||
tracks_->add((std::string(t.is_bus ? "Bus: " : "Track: ") + label).c_str());
|
||||
if (t.is_bus) { routes_->add(label.c_str()); route_ids_.push_back(t.id); }
|
||||
if (t.id == selected)
|
||||
index = static_cast<int>(i);
|
||||
}
|
||||
|
|
@ -311,6 +328,13 @@ void MixerWidget::selection()
|
|||
pan_->value(t.pan);
|
||||
mute_->value(t.mute);
|
||||
solo_->value(t.solo);
|
||||
if (valid && !t.is_bus) routes_->activate(); else routes_->deactivate();
|
||||
routes_->value(0);
|
||||
for (size_t i = 0; i < route_ids_.size(); ++i) if (route_ids_[i] == t.bus_id) routes_->value(static_cast<int>(i));
|
||||
if (meter_track_ != selected_track()) {
|
||||
meter_track_ = selected_track();
|
||||
xtc_peak_meter_reset(track_meter_); xtc_peak_meter_reset(track_meter_+1);
|
||||
}
|
||||
lane_->redraw();
|
||||
}
|
||||
|
||||
|
|
@ -325,10 +349,12 @@ void MixerWidget::selection()
|
|||
*/
|
||||
void MixerWidget::peaks(float left, float right)
|
||||
{
|
||||
left_->value(std::min(left, 1.0f));
|
||||
right_->value(std::min(right, 1.0f));
|
||||
left_->selection_color(left > 1 ? FL_RED : FL_GREEN);
|
||||
right_->selection_color(right > 1 ? FL_RED : FL_GREEN);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
meter_elapsed_ = std::clamp(std::chrono::duration<double>(now-peak_time_).count(), 0.0, 60.0);
|
||||
peak_time_ = now;
|
||||
(void)xtc_peak_meter_update(master_, left, meter_elapsed_);
|
||||
(void)xtc_peak_meter_update(master_+1, right, meter_elapsed_);
|
||||
display_peaks(left_, right_, master_, "Master");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -456,3 +482,53 @@ int MixerWidget::handle(int e)
|
|||
}
|
||||
return Fl_Group::handle(e);
|
||||
}
|
||||
|
||||
void MixerWidget::display_peaks(Fl_Progress *left, Fl_Progress *right, xtc_peak_meter *meters, const char *prefix)
|
||||
{
|
||||
for (unsigned int side = 0; side < 2; ++side) {
|
||||
auto *bar = side ? right : left;
|
||||
char label[80];
|
||||
std::snprintf(label, sizeof(label), "%s %c %.1f dBFS%s", prefix, side ? 'R' : 'L',
|
||||
static_cast<double>(meters[side].db), meters[side].clipped ? " CLIP" : "");
|
||||
bar->copy_label(label);
|
||||
bar->value(std::clamp(meters[side].db, -90.0f, 6.0f));
|
||||
bar->selection_color(meters[side].clipped ? FL_RED : FL_GREEN);
|
||||
}
|
||||
}
|
||||
void MixerWidget::track_peaks(float left, float right)
|
||||
{
|
||||
(void)xtc_peak_meter_update(track_meter_, left, meter_elapsed_);
|
||||
(void)xtc_peak_meter_update(track_meter_+1, right, meter_elapsed_);
|
||||
xtc_track_info track{};
|
||||
bool bus = xtc_project_get_track(project_, static_cast<size_t>(tracks_->value()), &track) == XTC_OK && track.is_bus;
|
||||
display_peaks(track_left_, track_right_, track_meter_, bus ? "Bus" : "Track");
|
||||
}
|
||||
void MixerWidget::reset_meters()
|
||||
{
|
||||
for (unsigned int i = 0; i < 2; ++i) { xtc_peak_meter_reset(master_+i); xtc_peak_meter_reset(track_meter_+i); }
|
||||
display_peaks(left_, right_, master_, "Master");
|
||||
display_peaks(track_left_, track_right_, track_meter_, "Track");
|
||||
}
|
||||
xtc_result MixerWidget::add_bus()
|
||||
{
|
||||
uint64_t id;
|
||||
xtc_result r = before_ ? before_(context_) : XTC_OK;
|
||||
if (r == XTC_OK) r = xtc_project_add_bus(project_, "Bus", &id);
|
||||
if (r == XTC_OK) { changed(r); select_track(id); return r; }
|
||||
return changed(r);
|
||||
}
|
||||
xtc_result MixerWidget::route(uint64_t bus)
|
||||
{
|
||||
xtc_result r = before_ ? before_(context_) : XTC_OK;
|
||||
if (r == XTC_OK) r = xtc_project_route_track(project_, selected_track(), bus);
|
||||
return changed(r);
|
||||
}
|
||||
bool MixerWidget::select_track(uint64_t id)
|
||||
{
|
||||
size_t n = 0, clips = 0; xtc_project_get_counts(project_, &n, &clips);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
xtc_track_info track{}; xtc_project_get_track(project_, i, &track);
|
||||
if (track.id == id) { tracks_->value(static_cast<int>(i)); selection(); return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
#define XTC_MIXER_WIDGET_HPP
|
||||
#include "xtc/xtc_mixer.h"
|
||||
#include <FL/Fl_Group.H>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
class Fl_Choice;
|
||||
class Fl_Value_Slider;
|
||||
|
|
@ -118,6 +120,12 @@ class MixerWidget : public Fl_Group {
|
|||
* @param solo Solo state: 0 or 1.
|
||||
* @return XTC_OK on success, or the before-edit or project mutation error.
|
||||
*/
|
||||
xtc_result add_bus();
|
||||
xtc_result route(uint64_t bus);
|
||||
bool select_track(uint64_t id);
|
||||
void track_peaks(float left, float right);
|
||||
void reset_meters();
|
||||
bool master_clipped() const { return master_[0].clipped || master_[1].clipped; }
|
||||
xtc_result mix(float gain, float pan, int mute, int solo);
|
||||
|
||||
/**
|
||||
|
|
@ -135,10 +143,17 @@ class MixerWidget : public Fl_Group {
|
|||
xtc_project *project_;
|
||||
BeforeEdit before_;
|
||||
void *context_;
|
||||
Fl_Choice *tracks_, *parameter_;
|
||||
Fl_Choice *tracks_, *parameter_, *routes_;
|
||||
std::vector<uint64_t> route_ids_;
|
||||
Fl_Value_Slider *gain_, *pan_;
|
||||
Fl_Check_Button *mute_, *solo_;
|
||||
Fl_Progress *left_, *right_;
|
||||
Fl_Progress *left_, *right_, *track_left_, *track_right_;
|
||||
xtc_peak_meter master_[2]{}, track_meter_[2]{};
|
||||
uint64_t meter_track_ = UINT64_MAX;
|
||||
std::chrono::steady_clock::time_point peak_time_ = std::chrono::steady_clock::now();
|
||||
double meter_elapsed_ = 0.1;
|
||||
void display_peaks(Fl_Progress *left, Fl_Progress *right, xtc_peak_meter *meters, const char *prefix);
|
||||
|
||||
Fl_Value_Input *start_;
|
||||
AutomationLane *lane_;
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -25,7 +25,9 @@ typedef struct {
|
|||
} voice;
|
||||
|
||||
struct xtc_midi_plan {
|
||||
size_t fade_frames, controller_frames;
|
||||
size_t fade_frames, controller_frames, monitor_stride;
|
||||
float *monitor;
|
||||
uint64_t monitor_position;
|
||||
xtc_gain_ramp volume[16], pan[16];
|
||||
unsigned int sustain[16];
|
||||
xtc_mix_snapshot mix;
|
||||
|
|
@ -334,6 +336,8 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
|
|||
if (distance < p->fade_frames)
|
||||
sample *= (float)distance / (float)p->fade_frames;
|
||||
}
|
||||
if (p->monitor) xtc_mix_monitor(&p->mix, p->monitor, p->monitor_stride, s->track, position+j,
|
||||
(size_t)(position-p->monitor_position)+j, sample, right != NULL);
|
||||
out[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, right ? 0U : 2U);
|
||||
if (right)
|
||||
right[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, 1);
|
||||
|
|
@ -393,6 +397,7 @@ xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position, cons
|
|||
(e->type == XTC_MIDI_NOTE_ON && !e->velocity))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
p->monitor_position = position;
|
||||
if (!frames)
|
||||
return XTC_OK;
|
||||
if (p->next_position != position)
|
||||
|
|
@ -431,3 +436,6 @@ xtc_result xtc_midi_plan_render(xtc_midi_plan *p, uint64_t position, const xtc_m
|
|||
{
|
||||
return xtc_midi_plan_render_stereo(p, position, events, count, out, NULL, frames);
|
||||
}
|
||||
|
||||
void xtc_midi_plan_monitor(xtc_midi_plan *p, float *samples, size_t stride)
|
||||
{ if (p) { p->monitor = samples; p->monitor_stride = stride; } }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define XTC_MIX_INTERNAL_H
|
||||
|
||||
#include "../project/project_internal.h"
|
||||
#include "xtc/xtc_audio.h"
|
||||
|
||||
typedef struct {
|
||||
uint64_t sample, tick;
|
||||
|
|
@ -15,6 +16,7 @@ typedef struct {
|
|||
size_t count, offsets[XTC_PROJECT_MAX_TRACKS][2], lengths[XTC_PROJECT_MAX_TRACKS][2];
|
||||
xtc_mix_point points[XTC_MAX_AUTOMATION_POINTS];
|
||||
int any_solo;
|
||||
size_t bus[XTC_PROJECT_MAX_TRACKS];
|
||||
} xtc_mix_snapshot;
|
||||
|
||||
/**
|
||||
|
|
@ -45,4 +47,11 @@ size_t xtc_mix_track(const xtc_mix_snapshot * m, uint64_t id);
|
|||
* @return Linear level for the chosen side, or zero for an absent or inaudible track.
|
||||
*/
|
||||
float xtc_mix_level(const xtc_mix_snapshot * m, size_t track, uint64_t sample, unsigned int side);
|
||||
float xtc_mix_leaf_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side);
|
||||
/* Borrowed control-installed scratch: 2*track_count channels, stride frames.
|
||||
* Clear once per callback before audio+MIDI rendering. No pointer changes active. */
|
||||
void xtc_audio_plan_monitor(xtc_audio_plan *plan, float *samples, size_t stride);
|
||||
void xtc_midi_plan_monitor(xtc_midi_plan *plan, float *samples, size_t stride);
|
||||
void xtc_mix_monitor(const xtc_mix_snapshot *mix, float *samples, size_t stride, size_t track,
|
||||
uint64_t position, size_t frame, float sample, int stereo);
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -129,3 +129,38 @@ xtc_result xtc_project_get_automation(const xtc_project *p, size_t i, xtc_automa
|
|||
*a = p->automation[i];
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
xtc_result xtc_project_add_bus(xtc_project *p, const char *name, uint64_t *out)
|
||||
{
|
||||
xtc_result r = xtc_project_add_track(p, name, out);
|
||||
if (r == XTC_OK) p->tracks[p->track_count-1].is_bus = 1;
|
||||
return r;
|
||||
}
|
||||
xtc_result xtc_project_route_track(xtc_project *p, uint64_t id, uint64_t bus)
|
||||
{
|
||||
size_t i, destination;
|
||||
if (!p) return XTC_ERR_INVALID_ARGUMENT;
|
||||
i = find_track(p, id);
|
||||
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
|
||||
if (p->tracks[i].is_bus) return XTC_ERR_INVALID_STATE;
|
||||
destination = find_track(p, bus);
|
||||
if (bus != UINT64_MAX && (destination == SIZE_MAX || !p->tracks[destination].is_bus)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
p->tracks[i].bus_id = bus;
|
||||
return XTC_OK;
|
||||
}
|
||||
void xtc_peak_meter_reset(xtc_peak_meter *m)
|
||||
{
|
||||
if (m) { m->db = -90; m->clipped = 0; }
|
||||
}
|
||||
xtc_result xtc_peak_meter_update(xtc_peak_meter *m, float peak, double seconds)
|
||||
{
|
||||
float db, released;
|
||||
if (!m || !isfinite(peak) || peak < 0 || !isfinite(seconds) || seconds < 0 || seconds > 60)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
db = peak > 0 ? 20.0f * log10f(peak) : -90;
|
||||
if (db < -90) db = -90;
|
||||
released = m->db - (float)(20 * seconds);
|
||||
m->db = db > released ? db : released;
|
||||
if (peak >= 1) m->clipped = 1;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,9 +50,10 @@ xtc_result xtc_mix_prepare(const xtc_project *p, unsigned int rate, xtc_mix_snap
|
|||
memset(m, 0, sizeof(*m));
|
||||
m->count = p->track_count;
|
||||
memcpy(m->tracks, p->tracks, p->track_count * sizeof(*m->tracks));
|
||||
for (i = 0; i < m->count; ++i)
|
||||
if (m->tracks[i].solo)
|
||||
m->any_solo = 1;
|
||||
for (i = 0; i < m->count; ++i) {
|
||||
if (m->tracks[i].solo) m->any_solo = 1;
|
||||
m->bus[i] = xtc_mix_track(m, m->tracks[i].bus_id);
|
||||
}
|
||||
for (i = 0; i < p->automation_count; ++i) {
|
||||
const xtc_automation_point *a = p->automation + i;
|
||||
xtc_mix_point *b = m->points + i;
|
||||
|
|
@ -117,12 +118,13 @@ static float value(const xtc_mix_snapshot *m, size_t track, unsigned int param,
|
|||
* @param side Output side: 0 for left, 1 for right, 2 for mono gain only.
|
||||
* @return Linear level for the chosen side, or zero for an absent or inaudible track.
|
||||
*/
|
||||
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
|
||||
float xtc_mix_leaf_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
|
||||
{
|
||||
float gain, pan;
|
||||
if (track >= m->count)
|
||||
return 0;
|
||||
if (m->tracks[track].mute || (m->any_solo && !m->tracks[track].solo))
|
||||
if (m->tracks[track].mute || (m->any_solo && !m->tracks[track].solo &&
|
||||
!(m->bus[track] < m->count && m->tracks[m->bus[track]].solo)))
|
||||
return 0;
|
||||
gain = value(m, track, 0, sample);
|
||||
if (side == 2)
|
||||
|
|
@ -130,3 +132,31 @@ float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, un
|
|||
pan = value(m, track, 1, sample);
|
||||
return gain * (side == 0 ? (pan > 0 ? 1 - pan : 1) : (pan < 0 ? 1 + pan : 1));
|
||||
}
|
||||
|
||||
float xtc_mix_level(const xtc_mix_snapshot *m, size_t track, uint64_t sample, unsigned int side)
|
||||
{
|
||||
float level = xtc_mix_leaf_level(m, track, sample, side);
|
||||
size_t bus;
|
||||
if (track >= m->count) return 0;
|
||||
bus = m->bus[track];
|
||||
if (bus < m->count) {
|
||||
float gain, pan;
|
||||
if (m->tracks[bus].mute) return 0;
|
||||
gain = value(m, bus, 0, sample);
|
||||
pan = side == 2 ? 0 : value(m, bus, 1, sample);
|
||||
level *= gain * (side == 0 ? (pan > 0 ? 1-pan : 1) : side == 1 ? (pan < 0 ? 1+pan : 1) : 1);
|
||||
}
|
||||
return level;
|
||||
}
|
||||
void xtc_mix_monitor(const xtc_mix_snapshot *m, float *samples, size_t stride, size_t track,
|
||||
uint64_t position, size_t frame, float sample, int stereo)
|
||||
{
|
||||
unsigned int side;
|
||||
if (!samples || track >= m->count || frame >= stride) return;
|
||||
for (side = 0; side < 2; ++side) {
|
||||
unsigned int channel = stereo ? side : 2;
|
||||
samples[(track*2+side)*stride+frame] += sample * xtc_mix_leaf_level(m, track, position, channel);
|
||||
if (m->bus[track] < m->count)
|
||||
samples[(m->bus[track]*2+side)*stride+frame] += sample * xtc_mix_level(m, track, position, channel);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -311,6 +311,14 @@ static xtc_result read_project(reader *input, xtc_project *project, const char *
|
|||
return XTC_ERR_FORMAT;
|
||||
project->tracks[i].solo = (int)value;
|
||||
}
|
||||
project->tracks[i].bus_id = UINT64_MAX;
|
||||
if (format_version >= 9U) {
|
||||
READ("ROUTE", 3U);
|
||||
NUM(1U, value);
|
||||
if (value > 1) return XTC_ERR_FORMAT;
|
||||
project->tracks[i].is_bus = (int)value;
|
||||
NUM(2U, project->tracks[i].bus_id);
|
||||
}
|
||||
}
|
||||
READ("CLIPS", 2U);
|
||||
NUM(1U, value);
|
||||
|
|
@ -496,7 +504,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project, const ch
|
|||
memcpy(&gain, &t->gain, sizeof(gain));
|
||||
memcpy(&pan, &t->pan, sizeof(pan));
|
||||
if (fprintf(file, "TRACK %" PRIu64 " ", t->id) < 0 || !write_name(file, t->name) ||
|
||||
fprintf(file, "MIX %08" PRIx32 " %08" PRIx32 " %d %d\n", gain, pan, t->mute, t->solo) < 0)
|
||||
fprintf(file, "MIX %08" PRIx32 " %08" PRIx32 " %d %d\nROUTE %d %" PRIu64 "\n", gain, pan, t->mute, t->solo, t->is_bus, t->bus_id) < 0)
|
||||
return XTC_ERR_IO;
|
||||
}
|
||||
if (fprintf(file, "CLIPS %zu\n", project->clip_count) < 0)
|
||||
|
|
|
|||
|
|
@ -296,6 +296,7 @@ xtc_result xtc_project_add_track(xtc_project *project, const char *name, uint64_
|
|||
return XTC_ERR_CAPACITY;
|
||||
track.id = project->next_track_id++;
|
||||
track.gain = 1.0f;
|
||||
track.bus_id = UINT64_MAX;
|
||||
memcpy(track.name, name, strlen(name) + 1U);
|
||||
project->tracks[project->track_count++] = track;
|
||||
*id = track.id;
|
||||
|
|
@ -321,6 +322,8 @@ xtc_result xtc_project_remove_track(xtc_project *project, uint64_t id)
|
|||
if (project->clips[i].track_id != id)
|
||||
project->clips[count++] = project->clips[i];
|
||||
project->clip_count = count;
|
||||
for (i = 0; i < project->track_count; ++i)
|
||||
if (project->tracks[i].bus_id == id) project->tracks[i].bus_id = UINT64_MAX;
|
||||
memmove(project->tracks + index, project->tracks + index + 1U,
|
||||
(project->track_count - index - 1U) * sizeof(*project->tracks));
|
||||
--project->track_count;
|
||||
|
|
@ -356,6 +359,7 @@ xtc_result xtc_project_add_clip(xtc_project *project, uint64_t track, const char
|
|||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (track_index(project, track) == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
if (project->tracks[track_index(project, track)].is_bus) return XTC_ERR_INVALID_STATE;
|
||||
if (project->clip_count == XTC_PROJECT_MAX_CLIPS || project->next_clip_id == UINT64_MAX)
|
||||
return XTC_ERR_CAPACITY;
|
||||
clip.gain = 1.0f;
|
||||
|
|
@ -417,6 +421,7 @@ xtc_result xtc_project_edit_clip(xtc_project *project, uint64_t id, uint64_t tra
|
|||
index = clip_index(project, id);
|
||||
if (index == SIZE_MAX || track_index(project, track) == SIZE_MAX)
|
||||
return XTC_ERR_NOT_FOUND;
|
||||
if (project->tracks[track_index(project, track)].is_bus) return XTC_ERR_INVALID_STATE;
|
||||
if (!valid_range(start, length, offset, project->clips[index].source_length_tick))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
project->clips[index].track_id = track;
|
||||
|
|
@ -514,6 +519,13 @@ xtc_result xtc_project_validate(const xtc_project *project)
|
|||
for (i = 0U; i < project->track_count; ++i) {
|
||||
if (project->tracks[i].id >= project->next_track_id || !xtc_project_valid_name(project->tracks[i].name))
|
||||
return XTC_ERR_FORMAT;
|
||||
{
|
||||
const xtc_track_info *t = project->tracks+i;
|
||||
size_t bus = track_index(project, t->bus_id);
|
||||
if ((t->is_bus != 0 && t->is_bus != 1) ||
|
||||
(t->bus_id != UINT64_MAX && (t->is_bus || bus == SIZE_MAX || !project->tracks[bus].is_bus)))
|
||||
return XTC_ERR_FORMAT;
|
||||
}
|
||||
for (j = 0U; j < i; ++j)
|
||||
if (project->tracks[j].id == project->tracks[i].id)
|
||||
return XTC_ERR_FORMAT;
|
||||
|
|
@ -538,6 +550,7 @@ xtc_result xtc_project_validate(const xtc_project *project)
|
|||
!xtc_project_valid_name(clip->name) ||
|
||||
!valid_range(clip->start_tick, clip->length_tick, clip->offset_tick, clip->source_length_tick))
|
||||
return XTC_ERR_FORMAT;
|
||||
if (project->tracks[track_index(project, clip->track_id)].is_bus) return XTC_ERR_FORMAT;
|
||||
for (j = 0U; j < i; ++j)
|
||||
if (project->clips[j].id == clip->id)
|
||||
return XTC_ERR_FORMAT;
|
||||
|
|
@ -613,6 +626,7 @@ xtc_result xtc_project_duplicate_clip(xtc_project *p, uint64_t id, uint64_t trac
|
|||
if (!p || !out) return XTC_ERR_INVALID_ARGUMENT;
|
||||
index = clip_index(p,id);
|
||||
if (index == SIZE_MAX || track_index(p,track) == SIZE_MAX) return XTC_ERR_NOT_FOUND;
|
||||
if (p->tracks[track_index(p,track)].is_bus) return XTC_ERR_INVALID_STATE;
|
||||
c = p->clips[index];
|
||||
if (!valid_range(start,c.length_tick,c.offset_tick,c.source_length_tick)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i = 0; i < p->note_count; ++i) if (p->notes[i].clip_id == id) ++count;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue