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
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue