You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

230 lines
9.4 KiB

/* SPDX-License-Identifier: MIT
* Copyright (c) 2026 Dan Chokola, KD2HSG
*
* cm_tx: build chirpmodem frames into a SigMF recording at an SDR's rate
* (the C counterpart of tx_file.py; tools/pluto_tx.py transmits the result).
* Uses only the public library API (chirpmodem.h).
*
* ./cm_tx hello --src KD2HSG --msg "test de KD2HSG" --sf 9 --repeat 3 --cw-id --fs 600e3
*
* Payload mode: non-coherent at SF >= 10 or <= 62.5 kHz, coherent otherwise
* (--coherent / --noncoherent override). Writes OUT.sigmf-data (cf32_le, or
* ci16_le with --format ci16) and OUT.sigmf-meta. The signal sits
* --if-offset above the SDR's LO, frames get raised-cosine edges, the
* licensed profile requires --src, and --cw-id appends "DE <src>" in Morse
* (20 WPM) at the signal's center.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <getopt.h>
#include "chirpmodem.h"
typedef struct {
FILE *f;
int ci16;
long n;
} out_t;
static void emit(out_t *o, const float *iq, long n)
{
if (!o->ci16) {
fwrite(iq, sizeof(float), (size_t)(2 * n), o->f);
} else {
for (long i = 0; i < 2 * n; i++) { /* ci16: 2^14 of 2^15, as tx_file.py */
double a = nearbyint(iq[i] * 0.5 * 32768);
int16_t v = (int16_t)(a > 32767 ? 32767 : a < -32768 ? -32768 : a);
fwrite(&v, sizeof v, 1, o->f);
}
}
o->n += n;
}
static void emit_zeros(out_t *o, long n)
{
static const float z[2048];
while (n > 0) {
long k = n < 1024 ? n : 1024;
emit(o, z, k);
n -= k;
}
}
int main(int argc, char **argv)
{
const char *src = NULL, *dst = NULL, *msg = "test", *fmt = "cf32";
double bw = 62.5e3, fs = 1e6, freq = 223.58e6, if_off = 100e3, gap = 0.5, lead = 0.2, amp = 0.7;
int sf = 9, repeat = 1, no_seq = 0, cw_id = 0, unlic = 0, coherent = CHIRPMODEM_MODE_AUTO;
static struct option opts[] = {
{"src", required_argument, 0, 's'}, {"dst", required_argument, 0, 'd'},
{"msg", required_argument, 0, 'm'}, {"unlicensed", no_argument, 0, 'u'},
{"sf", required_argument, 0, 'S'}, {"bw", required_argument, 0, 'b'},
{"fs", required_argument, 0, 'F'}, {"freq", required_argument, 0, 'f'},
{"if-offset", required_argument, 0, 'i'}, {"repeat", required_argument, 0, 'r'},
{"gap", required_argument, 0, 'g'}, {"lead", required_argument, 0, 'l'},
{"no-seq", no_argument, 0, 'n'}, {"amp", required_argument, 0, 'a'},
{"cw-id", no_argument, 0, 'c'}, {"format", required_argument, 0, 'o'},
{"noncoherent", no_argument, 0, 'N'}, {"coherent", no_argument, 0, 'C'}, {0, 0, 0, 0}};
int o;
while ((o = getopt_long(argc, argv, "", opts, NULL)) != -1) {
switch (o) {
case 's': src = optarg; break;
case 'd': dst = optarg; break;
case 'm': msg = optarg; break;
case 'u': unlic = 1; break;
case 'S': sf = atoi(optarg); break;
case 'b': bw = atof(optarg); break;
case 'F': fs = atof(optarg); break;
case 'f': freq = atof(optarg); break;
case 'i': if_off = atof(optarg); break;
case 'r': repeat = atoi(optarg); break;
case 'g': gap = atof(optarg); break;
case 'l': lead = atof(optarg); break;
case 'n': no_seq = 1; break;
case 'a': amp = atof(optarg); break;
case 'c': cw_id = 1; break;
case 'o': fmt = optarg; break;
case 'N': coherent = CHIRPMODEM_MODE_NONCOHERENT; break;
case 'C': coherent = CHIRPMODEM_MODE_COHERENT; break;
default: return 2;
}
}
if (optind >= argc) {
fprintf(stderr, "usage: %s OUT [--src CALL] [--msg TEXT] [--sf N] ... (see source)\n",
argv[0]);
return 2;
}
const char *out = argv[optind];
if (!unlic && !src) {
fprintf(stderr, "refusing to transmit: licensed profile requires a source callsign\n");
return 1;
}
if (cw_id && !src) {
fprintf(stderr, "--cw-id needs --src\n");
return 1;
}
if (fs < 2 * (bw / 2 + fabs(if_off)) * 1.1) {
fprintf(stderr, "--fs %g too low for a %g Hz signal at %+g Hz offset\n", fs, bw, if_off);
return 1;
}
if (repeat < 1)
repeat = 1;
chirpmodem_tx_config cfg;
chirpmodem_tx_config_init(&cfg);
cfg.sf = sf;
cfg.bw = bw;
cfg.fs = fs;
cfg.if_offset = if_off;
cfg.coherent = coherent;
cfg.profile = unlic ? CHIRPMODEM_UNLICENSED : CHIRPMODEM_LICENSED;
cfg.amp = amp;
int err;
chirpmodem_tx *tx = chirpmodem_tx_new(&cfg, &err);
if (!tx) {
fprintf(stderr, "%s\n", chirpmodem_strerror(err));
return 1;
}
coherent = chirpmodem_tx_coherent(tx);
char data_path[1024], meta_path[1024];
snprintf(data_path, sizeof data_path, "%s.sigmf-data", out);
snprintf(meta_path, sizeof meta_path, "%s.sigmf-meta", out);
/* build every frame first, so a refusal leaves no partial files */
int n_fr = repeat;
float **ys = calloc((size_t)n_fr, sizeof *ys);
long *ny = calloc((size_t)n_fr, sizeof *ny);
double frame_s = 0;
for (int i = 0; i < n_fr; i++) {
char body[300];
if (repeat == 1 || no_seq)
snprintf(body, sizeof body, "%s", msg);
else
snprintf(body, sizeof body, "%s [%d/%d]", msg, i + 1, repeat);
int nb = (int)strlen(body);
long n = chirpmodem_tx_frame(tx, src, dst, (const uint8_t *)body, nb, 0, NULL, 0);
if (n < 0) {
fprintf(stderr, "%s\n", n == CHIRPMODEM_ECALLSIGN || n == CHIRPMODEM_ETOOLONG
? "bad callsign or message too long" : chirpmodem_strerror((int)n));
return 1;
}
ys[i] = malloc(sizeof(float) * 2 * (size_t)n);
ny[i] = chirpmodem_tx_frame(tx, src, dst, (const uint8_t *)body, nb, 0, ys[i], n);
frame_s = chirpmodem_frame_seconds(sf, bw, 1 + (src ? 6 : 0) + (dst ? 6 : 0) + nb, coherent);
}
out_t ot = {fopen(data_path, "wb"), !strcmp(fmt, "ci16"), 0};
if (!ot.f) {
perror(data_path);
return 1;
}
long *ann_start = calloc((size_t)n_fr + 1, sizeof(long));
long cw_start = 0, cw_len = 0;
emit_zeros(&ot, (long)(lead * fs));
for (int i = 0; i < n_fr; i++) {
ann_start[i] = ot.n;
emit(&ot, ys[i], ny[i]);
if (i + 1 < n_fr)
emit_zeros(&ot, (long)(gap * fs));
}
if (cw_id) {
emit_zeros(&ot, (long)(0.3 * fs));
long n = chirpmodem_tx_cw_id(tx, src, 20, NULL, 0);
float *tone = n > 0 ? malloc(sizeof(float) * 2 * (size_t)n) : NULL;
if (n < 0 || !tone || chirpmodem_tx_cw_id(tx, src, 20, tone, n) != n) {
fprintf(stderr, "CW ID: %s\n", chirpmodem_strerror(n < 0 ? (int)n : CHIRPMODEM_ENOMEM));
return 1;
}
cw_start = ot.n;
cw_len = n;
emit(&ot, tone, n);
free(tone);
}
emit_zeros(&ot, (long)(0.1 * fs));
fclose(ot.f);
FILE *m = fopen(meta_path, "w");
if (!m) {
perror(meta_path);
return 1;
}
time_t tt = time(NULL);
char ts[32];
strftime(ts, sizeof ts, "%Y-%m-%dT%H:%M:%SZ", gmtime(&tt));
fprintf(m, "{\n \"global\": {\n \"core:datatype\": \"%s\",\n \"core:version\": \"1.2.0\",\n"
" \"core:sample_rate\": %.1f,\n \"core:recorder\": \"chirpmodem cm_tx %s\",\n"
" \"core:extensions\": [{\"name\": \"chirpmodem\", \"version\": \"0.1.0\", \"optional\": true}],\n"
" \"core:description\": \"chirpmodem transmit file: %d frame(s), SF%d, %g kHz, signal at %.6f MHz\",\n",
ot.ci16 ? "ci16_le" : "cf32_le", fs, chirpmodem_version(), n_fr, sf, bw / 1e3, freq / 1e6);
if (src)
fprintf(m, " \"core:author\": \"%s\",\n", src);
fprintf(m, " \"chirpmodem:sf\": %d,\n \"chirpmodem:bw\": %.1f,\n \"chirpmodem:signal_freq\": %.1f,\n"
" \"chirpmodem:if_offset\": %.1f,\n \"chirpmodem:profile\": \"%s\"\n },\n",
sf, bw, freq, if_off, unlic ? "unlicensed" : "licensed");
fprintf(m, " \"captures\": [{\"core:sample_start\": 0, \"core:frequency\": %.1f, \"core:datetime\": \"%s\"}],\n"
" \"annotations\": [\n", freq - if_off, ts);
for (int i = 0; i < n_fr; i++)
fprintf(m, " {\"core:sample_start\": %ld, \"core:sample_count\": %ld, \"core:freq_lower_edge\": %.1f, "
"\"core:freq_upper_edge\": %.1f, \"core:label\": \"chirpmodem SF%d %g kHz frame %d\"}%s\n",
ann_start[i], ny[i], freq - bw / 2, freq + bw / 2, sf, bw / 1e3, i + 1,
(i + 1 < n_fr || cw_id) ? "," : "");
if (cw_id)
fprintf(m, " {\"core:sample_start\": %ld, \"core:sample_count\": %ld, \"core:freq_lower_edge\": %.1f, "
"\"core:freq_upper_edge\": %.1f, \"core:label\": \"CW ID DE %s\"}\n",
cw_start, cw_len, freq - 100.0, freq + 100.0, src);
fprintf(m, " ]\n}\n");
fclose(m);
printf("wrote %s + %s\n %d %s frame(s), %.3f s each, %.2f s total, %ld samples at %g MS/s (%s)\n"
" signal center %.6f MHz -> tune the SDR LO to %.6f MHz\n", meta_path, data_path, n_fr,
coherent ? "coherent" : "non-coherent",
frame_s, ot.n / fs, ot.n, fs / 1e6, ot.ci16 ? "ci16_le" : "cf32_le", freq / 1e6,
(freq - if_off) / 1e6);
for (int i = 0; i < n_fr; i++)
free(ys[i]);
free(ys);
free(ny);
free(ann_start);
chirpmodem_tx_free(tx);
return 0;
}