Skip to content

Instantly share code, notes, and snippets.

@J3698
Created January 26, 2019 23:58
Show Gist options
  • Save J3698/db8260c7b91e0528820e879394b0b5b5 to your computer and use it in GitHub Desktop.
Save J3698/db8260c7b91e0528820e879394b0b5b5 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include <getopt.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include <jack/jack.h>
#include <jack/transport.h>
typedef jack_default_audio_sample_t sample_t;
char *client_name;
jack_client_t *client;
jack_port_t *output_port_l;
jack_port_t *output_port_r;
jack_port_t *input_port_l;
jack_port_t *input_port_r;
int process(jack_nframes_t nframes, void *arg) {
sample_t *outBufferLeft = (sample_t *) jack_port_get_buffer(output_port_l, nframes);
sample_t *outBufferRight = (sample_t *) jack_port_get_buffer(output_port_r, nframes);
sample_t *inBufferLeft = (sample_t *) jack_port_get_buffer(input_port_l, nframes);
sample_t *inBufferRight = (sample_t *) jack_port_get_buffer(input_port_r, nframes);
memcpy(outBufferLeft, inBufferLeft, sizeof(sample_t) * nframes);
memcpy(outBufferRight, inBufferRight, sizeof(sample_t) * nframes);
double total = 0;
for (int i = 0; i < nframes; i++) {
total += pow(outBufferLeft[i], 2);
}
if (total / nframes > 0.025) {
printf("\n111111111111111111");
} else {
printf("\n000000000000000000");
}
return 0;
}
int sample_rate_change() {
printf("Sample rate has changed! Exiting...\n");
exit(-1);
}
bool startClient() {
jack_status_t status;
while ((client = jack_client_open(client_name, JackNoStartServer, &status)) == 0) {
fprintf(stderr, "jack server not running?\n");
//return false;
}
printf("Client started\n");
return true;
}
void createPorts() {
output_port_l = jack_port_register(client, "lag_out_l", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
output_port_r = jack_port_register(client, "lag_out_r", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
input_port_l = jack_port_register(client, "lag_in_l", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
input_port_r = jack_port_register(client, "lag_in_r", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
printf("Ports created\n");
}
void connectPorts() {
jack_connect(client, "PulseAudio JACK Sink:front-left", jack_port_name(input_port_l));
jack_connect(client, "PulseAudio JACK Sink:front-right", jack_port_name(input_port_r));
jack_connect(client, jack_port_name(output_port_l), "system:playback_1");
jack_connect(client, jack_port_name(output_port_r), "system:playback_2");
printf("Ports connected\n");
}
int main(int argc, char *argv[]) {
client_name = "Visualizer";
if (!startClient()) {
return -1;
}
createPorts();
jack_set_process_callback(client, process, 0);
if (jack_activate(client)) {
fprintf(stderr, "cannot activate client");
return 1;
}
printf("Jack activated\n");
connectPorts();
const char **port_names = jack_get_ports(client, NULL, NULL, 0);
for (int j = 0; port_names[j] != NULL; j++) {
printf("%s\n", port_names[j]);
}
while (true) {
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment