alexandrebarachant / muse-lsl

Python script to stream EEG data from the muse 2016 headset
BSD 3-Clause "New" or "Revised" License
621 stars 182 forks source link

add a new record method to return collected data instead of storing it into disks #84

Open thautwarm opened 5 years ago

thautwarm commented 5 years ago

I'm developing a real-time application based on muse-lsl, which asks for such an interface with regards of convenience and functionalities:

def record_to_memory(duration, dejitter=False):
   ...
data = record_to_memory(10)
# do something with `data`

Exchanging data with disks heavily hurts the performance.

thautwarm commented 5 years ago

I've done this in my own package but I think it better to add this functionality into upstream.

from pylsl import StreamInlet, resolve_byprop
from muselsl.constants import LSL_SCAN_TIMEOUT, LSL_CHUNK
__all__ = ['record_stream']

def record_stream():
    """
    :return: a generator to fetch EEG data from existed stream.
    """
    streams = resolve_byprop("type", "EEG", timeout=LSL_SCAN_TIMEOUT)
    if len(streams) == 0:
        raise IOError("Can't find EEG stream.")

    inlet = StreamInlet(streams[0], max_buflen=LSL_CHUNK)

    info = inlet.info()
    description = info.desc()
    Nchan = info.channel_count()

    ch = description.child('channels').first_child()
    ch_names = [ch.child_value("label")]
    for i in range(1, Nchan):
        ch = ch.next_sibling()
        ch_names.append(ch.child_value('label'))

    while True:
        yield inlet.pull_chunk(timeout=1.0, max_samples=LSL_CHUNK)
zzxxyy123456 commented 7 months ago

I also want to develop a real-time project,can you share your complete code