Skip to content

Commit

Permalink
Initial code for monitor_audio_levels command
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Oct 29, 2024
1 parent d157ce0 commit 1117c8e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/effects.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ typedef struct HMI_ADDRESSING_T hmi_addressing_t;
#endif
typedef struct PORT_T port_t;

typedef struct AUDIO_MONITOR_T {
jack_port_t *port;
char *source_port_name;
float value;
bool updated;
} audio_monitor_t;

typedef struct CV_SOURCE_T {
port_t *port;
jack_port_t *jack_port;
Expand Down Expand Up @@ -725,6 +732,9 @@ static jack_port_t *g_audio_in2_port;
static jack_port_t *g_audio_out1_port;
static jack_port_t *g_audio_out2_port;
#endif
static audio_monitor_t *g_audio_monitors;
static pthread_mutex_t g_audio_monitor_mutex;
static int g_audio_monitor_count;
static jack_port_t *g_midi_in_port;
static jack_position_t g_jack_pos;
static bool g_jack_rolling;
Expand Down Expand Up @@ -2790,6 +2800,36 @@ static int ProcessGlobalClient(jack_nframes_t nframes, void *arg)
}
#endif

// Handle audio monitors
if (pthread_mutex_trylock(&g_audio_monitor_mutex) == 0)
{
float *monitorbuf;
float absvalue, oldvalue;

for (int i = 0; i < g_audio_monitor_count; ++i)
{
monitorbuf = (float*)jack_port_get_buffer(g_audio_monitors[i].port, nframes);
oldvalue = value = g_audio_monitors[i].value;

for (jack_nframes_t i = 0 ; i < nframes; i++)
{
absvalue = fabsf(monitorbuf[i]);

if (absvalue > value)
value = absvalue;
}

if (oldvalue < value)
{
g_audio_monitors[i].value = value;
g_audio_monitors[i].updated = true;
needs_post = true;
}
}

pthread_mutex_unlock(&g_audio_monitor_mutex);
}

if (UpdateGlobalJackPosition(pos_flag, false))
needs_post = true;

Expand Down Expand Up @@ -3958,6 +3998,7 @@ int effects_init(void* client)

pthread_mutex_init(&g_rtsafe_mutex, &mutex_atts);
pthread_mutex_init(&g_raw_midi_port_mutex, &mutex_atts);
pthread_mutex_init(&g_audio_monitor_mutex, &mutex_atts);
pthread_mutex_init(&g_midi_learning_mutex, &mutex_atts);
#ifdef __MOD_DEVICES__
pthread_mutex_init(&g_hmi_mutex, &mutex_atts);
Expand Down Expand Up @@ -4533,6 +4574,7 @@ int effects_finish(int close_client)
sem_destroy(&g_postevents_semaphore);
pthread_mutex_destroy(&g_rtsafe_mutex);
pthread_mutex_destroy(&g_raw_midi_port_mutex);
pthread_mutex_destroy(&g_audio_monitor_mutex);
pthread_mutex_destroy(&g_midi_learning_mutex);
#ifdef __MOD_DEVICES__
pthread_mutex_destroy(&g_hmi_mutex);
Expand Down Expand Up @@ -8255,6 +8297,71 @@ int effects_processing_enable(int enable)
return SUCCESS;
}

