MedEx
MedEx is a generalized, composable Elixir SDK for medical-device waveform, sparse-measurement, and technical-status acquisition.
Medical-device software needs faithful source bytes, explicit sample and epoch coordinates, honest representation of missing data and clock uncertainty, durable replay across intermittent networks, and clear separation between authoritative capture and disposable live monitoring. MedEx provides pure functional contracts and bounded local recording primitives so Elixir clinical servers, device gateways, and standalone handheld clients share one verifiable evidence model—while leaving authentication, patient/encounter identity, OTP supervision, clinical review, and EHR/FHIR export in the host application.
Scope And Capabilities
- Pure functional contracts (
core/med_core):- Immutable regular-waveform descriptors (
MedEx.StreamSpec) with:s16le,:s24le, and:s32leinterleaved encodings, rational sample rates, explicit channel calibration (or:unknown), clock domains, and:observed/:claimedprocessing provenance. - Host-admitted waveform frames (
MedEx.SampleFrame) with transport-independent SHA-256 semantic identity (MedEx.FrameFingerprint). - Sparse exact-decimal measurements (
MedEx.MeasurementEventwith{:value, mantissa, exponent, unit}or{:missing, reason}and raw payload provenance) and technical observations (MedEx.StatusEvent). - Explicit sample-range or event-scoped qualifiers (
MedEx.IntegritySpanfor:source_gap,:clock_unavailable,:configuration_unknown,:device_status_invalid,:source_processing_unverified, and:duplicate_conflict). - Canonical
MEX1binary waveform framing (MedEx.Wire.FrameV1), boundedmedex.device.v1JSON control envelopes (MedEx.Wire.Control), and deterministicMEJ1JSON serialization (MedEx.Serialization,MedEx.CanonicalJSON). - Affine rational polytope clock-bound estimation (
MedEx.ClockAlign) and persist-before-dispatch device command state transitions with explicit:outcome_unknowntracking (MedEx.ControlInterlock).
- Immutable regular-waveform descriptors (
- Authoritative capture, durable acknowledgement, and POSIX recovery (
core/med_capture):- Per-stream fenced
MedEx.StreamWriterbacked by content-addressed.pcmchunks and a checksummed, hash-chainedstream.mejournal(MedEx.Storage.File). - Receipts that separate volatile
accepted_*positions from fsynceddurable_*positions so upstream spools never delete unacknowledged data. - Deterministic replay reconciliation, duplicate-conflict quarantine, explicit
:source_gapcommits, crash recovery (MedEx.Storage.File.recover/2), and frozen predecessor lineage import across writer replacements.
- Per-stream fenced
- Browser WAV compatibility and opt-in media extensions:
- Preserves continuous 48 kHz mono PCM16LE browser capture (
MedEx.Capture,MedEx.Evidence,MedEx.Wav) and packaged JavaScript AudioWorklet / LiveView helpers (MedEx.Web). - Isolates native Opus encoding (
:med_ex_opus) and WebRTC peer transport (:med_ex_rtc) into optional extension packages behindMedEx.PreviewProviderandMedEx.RTCProvider, so headless servers and edge clients compile with zero C/NIF or media dependencies.
- Preserves continuous 48 kHz mono PCM16LE browser capture (
Canonical Application Composition
The canonical Mix application is :med_ex (Elixir ~> 1.20). It compiles core/med_core/lib and core/med_capture/lib into a single OTP application with zero runtime Hex dependencies (extra_applications: [:logger, :crypto]) and starts no global supervision tree or background processes. Hosts start MedEx.StreamWriter or MedEx.Capture directly under their own supervisors.
The two private component projects compile and test independently. The root Mix
project and the Weld base artifact derive their component lists from the same
declaration. Consumers choose :med_ex and add the independently installable
:med_ex_opus or :med_ex_rtc package when the host uses that provider.
Installation And Dependency Consumption
The 0.2.0 SDK implements contract revision 1 and uses separate media
providers. The 0.1.0 package has the original
browser WAV capture and preview API. Hex package history
Consumers use standard Mix dependencies and never need Weld installed:
# Hex, once 0.2.0 is available (headless base SDK)
{:med_ex, "~> 0.2.0"}
# Local source checkout (headless base SDK)
{:med_ex, "~> 0.2.0", path: "../med_ex"}
# Pinned Git commit on main (headless base SDK)
{:med_ex, "~> 0.2.0", github: "nshkrdotcom/med_ex", ref: "<commit_sha>"}
# Optional live media extensions (from checkout or Git subdir; use override: true on :med_ex so extensions converge on your chosen base)
{:med_ex, "~> 0.2.0", github: "nshkrdotcom/med_ex", ref: "<commit_sha>", override: true},
{:med_ex_opus, "~> 0.2.0", github: "nshkrdotcom/med_ex", ref: "<commit_sha>", subdir: "extensions/med_ex_opus"},
{:med_ex_rtc, "~> 0.2.0", github: "nshkrdotcom/med_ex", ref: "<commit_sha>", subdir: "extensions/med_ex_rtc"}
# Previously published 0.1.0 legacy browser WAV/preview package on Hex
{:med_ex, "~> 0.1.0"}The optional tools/maintainer project generates graph-composed base and
standalone extension views with Weld. The three artifacts are med_ex,
med_ex_opus, and med_ex_rtc. Consume each live, full or prepared project
with an ordinary Mix path dependency, or use an exact Git revision of a tracked
snapshot. Consumption, Extensions, and Verification
Quick Example: Authoritative Stream Recording
alias MedEx.{SampleFrame, Serialization, StreamSpec, StreamWriter}
base_spec = %StreamSpec{
acquisition_id: "acq-01",
source_ref: "dev-gateway-01",
boot_id: "boot-9f2a",
stream_id: "ecg-lead-ii",
source_epoch: 0,
descriptor_ref: String.duplicate("0", 64),
modality: "cardiac:ecg",
encoding: :s16le,
channels: [
%{
id: "lead_ii",
label: "Lead_II",
unit: "uV",
calibration: %{zero: 0, scale_num: 5, scale_den: 2, reference: "factory_cal_v1"}
}
],
sample_rate: {500, 1},
capabilities: %{
encodings: [:s16le],
max_frame_samples: 250,
max_payload_bytes: 1_000,
replay: :durable_spool,
commands: []
}
}
spec = %{base_spec | descriptor_ref: Serialization.descriptor_ref(base_spec)}
{:ok, writer} =
StreamWriter.start_link(
owner: self(),
stream: spec,
artifact_dir: "/var/lib/my_host/acquisitions/acq-01/ecg-fence-1",
fence: "fence-1"
)
frame = %SampleFrame{
stream_id: spec.stream_id,
source_epoch: spec.source_epoch,
descriptor_ref: spec.descriptor_ref,
sequence: 0,
first_sample: 0,
sample_count: 100,
config_epoch: 0,
payload: :binary.copy(<<0, 0>>, 100)
}
{:ok, _accepted_receipt} = StreamWriter.push(writer, "fence-1", frame)
{:ok, durable_receipt} = StreamWriter.checkpoint(writer)
{:ok, sealed_index} = StreamWriter.finish(writer)
IO.inspect({durable_receipt.durable_sequence, durable_receipt.durable_sample, sealed_index})Only durable_receipt.durable_sequence / durable_sample authorize upstream spool truncation; sealed_index (%MedEx.StreamIndex{}) records verified .pcm segment digests, any explicit :source_gap or qualifier spans, and the journal digest.
Optional Opus And WebRTC Providers
:med_ex_opus (MedEx.Opus.Provider, backed by Membrane Opus) and :med_ex_rtc (MedEx.RTC.Provider, backed by ExWebRTC / ExRTP) are independent OTP applications:
- Use
:med_ex_opusalone to encode 20 ms 48 kHz mono PCM16LE frames under single-frame backpressure (MedEx.PreviewProvider). - Use
:med_ex_rtcalone to negotiate send-only WebRTC peers and transmit pre-encoded Opus RTP packets (MedEx.RTCProvider). - Configure both in-VM for legacy
MedEx.Capture/MedEx.Previewbrowser sessions:
config :med_ex,
preview_provider: MedEx.Opus.Provider,
rtc_provider: MedEx.RTC.ProviderBecause native C/NIF codecs share the OS process failure domain of the VM that loads them, strict deployments can keep the authoritative :med_ex recording host headless and run :med_ex_opus + :med_ex_rtc inside a separate sidecar OS process.
Local Verification
With Elixir ~> 1.20 (and Node.js for browser worklet unit tests):
mix deps.get
mix format --check-formatted
mix compile --warnings-as-errors
mix test
mix docs --warnings-as-errors
node --test test/*.mjs
./build_support/check_architecture.sh
Guides
- Architecture and Composition — Canonical
:med_exsource ownership, pure-vs-effectful boundaries, OTP supervision, and media fault isolation. - Streams, Storage, Clock, and Control —
StreamSpec,MEX1/MEJ1framing, sparse measurements,StreamWriter, POSIX crash recovery, predecessor lineage,ClockAlign, andControlInterlock. - Host Integration and Browser Compatibility — Practical host gateway patterns alongside browser
MedEx.Capture,MedEx.Preview, andMedEx.WebJavaScript integration. - Consumption, Extensions, and Verification — Direct, Git, projection, and Hex consumption, extension development, and package verification.
- Medical-Device Product Horizon — Implemented capabilities versus physical-device characterization, clinical validation, and future device families.
License
Released under the MIT License.