-
Notifications
You must be signed in to change notification settings - Fork 2
/
fig_collect.m
941 lines (898 loc) · 38.1 KB
/
fig_collect.m
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
function fig_collect
%FIG_COLLECT Window for the collection of ratings
% License: https://github.com/jmgirard/DARMA/blob/master/LICENSE.txt
% Get default settings
handles.settings = getpref('darma');
% Create and center main window
defaultBackground = get(0,'defaultUicontrolBackgroundColor');
handles.figure_collect = figure( ...
'Name','DARMA: Collect Ratings', ...
'MenuBar','none', ...
'ToolBar','none', ...
'NumberTitle','off', ...
'Visible','off', ...
'Color',defaultBackground, ...
'ResizeFcn',@figure_collect_Resize, ...
'KeyPressFcn',@figure_collect_KeyPress, ...
'CloseRequestFcn',@figure_collect_CloseReq);
% Create menu bar elements
handles.menu_media = uimenu(handles.figure_collect, ...
'Text','Media');
handles.menu_openmedia = uimenu(handles.menu_media, ...
'Text','Open Media File', ...
'Callback',@menu_openmedia_Callback);
handles.menu_volume = uimenu(handles.menu_media, ...
'Text','Adjust Volume', ...
'Callback',@menu_volume_Callback);
handles.menu_preview = uimenu(handles.menu_media, ...
'Text','Preview Media File', ...
'Enable','off', ...
'Callback',@menu_preview_Callback);
handles.menu_closemedia = uimenu(handles.menu_media, ...
'Text','Close Media File', ...
'Enable','off', ...
'Callback',@menu_closemedia_Callback);
handles.menu_settings = uimenu(handles.figure_collect, ...
'Text','Settings');
handles.menu_axislabels = uimenu(handles.menu_settings, ...
'Text','Set Axis Labels', ...
'Callback',@menu_axislabels_Callback);
handles.menu_magnitude = uimenu(handles.menu_settings, ...
'Text','Set Axis Magnitude', ...
'Callback',@menu_magnitude_Callback);
handles.menu_samplerate = uimenu(handles.menu_settings, ...
'Text','Set Sampling Rate', ...
'Callback',@menu_srate_Callback);
handles.menu_binsize = uimenu(handles.menu_settings, ...
'Text','Set Bin Size', ...
'Callback',@menu_binsize_Callback);
handles.menu_defaultdir = uimenu(handles.menu_settings, ...
'Text','Set Default Folder', ...
'Callback',@menu_defaultdir_Callback);
handles.menu_logging = uimenu(handles.menu_settings, ...
'Text','Create log files', ...
'Checked','off', ...
'Callback',@menu_logging_Callback);
handles.menu_help = uimenu(handles.figure_collect, ...
'Text','Help');
handles.menu_about = uimenu(handles.menu_help, ...
'Text','About', ...
'Callback',@menu_about_Callback);
handles.menu_document = uimenu(handles.menu_help, ...
'Text','Documentation', ...
'Callback',@menu_document_Callback);
handles.menu_report = uimenu(handles.menu_help, ...
'Text','Report Issues', ...
'Callback',@menu_report_Callback);
% Set minimum size
set(handles.figure_collect,'Units','normalized','Position',[0.1,0.1,0.8,0.8],'Visible','on');
drawnow;
warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
jFig = get(handle(handles.figure_collect),'JavaFrame');
jClient = jFig.fHG2Client;
jWindow = jClient.getWindow;
jWindow.setMinimumSize(java.awt.Dimension(1024,768));
% Create uicontrol elements
handles.axis_info = axes(handles.figure_collect, ...
'Units','normalized', ...
'Position',[.01 .02 .63 .05], ...
'XLim',[0,100],'YLim',[0,1], ...
'XTick',(0:10:100),'TickLength',[0.005 0],'XTickLabel',[], ...
'Box','on','Layer','top','YTick',[]);
handles.timebar = rectangle(handles.axis_info, ...
'Position',[0,0,0,1],'FaceColor',[0.000,0.447,0.741]);
handles.text_filename = text(handles.axis_info, ...
50,0.5,'Media Filename','HorizontalAlignment','center','Interpreter','none');
handles.text_timestamp = text(handles.axis_info, ...
05,0.5,'00:00:00','HorizontalAlignment','center');
handles.text_duration = text(handles.axis_info, ...
95,0.5,'00:00:00','HorizontalAlignment','center');
pos = getpixelposition(handles.figure_collect);
handles.table_info = uitable(handles.figure_collect, ...
'Units','normalized', ...
'Position',[.65 .02 .22 .05], ...
'RowName',[],'ColumnName',{'Bin Size','Axis Magnitude'}, ...
'ColumnWidth',num2cell(repmat(round(pos(3)*.22 / 2 - 2),1,2)),'FontSize',10, ...
'Data',{handles.settings.binsizenum,handles.settings.magnum});
jscrollpane = findjobj(handles.table_info);
jTable = jscrollpane.getViewport.getView;
cellStyle = jTable.getCellStyleAt(0,0);
cellStyle.setHorizontalAlignment(cellStyle.CENTER);
jTable.repaint;
handles.toggle_playpause = uicontrol(handles.figure_collect, ...
'Style','togglebutton', ...
'Units','Normalized', ...
'Position',[.88 .02 .11 .05], ...
'String','Begin Rating', ...
'FontSize',14.0, ...
'Callback',@toggle_playpause_Callback, ...
'Enable','off');
handles.axis_guide = axes(handles.figure_collect, ...
'Units','normalized', ...
'Position',[.01 .09 .63 .89], ...
'Box','on','XTick',[],'YTick',[],'Color','black');
handles.axis_circle = axes(handles.figure_collect, ...
'Units','normalized', ...
'OuterPosition',[.65 .09 .34 .89], ...
'LooseInset',[0 0 0 0], ...
'XLim',[-1,1],'YLim',[-1,1], ...
'NextPlot','add', ...
'Box','on','XTick',[],'YTick',[],'Layer','top', ...
'PlotBoxAspectRatioMode','manual','PlotBoxAspectRatio',[1 1 1]);
%axis square;
% Invoke and configure VLC ActiveX Controller
handles.vlc = actxcontrol('VideoLAN.VLCPlugin.2',getpixelposition(handles.axis_guide),handles.figure_collect);
handles.vlc.AutoPlay = 0;
handles.vlc.Toolbar = 0;
handles.vlc.FullscreenEnabled = 0;
try
handles.joy = vrjoystick(1);
catch
e = errordlg(sprintf('DARMA could not detect a USB joystick.\nPlease plug in a USB joystick and restart DARMA. Until then, you will be able to view the Collect Window but not collect ratings.'),'Error','modal');
set(handles.toggle_playpause,'Enable','off');
waitfor(e);
%close force;
%return;
end
% Create timer
handles.recording = 0;
handles.timer = timer(...
'ExecutionMode','fixedRate', ...
'Period',round(1/handles.settings.sratenum,3), ...
'TimerFcn',{@timer_Callback,handles}, ...
'ErrorFcn',{@timer_ErrorFcn,handles});
% Start system clock to improve VLC time stamp precision
global global_tic recording;
global_tic = tic;
recording = 0;
% Save handles to guidata
handles.figure_collect.Visible = 'on';
guidata(handles.figure_collect,handles);
create_axis(handles);
addpath('Functions');
start(handles.timer);
end
% =========================================================
function menu_openmedia_Callback(hObject,~)
handles = guidata(hObject);
% Reset the GUI elements
program_reset(handles);
global ratings last_ts_vlc last_ts_sys;
ratings = [];
last_ts_vlc = 0;
last_ts_sys = 0;
handles.vlc.playlist.items.clear();
% Browse for, load, and get text_duration for a media file
[video_name,video_path] = uigetfile({'*.*','All Files (*.*)'},'Select an audio or video file',handles.settings.defaultdir);
if video_name==0, return; end
try
MRL = fullfile(video_path,video_name);
handles.VID = MRL;
MRL(MRL=='\') = '/';
handles.MRL = sprintf('file://localhost/%s',MRL);
handles.vlc.playlist.add(handles.MRL);
handles.vlc.playlist.play();
while handles.vlc.input.state ~= 3
pause(0.001);
end
handles.vlc.playlist.togglePause();
handles.vlc.input.time = 0;
handles.dur = handles.vlc.input.length / 1000;
handles.vlc.audio.volume = 100;
if handles.dur == 0
handles.vlc.playlist.items.clear();
error('Could not read duration of media file. The file meta-data may be damaged. Transcoding the streams (e.g., with HandBrake) may fix this problem.');
end
catch err
msgbox(err.message,'Error loading media file.'); return;
end
% Update GUI elements
set(handles.timebar,'Position',[0 0 0 1]);
set(handles.text_timestamp,'String','00:00:00');
set(handles.text_filename,'String',video_name);
set(handles.text_duration,'String',datestr(handles.dur/24/3600,'HH:MM:SS'));
set(handles.menu_preview,'Enable','on');
set(handles.menu_closemedia,'Enable','on');
set(handles.toggle_playpause,'Enable','on');
guidata(hObject,handles);
end
% ===============================================================================
function menu_volume_Callback(hObject,~)
handles = guidata(hObject);
ovol = handles.vlc.audio.volume;
nvol = inputdlg(sprintf('Enter volume percentage:\n0=Mute, 100=Full Sound'),'',1,{num2str(ovol)});
nvol = str2double(nvol);
if isempty(nvol), return; end
if isnan(nvol), return; end
if nvol < 0, nvol = 0; end
if nvol > 100, nvol = 100; end
handles.vlc.audio.volume = nvol;
guidata(handles.figure_collect,handles);
end
% ===============================================================================
function menu_preview_Callback(hObject,~)
handles = guidata(hObject);
winopen(handles.VID);
end
% ===============================================================================
function menu_closemedia_Callback(hObject,~)
handles = guidata(hObject);
handles.vlc.playlist.stop();
handles.vlc.playlist.items.clear();
set(handles.menu_settings,'Enable','on');
set(handles.menu_closemedia,'Enable','off');
set(handles.menu_preview,'Enable','off');
set(handles.timebar,'Position',[0 0 0 1]);
set(handles.text_timestamp,'String','00:00:00');
set(handles.text_filename,'String','Media Filename');
set(handles.text_duration,'String','00:00:00');
guidata(handles.figure_collect,handles);
end
% ===============================================================================
function menu_axislabels_Callback(hObject,~)
handles = guidata(hObject);
settings = handles.settings;
stop(handles.timer);
d = dialog('Position',[0 0 500 480],'Name','Set Axis Labels','Visible','off');
movegui(d,'center');
set(d,'Visible','on');
uicontrol(d, ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[.10 .875 .25 .075], ...
'String','Interpersonal', ...
'FontSize',12, ...
'Callback',@push_interpersonal_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[.375 .875 .25 .075], ...
'String','Affective', ...
'FontSize',12, ...
'Callback',@push_affective_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[.65 .875 .25 .075], ...
'String','Clear', ...
'FontSize',12, ...
'Callback',@push_clear_Callback);
axes(d, ...
'Units','normalized', ...
'Position',[.10 .25 .80 .60], ...
'NextPlot','add', ...
'XTick',[],'YTick',[],'Box','on');
plot([-1,1],[0,0],'k');
plot([0,0],[-1,1],'k');
edit_label1 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.35 .75 .3 .075], ...
'String',settings.label1, ...
'FontSize',12);
edit_label2 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.175 .65 .3 .075], ...
'String',settings.label2, ...
'FontSize',12);
edit_label3 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.525 .65 .3 .075], ...
'String',settings.label3, ...
'FontSize',12);
edit_label4 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.125 .515 .3 .075], ...
'String',settings.label4, ...
'FontSize',12);
edit_label5 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.575 .515 .3 .075], ...
'String',settings.label5, ...
'FontSize',12);
edit_label6 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.175 .45 .3 .075], ...
'String',settings.label6, ...
'FontSize',12);
edit_label7 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.525 .45 .3 .075], ...
'String',settings.label7, ...
'FontSize',12);
edit_label8 = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.35 .275 .3 .075], ...
'String',settings.label8, ...
'FontSize',12);
align([edit_label8,edit_label7,edit_label5],'none','distribute');
align([edit_label5,edit_label3,edit_label1],'none','distribute');
align([edit_label8,edit_label6,edit_label4],'none','distribute');
align([edit_label4,edit_label2,edit_label1],'none','distribute');
uicontrol(d, ...
'Style','text', ...
'Units','normalized', ...
'Position',[.10 .135 .10 .075], ...
'HorizontalAlignment','Left', ...
'FontSize',12, ...
'String','X-Axis');
edit_labelX = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.20 .15 .29 .075], ...
'String',settings.labelX, ...
'FontSize',12);
edit_labelY = uicontrol(d, ...
'Style','edit', ...
'Units','normalized', ...
'Position',[.51 .15 .29 .075], ...
'String',settings.labelY, ...
'FontSize',12);
uicontrol(d, ...
'Style','text', ...
'Units','normalized', ...
'Position',[.80 .135 .10 .075], ...
'HorizontalAlignment','Right', ...
'FontSize',12, ...
'String','Y-Axis');
uicontrol(d, ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[.10 .05 .35 .075], ...
'String','Save as Default', ...
'FontSize',12, ...
'Callback',@push_save_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','normalized', ...
'Position',[.55 .05 .35 .075], ...
'String','Apply this Session', ...
'FontSize',12, ...
'Callback',@push_apply_Callback);
uiwait(d);
set(handles.l1,'String',settings.label1);
set(handles.l2,'String',settings.label2);
set(handles.l3,'String',settings.label3);
set(handles.l4,'String',settings.label4);
set(handles.l5,'String',settings.label5);
set(handles.l6,'String',settings.label6);
set(handles.l7,'String',settings.label7);
set(handles.l8,'String',settings.label8);
handles.settings = settings;
start(handles.timer);
guidata(handles.figure_collect,handles);
function push_interpersonal_Callback(~,~)
set(edit_labelX,'string','Communion');
set(edit_labelY,'string','Agency');
set(edit_label1,'string','Dominant');
set(edit_label2,'string','Disagreeable');
set(edit_label3,'string','Extraverted');
set(edit_label4,'string','Separate');
set(edit_label5,'string','Friendly');
set(edit_label6,'string','Introverted');
set(edit_label7,'string','Agreeable');
set(edit_label8,'string','Submissive');
end
function push_affective_Callback(~,~)
set(edit_labelX,'string','Valence');
set(edit_labelY,'string','Arousal');
set(edit_label1,'string','Activated');
set(edit_label2,'string','Stressed');
set(edit_label3,'string','Excited');
set(edit_label4,'string','Unpleasant');
set(edit_label5,'string','Pleasant');
set(edit_label6,'string','Depressed');
set(edit_label7,'string','Relaxed');
set(edit_label8,'string','Deactivated');
end
function push_clear_Callback(~,~)
set(edit_labelX,'string','');
set(edit_labelY,'string','');
set(edit_label1,'string','');
set(edit_label2,'string','');
set(edit_label3,'string','');
set(edit_label4,'string','');
set(edit_label5,'string','');
set(edit_label6,'string','');
set(edit_label7,'string','');
set(edit_label8,'string','');
end
function push_save_Callback(~,~)
settings.labelX = get(edit_labelX,'string');
settings.labelY = get(edit_labelY,'string');
settings.label1 = get(edit_label1,'string');
settings.label2 = get(edit_label2,'string');
settings.label3 = get(edit_label3,'string');
settings.label4 = get(edit_label4,'string');
settings.label5 = get(edit_label5,'string');
settings.label6 = get(edit_label6,'string');
settings.label7 = get(edit_label7,'string');
settings.label8 = get(edit_label8,'string');
setpref('darma', ...
{'labelX','labelY','label1','label2','label3','label4', ...
'label5','label6','label7','label8'}, ...
{settings.labelX, settings.labelY, settings.label1, ...
settings.label2, settings.label3, settings.label4, ...
settings.label5, settings.label6, settings.label7, ...
settings.label8});
delete(d);
msgbox(sprintf('Saved the current settings as the default settings.\nNext time DARMA is opened, these settings will be used.'));
end
function push_apply_Callback(~,~)
settings.labelX = get(edit_labelX,'string');
settings.labelY = get(edit_labelY,'string');
settings.label1 = get(edit_label1,'string');
settings.label2 = get(edit_label2,'string');
settings.label3 = get(edit_label3,'string');
settings.label4 = get(edit_label4,'string');
settings.label5 = get(edit_label5,'string');
settings.label6 = get(edit_label6,'string');
settings.label7 = get(edit_label7,'string');
settings.label8 = get(edit_label8,'string');
delete(d);
msgbox(sprintf('Applied the current settings for the current session.\nNext time DARMA is opened, these settings will be lost.'));
end
end
% ===============================================================================
function menu_magnitude_Callback(hObject,~)
handles = guidata(hObject);
settings = handles.settings;
d = dialog('Position',[0 0 500 200],'Name','Set Axis Magnitude','Visible','off');
movegui(d,'center');
set(d,'Visible','on');
uicontrol(d, ...
'Style','text', ...
'Units','Normalized', ...
'Position',[.10 .50 .80 .40], ...
'String','The Axis Magnitude determines the bipolar scaling of the output values. For instance, setting the magnitude to 100 will produce raing values ranging from -100 to 100. Select an axis magnitude below:');
popup_mag = uicontrol(d, ...
'Style','popup', ...
'Units','Normalized', ...
'Position',[.10 .35 .80 .20], ...
'String',{'1','10','100','1000'}, ...
'Value',settings.magval);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.10 .10 .30 .20], ...
'String','Save as Default', ...
'Callback',@push_save_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.60 .10 .30 .20], ...
'String','Apply Magnitude', ...
'Callback',@push_apply_Callback);
stop(handles.timer);
uiwait(d);
start(handles.timer);
handles.settings = settings;
guidata(handles.figure_collect,handles);
function push_save_Callback(~,~)
magval = popup_mag.Value;
magnum = popup_mag.String{magval};
magnum = str2double(magnum);
settings.magval = magval;
settings.magnum = magnum;
setpref('darma', ...
{'magval','magnum'}, ...
{settings.magval,settings.magnum});
handles.table_info.Data = ...
{settings.binsizenum,settings.magnum};
guidata(handles.figure_collect,handles);
delete(d);
drawnow();
msgbox(sprintf('Saved the current settings as the default settings.\nNext time DARMA is opened, these settings will be used.'));
end
function push_apply_Callback(~,~)
magval = popup_mag.Value;
magnum = popup_mag.String{magval};
magnum = str2double(magnum);
settings.magval = magval;
settings.magnum = magnum;
handles.table_info.Data = ...
{settings.binsizenum,settings.magnum};
guidata(handles.figure_collect,handles);
delete(d);
drawnow();
msgbox(sprintf('Applied the current settings for the current session.\nNext time DARMA is opened, these settings will be lost.'));
end
end
% ===============================================================================
function menu_srate_Callback(hObject,~)
handles = guidata(hObject);
settings = handles.settings;
d = dialog('Position',[0 0 500 200],'Name','Set Sampling Rate','Visible','off');
movegui(d,'center');
set(d,'Visible','on');
uicontrol(d, ...
'Style','text', ...
'Units','Normalized', ...
'Position',[.10 .50 .80 .40], ...
'String','DARMA can sample the joystick at different frequencies. Higher sampling rates provide more data redundancy but also impose a larger computational load. A sampling rate of 20 or 30 Hz is recommended for modern computers and 10 Hz is recommended for older or slower computers. Select a sampling rate below:');
popup_srate = uicontrol(d, ...
'Style','popup', ...
'Units','Normalized', ...
'Position',[.10 .35 .80 .20], ...
'String',{'10 Hz','20 Hz','30 Hz'}, ...
'Value',settings.srateval);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.10 .10 .30 .20], ...
'String','Save as Default', ...
'Callback',@push_save_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.60 .10 .30 .20], ...
'String','Apply this Session', ...
'Callback',@push_apply_Callback);
stop(handles.timer);
uiwait(d);
handles.settings = settings;
guidata(handles.figure_collect,handles);
start(handles.timer);
function push_save_Callback(~,~)
srateval = popup_srate.Value;
sratenum = popup_srate.String{binsizeval};
sratenum = str2double(sratenum(1,1:2));
settings.srateval = srateval;
settings.sratenum = sratenum;
setpref('darma', ...
{'srateval','sratenum'}, ...
{settings.srateval,settings.sratenum});
if handles.timer.Running, stop(handles.timer); end
set(handles.timer,'Period',round(1/settings.sratenum,3));
if ~handles.timer.Running, start(handles.timer); end
delete(d);
msgbox(sprintf('Saved the current settings as the default settings.\nNext time DARMA is opened, these settings will be used.'));
end
function push_apply_Callback(~,~)
srateval = popup_srate.Value;
sratenum = popup_srate.String{srateval};
sratenum = str2double(sratenum(1,1:2));
settings.srateval = srateval;
settings.sratenum = sratenum;
delete(d);
if handles.timer.Running, stop(handles.timer); end
set(handles.timer,'Period',round(1/settings.sratenum,3));
if ~handles.timer.Running, start(handles.timer); end
msgbox(sprintf('Applied the current settings for the current session.\nNext time DARMA is opened, these settings will be lost.'));
end
end
% ===============================================================================
function menu_binsize_Callback(hObject,~)
handles = guidata(hObject);
settings = handles.settings;
d = dialog('Position',[0 0 500 200],'Name','Set Bin Size','Visible','off');
movegui(d,'center');
set(d,'Visible','on');
uicontrol(d, ...
'Style','text', ...
'Units','Normalized', ...
'Position',[.10 .50 .80 .40], ...
'String','DARMA samples the joystick at 10, 20, or 30 Hz. Samples are then averaged into temporal bins which are output in an annotation file. Bin size determines how long each bin is and thus how many samples contribute to it. Select a bin size below:');
popup_bsize = uicontrol(d, ...
'Style','popup', ...
'Units','Normalized', ...
'Position',[.10 .35 .80 .20], ...
'String',{'0.25 sec','0.50 sec','1.00 sec','2.00 sec','4.00 sec'}, ...
'Value',settings.binsizeval);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.10 .10 .30 .20], ...
'String','Save as Default', ...
'Callback',@push_save_Callback);
uicontrol(d, ...
'Style','pushbutton', ...
'Units','Normalized', ...
'Position',[.60 .10 .30 .20], ...
'String','Apply this Session', ...
'Callback',@push_apply_Callback);
stop(handles.timer);
uiwait(d);
handles.settings = settings;
guidata(handles.figure_collect,handles);
start(handles.timer);
function push_save_Callback(~,~)
binsizeval = popup_bsize.Value;
binsizenum = popup_bsize.String{binsizeval};
binsizenum = str2double(binsizenum(1,1:4));
settings.binsizeval = binsizeval;
settings.binsizenum = binsizenum;
handles.table_info.Data = ...
{settings.binsizenum,settings.magnum};
guidata(handles.figure_collect,handles);
delete(d);
drawnow();
setpref('darma', ...
{'binsizeval','binsizenum'}, ...
{settings.binsizeval,settings.binsizenum});
msgbox(sprintf('Saved the current settings as the default settings.\nNext time DARMA is opened, these settings will be used.'));
end
function push_apply_Callback(~,~)
binsizeval = popup_bsize.Value;
binsizenum = popup_bsize.String{binsizeval};
binsizenum = str2double(binsizenum(1,1:4));
settings.binsizeval = binsizeval;
settings.binsizenum = binsizenum;
handles.table_info.Data = ...
{settings.binsizenum,settings.magnum};
guidata(handles.figure_collect,handles);
delete(d);
drawnow();
msgbox(sprintf('Applied the current settings for the current session.\nNext time DARMA is opened, these settings will be lost.'));
end
end
% ===============================================================================
function menu_defaultdir_Callback(hObject,~)
handles = guidata(hObject);
settings = handles.settings;
path = uigetdir(settings.defaultdir,'Select a new default folder:');
if isequal(path,0), return; end
settings.defaultdir = path;
setpref('darma', ...
{'defaultdir'}, ...
{settings.defaultdir});
handles.settings = settings;
guidata(handles.figure_collect,handles);
end
% ===============================================================================
function menu_logging_Callback(hObject,~)
handles = guidata(hObject);
log = get(handles.menu_logging,'Checked');
if strcmpi(log,'on')
set(handles.menu_logging,'Checked','off');
else
set(handles.menu_logging,'Checked','on');
end
guidata(handles.figure_collect,handles);
end
% ===============================================================================
function menu_about_Callback(~,~)
global version year;
msgbox(sprintf('DARMA version %.2f\nJeffrey M Girard (c) 2014-%d\nhttps://darma.jmgirard.com\nGNU General Public License v3',version,year),'About','Help');
end
% ===============================================================================
function menu_document_Callback(~,~)
web('https://github.com/jmgirard/DARMA/wiki','-browser');
end
% ===============================================================================
function menu_report_Callback(~,~)
web('https://github.com/jmgirard/DARMA/issues','-browser');
end
% ===============================================================================
function figure_collect_KeyPress(hObject,eventdata)
handles = guidata(hObject);
global recording;
% Escape if the playpause button is disabled
if strcmp(get(handles.toggle_playpause,'Enable'),'off'), return; end
% Pause playback if the pressed key is spacebar
if strcmp(eventdata.Key,'space') && get(handles.toggle_playpause,'Value')
handles.vlc.playlist.togglePause();
recording = 0;
set(handles.toggle_playpause,'String','Resume Rating','Value',0);
else
return;
end
guidata(hObject,handles);
end
% =========================================================
function toggle_playpause_Callback(hObject,~)
handles = guidata(hObject);
global recording;
if get(hObject,'Value')
% If toggle button is set to play, update GUI elements
set(hObject,'Enable','off','String','...');
set(handles.menu_media,'Enable','off');
set(handles.menu_settings,'Enable','off');
set(handles.menu_help,'Enable','off');
% Start three second countdown before starting
set(hObject,'String','...3...'); pause(1);
set(hObject,'String','..2..'); pause(1);
set(hObject,'String','.1.'); pause(1);
set(hObject,'Enable','On','String','Pause Rating');
recording = 1;
guidata(hObject,handles);
% Send play() command to VLC and wait for it to start playing
handles.vlc.playlist.play();
else
% If toggle button is set to pause, send pause() command to VLC
handles.vlc.playlist.togglePause();
recording = 0;
set(hObject,'String','Resume Rating','Value',0);
set(handles.menu_help,'Enable','on');
guidata(hObject,handles);
end
end
% =========================================================
function timer_Callback(~,~,handles)
handles = guidata(handles.figure_collect);
global ratings last_ts_vlc last_ts_sys global_tic marker recording;
% Before playing
if recording == 0
[a,b,~] = read(handles.joy);
x = a(1); y = a(2)*-1;
if b(1)==0, color = 'w'; else, color = 'y'; end
set(marker,'XData',x,'YData',y,'MarkerFace',color);
return;
end
% While playing
if handles.vlc.input.state == 3
try
% Read status of the joystick
[a,b,~] = read(handles.joy);
catch
%If failed, recreate the joystick
handles.joy = vrjoystick(1);
guidata(handles.figure_collect,handles);
return;
end
ts_vlc = handles.vlc.input.time/1000;
ts_sys = toc(global_tic);
if ts_vlc == last_ts_vlc && last_ts_vlc ~= 0
ts_diff = ts_sys - last_ts_sys;
ts_vlc = ts_vlc + ts_diff;
else
last_ts_vlc = ts_vlc;
last_ts_sys = ts_sys;
end
x = a(1); y = a(2)*-1;
if b(1)==0, color = 'r'; else, color = 'g'; end
set(marker,'XData',x,'YData',y,'MarkerFace',color);
ratings = [ratings; ts_vlc,x*handles.settings.magnum,y*handles.settings.magnum,b(1)];
set(handles.text_timestamp,'String',datestr(handles.vlc.input.time/1000/24/3600,'HH:MM:SS'));
frac = (ts_vlc / handles.dur) * 100;
set(handles.timebar,'Position',[0 0 frac 1]);
drawnow();
guidata(handles.figure_collect,handles);
% After playing
elseif handles.vlc.input.state == 5 || handles.vlc.input.state == 6
recording = 0;
handles.vlc.playlist.stop();
set(handles.toggle_playpause,'Value',0);
% Average ratings per second of playback
rating = ratings;
disp(rating);
if strcmpi(get(handles.menu_logging,'Checked'),'on')
writematrix(rating,fullfile(handles.settings.defaultdir,sprintf('log_%s.csv',datestr(now,30))));
end
anchors = [0,(handles.settings.binsizenum:handles.settings.binsizenum:floor(handles.dur))];
mean_ratings = nan(length(anchors)-1,4);
mean_ratings(:,1) = anchors(2:end)';
for i = 1:length(anchors)-1
s_start = anchors(i);
s_end = anchors(i+1);
index = (rating(:,1) >= s_start) & (rating(:,1) < s_end);
bin = rating(index,2:end);
if isempty(bin), continue; end
mean_ratings(i,:) = [s_end,nanmean(bin(:,1)),nanmean(bin(:,2)),nanmax(bin(:,3))];
end
% Prompt user to save the collected annotations
[~,defaultname,ext] = fileparts(handles.MRL);
[filename,pathname] = uiputfile({'*.csv','Comma-Separated Values (*.csv)'},'Save as',fullfile(handles.settings.defaultdir,defaultname));
if ~isequal(filename,0) && ~isequal(pathname,0)
% Add metadata to mean ratings and timestamps
output = [ ...
{'Time of Rating'},{datestr(now, 'yyyy-mm-dd HH:MM:SS')},{''},{''}; ...
{'Multimedia File'},{sprintf('%s%s',defaultname,ext)},{handles.dur},{''}; ...
{'Magnitude'},{handles.settings.magnum},{''},{''}; ...
{'Second'},{handles.settings.labelX},{handles.settings.labelY},{'Button'}; ...
{'%%%%%%'},{'%%%%%%'},{'%%%%%%'},{'%%%%%%'}; ...
num2cell(mean_ratings)];
output(cellfun(@(x) any(ismissing(x)), output)) = {''};
% Create export file
try
writecell(output,fullfile(pathname,filename), ...
'FileType','text','Delimiter','comma', ...
'QuoteStrings',true,'Encoding','UTF-8');
msgbox('Export successful.','Success');
catch err
errordlg(err.message,'Error saving');
end
end
program_reset(handles);
% While transitioning or paused
else
return;
end
end
% =========================================================
function timer_ErrorFcn(hObject,event,~)
disp(event.Data);
handles = guidata(hObject);
global ratings;
handles.vlc.playlist.togglePause();
stop(handles.timer);
msgbox(sprintf('Timer callback error:\n%s\nAn error log has been saved.',event.Data.message),'Error','error');
writematrix(ratings,fullfile(handles.settings.defaultdir,sprintf('log_%s.csv',datestr(now,30))));
guidata(handles.figure_collect,handles);
end
% =========================================================
function create_axis(handles)
handles = guidata(handles.figure_collect);
settings = handles.settings;
global marker;
axes(handles.axis_circle);
plot(handles.axis_circle,[-1,1],[0,0],'k-');
plot(handles.axis_circle,[0,0],[-1,1],'k-');
handles.l1 = text(0.0,0.9,settings.label1,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l2 = text(-0.64,0.64,settings.label2,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l3 = text(0.64,0.64,settings.label3,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l4 = text(-0.9,0.0,settings.label4,'HorizontalAlignment','left','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l5 = text(0.9,0.0,settings.label5,'HorizontalAlignment','right','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l6 = text(-0.64,-0.64,settings.label6,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l7 = text(0.64,-0.64,settings.label7,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
handles.l8 = text(0.0,-0.9,settings.label8,'HorizontalAlignment','center','BackgroundColor',[1 1 1],'FontSize',12,'Margin',5);
marker = plot(handles.axis_circle,0,0,'ko','LineWidth',2,'MarkerSize',15,'MarkerFaceColor','white');
guidata(handles.figure_collect,handles);
end
% =========================================================
function figure_collect_Resize(hObject,~)
handles = guidata(hObject);
if isfield(handles,'figure_collect')
% Force to remain above a minimum size
set(handles.toggle_playpause,'FontSize',14);
set(handles.table_info,'ColumnName',{'Bin Size','Axis Magnitude'});
% Update the size and position of the VLC controller
if isfield(handles,'vlc')
move(handles.vlc,getpixelposition(handles.axis_guide));
end
pos = getpixelposition(handles.figure_collect);
set(handles.table_info,'ColumnWidth',num2cell(repmat(round(pos(3)*.22 / 2 - 2),1,2)));
end
end
% =========================================================
function program_reset(handles)
global recording;
handles = guidata(handles.figure_collect);
recording = 0;
% Update GUI elements to starting configuration
set(handles.timebar,'Position',[0 0 0 1]);
set(handles.text_timestamp,'String','00:00:00');
set(handles.text_filename,'String','Media Filename');
set(handles.text_duration,'String','00:00:00');
set(handles.toggle_playpause,'Enable','off','String','Begin Rating');
set(handles.menu_media,'Enable','on');
set(handles.menu_settings,'Enable','on');
set(handles.menu_closemedia,'Enable','off');
set(handles.menu_preview,'Enable','off');
set(handles.menu_help,'Enable','on');
guidata(handles.figure_collect,handles);
end
% =========================================================
function figure_collect_CloseReq(hObject,~)
handles = guidata(hObject);
global recording;
% Pause playback and rating
if handles.vlc.input.state==3,handles.vlc.playlist.togglePause(); end
set(handles.toggle_playpause,'String','Resume Rating','Value',0);
recording = 0;
guidata(handles.figure_collect,handles);
if handles.vlc.input.state==4 || handles.vlc.input.state==3
%If ratings are being collected, prompt user to cancel them
choice = questdlg('Do you want to cancel your current ratings?', ...
'DARMA','Yes','No','No');
switch choice
case 'Yes'
handles.vlc.playlist.stop();
program_reset(handles);
case 'No'
return;
end
else
%If ratings are not being collected, exit DARMA
if strcmp(handles.timer.Running,'on'), stop(handles.timer); end
delete(timerfind);
delete(handles.figure_collect);
end
end