# brainvision
Rust library and terminal UI for **Brain Products BrainVision Recorder RDA** EEG streams over TCP/IP.
## Features
- Pure-Rust BrainVision RDA protocol parser (Start / Data16 / Data32 / Stop)
- High-level `BrainVisionDevice` API over `TcpStream`
- Reconnect with exponential backoff
- Stream abstractions: `next_block()`, `next_scan()`, `capture()`
- Marker parsing and export helpers (`CSV`, BrainVision-like triplet)
- CLI and ratatui TUI binaries
- Optional feature flags: `dsp`, `verify`, `sandbox`, `tui`
## Installation
```bash
cargo add brainvision
```
## Quick start
```rust
use brainvision::prelude::*;
let mut dev = BrainVisionDevice::connect_default("127.0.0.1")?;
let header = dev.wait_for_start()?;
println!("{} channels @ {:.2} Hz", header.channel_count, header.sampling_rate_hz());
let scans = dev.capture(1000)?;
println!("Captured {} scans", scans.len());
```
## Recorder settings
In BrainVision Recorder:
1. Enable **Remote Data Access (RDA)**
2. Select port:
- `51244` (int16 stream)
- `51234` (float32 stream)
3. Start acquisition in Recorder before running client
Environment variables:
- `BRAINVISION_HOST` (default `127.0.0.1`)
- `BRAINVISION_PORT` (default `51244`)
## Protocol frame layout
```text
[GUID:16] [SIZE:4 little-endian] [PAYLOAD: SIZE-20 bytes]
```
Message GUIDs:
- Start/header
- Data16 (int16 samples)
- Data32 (float32 samples)
- Stop
Data blocks include:
- block number
- number of points
- marker count
- interleaved channel samples
- marker records (`size, position, points, channel, type, description`)
## Export helpers
- `write_scans_csv(path, scans)`
- `write_markers_csv(path, markers)`
- `write_brainvision_triplet(prefix, header, scans, markers)` writes:
- `<prefix>.vhdr`
- `<prefix>.eeg.csv`
- `<prefix>.vmrk.csv`
## Troubleshooting
- **Connected but no Start message**
- Ensure Recorder RDA is enabled and acquisition actually started.
- **Connection refused**
- Check host/port and local firewall.
- **Frequent reconnects**
- Increase Recorder stability / network quality.
- Use `next_block_resilient()` backoff.
- **Flat lines or wrong amplitudes**
- Verify channel resolutions from Start header.
## Project layout
```
brainvision-rs/
├── src/
│ ├── lib.rs
│ ├── main.rs
│ ├── bin/tui.rs
│ ├── types.rs
│ ├── protocol.rs
│ ├── device.rs
│ ├── export.rs
│ ├── dsp.rs
│ ├── verify.rs
│ ├── sandbox.rs
│ └── error.rs
├── examples/
│ ├── scan.rs
│ ├── stream.rs
│ └── read_eeg.rs
└── tests/
└── types_tests.rs
```
## License
[MIT](./LICENSE)