Loading...
Searching...
No Matches
ear_cam_pipe.c
Go to the documentation of this file.
192static struct ear_session_snapshot session_buffers[EAR_SESSION_BUFFER_COUNT] = {{.owned = true}, {.owned = false}};
208 "pose_aligned,pose_uncertainty_m,window_earliest_us,window_latest_us,sequence,request_lat_deg,request_lon_deg,session_id,detector_kind,classification_score,classification_class\n";
257 band_set = isfinite(low_cut_hz) && isfinite(high_cut_hz) && low_cut_hz > 0.0 && high_cut_hz > low_cut_hz;
273 return ((double)(x % EAR_SIM_NOISE_STEPS) / EAR_SIM_NOISE_STEPS_PER_DB) - EAR_SIM_NOISE_HALF_RANGE_DB;
281static struct latest_measurement simulated_measurement(const union dc_shot_union *shot, uint64_t now)
287 double east = (lon - simulated_lon_deg) * DEG_TO_RAD * EARTH_RADIUS_M * cos(simulated_lat_deg * DEG_TO_RAD);
413 if (!parse_unsigned_field(line, "begin_us", &begin_us) || !parse_unsigned_field(line, "end_us", &end_us)
414 || !parse_unsigned_field(line, "error_us", &error_us) || !parse_unsigned_field(line, "sequence", &sequence)) return;
548 fprintf(stderr, "EAR_CAM_PIPE:\tchild %ld not reaped; replacement deferred\n", (long)server_pid);
561 const char *log_dir = usher_resolve_path(USHER_EAR_CAM_LOG_DIR, resolved_log, sizeof(resolved_log));
578 descriptor = open(session_log_path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, EAR_LOG_FILE_MODE);
611 for (size_t index = 0; index < sizeof(batch[row]); ++index) batch[row][index] = log_queue[log_head][index];
670 if (atomic_load(&log_dropped)) fprintf(stderr, "EAR_CAM_PIPE:\taudio log dropped/failed rows: %llu\n",
934static int format_session_row(const struct ear_sample *sample, uint32_t session_id, char *line, size_t size)
937 "%llu,%d,%.7f,%.7f,%.2f,%.2f,%.2f,%.2f,%.2f,%.0f,%d,%d,%d,%.3f,%llu,%llu,%llu,%.7f,%.7f,%u,%s,%.6f,%u\n",
941 sample->alarm ? 1 : 0, sample->clipped ? 1 : 0, sample->pose_aligned ? 1 : 0, sample->pose_uncertainty_m,
943 (unsigned long long)sample->window_sequence, sample->request_lat / EAR_COORDINATE_UNITS_PER_DEG,
945 session_id, sample->classifier ? "onnx_pcm" : "tone", sample->classifier_score, sample->classifier_class);
949 * @brief Append on the serial owner using aligned pose when available, labelled request pose otherwise.
953static void commit_audio_sample(const union dc_shot_union *request, const struct latest_measurement *measurement)
955 const union dc_shot_union *shot = measurement->observation.pose_valid ? &measurement->observation.pose : request;
981 sample->pose_uncertainty_m = measurement->observation.pose_valid ? measurement->observation.pose_uncertainty_m : -1;
1002 struct capture_observation *observation = &pending_audio[pending_head].measurement.observation;
1028 if (!ready || !measurement.valid || measurement.timestamp_ms > now + EAR_SAMPLE_FUTURE_SLACK_MS
1032 if (warning_due(now)) fprintf(stderr, "EAR_CAM_PIPE-%d:\tno fresh microphone measurement available\n", shot->data.nr);
1085 snprintf(line, sizeof(line), "# stop session_id=%u samples=%zu\n", log_session_id, session_count);
1095int ear_cam_pipe_snapshot_solve(const struct ear_session_snapshot *snapshot, struct ear_loudest_spot *result)
1118static int render_session(const struct ear_sample *samples, size_t count, int32_t number, uint32_t session_id,
1131 const char *photo_dir = usher_resolve_path(USHER_EAR_CAM_PHOTO_DIR, resolved_photo, sizeof(resolved_photo));
1148 int descriptor = open(filename, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, EAR_EVIDENCE_FILE_MODE);
1153 int csv_length = snprintf(csv_path, sizeof(csv_path), "%.*s.csv", length - EAR_JPEG_SUFFIX_LENGTH, filename);
1155 csv_descriptor = open(csv_path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, EAR_EVIDENCE_FILE_MODE);
1185 "# result session_id=%u valid=%d lat=%.7f lon=%.7f agl=%.2f alt=%.2f level=%.2f conf=%.3f used=%u/%u\n",
1187 result->alt_m, result->level_db, result->confidence, result->used_count, result->sample_count) < 0)
1191 if (!csv_saved) fprintf(stderr, "EAR_CAM_PIPE:\tfailed/incomplete snapshot CSV retained: %s\n", csv_path);
1206 length = snprintf(field, sizeof(field), "%.*s_field.jpg", (int)strlen(filename) - EAR_JPEG_SUFFIX_LENGTH, filename);
1214int ear_cam_pipe_render(const struct ear_loudest_spot *result, char *filename, size_t filename_size,
1221int ear_cam_pipe_snapshot_render(const struct ear_session_snapshot *snapshot, uint32_t session_id,
1226 return render_session(snapshot->samples, snapshot->count, ear_cam_pipe_snapshot_number(snapshot),
1230/** @brief Solve the current session without consuming it, for intermediate mission feedback. */
1252 "# result session_id=%u valid=%d lat=%.7f lon=%.7f agl=%.2f alt=%.2f level=%.2f conf=%.3f used=%u/%u\n",
char ** environ
static bool capture_observation_valid(const struct capture_observation *observation)
Check that an observation supplies a usable time interval.
Definition capture_timing.h:63
static bool poll_result(struct camera_point_result *point)
Drain a bounded reply batch and publish only the active request's result.
Definition dipla_camera_backend.c:234
static uint64_t milliseconds(void)
Read elapsed-time evidence unaffected by changes to civil time.
Definition dipla_camera_backend.c:59
static bool parse_unsigned_field(const char *line, const char *key, uint64_t *value)
Parse exact unsigned timing/sequence evidence without rounding through double.
Definition ear_cam_pipe.c:355
static void * supervise_server(void *unused)
Retry missing/disconnected capture independently of optical operation.
Definition ear_cam_pipe.c:833
bool ear_cam_pipe_set_settings(const char *settings)
Copy an assignment path before the microphone supervisor starts.
Definition ear_cam_pipe.c:165
bool ear_cam_pipe_set_device(const char *device)
Copy a startup ALSA PCM name into bounded adapter-owned storage.
Definition ear_cam_pipe.c:224
static struct ear_session_snapshot session_buffers[EAR_SESSION_BUFFER_COUNT]
Definition ear_cam_pipe.c:192
int ear_cam_pipe_snapshot_solve(const struct ear_session_snapshot *snapshot, struct ear_loudest_spot *result)
Solve immutable detached evidence without touching current-session state.
Definition ear_cam_pipe.c:1095
static int format_session_row(const struct ear_sample *sample, uint32_t session_id, char *line, size_t size)
Format one evidence row with both chosen pose and original request provenance.
Definition ear_cam_pipe.c:934
bool ear_cam_pipe_ready(void)
Report whether an actual or simulated measurement source is ready.
Definition ear_cam_pipe.c:925
int ear_cam_pipe_init(const char *unused)
Reset session storage and start or configure the acoustic backend.
Definition ear_cam_pipe.c:871
int ear_cam_pipe_snapshot_render(const struct ear_session_snapshot *snapshot, uint32_t session_id, const struct ear_loudest_spot *result, char *filename, size_t filename_size, bool simulated)
Render detached evidence using collision-safe session-tagged JPEG/CSV names.
Definition ear_cam_pipe.c:1221
int32_t ear_cam_pipe_snapshot_number(const struct ear_session_snapshot *snapshot)
Read the last request number from immutable evidence.
Definition ear_cam_pipe.c:1102
void ear_cam_pipe_set_pose_lookup(bool(*lookup)(struct capture_observation *, uint64_t))
Set the startup-only pose callback, later called without waiting on the serial owner.
Definition ear_cam_pipe.c:219
void ear_cam_pipe_log_result(uint32_t session_id, const struct ear_loudest_spot *result)
Best-effort enqueue of a correlated result row on the serial owner.
Definition ear_cam_pipe.c:1247
static struct latest_measurement simulated_measurement(const union dc_shot_union *shot, uint64_t now)
Model a virtual point source using free-field inverse-distance attenuation.
Definition ear_cam_pipe.c:281
static int parse_field(const char *line, const char *key, double *value)
Parse one named numeric field from an EARcam server line.
Definition ear_cam_pipe.c:328
int ear_cam_pipe_finish(struct ear_loudest_spot *result)
Solve and close the current session, clearing accumulated samples afterward.
Definition ear_cam_pipe.c:1259
static double simulated_noise_db(void)
Produce reproducible bounded simulation noise without shared libc PRNG state.
Definition ear_cam_pipe.c:266
static void sleep_ms(unsigned int milliseconds)
Sleep through signal interruption for bounded lifecycle polling.
Definition ear_cam_pipe.c:314
static bool(* pose_lookup)(struct capture_observation *, uint64_t)
Definition ear_cam_pipe.c:216
void ear_cam_pipe_deinit(void)
Stop the child and clear all bounded session state.
Definition ear_cam_pipe.c:909
struct ear_session_snapshot * ear_cam_pipe_detach_session(void)
Transfer active storage to an immutable worker snapshot without copying samples.
Definition ear_cam_pipe.c:1078
static bool warning_due(uint64_t now)
Rate-limit repeated unavailable/stale microphone diagnostics.
Definition ear_cam_pipe.c:860
bool ear_cam_pipe_begin_session(uint32_t session_id)
Begin/reset active collection without solving or waiting for log I/O.
Definition ear_cam_pipe.c:1054
int ear_cam_pipe_solve(struct ear_loudest_spot *result)
Solve the current session without consuming it, for intermediate mission feedback.
Definition ear_cam_pipe.c:1231
int ear_cam_pipe_render(const struct ear_loudest_spot *result, char *filename, size_t filename_size, bool simulated)
Render the current session as a north-up acoustic evidence JPEG.
Definition ear_cam_pipe.c:1214
static struct @264130032040232221072363340307106354175135130231 pending_audio[EAR_PENDING_AUDIO_WINDOWS]
static void flush_pending_audio(bool force)
Retry pose lookup without waiting; bound unresolved evidence by count and age.
Definition ear_cam_pipe.c:998
void ear_cam_pipe_update_pose(void)
Retry bounded pending audio windows when the serial owner receives new poses.
Definition ear_cam_pipe.c:1012
static void queue_session_log(const char *line)
Best-effort serial-owner enqueue with copied rows and no filesystem wait.
Definition ear_cam_pipe.c:633
static void * write_session_log(void *unused)
Drain bounded row batches; this worker alone opens/writes/flushes/closes the log.
Definition ear_cam_pipe.c:597
int ear_cam_pipe_record(const union dc_shot_union *shot)
Associate the newest sound window with one flight-controller shot pose.
Definition ear_cam_pipe.c:1018
static uint64_t monotonic_ms(void)
Return monotonic milliseconds for server freshness and retry decisions.
Definition ear_cam_pipe.c:306
void ear_cam_pipe_set_band(double low_cut_hz, double high_cut_hz)
Set the detector band forwarded to a subsequently spawned EARcam child.
Definition ear_cam_pipe.c:254
static char log_queue[EAR_LOG_QUEUE_ROWS][EAR_LOG_ROW_CAPACITY]
Definition ear_cam_pipe.c:199
static int start_server(void)
Spawn earcam --server and its asynchronous line reader.
Definition ear_cam_pipe.c:714
static int add_inherited_descriptor_closes(posix_spawn_file_actions_t *actions, const int command_pipe[2], const int response_pipe[2])
Prevent the audio child from inheriting unrelated USHER descriptors.
Definition ear_cam_pipe.c:679
static void close_session_log(void)
Join the log writer after queued rows drain; not a hard filesystem deadline.
Definition ear_cam_pipe.c:661
void ear_cam_pipe_snapshot_release(struct ear_session_snapshot *snapshot)
Return detached storage to admission after all snapshot access has ended.
Definition ear_cam_pipe.c:1107
static void handle_server_line(const char *line)
Interpret one complete server output line and atomically update shared state.
Definition ear_cam_pipe.c:382
static int render_session(const struct ear_sample *samples, size_t count, int32_t number, uint32_t session_id, const struct ear_loudest_spot *result, char *filename, size_t filename_size, bool simulated)
Write evidence from borrowed active or immutable snapshot samples.
Definition ear_cam_pipe.c:1118
static int open_session_log(void)
Create the CSV audit log lazily on the first accepted acoustic sample.
Definition ear_cam_pipe.c:558
static void commit_audio_sample(const union dc_shot_union *request, const struct latest_measurement *measurement)
Append on the serial owner using aligned pose when available, labelled request pose otherwise.
Definition ear_cam_pipe.c:953
static void * reader_main(void *unused)
Read and frame EARcam output in a dedicated thread.
Definition ear_cam_pipe.c:459
static void stop_server(void)
Request child shutdown, join its reader, and reap the owned process.
Definition ear_cam_pipe.c:515
static char microphone_device[EAR_MICROPHONE_NAME_CAPACITY]
Definition ear_cam_pipe.c:162
int32_t ear_cam_pipe_last_shot_nr(void)
Get the shot number associated with the newest stored acoustic sample.
Definition ear_cam_pipe.c:1049
void ear_cam_pipe_set_simulated_source(double lat_deg, double lon_deg, double level_db_at_1m)
Select deterministic virtual acoustic measurements for simulation.
Definition ear_cam_pipe.c:241
Audio/pose adapter and zero-copy snapshot API for the EAR backend.
int calculated_loudestspot(const struct ear_sample *samples, size_t count, struct ear_loudest_spot *result)
Estimate a ground source from relative loudness along a survey track.
Definition ear_fusion.c:129
int ear_heatmap_write(const char *filename, const struct ear_sample *samples, size_t count, const struct ear_loudest_spot *spot)
Write a decorated north-up JPEG of relative measured acoustic intensity.
Definition ear_heatmap.c:574
int ear_heatmap_write_field(const char *filename, const struct ear_sample *samples, size_t count, const struct ear_loudest_spot *spot)
Write the same field without track dots, source marker or scale bar.
Definition ear_heatmap.c:580
Render geotagged acoustic measurements into north-up JPEG heatmaps.
Small path and writable-directory helpers shared by USHER backends.
static const char * usher_resolve_path(const char *path, char *buffer, size_t buffer_size)
Expand a leading home-directory shorthand without allocating memory.
Definition path_utils.h:74
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Definition path_utils.h:97
Definition capture_timing.h:41
Estimated ground source, not a surveyed target or guaranteed alarm detection.
Definition earcam_core.h:37
One audio window associated with microphone position and provenance.
Definition earcam_core.h:55
Statically reserved sample buffer with a single active or detached owner.
Definition ear_cam_pipe.c:187
struct ear_sample samples[EAR_MAX_SAMPLES]
Definition ear_cam_pipe.c:188
Latest parsed window plus optional producer timing; published under ear_mutex.
Definition ear_cam_pipe.c:139
Definition usher_protocol.h:51