|
| 1 | +classdef lsl_inlet < handle |
| 2 | + % A stream inlet. |
| 3 | + % Inlets are used to receive streaming data (and meta-data) from the lab network. |
| 4 | + % |
| 5 | + % Note: |
| 6 | + % Most functions of the inlet can throw an error of type 'lsl:lost_error' if the stream |
| 7 | + % read by this inlet has been lost irretrievably. Those functions that take a timeout can in |
| 8 | + % addition throw an error of type 'lsl:timeout_error'. |
| 9 | + |
| 10 | + properties (Hidden) |
| 11 | + LibHandle = 0; % this is a handle to the liblsl library (to call its functions) |
| 12 | + InletHandle = 0; % this is a handle to an lsl_inlet object within the library. |
| 13 | + ChannelCount = 0; % copy of the inlet's channel count |
| 14 | + IsString = 0; % whether this is a string-formatted inlet |
| 15 | + end |
| 16 | + |
| 17 | + methods |
| 18 | + |
| 19 | + function self = lsl_inlet(info, maxbuffered, chunksize, recover) |
| 20 | + % Inlet = lsl_inlet(Streaminfo, MaxBuffered, ChunkSize, Recover) |
| 21 | + % Construct a new stream inlet from a resolved stream info. |
| 22 | + % |
| 23 | + % In: |
| 24 | + % Streaminfo : A resolved stream info object (as coming from one of the lsl_resolve_* functions). |
| 25 | + % |
| 26 | + % MaxBuffered : Optionally the maximum amount of data to buffer (in seconds if there is a nominal |
| 27 | + % sampling rate, otherwise x100 in samples). Recording applications want to use a fairly |
| 28 | + % large buffer size here, while real-time applications would only buffer as much as |
| 29 | + % they need to perform their next calculation. (default: 360) |
| 30 | + % |
| 31 | + % ChunkSize : Optionally the maximum size, in samples, at which chunks are transmitted |
| 32 | + % (0 corresponds to the chunk sizes used by the sender). |
| 33 | + % Recording applications can use a generous size here (leaving it to the network how |
| 34 | + % to pack things), while real-time applications may want a finer (perhaps 1-sample) granularity. |
| 35 | + % (default 0) |
| 36 | + % |
| 37 | + % Recover : Try to silently recover lost streams that are recoverable (=those that that have a source_id set). |
| 38 | + % In all other cases (recover is false or the stream is not recoverable) functions may throw a |
| 39 | + % lost_error if the stream's source is lost (e.g., due to an app or computer crash). |
| 40 | + % (default: 1) |
| 41 | + |
| 42 | + if ~exist('maxbuffered','var') || isempty(maxbuffered) maxbuffered = 360; end |
| 43 | + if ~exist('chunksize','var') || isempty(chunksize) chunksize = 0; end |
| 44 | + if ~exist('recover','var') || isempty(recover) recover = 1; end |
| 45 | + self.LibHandle = info.LibHandle; |
| 46 | + self.ChannelCount = info.channel_count(); |
| 47 | + self.IsString = strcmp(info.channel_format(),'cf_string'); |
| 48 | + self.InletHandle = lsl_create_inlet(info.LibHandle,info.InfoHandle,maxbuffered,chunksize,recover); |
| 49 | + end |
| 50 | + |
| 51 | + |
| 52 | + function delete(self) |
| 53 | + % Destroy the inlet when it is deleted. |
| 54 | + % The inlet will automatically disconnect if destroyed. |
| 55 | + |
| 56 | + lsl_destroy_inlet(self.LibHandle,self.InletHandle); |
| 57 | + end |
| 58 | + |
| 59 | + |
| 60 | + function result = info(self, timeout) |
| 61 | + % Retrieve the complete information of the given stream, including the extended description. |
| 62 | + % Fullinfo = info(Timeout) |
| 63 | + % |
| 64 | + % Can be invoked at any point of the stream's lifetime. |
| 65 | + % |
| 66 | + % In: |
| 67 | + % Timeout : Timeout of the operation, in seconds (default: 60). |
| 68 | + % |
| 69 | + % Out: |
| 70 | + % Fullinfo : the full information, including description field, of the stream. |
| 71 | + |
| 72 | + if ~exist('timeout','var') || isempty(timeout) timeout = 60; end |
| 73 | + result = lsl_streaminfo(self.LibHandle,lsl_get_fullinfo(self.LibHandle, self.InletHandle, timeout)); |
| 74 | + end |
| 75 | + |
| 76 | + |
| 77 | + function open_stream(self, timeout) |
| 78 | + % Subscribe to the data stream from this moment onwards. |
| 79 | + % open_stream(Timeout) |
| 80 | + % |
| 81 | + % All samples pushed in at the other end from this moment onwards will be queued and |
| 82 | + % eventually be delivered in response to pull_sample() or pull_chunk() calls. |
| 83 | + % Pulling a sample without some preceding open_stream is permitted (the stream will then |
| 84 | + % be opened implicitly). |
| 85 | + % |
| 86 | + % In: |
| 87 | + % Timeout : Timeout of the operation (default: 60). |
| 88 | + % |
| 89 | + % Out: |
| 90 | + % Fullinfo : the full information, including description field, of the stream. |
| 91 | + |
| 92 | + if ~exist('timeout','var') || isempty(timeout) timeout = 60; end |
| 93 | + lsl_open_stream(self.LibHandle, self.InletHandle, timeout); |
| 94 | + end |
| 95 | + |
| 96 | + |
| 97 | + function close_stream(self) |
| 98 | + % Drop the current data stream. |
| 99 | + % close_stream() |
| 100 | + % |
| 101 | + % All samples that are still buffered or in flight will be dropped and the source will halt its buffering of data for this inlet. |
| 102 | + % If an application stops being interested in data from a source (temporarily or not) but keeps the outlet alive, it should |
| 103 | + % call close_streams() to not pressure the source outlet to buffer unnecessarily large amounts of data. |
| 104 | + |
| 105 | + lsl_close_stream(self.LibHandle, self.InletHandle); |
| 106 | + end |
| 107 | + |
| 108 | + |
| 109 | + function result = time_correction(self,timeout) |
| 110 | + % Retrieve an estimated time correction offset for the given stream. |
| 111 | + % |
| 112 | + % The first call to this function takes several miliseconds until a reliable first estimate is obtained. |
| 113 | + % Subsequent calls are instantaneous (and rely on periodic background updates). |
| 114 | + % The precision of these estimates should be below 1 ms (empirically within +/-0.2 ms). |
| 115 | + % |
| 116 | + % In: |
| 117 | + % Timeout : Timeout to acquire the first time-correction estimate (default: 60). |
| 118 | + % |
| 119 | + % Out: |
| 120 | + % Offset : The time correction estimate. If the first estimate cannot within the alloted time, |
| 121 | + % the result is NaN. |
| 122 | + |
| 123 | + if ~exist('timeout','var') || isempty(timeout) timeout = 60; end |
| 124 | + result = lsl_time_correction(self.LibHandle, self.InletHandle,timeout); |
| 125 | + end |
| 126 | + |
| 127 | + |
| 128 | + function [data,timestamp] = pull_sample(self,timeout,binary_blobs) |
| 129 | + % Pull a sample from the inlet and read it into an array of values. |
| 130 | + % [SampleData,Timestamp] = pull_sample(Timeout) |
| 131 | + % |
| 132 | + % In: |
| 133 | + % Timeout : The timeout for this operation, if any. (default: 60) |
| 134 | + % Use 0 to make the function non-blocking. |
| 135 | + % |
| 136 | + % Out: |
| 137 | + % SampleData : The sample's contents. This is either a numeric vector (type: double) if |
| 138 | + % the stream holds numeric contents, or a cell array of strings if the stream |
| 139 | + % is string-formatted, or a cell array of uint8 vectors if BinaryBlobs is set |
| 140 | + % to true. |
| 141 | + % |
| 142 | + % Note: this is empty if the operation times out. |
| 143 | + % |
| 144 | + % Timeout : Timeout for the operation. (default: 60) |
| 145 | + % |
| 146 | + % BinaryBlobs : If true, strings will be received as uint8 |
| 147 | + % arrays that may contain the \0 character. |
| 148 | + % Otherwise strings will be received as char, |
| 149 | + % which cannot contain \0's. (default: false) |
| 150 | + % |
| 151 | + % Notes: |
| 152 | + % It is not a good idea to set the timeout to a very large value since MATLAB would |
| 153 | + % otherwise become unresponsive - better call pull_sample in a loop until it succeeds. |
| 154 | + |
| 155 | + if ~exist('timeout','var') || isempty(timeout) timeout = 60; end |
| 156 | + if self.IsString |
| 157 | + if ~exist('binary_blobs','var') || isempty(binary_blobs) binary_blobs = false; end |
| 158 | + if binary_blobs |
| 159 | + [data,timestamp] = lsl_pull_sample_buf(self.LibHandle,self.InletHandle,self.ChannelCount,timeout); |
| 160 | + else |
| 161 | + [data,timestamp] = lsl_pull_sample_str(self.LibHandle,self.InletHandle,self.ChannelCount,timeout); |
| 162 | + end |
| 163 | + else |
| 164 | + [data,timestamp] = lsl_pull_sample_d(self.LibHandle,self.InletHandle,self.ChannelCount,timeout); |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + |
| 169 | + function [chunk,timestamps] = pull_chunk(self) |
| 170 | + % Pull a chunk of numeric samples and their timestamps from the inlet. |
| 171 | + % [ChunkData,Timestamps] = pull_chunk() |
| 172 | + % |
| 173 | + % This function obtains a chunk of data from the inlet; the chunk contains all samples |
| 174 | + % that have become available since the last chunk/sample was requested. Note that the |
| 175 | + % result may be empty. For each returned sample there is also a timestamp being |
| 176 | + % returned. |
| 177 | + % |
| 178 | + % Out: |
| 179 | + % ChunkData : The chunk contents; this is a MxN matrix with one column per returned |
| 180 | + % sample (and as many rows as the stream has channels). |
| 181 | + % |
| 182 | + % Timestamps : A vector of timestamps for the returned samples. |
| 183 | + |
| 184 | + [chunk,timestamps] = lsl_pull_chunk_d(self.LibHandle,self.InletHandle,self.ChannelCount); |
| 185 | + end |
| 186 | + |
| 187 | + function h = get_libhandle(self) |
| 188 | + % get the library handle (e.g., to query the clock) |
| 189 | + h = self.LibHandle; |
| 190 | + end |
| 191 | + end |
| 192 | +end |
0 commit comments