79 lines
2.4 KiB
C
79 lines
2.4 KiB
C
#include <jack/jack.h>
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
static jack_port_t *output_port;
|
|
static jack_client_t *client;
|
|
static volatile int running = 1;
|
|
static double phase = 0.0;
|
|
static double freq = 440.0;
|
|
static int sample_rate = 48000;
|
|
static int total_samples = 0;
|
|
static int samples_written = 0;
|
|
|
|
int process(jack_nframes_t nframes, void *arg) {
|
|
jack_default_audio_sample_t *out =
|
|
(jack_default_audio_sample_t *)jack_port_get_buffer(output_port, nframes);
|
|
if (!out) return 0;
|
|
for (jack_nframes_t i = 0; i < nframes; i++) {
|
|
out[i] = sin(2 * M_PI * phase);
|
|
phase += freq / sample_rate;
|
|
if (phase >= 1.0) phase -= 1.0;
|
|
samples_written++;
|
|
if (total_samples > 0 && samples_written >= total_samples) {
|
|
running = 0;
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void shutdown(void *arg) { running = 0; }
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc < 3) {
|
|
fprintf(stderr, "Usage: gen_tone <duration_seconds> <target_port> [frequency]\n");
|
|
return 1;
|
|
}
|
|
double duration = atof(argv[1]);
|
|
const char *target = argv[2];
|
|
if (argc >= 4) freq = atof(argv[3]);
|
|
|
|
jack_status_t status;
|
|
client = jack_client_open("gen_tone", JackNoStartServer, &status);
|
|
if (!client) { fprintf(stderr, "Cannot open JACK client\n"); return 1; }
|
|
|
|
sample_rate = jack_get_sample_rate(client);
|
|
total_samples = (int)(duration * sample_rate + 0.5);
|
|
|
|
output_port = jack_port_register(client, "output",
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
JackPortIsOutput, 0);
|
|
if (!output_port) { fprintf(stderr, "Cannot register port\n"); return 1; }
|
|
|
|
jack_set_process_callback(client, process, NULL);
|
|
jack_on_shutdown(client, shutdown, NULL);
|
|
if (jack_activate(client)) { fprintf(stderr, "Cannot activate client\n"); return 1; }
|
|
|
|
// Connect to target
|
|
const char **ports = jack_get_ports(client, target,
|
|
JACK_DEFAULT_AUDIO_TYPE,
|
|
JackPortIsInput);
|
|
if (!ports || !ports[0]) {
|
|
fprintf(stderr, "Target port '%s' not found\n", target);
|
|
return 1;
|
|
}
|
|
if (jack_connect(client, jack_port_name(output_port), ports[0])) {
|
|
fprintf(stderr, "Cannot connect port\n");
|
|
return 1;
|
|
}
|
|
|
|
while (running) sleep(1);
|
|
|
|
jack_client_close(client);
|
|
return 0;
|
|
}
|