18#define _POSIX_C_SOURCE 200809L
31#include <alsa/asoundlib.h>
49#define DEFAULT_DEVICE "auto"
50#define AUTO_DEVICE_NAME_MAX 64U
51#define DEFAULT_RATE 48000U
52#define DEFAULT_INTERVAL_MS 50U
53#define MIN_RATE_HZ 8000U
54#define MIN_INTERVAL_MS 20U
55#define MAX_RATE 384000U
56#define MAX_INTERVAL_MS 10000U
57#define MILLISECONDS_PER_SECOND 1000U
58#define MICROSECONDS_PER_SECOND 1000000U
59#define NANOSECONDS_PER_SECOND 1000000000L
60#define NANOSECONDS_PER_MILLISECOND 1000000U
61#define NANOSECONDS_PER_MICROSECOND 1000U
62#define MILLISECONDS_PER_SECOND_F 1000.0
64#define CAPTURE_PERIODS_PER_SECOND 200U
65#define CAPTURE_BUFFERS_PER_SECOND 50U
66#define CAPTURE_MONO_CHANNELS 1U
67#define CAPTURE_WAIT_MS 100
68#define CAPTURE_STALL_TIMEOUT_MS 2000U
69#define MAX_INTERVAL_FRAMES ((MAX_RATE * MAX_INTERVAL_MS) / MILLISECONDS_PER_SECOND)
70#define MAX_PERIOD_FRAMES (MAX_RATE / CAPTURE_BUFFERS_PER_SECOND)
72#define ALSA_OBJECT_CAPACITY 4096U
73#define EARCAM_MAX_ARGUMENTS 64
74#define EARCAM_MAX_PARSED_ARGUMENTS (EARCAM_MAX_ARGUMENTS * 2 + 1)
75#define DEFAULT_LOW_CUT_HZ 1800.0
76#define DEFAULT_HIGH_CUT_HZ 3200.0
78#define DEFAULT_MIN_LEVEL_DBFS (-90.0)
79#define DB_FLOOR (-120.0)
80#define METER_WIDTH 50U
81#define TONE_BIN_COUNT 57U
82#define TONE_CONTRAST_THRESHOLD_DB (-3.0)
83#define TREND_THRESHOLD_DB 0.8
84#define ENVELOPE_RELEASE_DB_PER_SECOND 6.0
87#define SOURCE_LEVEL_DB_SPL 95.0
88#define SOURCE_REFERENCE_METERS 3.0
89#define CLIP_THRESHOLD 32700
90#define PI 3.14159265358979323846
93#define MAX_CUTOFF_HZ 190000.0
94#define MIN_CALIBRATION_DB -300.0
95#define MAX_CALIBRATION_DB 300.0
96#define CONTROL_DEVICE_NAME_CAPACITY 32
97#define SHORT_OPTION_STORAGE 3
98#define SERVER_SAMPLE_LINE_CAPACITY 384
99#define SERVER_COMMAND_CAPACITY 64
100#define SERVER_READ_CHUNK_CAPACITY 64
102#define BUTTERWORTH_Q 0.7071067811865476
103#define FILTER_SECTIONS_PER_EDGE 2
104#define PCM_S16_FULL_SCALE 32768.0
105#define AMPLITUDE_FLOOR 0.000001
106#define AMPLITUDE_DB_PER_DECADE 20.0
107#define DB_LOG_BASE 10.0
109#define HANN_TONE_AMPLITUDE_SCALE 4.0
111#define HOMING_FAST_TIME_CONSTANT_S 0.20
112#define HOMING_SLOW_TIME_CONSTANT_S 1.20
113#define HOMING_ACQUISITION_WINDOWS 4U
115#define TEST_TIMESTAMP_US 1000000
116#define TEST_UNDERFLOW_TIMESTAMP_US 100
117#define TEST_UNREAD_FRAMES 480
118#define TEST_RATE_HZ 48000
119#define TEST_EXPECTED_READ_END_US 990000
120#define TEST_TARGET_HZ 2700.0
121#define TEST_INTERFERENCE_HZ 800.0
122#define TEST_TARGET_AMPLITUDE 0.02
123#define TEST_INTERFERENCE_AMPLITUDE 0.20
124#define TEST_TONE_MIN_DB -36.0
125#define TEST_TONE_MAX_DB -32.0
126#define TEST_CONTRAST_MAX_DB -15.0
127#define TEST_TREND_WINDOWS 40U
128#define TEST_APPROACH_START_DB -65.0
129#define TEST_RECEDE_START_DB -45.0
130#define TEST_TREND_STEP_DB 0.3
205static uint64_t
audio_read_end_us(uint64_t hardware_us, uint64_t unread_frames,
unsigned int rate)
209 return hardware_us > delay_us ? hardware_us - delay_us : 0;
231 size_t output_count = 0;
232 normalized[output_count++] = argv[0];
233 for (
int index = 1; index < argc; ++index) {
234 const char *argument = argv[index];
235 if (strcmp(argument,
"--") == 0) {
239 normalized[output_count++] = argv[index];
240 for (++index; index < argc; ++index) {
244 normalized[output_count++] = argv[index];
248 if (strncmp(argument,
"--", 2) != 0 || argument[2] ==
'\0') {
252 normalized[output_count++] = argv[index];
255 const char *name = argument + 2;
256 size_t name_length = strcspn(name,
"=");
257 const char *inline_value = name[name_length] ==
'=' ? name + name_length + 1 : NULL;
259 for (
size_t alias_index = 0; alias_index < alias_count; ++alias_index) {
260 if (strlen(aliases[alias_index].
name) == name_length
261 && strncmp(
name, aliases[alias_index].
name, name_length) == 0) {
262 alias = &aliases[alias_index];
270 normalized[output_count++] = (
char *)alias->
option;
272 if (inline_value == NULL) {
273 if (++index >= argc) {
276 inline_value = argv[index];
281 normalized[output_count++] = (
char *)inline_value;
287 normalized[output_count] = NULL;
288 return (
int)output_count;
305 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0 || now.tv_sec < 0 || now.tv_nsec < 0 ||
317 fprintf(stream,
"Model options: --settings FILE, --check-model, --analyze-audio MONO_S16LE.pcm\n");
319 "Usage: %s [options]\n"
320 "Measure microphone level and display it in the terminal.\n\n"
322 " -d, --device NAME ALSA capture device (default: %s = first USB capture card)\n"
323 " -r, --rate HZ Sample rate (default: %u)\n"
324 " -i, --interval MS Display interval (default: %u)\n"
325 " -l, --low-cut HZ Search band lower edge (default: %.0f)\n"
326 " -u, --high-cut HZ Search band upper edge (default: %.0f)\n"
327 " -n, --min-level DBFS Minimum alarm tone level (default: %.0f)\n"
328 " -c, --calibration DB Add DB to dBFS readings to report dB SPL\n"
329 " --server Machine-readable SAMPLE lines for USHER; STOP/QUIT on stdin\n"
330 " --self-test Test detector without a microphone\n"
331 " --version Show application version and build Git revision\n"
332 " -h, --help Show this help\n\n"
334 " %s --device plughw:1,0\n"
335 " %s -d default motionSCOUT rescue profile\n"
336 " arecord -L List ALSA capture device names\n",
345 unsigned int maximum,
unsigned int *value)
348 unsigned long parsed;
351 parsed = strtoul(text, &end, 10);
352 if (errno != 0 || end == text || *end !=
'\0' || parsed < minimum ||
356 *value = (
unsigned int)parsed;
379 while (found != 0 && snd_card_next(&card) == 0 && card >= 0) {
381 if (snprintf(control_name,
sizeof(control_name),
"hw:%d", card) <= 0) {
384 snd_ctl_t *control = NULL;
385 if (snd_ctl_open(&control, control_name, 0) < 0) {
388 if (snd_ctl_card_info(control, info) < 0) {
389 snd_ctl_close(control);
392 const char *driver = snd_ctl_card_info_get_driver(info);
393 const char *card_id = snd_ctl_card_info_get_id(info);
394 if (driver == NULL || card_id == NULL || strcmp(driver,
"USB-Audio") != 0) {
395 snd_ctl_close(control);
399 while (snd_ctl_pcm_next_device(control, &device) == 0 && device >= 0) {
400 snd_pcm_info_set_device(pcm_info, (
unsigned int)device);
401 snd_pcm_info_set_subdevice(pcm_info, 0);
402 snd_pcm_info_set_stream(pcm_info, SND_PCM_STREAM_CAPTURE);
403 if (snd_ctl_pcm_info(control, pcm_info) < 0) {
406 int length = snprintf(name, name_size,
"hw:CARD=%s,DEV=%d", card_id, device);
407 found = (length > 0 && (size_t)length < name_size) ? 0 : -1;
410 snd_ctl_close(control);
424 parsed = strtod(text, &end);
425 if (errno != 0 || end == text || *end !=
'\0' || !isfinite(parsed) ||
426 parsed < minimum || parsed > maximum) {
440 {
"device",
"-d",
true},
441 {
"rate",
"-r",
true},
442 {
"interval",
"-i",
true},
443 {
"low-cut",
"-l",
true},
444 {
"high-cut",
"-u",
true},
445 {
"min-level",
"-n",
true},
446 {
"calibration",
"-c",
true},
447 {
"self-test",
"-S",
false},
448 {
"server",
"-M",
false},
449 {
"settings",
"-Z",
true},
450 {
"analyze-audio",
"-X",
true},
451 {
"check-model",
"-Q",
false},
452 {
"version",
"-V",
false},
453 {
"help",
"-h",
false}
459 sizeof(long_options) /
sizeof(long_options[0]));
460 if (normalized_argc < 0) {
461 fprintf(stderr,
"Invalid or excessive command-line arguments.\n");
465 while ((
option = getopt(normalized_argc, normalized_argv,
466 "d:r:i:l:u:n:c:hSMVZ:X:Q")) != -1) {
476 fprintf(stderr,
"Invalid sample rate: %s\n", optarg);
482 fprintf(stderr,
"Invalid display interval: %s\n", optarg);
488 fprintf(stderr,
"Invalid low-cut frequency: %s\n", optarg);
494 fprintf(stderr,
"Invalid high-cut frequency: %s\n", optarg);
501 fprintf(stderr,
"Invalid minimum level: %s\n", optarg);
507 fprintf(stderr,
"Invalid calibration offset: %s\n", optarg);
530 if (optind != normalized_argc) {
531 fprintf(stderr,
"Unexpected argument: %s\n", normalized_argv[optind]);
535 fprintf(stderr,
"Search-band frequencies must be greater than zero.\n");
540 fprintf(stderr,
"Filter cutoffs must be below the Nyquist frequency (%.0f Hz).\n",
545 fprintf(stderr,
"Low-cut frequency must be below high-cut frequency.\n");
555 unsigned int sample_rate,
bool high_pass)
563 memset(filter, 0,
sizeof(*filter));
564 if (cutoff_hz <= 0.0) {
570 omega = 2.0 *
PI * cutoff_hz / (double)sample_rate;
572 alpha = sin(omega) / (2.0 * q);
576 filter->
b0 = (1.0 + cosine) / (2.0 * a0);
577 filter->
b1 = -(1.0 + cosine) / a0;
578 filter->
b2 = filter->
b0;
580 filter->
b0 = (1.0 - cosine) / (2.0 * a0);
581 filter->
b1 = (1.0 - cosine) / a0;
582 filter->
b2 = filter->
b0;
584 filter->
a1 = (-2.0 * cosine) / a0;
585 filter->
a2 = (1.0 - alpha) / a0;
599 filter->
z1 = filter->
b1 * input - filter->
a1 *
output + filter->
z2;
628 bins[index].
first = 0.0;
645 double value = sample * window + bins[index].
coefficient * bins[index].
first -
648 bins[index].
first = value;
658 double *frequency_hz)
660 double maximum_power = 0.0;
664 double power = bins[index].
first * bins[index].
first +
668 if (power > maximum_power) {
669 maximum_power = power;
672 bins[index].
first = 0.0;
683 double tone_db,
bool alarm_present,
728 double target_amplitude,
729 double interference_amplitude,
730 double *tone_db,
double *contrast_db)
732 uint64_t sample_count =
734 uint64_t sample_index;
735 double sum_squares = 0.0;
736 double frequency_hz = 0.0;
742 for (sample_index = 0; sample_index < sample_count; ++sample_index) {
744 ? 0.5 - 0.5 * cos(2.0 *
PI * (
double)sample_index /
745 (
double)(sample_count - 1U))
749 for (sample_index = 0; sample_index < sample_count; ++sample_index) {
750 double time = (double)sample_index / (
double)
options->
rate;
754 sum_squares += sample * sample;
757 *contrast_db = *tone_db -
775 double trend_db = 0.0;
776 const char *direction = NULL;
784 fprintf(stderr,
"Self-test failed: tone selectivity (%.1f, %.1f dB)\n",
785 tone_db, contrast_db);
794 if (strcmp(direction,
"APPROACH ") != 0) {
795 fprintf(stderr,
"Self-test failed: approaching trend (%.1f dB)\n",
805 if (strcmp(direction,
"RECEDING ") != 0) {
806 fprintf(stderr,
"Self-test failed: receding trend (%.1f dB)\n", trend_db);
810 printf(
"motionSCOUT detector self-test passed\n");
816 double contrast_db,
bool alarm_present,
817 const char *direction,
double trend_db,
826 if (meter_level < 0.0) {
828 }
else if (meter_level > 1.0) {
831 filled = (
unsigned int)(meter_level * (
double)
METER_WIDTH + 0.5);
835 putchar(index < filled ?
'#' :
' ');
837 printf(
"] %s %s %6.1f %s %4.0f Hz trend %+5.1f dB conf %+4.1f dB",
838 alarm_present ?
"ALARM" :
"listen", direction, displayed_level,
844 printf(
" ~%5.1f m", distance);
867 result = snd_pcm_hw_params_any(pcm, hardware);
869 result = snd_pcm_hw_params_set_access(
870 pcm, hardware, SND_PCM_ACCESS_RW_INTERLEAVED);
873 result = snd_pcm_hw_params_set_format(pcm, hardware,
874 SND_PCM_FORMAT_S16_LE);
880 result = snd_pcm_hw_params_set_rate(pcm, hardware, sample_rate, 0);
883 result = snd_pcm_hw_params_set_period_size_near(
884 pcm, hardware, &period_frames, &direction);
887 result = snd_pcm_hw_params_set_buffer_size_near(
888 pcm, hardware, &buffer_frames);
891 result = snd_pcm_hw_params(pcm, hardware);
897 result = snd_pcm_sw_params_current(pcm, software);
899 && snd_pcm_sw_params_set_tstamp_mode(pcm, software, SND_PCM_TSTAMP_ENABLE) >= 0
900 && snd_pcm_sw_params_set_tstamp_type(pcm, software, SND_PCM_TSTAMP_TYPE_MONOTONIC) >= 0;
902 result = snd_pcm_sw_params_set_avail_min(pcm, software, period_frames);
905 result = snd_pcm_sw_params_set_start_threshold(pcm, software, 1U);
908 result = snd_pcm_sw_params_set_stop_threshold(pcm, software,
912 result = snd_pcm_sw_params(pcm, software);
915 result = snd_pcm_prepare(pcm);
932 const int length = snprintf(line,
sizeof(line),
933 "CLASS t=%llu level=%.2f score=%.6f class=%u alarm=%u clip=%u cal=%u "
934 "begin_us=%llu end_us=%llu error_us=%llu sequence=%llu\n",
938 (
unsigned long long)
result->end_us, (
unsigned long long)
result->error_us,
939 (
unsigned long long)
result->sequence);
940 if (length <= 0 || (
size_t)length >=
sizeof(line))
return;
941 const ssize_t written = write(STDOUT_FILENO, line, (
size_t)length);
949 double frequency_hz,
double contrast_db,
951 bool alarm_present,
bool clipped,
953 uint64_t end_us, uint64_t error_us, uint64_t sequence)
956 int length = snprintf(line,
sizeof(line),
957 "SAMPLE t=%llu level=%.2f freq=%.0f contrast=%.2f "
958 "trend=%.2f env=%.2f alarm=%d clip=%d cal=%d"
959 " begin_us=%llu end_us=%llu error_us=%llu sequence=%llu\n",
960 (
unsigned long long)timestamp_ms,
962 contrast_db, trend_db,
964 alarm_present ? 1 : 0, clipped ? 1 : 0,
966 (
unsigned long long)begin_us, (
unsigned long long)end_us,
967 (
unsigned long long)error_us, (
unsigned long long)sequence);
968 if (length <= 0 || (
size_t)length >=
sizeof(line)) {
972 while (sent < (
size_t)length) {
973 ssize_t written = write(STDOUT_FILENO, line + sent, (
size_t)length - sent);
975 sent += (size_t)written;
978 if (written < 0 && errno == EINTR) {
993 struct pollfd
descriptor = {.fd = STDIN_FILENO, .events = POLLIN, .revents = 0};
995 static size_t command_size = 0;
1000 if ((
descriptor.revents & (POLLHUP | POLLERR | POLLNVAL)) != 0
1005 ssize_t
count = read(STDIN_FILENO, chunk,
sizeof(chunk));
1010 return (errno == EAGAIN || errno == EINTR) ? 0 : 1;
1012 for (ssize_t index = 0; index <
count; ++index) {
1013 if (chunk[index] ==
'\n') {
1014 command[command_size] =
'\0';
1016 if (strcmp(command,
"STOP") == 0 || strcmp(command,
"QUIT") == 0) {
1019 }
else if (command_size + 1 <
sizeof(command)) {
1020 command[command_size++] = chunk[index];
1032 snd_pcm_uframes_t buffer_frames;
1033 snd_pcm_uframes_t period_frames;
1038 uint64_t interval_frames =
1040 uint64_t accumulated_frames = 0U;
1041 double sum_squares = 0.0;
1042 bool window_clipped =
false;
1043 bool window_timing_valid =
true;
1044 uint64_t window_sequence = 0;
1046 if (snd_pcm_get_params(pcm, &buffer_frames, &period_frames) < 0 ||
1048 fprintf(stderr,
"Cannot determine ALSA capture period size.\n");
1052 fprintf(stderr,
"Analysis interval exceeds the fixed workspace.\n");
1057 for (uint64_t index = 0; index < interval_frames; ++index) {
1059 ? 0.5 - 0.5 * cos(2.0 *
PI * (
double)index /
1060 (
double)(interval_frames - 1U))
1069 if (snd_pcm_nonblock(pcm, 1) < 0) { errno = EIO;
return -1; }
1071 if (!last_progress_ms) { errno = EIO;
return -1; }
1078 fprintf(stderr,
"Capture made no progress within its deadline.\n");
1082 snd_pcm_sframes_t frames_read = snd_pcm_readi(pcm,
capture_samples, period_frames);
1083 snd_pcm_sframes_t
frame;
1087 if (!completed_ms || completed_ms < last_progress_ms || completed_ms - last_progress_ms >=
CAPTURE_STALL_TIMEOUT_MS) {
1093 if (frames_read == 0 || frames_read == -EAGAIN || frames_read == -EINTR) {
1096 frames_read =
ready;
1098 if (frames_read < 0) {
1100 if (frames_read == -ESTRPIPE) {
1103 if (poll(NULL, 0,
wait_ms) < 0 && errno != EINTR)
return -1;
1107 }
else recovery = snd_pcm_recover(pcm, (
int)frames_read, 1);
1109 fprintf(stderr,
"\nCapture failed: %s\n", snd_strerror(
recovery));
1111 printf(
"EARCAM_ERROR capture %s\n", snd_strerror(
recovery));
1118 accumulated_frames = 0;
1121 window_clipped =
false;
1122 window_timing_valid =
false;
1128 if ((snd_pcm_uframes_t)frames_read > period_frames) { errno = EOVERFLOW;
return -1; }
1129 last_progress_ms = completed_ms;
1130 snd_pcm_uframes_t available_frames = 0;
1131 snd_htimestamp_t timestamp = {0};
1132 uint64_t read_end_us = 0;
1133 if (
capture_timestamps && snd_pcm_htimestamp(pcm, &available_frames, ×tamp) == 0
1135 && available_frames <= buffer_frames) {
1146 else printf(
"class=%u score=%.6f alarm=%u pcm_rms=%.2f dBFS\n", classified.
category,
1151 window_timing_valid = (accumulated_frames == 0 || window_timing_valid) && read_end_us != 0;
1157 window_clipped =
true;
1165 sum_squares += sample * sample;
1166 ++accumulated_frames;
1168 if (accumulated_frames == interval_frames) {
1170 sqrt(sum_squares / (
double)accumulated_frames));
1171 double frequency_hz = 0.0;
1174 double contrast_db = tone_db - band_db;
1181 &homing, tone_db, alarm_present,
options, &trend_db);
1184 uint64_t
end_us = window_timing_valid
1197 display_homing(tone_db, frequency_hz, contrast_db, alarm_present,
1198 direction, trend_db, &homing,
options);
1200 accumulated_frames = 0U;
1202 window_clipped =
false;
1203 window_timing_valid = read_end_us != 0;
1224 .calibration_db = 0.0,
1225 .calibrated =
false,
1229 snd_pcm_t *pcm = NULL;
1230 struct sigaction action;
1235 return result > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1244 inference_error,
sizeof(inference_error))) {
1245 fprintf(stderr,
"earcam: %s\n", inference_error);
1246 return EXIT_FAILURE;
1250 puts(
options.
onnx ?
"earcam model ready adapter=ear_pcm_classifier_v1" :
"earcam native detector ready");
1251 return EXIT_SUCCESS;
1255 fprintf(stderr,
"earcam: %s\n", inference_error);
1256 return EXIT_FAILURE;
1259 memset(&action, 0,
sizeof(action));
1261 sigemptyset(&action.sa_mask);
1262 sigaction(SIGINT, &action, NULL);
1263 sigaction(SIGTERM, &action, NULL);
1265 signal(SIGPIPE, SIG_IGN);
1266 int flags = fcntl(STDOUT_FILENO, F_GETFL);
1268 fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);
1270 flags = fcntl(STDIN_FILENO, F_GETFL);
1272 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
1279 fprintf(stderr,
"No USB audio capture device found (use --device to select one).\n");
1281 printf(
"EARCAM_ERROR open no USB capture device\n");
1284 return EXIT_FAILURE;
1291 fprintf(stderr,
"Cannot open ALSA capture device '%s': %s\n",
1294 printf(
"EARCAM_ERROR open %s\n", snd_strerror(
result));
1297 return EXIT_FAILURE;
1302 fprintf(stderr,
"Cannot configure '%s' for %u Hz mono capture: %s\n",
1305 printf(
"EARCAM_ERROR configure %s\n", snd_strerror(
result));
1309 return EXIT_FAILURE;
1313 printf(
"EARCAM_READY device=%s rate=%u interval_ms=%u low=%.0f high=%.0f cal=%d\n",
1318 printf(
"earcam motionSCOUT: device=%s, rate=%u Hz, search=%.0f..%.0f Hz, "
1319 "update=%u ms, output=%s\n",
1323 printf(
"Press Ctrl+C to stop.\n");
1333 return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
static void wait_ms(int milliseconds)
Wait until a short polling interval elapses.
static unsigned capacity[COVERAGE_CAMERAS]
static unsigned remaining_ms(const struct transport *transport)
Compute the next USB call's timeout within the operation deadline.
void ear_inference_stop(void)
Request termination, join the classifier and release its model; repeat calls are harmless.
bool ear_inference_start(char *error, size_t capacity)
Start the sole classifier thread after successful ONNX configuration.
bool ear_inference_take(struct ear_inference_result *result)
Consume at most one completed original-window result without blocking capture.
int ear_inference_file(const char *filename)
Classify exact whole windows from a regular little-endian signed-16 mono PCM file.
bool ear_inference_configure(const char *settings_path, unsigned rate, bool *enabled, char *error, size_t capacity)
Select the explicit detector and qualify an ONNX PCM contract before capture starts.
void ear_inference_push(const int16_t *samples, size_t count, uint64_t end_us, uint64_t error_us)
Supply capture samples without waiting for model execution or a busy model mailbox.
void ear_inference_discontinuity(void)
Invalidate queued/completed evidence after capture continuity breaks, including in-flight generations...
Bounded PCM classification without allowing model latency to stall ALSA capture.
#define HOMING_ACQUISITION_WINDOWS
static int parse_options(int argc, char **argv, struct options *options)
Parse and cross-validate EARcam command-line configuration.
#define SOURCE_LEVEL_DB_SPL
static int run_meter(snd_pcm_t *pcm, const struct options *options)
Capture, filter, analyze, and report audio until stopped.
#define DEFAULT_LOW_CUT_HZ
#define DEFAULT_HIGH_CUT_HZ
#define TEST_RECEDE_START_DB
static void update_tone_bins(struct tone_bin *bins, double sample, double window)
Feed one windowed, band-passed sample into every Goertzel recurrence.
#define PCM_S16_FULL_SCALE
#define TEST_TIMESTAMP_US
static union alsa_object_storage software_params_storage
static int find_usb_capture_device(char *name, size_t name_size)
Discover the first USB ALSA card offering a capture PCM.
#define TEST_CONTRAST_MAX_DB
#define ENVELOPE_RELEASE_DB_PER_SECOND
#define FILTER_SECTIONS_PER_EDGE
static int parse_unsigned(const char *text, unsigned int minimum, unsigned int maximum, unsigned int *value)
Parse a bounded unsigned command-line value.
static double analysis_window[MAX_INTERVAL_FRAMES]
static uint64_t audio_read_end_us(uint64_t hardware_us, uint64_t unread_frames, unsigned int rate)
Subtract unread PCM duration from a monotonic ALSA timestamp.
static int normalize_long_options(int argc, char *const argv[], char *normalized[], size_t capacity, const struct option_alias aliases[], size_t alias_count)
Normalize –name value and –name=value into bounded getopt arguments.
static void emit_classifier_sample(const struct ear_inference_result *result, const struct options *options)
Emit one classifier window to the server's nonblocking output.
int main(int argc, char **argv)
Configure EARcam, optionally self-test it, then run the ALSA meter.
#define NANOSECONDS_PER_MICROSECOND
#define TREND_THRESHOLD_DB
#define TEST_TREND_STEP_DB
static void display_homing(double level_db, double frequency_hz, double contrast_db, bool alarm_present, const char *direction, double trend_db, const struct homing_state *state, const struct options *options)
Render one human-facing measurement line for bench use.
#define TEST_INTERFERENCE_AMPLITUDE
#define AUTO_DEVICE_NAME_MAX
#define DEFAULT_INTERVAL_MS
static void configure_tone_bins(struct tone_bin *bins, const struct options *options)
Initialize evenly spaced Goertzel bins across the selected search band.
static unsigned long server_dropped_lines
#define HOMING_SLOW_TIME_CONSTANT_S
static const char * update_homing_state(struct homing_state *state, double tone_db, bool alarm_present, const struct options *options, double *trend_db)
Update envelope and fast/slow trend estimates for operator guidance.
#define CONTROL_DEVICE_NAME_CAPACITY
#define NANOSECONDS_PER_SECOND
static volatile sig_atomic_t keep_running
#define MAX_PERIOD_FRAMES
static void configure_biquad(struct biquad *filter, double cutoff_hz, unsigned int sample_rate, bool high_pass)
Configure a second-order Butterworth high- or low-pass section.
#define TEST_INTERFERENCE_HZ
static int parse_double_value(const char *text, double minimum, double maximum, double *value)
Parse a finite bounded floating-point command-line value.
#define CAPTURE_MONO_CHANNELS
static void emit_server_sample(uint64_t timestamp_ms, double level_db, double frequency_hz, double contrast_db, double trend_db, double envelope_db, bool alarm_present, bool clipped, const struct options *options, uint64_t begin_us, uint64_t end_us, uint64_t error_us, uint64_t sequence)
Emit one self-describing USHER server sample without blocking capture.
#define MILLISECONDS_PER_SECOND_F
#define MICROSECONDS_PER_SECOND
#define HOMING_FAST_TIME_CONSTANT_S
#define TEST_UNDERFLOW_TIMESTAMP_US
#define SERVER_COMMAND_CAPACITY
#define MAX_INTERVAL_FRAMES
static bool capture_timestamps
#define NANOSECONDS_PER_MILLISECOND
#define MIN_CALIBRATION_DB
#define TONE_CONTRAST_THRESHOLD_DB
static void analyze_test_window(struct tone_bin *bins, const struct options *options, double target_amplitude, double interference_amplitude, double *tone_db, double *contrast_db)
Analyze a deterministic synthetic tone mixture for self-test coverage.
static uint64_t monotonic_ms(void)
Return monotonic milliseconds for correlating samples without wall-clock jumps.
#define CAPTURE_BUFFERS_PER_SECOND
#define SHORT_OPTION_STORAGE
#define TEST_UNREAD_FRAMES
static int run_self_test(const struct options *options)
Verify selectivity and trend classification without ALSA hardware.
static void print_usage(FILE *stream, const char *program)
Print command-line help or version-led usage text.
#define SERVER_SAMPLE_LINE_CAPACITY
static union alsa_object_storage card_info_storage
#define TEST_APPROACH_START_DB
#define EARCAM_MAX_PARSED_ARGUMENTS
static double amplitude_to_db(double amplitude)
Convert a linear amplitude to bounded dBFS.
#define TEST_EXPECTED_READ_END_US
#define CAPTURE_STALL_TIMEOUT_MS
static int16_t capture_samples[MAX_PERIOD_FRAMES]
static union alsa_object_storage pcm_info_storage
#define EARCAM_MAX_ARGUMENTS
#define ALSA_OBJECT_CAPACITY
#define MAX_CALIBRATION_DB
#define TEST_TARGET_AMPLITUDE
#define CAPTURE_PERIODS_PER_SECOND
static int configure_capture(snd_pcm_t *pcm, unsigned int sample_rate)
Configure mono signed-16 ALSA capture with low-latency period sizes.
#define SOURCE_REFERENCE_METERS
static double apply_biquad(struct biquad *filter, double input)
Filter one PCM sample while retaining the section delay state.
#define HANN_TONE_AMPLITUDE_SCALE
#define DEFAULT_MIN_LEVEL_DBFS
#define AMPLITUDE_DB_PER_DECADE
static int poll_server_stdin(void)
Consume non-blocking STOP/QUIT commands from USHER.
#define SERVER_READ_CHUNK_CAPACITY
#define TEST_TREND_WINDOWS
static union alsa_object_storage hardware_params_storage
#define MILLISECONDS_PER_SECOND
static void handle_signal(int signal_number)
Request orderly capture-loop termination from a signal handler.
static double strongest_tone_db(struct tone_bin *bins, uint64_t sample_count, double *frequency_hz)
Extract and reset the strongest Goertzel-bin power estimate.
bool inference_settings_find(const char *application, const char *module, char *filename, size_t capacity)
Discover application_settings.ini in adjacent, development-module, then installed-share order.
Shared application_settings.ini discovery with module-owned extra keys.
#define INFERENCE_PATH_CAPACITY
uint8_t frame[LWIR_MAX_FRAME_BYTES]
Compile-time EARcam version and source-revision identity strings.
#define EARCAM_BUILD_VERSION
Normalized biquad coefficients and transposed-direct-form-II delay state.
One classified audio window; probability and measured level are different evidence.
Peak hold and fast/slow averages; direction is guidance, not a bearing measurement.
unsigned int alarm_windows
Long-to-short option mapping; borrowed values are passed to standard getopt.
char option[SHORT_OPTION_STORAGE]
Validated capture and reporting policy, fixed before the sample loop.
One Goertzel recurrence; state resets at each analysis-window boundary.
Aligned bounded storage for an opaque ALSA object after its sizeof guard succeeds.
unsigned char bytes[ALSA_OBJECT_CAPACITY]
struct usher_control_result result