labstreaminglayer / pylsl

Python bindings (pylsl) for liblsl
MIT License
142 stars 58 forks source link

StreamInlet always buffers 1024 samples, no matter what! #82

Closed mscheltienne closed 4 months ago

mscheltienne commented 4 months ago

While adding some functionalities to the base objects in mne-lsl, I got this surprising behavior. No matter the argument max_buflen in a StreamInlet, or the stream sampling rate, I'm always getting 1024 samples!

MWE:

import multiprocessing as mp
import time

import numpy as np
from pylsl import StreamInfo, StreamInlet, StreamOutlet, resolve_streams

def mock_stream(status):
    sinfo = StreamInfo("my_stream", "EEG", 8, 1000)
    outlet = StreamOutlet(sinfo, chunk_size=10)
    rng = np.random.default_rng()
    status.value = 1  # ready to start
    while status.value == 1:
        outlet.push_chunk(rng.integers(low=0, high=10, size=(10, 8)).astype(np.float32))
        time.sleep(10 / 1000)

if __name__ == "__main__":
    status = mp.Value("i", 0)
    process = mp.Process(target=mock_stream, args=(status,))
    process.start()
    while status.value == 0:
        pass  # wait for the stream to start

    sinfos = resolve_streams()
    inlet = StreamInlet(sinfos[0], max_buflen=5)
    inlet.open_stream()
    time.sleep(2)  # whatever
    data, ts = inlet.pull_chunk()
    assert len(ts) == 1024

    time.sleep(3)  # whatever
    data, ts = inlet.pull_chunk()
    assert len(ts) == 1024

    # stop
    status.value = 0
    process.join()

Today I'm on Windows, I hope this is not a platform specific problem. @cboulay Do you spot anything wrong with my MWE or do you have any idea as to what might be happening here?

mscheltienne commented 4 months ago

Looks like I'm missing the pull_chunk argument controlling the maximum number of samples returned..