Connects to a Muse device using Web Bluetooth API or uses mock data for development.
Parameters:
options(Object, optional) - Configuration optionsmock(boolean, default: false) - Enable mock mode to use pre-recorded datamockDataPath(string, optional) - Path to custom CSV file for mock data
Examples:
// Connect to real device
const muse = await connectMuse();
// Connect with mock data (no device required)
const muse = await connectMuse({ mock: true });
// Connect with custom mock data
const muse = await connectMuse({
mock: true,
mockDataPath: "/path/to/custom-data.csv",
});Returns a Muse instance.
The main class for interacting with the Muse device.
new Muse(options);Parameters:
options(Object, optional) - Configuration optionsmock(boolean, default: false) - Enable mock modemockDataPath(string, optional) - Path to custom CSV file for mock data
Example:
// Create Muse instance with mock mode
const muse = new Muse({ mock: true });
await muse.connect();eeg: Array ofMuseCircularBufferinstances for EEG channels (4-5 channels)ppg: Array ofMuseCircularBufferinstances for PPG channels (3 channels)accelerometer: Array ofMuseCircularBufferinstances for accelerometer data (3 axes)gyroscope: Array ofMuseCircularBufferinstances for gyroscope data (3 axes)batteryLevel: Current battery level (number, 0-100)state: Connection state (0: disconnected, 1: connecting, 2: connected)mock: Boolean indicating if mock mode is enabled
connect(): Initiates connection to the device (or loads mock data in mock mode)disconnect(): Disconnects from the device (or stops mock data stream)
Starts recording EEG data.
Stops recording and returns processed data:
const data = await stopRecording();
// Returns:
{
rawEEG: number[][], // Raw EEG data
spectraData: number[][], // Power spectra
powerData: object[], // Power by frequency band
alphaData: number[] // Alpha band power
}React context provider for EEG functionality.
<EEGProvider>
<App />
</EEGProvider>React hook for accessing EEG functionality.
const {
muse, // Muse instance
isConnected, // Connection status
isMockData, // Whether using mock data
rawEEG, // Latest EEG readings
connectMuse, // Function to connect to Muse
connectMockData, // Function to use mock data
disconnectEEG, // Function to disconnect
startRecording, // Start recording function
stopRecording, // Stop recording function
} = useEEG();The library processes EEG data into the following frequency bands:
- Delta: 0.5-4 Hz
- Theta: 4-8 Hz
- Alpha: 8-13 Hz
- Beta: 13-30 Hz
- Gamma: 30-100 Hz
- Raw data collection (256 Hz sampling rate)
- Signal filtering and artifact removal
- Power spectrum calculation using periodogram method
- Frequency band power extraction
- Real-time data streaming to application
Mock mode allows development and testing without a physical Muse device. When enabled, the library loads pre-recorded EEG data from a CSV file and streams it at the correct sample rate, looping continuously.
- No device required: Perfect for development and testing
- Realistic timing: Respects original timestamps from recordings
- Seamless API: Works identically to real device connection
- Custom data: Support for custom CSV files
The CSV file should follow this format:
Timestamp (ms),TP9 (left ear),AF7 (left forehead),AF8 (right forehead),TP10 (right ear)
5,-0.48828125,0,-0.48828125,-0.48828125
7,0,-0.48828125,-0.48828125,0
10,4.8828125,-0.48828125,2.44140625,3.90625
...Columns:
Timestamp (ms): Timestamp in millisecondsTP9: Left ear electrode dataAF7: Left forehead electrode dataAF8: Right forehead electrode dataTP10: Right ear electrode data
Data values should be in the range of approximately -1000 to 1000 (scaled EEG values).
Basic mock mode:
const muse = await connectMuse({ mock: true });
// Works just like real device!Custom mock data file:
const muse = await connectMuse({
mock: true,
mockDataPath: "/data/my-recording.csv",
});Switching between real and mock:
const isDevelopment = process.env.NODE_ENV === "development";
const muse = await connectMuse({ mock: isDevelopment });The library includes comprehensive error handling for:
- Bluetooth connection issues
- Data processing errors
- Device disconnection events
- Mock data loading errors
Errors can be caught using standard try-catch blocks:
try {
await connectMuse();
} catch (error) {
console.error("Connection error:", error);
}
// With mock mode
try {
await connectMuse({ mock: true });
} catch (error) {
console.error("Failed to load mock data:", error);
}