-
Notifications
You must be signed in to change notification settings - Fork 2
/
CPlay.cs
524 lines (482 loc) · 22 KB
/
CPlay.cs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
using System;
using System.Linq;
using System.Threading.Tasks;
using SDL2;
using System.Runtime.InteropServices;
using System.Collections;
using System.Windows.Forms;
using System.Threading;
using LSL;
using System.Xml;
using System.Globalization;
using System.Drawing;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace VisualStimuli
{
public class CPlay
{
private long TICKSPERSECONDS = TimeSpan.TicksPerSecond;
ArrayList m_listFlickers = new ArrayList();
string filepath; //indicate the environment path
public CPlay(string filepath)
{
this.filepath = filepath;
}
StreamOutlet outl;
public CPlay()
{
//lookup the position of the Application
filepath = Application.StartupPath;
// create stream info and outlet
StreamInfo inf = new StreamInfo("flickers_info", "Markers", 1, 0, channel_format_t.cf_string, "giu4h5600");
outl = new StreamOutlet(inf);
}
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public const UInt32 SWP_NOSIZE = 0x0001;
public const UInt32 SWP_NOMOVE = 0x0002;
public const UInt32 SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll")]
public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern int EnumDisplayDevices(string deviceName,int index,out DEVMODE devMode);
private static Dictionary<SDL.SDL_Keycode,List<ValueTuple<CFlicker,sequenceValue>>> SequenceKeyDict = new Dictionary<SDL.SDL_Keycode, List<ValueTuple<CFlicker, sequenceValue>>>();
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public System.Int16 dmSpecVersion;
public System.Int16 dmDriverVersion;
public System.Int16 dmSize;
public System.Int16 dmDriverExtra;
public System.Int32 dmFields;
public System.Int16 dmOrientation;
public System.Int16 dmPaperSize;
public System.Int16 dmPaperLength;
public System.Int16 dmPaperWidth;
public System.Int16 dmScale;
public System.Int16 dmCopies;
public System.Int16 dmDefaultSource;
public System.Int16 dmPrintQuality;
public System.Int16 dmColor;
public System.Int16 dmDuplex;
public System.Int16 dmYResolution;
public System.Int16 dmTTOption;
public System.Int16 dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public System.Int16 dmUnusedPadding;
public System.Int16 dmBitsPerPel;
public System.Int32 dmPelsWidth;
public System.Int32 dmPelsHeight;
public System.Int32 dmDisplayFlags;
public System.Int32 dmDisplayFrequency;
}
public enum Signal_Type //enum for reading the xml file
{
Sine = 1,
Root_Square = 3,
Square = 2,
Random = 0,
Maximum_Lenght_Sequence = 4,
None=5,
Custom=6
};
/// <summary>
/// Read a Xml file and add the flickers to the list of active flickers.
/// </summary>
/// <param name="filePath">Path to the xml file.</param>
public void Read_File(string filePath)
{
try {
sequenceValue loadSeq(XmlNode node)
{
double.TryParse(node.SelectSingleNode("value").InnerText, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double valueSeq);
Enum.TryParse<sequenceValue.type>(node.SelectSingleNode("Type").InnerText, out sequenceValue.type Type);
Enum.TryParse<sequenceValue.CondType>(node.SelectSingleNode("cond").InnerText, out sequenceValue.CondType cond);
sequenceValue seq= new sequenceValue(Type,cond,valueSeq);
foreach(XmlNode nodeSeq in node.SelectSingleNode("contained_sequence"))
{
seq.addSeq(loadSeq(nodeSeq));
}
return seq;
}
// Load the XML file into memory
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
// Get the root element of the document
XmlElement root = doc.DocumentElement;
//program instance for window creation
var instance=Marshal.GetHINSTANCE(this.GetType().Module);
int k = 0;
// Iterate over the child elements of the root
foreach (XmlNode node in root.ChildNodes)
{
try
{
int.TryParse(node.SelectSingleNode("X").InnerText, out int pos_x);
int.TryParse(node.SelectSingleNode("Y").InnerText, out int pos_y);
int.TryParse(node.SelectSingleNode("Width").InnerText, out int width);
int.TryParse(node.SelectSingleNode("Height").InnerText, out int height);
Enum.TryParse<Signal_Type>(node.SelectSingleNode("Type").InnerText, out Signal_Type type);
string name = node.SelectSingleNode("Name").InnerText;
double.TryParse(node.SelectSingleNode("Frequency").InnerText, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double freq); //culture info is necessary due to use of "," or "." for decimal number in different part of the world
double.TryParse(node.SelectSingleNode("Phase").InnerText, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double phase);
var C1Node = node.SelectSingleNode("color1");
Byte.TryParse(C1Node.SelectSingleNode("R").InnerText, out byte r1);
Byte.TryParse(C1Node.SelectSingleNode("G").InnerText, out byte g1);
Byte.TryParse(C1Node.SelectSingleNode("B").InnerText, out byte b1);
int.TryParse(node.SelectSingleNode("Opacity_Min").InnerText, out int a1);
int.TryParse(node.SelectSingleNode("Opacity_Max").InnerText, out int a2);
string image = string.Empty;
bool.TryParse(node.SelectSingleNode("IsImageFlicker").InnerText, out bool IsImage);
Console.WriteLine("parsed:{0} {1} {2} {3} ", pos_x, pos_y, width, height);
if (IsImage)
{
image = node.SelectSingleNode("image").InnerText;
}
//load the sequence
sequenceValue seq = null;
if (node.SelectSingleNode("sequence") != null)
{
seq=loadSeq(node.SelectSingleNode("sequence"));
}
//create a window and add the flickers to the list of flickers
CFlicker f;
if (seq != null)
{
CScreen screen = new CScreen(pos_x, pos_y, width, height, name + k.ToString(), false, r1, g1, b1, image,
instance);
f = new CFlicker(
name,
pos_x,
pos_y,
width,
height,
screen,
Color.FromArgb(255, r1, g1, b1), // color1 RGB
freq,
(int)Math.Round(a1 * 2.55), // alpha1
(int)Math.Round(a2 * 2.55), // alpha2
phase,
(int)type,
seq);
m_listFlickers.Add(f);
}
else
{
CScreen screen = new CScreen(pos_x, pos_y, width, height, name + k.ToString(), false, r1, g1, b1, image,
instance);
f = new CFlicker(
name,
pos_x,
pos_y,
width,
height,
screen,
Color.FromArgb(255, r1, g1, b1), // color1 RGB
freq,
(int)Math.Round(a1 * 2.55), // alpha1
(int)Math.Round(a2 * 2.55), // alpha2
phase,
(int)type);
m_listFlickers.Add(f);
}
if (type == Signal_Type.Custom) { f.force_data(node.SelectSingleNode("Code").InnerText); }
k++;
}
catch(Exception ex)
{
Console.WriteLine("Failed to create a flicker" + ex.Message + "\n" + ex.StackTrace);
}
}
Console.WriteLine("Created {0} Flickers", m_listFlickers.Count);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
/// <summary>
/// Initiate Flickers
/// </summary>
/// <returns>None</returns>
public void init_test()
{
Read_File(filepath+"\\Flickers.xml");
}
/// <summary>
/// Get refresh rate of the screen
/// </summary>
/// <returns>Refresh rate of the screen</returns>
public static double getFrameRate()
{
/*DEVMODE devMode = new DEVMODE();*/
List<DEVMODE> l = getAllScreen();
double frequencyMonitor = 0;
for(int i=0;i<l.Count;i++)
{
DEVMODE devMode = l[i];
devMode.dmSize = (short)Marshal.SizeOf(devMode);
devMode.dmDriverExtra = 0;
EnumDisplaySettings(null, -1, ref devMode);
frequencyMonitor = frequencyMonitor > devMode.dmDisplayFrequency ? frequencyMonitor : devMode.dmDisplayFrequency;
}
return frequencyMonitor;
}
public static List<DEVMODE> getAllScreen()
{
int i = 0,a=1;
List<DEVMODE> l = new List<DEVMODE>();
while(a!=0){
DEVMODE devMode = new DEVMODE();
a=EnumDisplayDevices(null, i, out devMode);
l.Add(devMode);
i++;
}
return l ;
}
private static double frameRate = getFrameRate();
private bool quit = false;
/// <summary>
/// Animate the flickers from the .xml file
/// </summary>
/// <returns>None</returns>
public void Animate_Flicker()
{
init_test();
string[] marker_info = new string[4];
// All the flickers foreground
for (int j = 0; j < m_listFlickers.Count; j++) {
CFlicker aFlicker = (CFlicker)m_listFlickers[j];
SetWindowPos(aFlicker.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// init vars
for (int j = 0; j < m_listFlickers.Count; j++)
{
// Send into LSL the ID,frequency,phase and amplitude of the flickers
CFlicker currentFlicker = (CFlicker)m_listFlickers[j];
marker_info[0] = j.ToString();
marker_info[1] = currentFlicker.Frequency.ToString();
marker_info[2] = currentFlicker.Phase.ToString();
marker_info[3] = ((Math.Abs(currentFlicker.Alpha2-currentFlicker.Alpha1)).ToString());
// Push the marker in LSL that the flicker will flicker now
outl.push_sample(marker_info);
//send a second message contaning the "code" of the flicker (useful for mseq)
string[] marker_data = new string[currentFlicker.size];
for(int k = 0; k < currentFlicker.size; k++)
{
marker_data[k] = currentFlicker.Data[k].ToString();
}
/*outl.push_sample(marker_data);*/
}
long frame = 0;
var watch = System.Diagnostics.Stopwatch.StartNew(); //watch for fps syncing
int lost_frame = 0; //only used if computer is too slow to display all frames, we will jump over some frames
long frame_ticks =(long) ((1d / frameRate) * TICKSPERSECONDS);
SDL.SDL_Event evt = new SDL.SDL_Event();
// ANIMATION LOOP
while (!quit && SDL.SDL_GetTicks() < 1000000000)
{
frame += 1;
var watchFPSMax=System.Diagnostics.Stopwatch.StartNew();
//parallel treatment for each flickers (useful when lots of flicker on slow computer with multiple proc core)
//flickers are still synchronized, only parallelized during 1 frame.
Parallel.ForEach<CFlicker>(m_listFlickers.Cast<CFlicker>(), c =>
{
if (c.index >= c.size) { c.index = 0; }
//check if this flicker is to be shown
// TODO: fasten this process with use of sorting or not checking when we are active and before endTime of activity
if (c.seq!=null)
{
if(c.sequenceDict.Count> 0)
{
var currentSeq = c.getSeq(c.sequenceDict.Count - 1);
var key = (sequenceValue)currentSeq.Key;
var timeStamp = (double)currentSeq.Value;
sequenceValue newSeq;
if (key.cond == sequenceValue.CondType.Always || key.cond == sequenceValue.CondType.None || key.Type == sequenceValue.type.Block || key.Type == sequenceValue.type.Loop)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
//Check Times
else
{
if (key.cond == sequenceValue.CondType.Time)
{
if (watch.Elapsed.TotalSeconds - timeStamp > key.value)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
else
{
newSeq = key;
}
}
else
{
newSeq = key;
}
}
if (newSeq.cond == sequenceValue.CondType.KeyPress)
{
var keyboardKey = (SDL.SDL_Keycode)(int)newSeq.value;
if (SequenceKeyDict.Keys.Contains(keyboardKey))
{
var l = SequenceKeyDict[keyboardKey];
if (!l.Contains((c,newSeq)))
{
l.Add((c, newSeq));
}
}
else
{
SequenceKeyDict[keyboardKey] = new List<(CFlicker, sequenceValue)>() { (c, newSeq) };
}
}
string[] marker_info_act = new string[2];
marker_info_act[0] = c.ToString();
if (newSeq.Type == sequenceValue.type.Active)
{
c.isActive= true;
marker_info_act[1] = "Active";
outl.push_sample(marker_info_act);
}
if (newSeq.Type == sequenceValue.type.Inactive)
{
c.isActive= false;
marker_info_act[1] = "Inactive";
outl.push_sample(marker_info_act);
}
}
else
{
c.nextSeq(watch.Elapsed.TotalSeconds);
}
}
if (c.isActive)
{
c.display();
c.index += 1 + lost_frame;
}
else
{
//full transparancy when inactive
c.Screen.show(0);
}
}
);
watchFPSMax.Stop();
if (SDL.SDL_PollEvent(out evt) != 0)
{
if(evt.type == SDL.SDL_EventType.SDL_KEYUP)
{
if (evt.key.keysym.sym == SDL.SDL_Keycode.SDLK_ESCAPE)
{
quit = true;
}
else
{
quit = false;
// will check if any flicker is waiting for a key input
if(SequenceKeyDict.Count> 0)
{
for(int keyIndex =0;keyIndex<SequenceKeyDict.Keys.Count;keyIndex++)
{
var key = (SequenceKeyDict.Keys.ToList())[keyIndex];
Console.WriteLine("checking:{0} received:{1}",key, evt.key.keysym.sym);
//there might be multiple flicker bound to the same key at the same time
if(key== evt.key.keysym.sym)
{
try
{
var linkedSeqs = SequenceKeyDict[key];
while (linkedSeqs.Count > 0)
{
//can be replaced with a stack
var tuple = linkedSeqs[0];
var c=tuple.Item1.nextSeq(tuple.Item2, watch.Elapsed.TotalSeconds);
var b=linkedSeqs.Remove(tuple);
//remove that flicker from the list
if (linkedSeqs.Count == 0)
{
SequenceKeyDict.Remove(key);
}
}
}
catch
{
Console.WriteLine("Failed KeyPress event");
}
}
}
}
}
}
}
// Remaining time for the frame after the display of all the flickers with the paralell loop
var timeleft = frame_ticks - watchFPSMax.ElapsedTicks;
//Console.WriteLine("Time rendering: {0} ms Total Time: {1} ms",watchFPSMax.ElapsedTicks/10000d, (timeleft + watchFPSMax.ElapsedTicks) / 10000d);
Console.WriteLine("frame {0}: watch {1} ms left: {2} ms\nEstimated FPS: {3}\nEstimated Max Fps: {4}", frame,watch.ElapsedTicks/10000d,timeleft/10000d,1000d/((timeleft+watchFPSMax.ElapsedTicks)/10000d),10000000d/watchFPSMax.ElapsedTicks);
// Wait until the full elapsed time for a frame
if (timeleft > 0L) {
if (timeleft > frame_ticks) { timeleft= frame_ticks; }
Thread.Sleep((int)(timeleft / (TICKSPERSECONDS/1000d)));
lost_frame= 0;
}
else
{
//# TODO: jump over frames in case of slow down, useful with sine flickers, breaks cVEP, but a slow down already breaks them anyway
Console.WriteLine("Warning Rendering can't keep up!! max FPS: {0}",frame*10000000d/watch.ElapsedTicks);
//left= Math.Abs(left);
//lost_frame = Convert.ToInt32(left / frame_ticks)+1;
//left -= (lost_frame-1) * frame_ticks;
}
//Console.WriteLine("Sleeped for: {0} ms, Asked: {1} ms",watchFPSMax.ElapsedMilliseconds,left/10000d);
//Console.WriteLine("Current frame rate: {0} -> frameTicks : {1} ms", frameRate,frame_ticks/(TICKSPERSECONDS/1000d));
}
//Kill all Flickers Windows
Console.WriteLine("Killing all Flickers");
Parallel.ForEach<CFlicker>(m_listFlickers.Cast<CFlicker>(), c =>
{
c.isActive = false;
c.Destroy();
});
SDL.SDL_Quit();
}
public void Close()
{
Environment.Exit(0);
}
public void Animate_Flicker(double time)
{
System.Timers.Timer timer = new System.Timers.Timer(time*1000)
{
AutoReset = false
};
timer.Elapsed += OnElapsed;
timer.Start();
void OnElapsed(object sender1, EventArgs e1)
{
quit=true;
}
try
{
Animate_Flicker();
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
Parallel.ForEach<CFlicker>(m_listFlickers.Cast<CFlicker>(), c =>
{
c.isActive = false;
c.Destroy();
});
SDL.SDL_Quit();
}
}
}
}