How upsidedownlabs/cortex works
Project Type and Purpose
• CortEX is a full-stack (primarily front-end) Next.js web application for real-time neurofeedback during meditation. It connects via Web Bluetooth to a BLE-enabled biopotential device (EEG/ECG), performs real-time signal processing in browser Web Workers, classifies mental and physical states, renders rich visualizations, and allows session management and export.
File Structure Analysis
• Top-level:
• .github/workflows: CI for Next.js deployment
• next.config.ts, package.json, tsconfig.json: Next.js/TypeScript config
• public: static assets
• src/app: Next.js routed pages (layout.tsx, page.tsx) and global styles
• src/components: React UI components (Bledata, Dataplotting, Hrvwebglplot, MeditationSession, etc.)
• src/components/ui: Radix/Tailwind UI primitives (accordion, dialog, button…)
• src/components/SessionExport: PDF export logic
• src/lib: core JS/TS modules for filtering (fft.ts, filters.tsx, notchfilter.ts), stateClassifier.ts, utils.ts
• src/webworker: three worker entry points (bandPower.worker.ts, bpm.worker.ts, dataProcessor.worker.ts)
• Quote sources, type declarations, linter/postcss configs
README Insights
• Real-time EEG/ECG via BLE, visualized with WebGL (webgl-plot) and Recharts
• Signal pipeline lives in Web Workers
• Classification algos (rule-based or ML) operate on band-power data
• Session logging: durations, band power averages, HRV metrics
• Next.js for SSR/static export, Tailwind CSS for styling, Radix UI
Main Components
a. BLE Device (NPG Lite)
b. Browser Runtime
– Web Bluetooth API module
– Web Workers
• DataProcessor Worker: bandpass/notch filters
• BandPower Worker: FFT and band-power integration
• BPM Worker: R-peak detection and HRV metrics
– Main Thread (React/Next.js)
• State management (React context/hooks)
• UI Components (charts, waveforms, state indicators)
• Session manager & export engine (PDF)
c. Next.js Server/Hosting Layer
– SSR/static hosting, optional API routes (if extended)
d. External Libraries
– webgl-plot, Recharts, Radix UI, Tailwind CSS
Relationships and Data Flows
1. BLE Device → Web Bluetooth API
2. Raw data → DataProcessor Worker → filtered streams
3. Filtered streams → BandPower Worker → band-power arrays
4. Filtered ECG → BPM Worker → R-peaks & HRV metrics
5. Workers send messages → Main Thread
6. Main Thread distributes data to React components → render UI
7. Session data stored in React state → Export to PDF / download
8. (Optional) Session data → Next.js API or external database
Architectural Patterns & Design Principles
• Pipeline pattern: chaining Web Workers for non-blocking signal processing
• Componentized UI: reusable React/Tailwind/Radix components
• Separation of concerns: filters, FFT, classification isolated in src/lib and workers
• Hook/event-driven data flow: postMessage between workers and main thread
• Single page app with server-side rendering fall-back (Next.js)
Technologies & Frameworks to Highlight
• Next.js & React (pages/app router)
• TypeScript
• Web Bluetooth API
• Web Workers (three distinct workers)
• webgl-plot (WebGL), Recharts (SVG charts)
• Tailwind CSS, Radix UI components
• PDF export library (custom TS in SessionExport)