int effects_monitor_audio_levels(const char *source_port_name, int enable)
{
if (g_jack_global_client == NULL)
return ERR_INVALID_OPERATION;

if (enable)
{
pthread_mutex_lock(&g_audio_monitor_mutex);
g_audio_monitors = realloc(g_audio_monitors, sizeof(audio_monitor_t) * (g_audio_monitor_count + 1));
pthread_mutex_lock(&g_audio_monitor_mutex);

if (g_audio_monitors == NULL)
return ERR_MEMORY_ALLOCATION;

audio_monitor_t *monitor = &g_audio_monitors[g_audio_monitor_count];

char port_name[0xff];
snprintf(port_name, sizeof(port_name) - 1, "monitor_%d", g_audio_monitor_count + 1);

jack_port_t *port = jack_port_register(g_jack_global_client,
port_name,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput,
0);
if (port == NULL)
return ERR_JACK_PORT_REGISTER;

snprintf(port_name, sizeof(port_name) - 1, "%s:monitor_%d",
jack_get_client_name(g_jack_global_client), g_audio_monitor_count + 1);
jack_connect(g_jack_global_client, source_port_name, port_name);

monitor->port = port;
monitor->source_port_name = strdup(source_port_name);
monitor->value = 0.f;
monitor->updated = false;

++g_audio_monitor_count;
}
else
{
if (g_audio_monitor_count == 0)
return ERR_INVALID_OPERATION;

audio_monitor_t *monitor = &g_audio_monitors[g_audio_monitor_count - 1];

if (strcmp(monitor->source_port_name, source_port_name))
return ERR_INVALID_OPERATION;

pthread_mutex_lock(&g_audio_monitor_mutex);
--g_audio_monitor_count;
pthread_mutex_unlock(&g_audio_monitor_mutex);

jack_port_unregister(g_jack_global_client, monitor->port);
free(monitor->source_port_name);

if (g_audio_monitor_count == 0)
{
free(g_audio_monitors);
g_audio_monitors = NULL;
}
}

return SUCCESS;
}

int effects_monitor_midi_program(int channel, int enable)
{
if (channel < 0 || channel > 15)
Expand Down
1 change: 1 addition & 0 deletions src/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ int effects_state_set_tmpdir(const char *dir);
int effects_aggregated_midi_enable(int enable);
int effects_freewheeling_enable(int enable);
int effects_processing_enable(int enable);
int effects_monitor_audio_levels(const char *source_port_name, int enable);
int effects_monitor_midi_program(int channel, int enable);
void effects_transport(int rolling, double beats_per_bar, double beats_per_minute);
int effects_transport_sync_mode(const char *mode);
Expand Down
9 changes: 9 additions & 0 deletions src/mod-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ static void monitor_output_cb(proto_t *proto)
protocol_response_int(resp, proto);
}

static void monitor_audio_levels_cb(proto_t *proto)
{
int resp;
resp = effects_monitor_audio_levels(proto->list[1], atoi(proto->list[2]));
protocol_response_int(resp, proto);
}

static void monitor_midi_program_cb(proto_t *proto)
{
int resp;
Expand Down Expand Up @@ -380,6 +387,7 @@ static void cc_map_cb(proto_t *proto)
}
else
{
scalepoints_count = 0;
scalepoints = NULL;
}

Expand Down Expand Up @@ -714,6 +722,7 @@ static int mod_host_init(jack_client_t* client, int socket_port, int feedback_po
protocol_add_command(EFFECT_SET_BPB, effects_set_beats_per_bar_cb);
protocol_add_command(MONITOR_ADDR_SET, monitor_addr_set_cb);
protocol_add_command(MONITOR_OUTPUT, monitor_output_cb);
protocol_add_command(MONITOR_AUDIO_LEVELS, monitor_audio_levels_cb);
protocol_add_command(MONITOR_MIDI_PROGRAM, monitor_midi_program_cb);
protocol_add_command(MIDI_LEARN, midi_learn_cb);
protocol_add_command(MIDI_MAP, midi_map_cb);
Expand Down
1 change: 1 addition & 0 deletions src/mod-host.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#define EFFECT_SET_BPB "set_bpb %f"
#define MONITOR_ADDR_SET "monitor %s %i %i"
#define MONITOR_OUTPUT "monitor_output %i %s"
#define MONITOR_AUDIO_LEVELS "monitor_audio_levels %i %s"
#define MONITOR_MIDI_PROGRAM "monitor_midi_program %i %i"
#define MIDI_LEARN "midi_learn %i %s %f %f"
#define MIDI_MAP "midi_map %i %s %i %i %f %f"
Expand Down

0 comments on commit 1117c8e

Please sign in to comment.