Skip to content

pblaas/brainwave

Repository files navigation

Enophone Brainwave Visualization

Real-time brainwave visualization using Enophone EEG headphones with BrainFlow SDK.

Images

WebGL Visual Art CLI Monitor GUI Monitor HTTP API Flow Fields Focus Mode Relaxation Mode

Requirements

  • Python 3.8+
  • Enophone headphones (optional - can run in simulation mode)
  • Bluetooth (for real device)

Setup

  1. Create a virtual environment:
python -m venv venv
  1. Activate the virtual environment:
# Linux/macOS
source venv/bin/activate

# Windows
venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Quick Start

Option 1: Visual Art (Recommended)

  1. Start the WebSocket server (simulation mode for testing):
python enophone_websocket_server.py --simulate
  1. Open enophone-art.html in a browser:
    • Drag the file into a browser, OR
    • Run: python -m http.server 8080 then visit http://localhost:8080/enophone-art.html

Option 2: Real Device

  1. Find your Enophone MAC address:

    • Linux: bluetoothctl devices
    • Windows: Device Manager → Bluetooth Address
  2. Start the server with your MAC:

python enophone_websocket_server.py --mac XX:XX:XX:XX:XX:XX
  1. Open enophone-art.html in a browser

Option 3: HTTP API for Grafana

python enophone_http_server.py --mac XX:XX:XX:XX:XX:XX
# Metrics available at http://localhost:8080/metrics

Channels

Enophone has 4 EEG sensors positioned at standard 10-20 system locations:

Channel Position Description
A1 Left ear (mastoid) Reference electrode
C3 Left motor cortex Left hemisphere brain activity
C4 Right motor cortex Right hemisphere brain activity
A2 Right ear (mastoid) Reference electrode

The sensors capture brainwave data from the frontal parietal lobe (involved in sustained attention) and can detect all 5 brainwave types:

  • Gamma (40 Hz): Peak focus, high alertness
  • Beta (13 Hz): Active thinking, concentration
  • Alpha (10 Hz): Relaxed flow, creative states
  • Theta (7.83 Hz): Deep relaxation, meditation
  • Delta (1-4 Hz): Deep sleep

Signal Processing

The raw EEG voltage data from the 4 sensors is processed to extract brainwave band powers:

Raw EEG Signal → FFT (Fast Fourier Transform) → Power Spectral Density (PSD) → Band Powers
  1. Raw EEG: Each channel records voltage fluctuations in microvolts (µV)
  2. FFT/PSD: Converts time-domain signal to frequency-domain, showing power at each frequency
  3. Band Power: Integrates power within specific frequency ranges for each band
Band Frequency Range How It's Calculated
Delta 0.5-4 Hz Sum of PSD from 0.5-4 Hz
Theta 4-8 Hz Sum of PSD from 4-8 Hz
Alpha 8-13 Hz Sum of PSD from 8-13 Hz
Beta 13-30 Hz Sum of PSD from 13-30 Hz
Gamma 30-50 Hz Sum of PSD from 30-50 Hz

Focus Score

The focus score is derived from the Beta/Alpha ratio:

Focus Score = (Beta Power / Alpha Power) × 100

Higher Beta (active thinking) relative to Alpha (relaxation) indicates better focus. Values are clamped between 0-100.

Scripts

Script Description
enophone_websocket_server.py WebSocket server for real-time visualization
enophone_http_server.py HTTP API for Grafana/polling
enophone_monitor.py CLI brainwave monitor
enophone_monitor_gui.py GUI visualization with matplotlib
enophone_ios_client.py iOS proxy - connects to remote server and re-broadcasts locally
enophone-art.html WebGL visual art (browser-based)

Usage

WebSocket Server

# With simulated data (no hardware needed)
python enophone_websocket_server.py --simulate

# With real device (MAC required on Linux)
python enophone_websocket_server.py --mac XX:XX:XX:XX:XX:XX --port 8765

CLI Monitor

python enophone_monitor.py --mac XX:XX:XX:XX:XX:XX

GUI Monitor

python enophone_monitor_gui.py --mac XX:XX:XX:XX:XX:XX --gui

iOS/macOS Usage (Proxy Mode)

Since BrainFlow requires direct Bluetooth access which doesn't work on iOS/macOS, use this architecture:

  1. Linux machine (has Bluetooth access):

    python enophone_websocket_server.py --mac XX:XX:XX:XX:XX:XX
  2. iOS/macOS device (runs the proxy):

    # Connect to remote Linux server and expose locally
    python enophone_ios_client.py --host <linux-ip> --remote-port 8765 --local-port 8765
  3. Local apps connect to:

    ws://localhost:8765
    

The proxy receives data from the Linux server and re-broadcasts it locally, allowing any app on iOS/macOS to connect as if the server were local.

Visual Effects

The WebGL art (enophone-art.html) displays:

  • Flow fields: Color-coded by channel (A1, C3, C4, A2)
  • Calm indicator: Central ripple effect when all channels near ideal range
  • High spike fireworks: When values exceed ±1000
  • Low zone fireworks: When values are between -25 and 25
  • Intensity damping: Values >1000 are reduced by 50% to prevent saturation

Troubleshooting

  • Connection refused: Ensure WebSocket server is running (python enophone_websocket_server.py --simulate)
  • No data from device: Check Bluetooth pairing and MAC address
  • Linux requires MAC: Unlike Windows/macOS, Linux requires explicit MAC address

Disclaimer

This software is provided "as is", without warranty of any kind, express or implied. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

This project is for educational and personal use only. It is not a medical device and should not be used for medical diagnosis or treatment. The brainwave data collected by this software may not be accurate or reliable enough for clinical purposes.