-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathBaseOutlet.cs
More file actions
205 lines (170 loc) · 6 KB
/
BaseOutlet.cs
File metadata and controls
205 lines (170 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LSL;
namespace LSL4Unity.Utils
{
public abstract class ABaseOutlet<TData> : MonoBehaviour
{
public string StreamName;
public string StreamType;
public MomentForSampling moment;
public bool IrregularRate = false;
private bool UniqueFromInstanceId = true;
public abstract List<string> ChannelNames { get; }
public virtual int ChannelCount
{
get { return ChannelNames.Count; }
}
public abstract channel_format_t Format { get; }
protected StreamOutlet outlet;
protected TData[] sample;
// Add an XML element for each channel. The automatic version adds only channel labels. Override to add unit, location, etc.
protected virtual void FillChannelsHeader(XMLElement channels)
{
foreach (var chanName in ChannelNames)
{
XMLElement chan = channels.append_child("channel");
chan.append_child_value("label", chanName);
}
}
protected virtual void Start()
{
// TODO: Check StreamName, StreamType, and moment are not null.
sample = new TData[ChannelCount];
var hash = new Hash128();
hash.Append(StreamName);
hash.Append(StreamType);
hash.Append(moment.ToString());
if (UniqueFromInstanceId)
hash.Append(gameObject.GetInstanceID());
ExtendHash(hash);
double dataRate = IrregularRate ? LSL.LSL.IRREGULAR_RATE : LSLCommon.GetSamplingRateFor(moment);
StreamInfo streamInfo =
new StreamInfo(StreamName, StreamType, ChannelCount, dataRate, Format, hash.ToString());
// Build XML header. See xdf wiki for recommendations: https://github.com/sccn/xdf/wiki/Meta-Data
XMLElement acq_el = streamInfo.desc().append_child("acquisition");
acq_el.append_child_value("manufacturer", "LSL4Unity");
XMLElement channels = streamInfo.desc().append_child("channels");
FillChannelsHeader(channels);
outlet = new StreamOutlet(streamInfo);
}
// Override to extend hash
protected virtual void ExtendHash(Hash128 hash)
{
}
protected abstract bool BuildSample();
protected abstract void pushSample(double timestamp = 0);
// Update is called once per frame
protected virtual void FixedUpdate()
{
if (moment != MomentForSampling.FixedUpdate || outlet == null) return;
if (!BuildSample()) return;
pushSample(TimeSync.Instance ? TimeSync.Instance.FixedUpdateTimestamp : 0.0);
}
protected virtual void Update()
{
if (outlet == null) return;
if (!BuildSample()) return;
if (moment == MomentForSampling.Update)
{
pushSample(TimeSync.Instance ? TimeSync.Instance.UpdateTimestamp : 0.0);
}
else if (moment == MomentForSampling.EndOfFrame)
{
// Send as close to render-time as possible. Use this to log stimulus events.
StartCoroutine(PushAfterRendered());
}
}
protected virtual void LateUpdate()
{
if (moment != MomentForSampling.LateUpdate || outlet == null) return;
if (!BuildSample()) return;
pushSample(TimeSync.Instance ? TimeSync.Instance.LateUpdateTimestamp : 0.0);
}
IEnumerator PushAfterRendered()
{
yield return new WaitForEndOfFrame();
pushSample();
yield return null;
}
}
public abstract class AFloatOutlet : ABaseOutlet<float>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_float32; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
public abstract class ADoubleOutlet : ABaseOutlet<double>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_double64; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
public abstract class AIntOutlet : ABaseOutlet<int>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_int32; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
public abstract class ACharOutlet : ABaseOutlet<char>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_int8; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
public abstract class AStringOutlet : ABaseOutlet<string>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_string; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
public abstract class AShortOutlet : ABaseOutlet<short>
{
public override channel_format_t Format
{
get { return channel_format_t.cf_int16; }
}
protected override void pushSample(double timestamp = 0)
{
if (outlet == null)
return;
outlet.push_sample(sample, timestamp);
}
}
}