This Python script bridges EEG data streams to OSC messages, converting brainwave amplitude and frequency (delta, theta, alpha, and beta waves) into MIDI-compatible values. It's designed to work with Lab Streaming Layer (LSL) for EEG input and sends the processed data via OSC messages to your desired ip/port.
- Reads EEG data (amp and freq) from LSL streams
- Processes four brainwave bands: delta, theta, alpha, and beta
- Applies smoothing and normalization to the signals
- Converts normalized values to MIDI range (0-127)
- Sends data as OSC messages
- Python 3.x
- pip (Python package installer)
- Clone this repository:
git clone https://github.com/zachjesus/EEG-To-Midi.git
cd EEG-To-Midi- Install the required Python packages:
pip install numpy
pip install scipy
pip install pylsl
pip install python-osc
pip install matplotlibThe script includes several configurable constants at the top:
# Maximum values for brainwave amplitude (µV)
delta_max_uv = 40
theta_max_uv = 40
alpha_max_uv = 40
beta_max_uv = 40
# Smoothing level
smooth_level = 2
# Network settings
ip = "127.0.0.1"
port = 8001Adjust these values based on your needs.
-
Start your EEG device and ensure it's streaming data via LSL
-
Run the script:
python eegToMidi.py- The script will:
- Look for available LSL streams
- Connect to the first available EEG stream
- Process the data
- Send OSC messages to the specified IP and port
You can run the EEG on one computer, and this program on another. By connecting them via ethernet and setting up the networking correctly it should be seamless.
These are what you pull out and use in MAX to make your silly music.
The script sends OSC bundles containing EIGHT messages:
-
/delta_midi_freq: Frequency MIDI value (0-127) for delta waves -
/theta_midi_freq: Frequency MIDI value (0-127) for theta waves -
/alpha_midi_freq: Frequency MIDI value (0-127) for alpha waves -
/beta_midi_freq: Frequency MIDI value (0-127) for beta waves -
/delta_midi_amp: Amplitude MIDI value (0-127) for delta waves -
/theta_midi_amp: Amplitude MIDI value (0-127) for theta waves -
/alpha_midi_amp: Amplitude MIDI value (0-127) for alpha waves -
/beta_midi_amp: Amplitude MIDI value (0-127) for beta waves
- Setting correct max numbers for the amplitude is essential for proper function (should be done based on testing). I did not add a minimum constant, and used zero, however, it may be set where the smoothstep function is called.
- You dont have to use my setup, but its a good starting point. This code is based on Barlow parameter extraction. Use other params. Use other extraction methods. Change the message names, change the bundle formatting. Its all easy to modify, so do so!
Example is using CNMAT o.dot for more control over OSC messages. See: https://github.com/CNMAT/CNMAT-odot.
