-
Notifications
You must be signed in to change notification settings - Fork 3
/
TempoController.m
executable file
·2681 lines (2242 loc) · 119 KB
/
TempoController.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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef TempoController < handle
properties
figure
recordings = {}
reporters = {}
duration = 300
zoom = 1
splitterPanel
splitter
videosPanel
videoPanels = {}
videoIconAxes
timelinesPanel
timelinePanels = {}
timeIndicatorPanel = []
timelineIconAxes
iconsPath
panelHandlingKeyPress
videoSlider
timelineSlider
fileMenu
editMenu
viewMenu
videoMenu
timelineMenu
playbackMenu
windowMenu
helpMenu
toolbar
jToolbar
saveTool
exportVideoTool
zoomOutTool
playBackwardsTool
pauseTool
playForwardsTool
playSlowerTool
playbackSpeedTool
playFasterTool
detectPopUpTool
annotatePopUpTool
timeLabelFormat = 1 % default to displaying time in minutes and seconds
panelEditingRange
objectBeingEdited
originalRangeBeingEdited
mouseConstraintTime
mouseConstraintFreq
mouseOffset
isPlaying = false
playRate = 1.0 % positive plays forward, negative plays backwards
playTimer
playRange
playStartTime
fpsFrameCount
recordingClassNames
recordingTypeNames
detectorClassNames
detectorTypeNames
importerClassNames
importerTypeNames
importing = false
recordingsToAdd = {}
showWaveforms
showSpectrograms
showFeatures
needsSave = false
savePath
lastOpenedPath
undoManager;
end
properties (SetObservable)
% The Tempo panels will listen for changes to these properties.
displayRange = [0 300 0 100000] % The window in time and frequency which all timeline panels should display (in seconds/Hz, [minTime maxTime minFreq maxFreq]).
currentTime = 0 % The time point currently being played (in seconds).
selectedRange = [0 0 -inf inf] % The range of time and frequency currently selected (in seconds/Hz). The first two values will be equal if there is a point selection.
windowSize = 0.01
end
methods
function obj = TempoController()
obj.showWaveforms = getpref('Tempo', 'ShowWaveforms', true);
obj.showSpectrograms = getpref('Tempo', 'ShowSpectrograms', true);
obj.showFeatures = getpref('Tempo', 'ShowFeatures', true);
obj.figure = figure('Name', 'Tempo', ...
'NumberTitle', 'off', ...
'Toolbar', 'none', ...
'MenuBar', 'none', ...
'Position', getpref('Tempo', 'MainWindowPosition', [100 100 1000 500]), ...
'Color', [0.4 0.4 0.4], ...
'Renderer', 'opengl', ...
'ResizeFcn', @(source, event)handleResize(obj, source, event), ...
'KeyPressFcn', @(source, event)handleKeyPress(obj, source, event), ...
'KeyReleaseFcn', @(source, event)handleKeyRelease(obj, source, event), ...
'CloseRequestFcn', @(source, event)handleClose(obj, source, event), ...
'WindowButtonDownFcn', @(source, event)handleMouseButtonDown(obj, source, event), ...
'WindowButtonMotionFcn', @(source, event)handleMouseMotion(obj, source, event), ...
'WindowButtonUpFcn', @(source, event)handleMouseButtonUp(obj, source, event)); %#ok<CPROP>
if isdeployed && exist(fullfile(ctfroot, 'Detectors'), 'dir')
% Look for the detectors in the CTF archive.
parentDir = ctfroot;
else
% Look for the detectors relative to this .m file.
tempoPath = mfilename('fullpath');
parentDir = fileparts(tempoPath);
end
addpath(fullfile(parentDir, 'Panels'));
addpath(fullfile(parentDir, 'Recordings'));
addpath(fullfile(parentDir, 'Utility'));
addpath(fullfile(parentDir, 'Legacy'));
[obj.recordingClassNames, obj.recordingTypeNames] = obj.findPlugIns(fullfile(parentDir, 'Recordings'));
[obj.detectorClassNames, obj.detectorTypeNames] = obj.findPlugIns(fullfile(parentDir, 'Detectors'));
[obj.importerClassNames, obj.importerTypeNames] = obj.findPlugIns(fullfile(parentDir, 'Importers'));
% Add the paths to the third-party code.
addpath(fullfile(parentDir, 'ThirdParty', 'export_fig'));
addpath(fullfile(parentDir, 'ThirdParty', 'dbutils'));
addpath(fullfile(parentDir, 'ThirdParty', 'ffmpeg'));
% Create an undo manager and listen to its changes.
obj.undoManager = UndoManager();
addlistener(obj.undoManager, 'UndoStackChanged', @(source, event)handleUndoStackChanged(obj, source, event));
% Create top-level panels to hold the video and timeline panels.
obj.videosPanel = uipanel(obj.figure, ...
'BorderType', 'none', ...
'BorderWidth', 0, ...
'BackgroundColor', 'black', ...
'SelectionHighlight', 'off', ...
'ResizeFcn', @(source, event)arrangeVideoPanels(obj, source, event), ...
'Visible', onOff(getpref('Tempo', 'ViewShowVideo', true)));
obj.timelinesPanel = uipanel(obj.figure, ...
'BorderType', 'none', ...
'BorderWidth', 0, ...
'BackgroundColor', 'white', ...
'SelectionHighlight', 'off', ...
'ResizeFcn', @(source, event)arrangeTimelinePanels(obj, source, event), ...
'Visible', onOff(getpref('Tempo', 'ViewShowTimeline', true)));
% Create icons in the middle of the video and timeline panels.
obj.videoIconAxes = axes(...
'Parent', obj.videosPanel, ...
'Color', 'none', ...
'HitTest', 'off', ...
'Units', 'pixels', ...
'Position', [0 100 124 128]);
image(obj.loadIcon('Video', [0 0 0]), 'HitTest', 'off');
text(0.5, 0.0, 'Video', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'center', ...
'FontSize', 24, ...
'FontWeight', 'bold', ...
'Color', [0.25 0.25 0.25]);
axis image;
obj.timelineIconAxes = axes(...
'Parent', obj.timelinesPanel, ...
'Color', 'none', ...
'HitTest', 'off', ...
'Units', 'pixels', ...
'Position', [0 100 344 128]);
image(obj.loadIcon('Timeline', [1 1 1]), 'HitTest', 'off');
text(0.5, 0.0, 'Timeline', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'center', ...
'FontSize', 24, ...
'FontWeight', 'bold', ...
'Color', [0.9 0.9 0.9]);
axis image;
% Create the time indicator at the bottom of the timelines.
obj.timeIndicatorPanel = TimeIndicatorPanel(obj);
% Insert a splitter at the top level to separate the video and timeline panels.
if strcmp(getpref('Tempo', 'VideoPlacement', 'left'), 'left')
orientation = 'horizontal';
else
orientation = 'vertical';
end
obj.splitter = UISplitter(obj.figure, obj.videosPanel, obj.timelinesPanel, orientation);
obj.createMenuBar();
if getpref('Tempo', 'ViewShowToolbar', true)
obj.createToolbar();
end
obj.updateMenuItemsAndToolbar();
% Create the scroll bars that let the user scrub through time.
videosPos = get(obj.videosPanel, 'Position');
obj.videoSlider = uicontrol(...
'Parent', obj.videosPanel, ...
'Style', 'slider',...
'Min', 0, ...
'Max', obj.duration, ...
'Value', 0, ...
'Position', [1 1 videosPos(3) 16]);
addlistener(obj.videoSlider, 'ContinuousValueChange', @(source, event)handleVideoSliderChanged(obj, source, event));
timelinesPos = get(obj.timelinesPanel, 'Position');
obj.timelineSlider = uicontrol(...
'Parent', obj.timelinesPanel, ...
'Style', 'slider',...
'Min', 0, ...
'Max', obj.duration, ...
'Value', 0, ...
'Position', [1 1 timelinesPos(3) 16]);
addlistener(obj.timelineSlider, 'ContinuousValueChange', @(source, event)handleTimelineSliderChanged(obj, source, event));
obj.arrangeTimelinePanels();
% Set up a timer to fire 30 times per second during playback.
% MATLAB can only seem to manage up to 15 FPS but one can hope.
obj.playTimer = timer('ExecutionMode', 'fixedRate', 'TimerFcn', @(timerObj, event)handlePlayTimer(obj, timerObj, event), 'Period', round(1.0 / 30.0 * 1000) / 1000);
% Display a welcome message the first time the user runs Tempo.
obj.welcomeUser();
end
function createMenuBar(obj)
obj.fileMenu = uimenu(obj.figure, 'Label', 'File');
uimenu(obj.fileMenu, 'Label', 'New', ...
'Callback', @(hObject, eventdata)handleNewWorkspace(obj, hObject, eventdata), ...
'Accelerator', 'n');
uimenu(obj.fileMenu, 'Label', 'Open...', ...
'Callback', @(hObject, eventdata)handleOpenFile(obj, hObject, eventdata), ...
'Accelerator', 'o');
uimenu(obj.fileMenu, 'Label', 'Close', ...
'Callback', @(hObject, eventdata)handleClose(obj, hObject, eventdata), ...
'Accelerator', 'w', ...
'Separator', 'on');
uimenu(obj.fileMenu, 'Label', 'Close All', ...
'Callback', '', ...
'Enable', 'off');
uimenu(obj.fileMenu, 'Label', 'Save...', ...
'Callback', @(hObject, eventdata)handleSaveWorkspace(obj, hObject, eventdata), ...
'Accelerator', 's', ...
'Tag', 'save');
uimenu(obj.fileMenu, 'Label', 'Save As...', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off', ...
'Tag', 'saveAs');
uimenu(obj.fileMenu, 'Label', 'Export Video of Selection...', ...
'Callback', @(hObject, eventdata)handleExportSelection(obj, hObject, eventdata), ...
'Separator', 'on', ...
'Tag', 'exportVideo');
uimenu(obj.fileMenu, 'Label', 'Export All Features...', ...
'Callback', '', ...
'Enable', 'off', ...
'Tag', 'exportFeatures');
uimenu(obj.fileMenu, 'Label', 'Take Screen Shot...', ...
'Callback', @(hObject, eventdata)handleSaveScreenshot(obj, hObject, eventdata), ...
'Separator', 'on');
obj.editMenu = uimenu(obj.figure, 'Label', 'Edit');
uimenu(obj.editMenu, 'Label', 'Undo', ...
'Callback', @(hObject, eventdata)handleUndo(obj, hObject, eventdata), ...
'Accelerator', 'z', ...
'Tag', 'undo');
uimenu(obj.editMenu, 'Label', 'Redo', ...
'Callback', @(hObject, eventdata)handleRedo(obj, hObject, eventdata), ...
'Accelerator', 'y', ...
'Tag', 'redo');
uimenu(obj.editMenu, 'Label', 'Cut', ...
'Callback', '', ...
'Accelerator', 'x', ...
'Separator', 'on', ...
'Enable', 'off');
uimenu(obj.editMenu, 'Label', 'Copy', ...
'Callback', '', ...
'Accelerator', 'c', ...
'Enable', 'off');
uimenu(obj.editMenu, 'Label', 'Paste', ...
'Callback', '', ...
'Accelerator', 'v', ...
'Enable', 'off');
uimenu(obj.editMenu, 'Label', 'Select All', ...
'Callback', @(hObject, eventdata)handleSelectAll(obj, hObject, eventdata), ...
'Accelerator', 'a', ...
'Separator', 'on');
uimenu(obj.editMenu, 'Label', 'Select None', ...
'Callback', @(hObject, eventdata)handleSelectNone(obj, hObject, eventdata), ...
'Accelerator', '');
detectorMenu = uimenu(obj.editMenu, 'Label', 'Detect Features', ...
'Callback', '', ...
'Separator', 'on');
for detectorIdx = 1:length(obj.detectorTypeNames)
uimenu(detectorMenu, 'Label', [obj.detectorTypeNames{detectorIdx} '...'], ...
'Callback', @(hObject, eventdata)handleDetectFeatures(obj, hObject, eventdata), ...
'UserData', detectorIdx);
end
obj.viewMenu = uimenu(obj.figure, 'Label', 'View');
uimenu(obj.viewMenu, 'Label', 'Show Toolbar', ...
'Callback', @(hObject, eventdata)handleShowToolbar(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Tag', 'showToolbar');
uimenu(obj.viewMenu, 'Label', 'Show Only Video', ...
'Callback', @(hObject, eventdata)handleShowVideoAndOrTimeline(obj, hObject, eventdata, true, false), ...
'Accelerator', '', ...
'Separator', 'on', ...
'Tag', 'showVideo');
uimenu(obj.viewMenu, 'Label', 'Show Only Timeline', ...
'Callback', @(hObject, eventdata)handleShowVideoAndOrTimeline(obj, hObject, eventdata, false, true), ...
'Accelerator', '', ...
'Tag', 'showTimeline');
uimenu(obj.viewMenu, 'Label', 'Show Video and Timeline', ...
'Callback', @(hObject, eventdata)handleShowVideoAndOrTimeline(obj, hObject, eventdata, true, true), ...
'Accelerator', '', ...
'Tag', 'showVideoAndTimeline');
uimenu(obj.viewMenu, 'Label', 'Place Videos Left of the Timeline', ...
'Callback', @(hObject, eventdata)handlePlaceVideoToTheLeft(obj, hObject, eventdata), ...
'Separator', 'on', ...
'Tag', 'placeVideoToTheLeft');
uimenu(obj.viewMenu, 'Label', 'Place Videos Above the Timeline', ...
'Callback', @(hObject, eventdata)handlePlaceVideoAbove(obj, hObject, eventdata), ...
'Tag', 'placeVideoAbove');
obj.updateViewMenuItems();
obj.videoMenu = uimenu(obj.figure, 'Label', 'Video');
uimenu(obj.videoMenu, 'Label', 'View at Actual Size', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'View at Double Size', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'View at Half Size', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'View at Maximum Size', ...
'Callback', '', ...
'Accelerator', '', ...
'Checked', 'on', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'Arrange Videos Left to Right', ...
'Callback', '', ...
'Separator', 'on', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'Arrange Videos Top to Bottom', ...
'Callback', '', ...
'Checked', 'on', ...
'Enable', 'off');
uimenu(obj.videoMenu, 'Label', 'Show Current Frame Number', ...
'Callback', @(hObject, eventdata)handleShowFrameNumber(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Separator', 'on', ...
'Tag', 'showFrameNumber');
obj.timelineMenu = uimenu(obj.figure, 'Label', 'Timeline');
uimenu(obj.timelineMenu, 'Label', 'Zoom In', ...
'Callback', @(hObject, eventdata)handleZoomIn(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Tag', 'zoomIn');
uimenu(obj.timelineMenu, 'Label', 'Zoom Out', ...
'Callback', @(hObject, eventdata)handleZoomOut(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Tag', 'zoomOut');
uimenu(obj.timelineMenu, 'Label', 'Zoom to Selection', ...
'Callback', @(hObject, eventdata)handleZoomToSelection(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Tag', 'zoomToSelection');
uimenu(obj.timelineMenu, 'Label', 'Go To Time...', ...
'Callback', @(hObject, eventdata)handleGoToTime(obj, hObject, eventdata), ...
'Separator', 'on', ...
'Accelerator', 't', ...
'Tag', 'goToTime');
uimenu(obj.timelineMenu, 'Label', 'Annotate Features...', ...
'Callback', @(hObject, eventdata)handleAnnotateFeatures(obj, hObject, eventdata), ...
'Separator', 'on', ...
'Tag', 'addManualAnnotations');
uimenu(obj.timelineMenu, 'Label', 'Open Waveform for New Recordings', ...
'Callback', @(hObject, eventdata)handleShowWaveformAndOrSpectrogramOnOpen(obj, hObject, eventdata, true, false), ...
'Separator', 'on', ...
'Tag', 'showWaveformOnOpen');
uimenu(obj.timelineMenu, 'Label', 'Open Spectrogram for New Recordings', ...
'Callback', @(hObject, eventdata)handleShowWaveformAndOrSpectrogramOnOpen(obj, hObject, eventdata, false, true), ...
'Tag', 'showSpectrogramOnOpen');
uimenu(obj.timelineMenu, 'Label', 'Open Both for New Recordings', ...
'Callback', @(hObject, eventdata)handleShowWaveformAndOrSpectrogramOnOpen(obj, hObject, eventdata, true, true), ...
'Tag', 'showBothOnOpen');
obj.playbackMenu = uimenu(obj.figure, 'Label', 'Playback');
uimenu(obj.playbackMenu, 'Label', 'Play', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'forwards'), ...
'Accelerator', '', ...
'Tag', 'play');
uimenu(obj.playbackMenu, 'Label', 'Play Backwards', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'backwards'), ...
'Accelerator', '', ...
'Tag', 'playBackwards');
uimenu(obj.playbackMenu, 'Label', 'Pause', ...
'Callback', @(hObject, eventdata)handlePause(obj, hObject, eventdata), ...
'Accelerator', '', ...
'Tag', 'pause');
uimenu(obj.playbackMenu, 'Label', 'Play at Regular Speed', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, '1x'), ...
'Accelerator', '1', ...
'Separator', 'on', ...
'Tag', 'regularSpeed');
uimenu(obj.playbackMenu, 'Label', 'Play at Double Speed', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, '2x'), ...
'Accelerator', '', ...
'Tag', 'doubleSpeed');
uimenu(obj.playbackMenu, 'Label', 'Play at Half Speed', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, '1/2x'), ...
'Accelerator', '2', ...
'Tag', 'halfSpeed');
uimenu(obj.playbackMenu, 'Label', 'Increase Speed', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'faster'), ...
'Accelerator', '', ...
'Separator', 'on', ...
'Tag', 'increaseSpeed');
uimenu(obj.playbackMenu, 'Label', 'Decrease Speed', ...
'Callback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'slower'), ...
'Accelerator', '', ...
'Tag', 'decreaseSpeed');
obj.windowMenu = uimenu(obj.figure, 'Label', 'Window');
uimenu(obj.windowMenu, 'Label', 'Arrange Windows Top to Bottom', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off');
uimenu(obj.windowMenu, 'Label', 'Arrange Windows Left to Right', ...
'Callback', '', ...
'Accelerator', '', ...
'Enable', 'off');
% TODO: add/manage the list of open windows
obj.helpMenu = uimenu(obj.figure, 'Label', 'Help');
uimenu(obj.helpMenu, 'Label', 'Tempo Help', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata));
uimenu(obj.helpMenu, 'Label', 'Getting Your Data Into Tempo', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata, 'Getting Your Data Into Tempo'), ...
'Separator', 'on');
uimenu(obj.helpMenu, 'Label', 'Browsing', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata, 'Browsing'));
uimenu(obj.helpMenu, 'Label', 'Analyzing', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata, 'Analyzing'));
uimenu(obj.helpMenu, 'Label', 'Annotating', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata, 'Annotating'));
uimenu(obj.helpMenu, 'Label', 'Customizing Tempo', ...
'Callback', @(hObject, eventdata)handleShowHelp(obj, hObject, eventdata, 'Customizing'), ...
'Separator', 'on');
uimenu(obj.helpMenu, 'Label', 'Visit the Tempo Web Site', ...
'Callback', @(hObject, eventdata)handleVisitWebSite(obj, hObject, eventdata), ...
'Separator', 'on');
end
function createToolbar(obj)
obj.toolbar = uitoolbar(obj.figure);
defaultBackground = get(0, 'defaultUicontrolBackgroundColor');
% Open and save
uipushtool(obj.toolbar, ...
'Tag', 'open', ...
'CData', obj.loadIcon('file_open'), ...
'TooltipString', 'Open a saved workspace, open audio/video files or import features',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handleOpenFile(obj, hObject, eventdata));
uipushtool(obj.toolbar, ...
'Tag', 'save', ...
'CData', obj.loadIcon('file_save'), ...
'TooltipString', 'Save the workspace',...
'ClickedCallback', @(hObject, eventdata)handleSaveWorkspace(obj, hObject, eventdata));
% Export and screenshot
obj.exportVideoTool = uipushtool(obj.toolbar, ...
'Tag', 'exportVideo', ...
'CData', obj.loadIcon('ExportToMovie'), ...
'TooltipString', 'Export the selected time window to a movie',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handleExportSelection(obj, hObject, eventdata));
uipushtool(obj.toolbar, ...
'Tag', 'saveScreenshot', ...
'CData', obj.loadIcon('screenshot'), ...
'TooltipString', 'Save a screenshot',...
'ClickedCallback', @(hObject, eventdata)handleSaveScreenshot(obj, hObject, eventdata));
% Annotate and detect features
obj.annotatePopUpTool = uipushtool('Parent', obj.toolbar, ...
'Tag', 'annotate', ...
'CData', obj.loadIcon('Annotate'), ...
'TooltipString', 'Annotate features',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handleAnnotateFeatures(obj, hObject, eventdata));
obj.detectPopUpTool = uisplittool('Parent', obj.toolbar, ...
'Tag', 'detectFeatures', ...
'CData', obj.loadIcon('detect'), ...
'TooltipString', 'Detect features',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handleDetectFeatures(obj, hObject, eventdata));
% Playback
obj.playBackwardsTool = uipushtool(obj.toolbar, ...
'CData', obj.loadIcon('PlayBackwards'), ...
'TooltipString', 'Play backwards',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'backwards'));
obj.pauseTool = uipushtool(obj.toolbar, ...
'CData', obj.loadIcon('pause'), ...
'TooltipString', 'Pause playback',...
'ClickedCallback', @(hObject, eventdata)handlePause(obj, hObject, eventdata), ...
'Enable', 'off');
obj.playForwardsTool = uipushtool(obj.toolbar, ...
'CData', obj.loadIcon('PlayForwards'), ...
'TooltipString', 'Play forwards',...
'ClickedCallback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'forwards'));
% Playback speed
obj.playSlowerTool = uipushtool(obj.toolbar, ...
'CData', obj.loadIcon('PlaySlower'), ...
'Separator', 'on', ...
'TooltipString', 'Decrease the speed of playback',...
'ClickedCallback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'slower'));
obj.playbackSpeedTool = uipushtool(obj.toolbar, ...
'CData', zeros(16, 16, 3), ...
'TooltipString', 'Choose the speed of playback',...
'ClickedCallback', @(hObject, eventdata)handleChoosePlaybackSpeed(obj, hObject, eventdata));
obj.playFasterTool = uipushtool(obj.toolbar, ...
'CData', obj.loadIcon('PlayFaster'), ...
'TooltipString', 'Increase the speed of playback',...
'ClickedCallback', @(hObject, eventdata)handlePlay(obj, hObject, eventdata, 'faster'));
% Zooming
uipushtool(obj.toolbar, ...
'Tag', 'zoomIn', ...
'CData', obj.loadIcon('tool_zoom_in'), ...
'TooltipString', 'Zoom in',...
'Separator', 'on', ...
'ClickedCallback', @(hObject, eventdata)handleZoomIn(obj, hObject, eventdata));
uipushtool(obj.toolbar, ...
'Tag', 'zoomOut', ...
'CData', obj.loadIcon('tool_zoom_out'), ...
'TooltipString', 'Zoom out',...
'ClickedCallback', @(hObject, eventdata)handleZoomOut(obj, hObject, eventdata));
% Make sure the Java components we need have been created.
drawnow;
obj.jToolbar = get(get(obj.toolbar, 'JavaContainer'), 'ComponentPeer');
if ~isempty(obj.jToolbar)
oldWarn = warning('off', 'MATLAB:hg:JavaSetHGProperty');
oldWarn2 = warning('off', 'MATLAB:hg:PossibleDeprecatedJavaSetHGProperty');
% Add the detector options to the pop-up menu.
jDetect = get(obj.detectPopUpTool,'JavaContainer');
jMenu = get(jDetect,'MenuComponent');
jMenu.removeAll;
jMenu.add('Detect Features:').setEnabled(false);
for actionIdx = 1:length(obj.detectorTypeNames)
jActionItem = jMenu.add([obj.detectorTypeNames{actionIdx} '...']);
handle(jActionItem, 'CallbackProperties');
set(ans, 'ActionPerformedCallback', @(hObject, eventdata)handleDetectFeatures(obj, hObject, eventdata),...
'ToolTipText', num2str(actionIdx));
% set(jActionItem, 'ActionPerformedCallback', @(hObject, eventdata)handleDetectFeatures(obj, hObject, eventdata), ...
% 'UserData', actionIdx);
end
% Try to replace the 'choose playback speed' tool with a Java button that can display the play rate.
obj.replaceToolbarTool(18, 'playbackSpeedTool');
if isjava(obj.playbackSpeedTool)
obj.playbackSpeedTool.setBorderPainted(false);
obj.playbackSpeedTool.setText('1x');
end
try
obj.jToolbar(1).repaint;
obj.jToolbar(1).revalidate;
catch
end
warning(oldWarn);
warning(oldWarn2);
end
end
function replaceToolbarTool(obj, position, toolName, iconName)
% Try to replace the tool with a Java button.
try
if nargin < 4
jButton = javax.swing.JButton('');
jButton.setMargin(java.awt.Insets(1, 2, 1, 2));
else
jButton = javax.swing.JButton(javax.swing.ImageIcon(fullfile('icons', [iconName '.png'])));
end
callback = get(obj.(toolName), 'ClickedCallback');
tooltip = get(obj.(toolName), 'TooltipString');
handle(jButton, 'CallbackProperties');
set(ans, 'ActionPerformedCallback', callback, ...
'ToolTipText', tooltip);
% set(jButton, 'ActionPerformedCallback', callback, ...
% 'ToolTipText', tooltip);
delete(obj.(toolName));
obj.jToolbar(1).add(jButton, position);
obj.(toolName) = jButton;
catch ME
rethrow(ME);
% Oh well, leave it alone.
end
end
function updateMenuItemsAndToolbar(obj)
obj.updateFileMenuItems();
obj.updateEditMenuItems();
obj.updateViewMenuItems();
obj.updateVideoMenuItems();
obj.updateTimelineMenuItems();
obj.updatePlaybackMenuItems();
end
function iconData = loadIcon(obj, iconName, backgroundColor)
if isempty(obj.iconsPath)
[tempoRoot, ~, ~] = fileparts(mfilename('fullpath'));
obj.iconsPath = fullfile(tempoRoot, 'Icons');
end
if nargin < 3
backgroundColor = get(0, 'defaultUicontrolBackgroundColor');
end
% First look in our icons folder.
iconPath = fullfile(obj.iconsPath, [iconName '.png']);
iconMax = 256;
if ~exist(iconPath, 'file')
% Not one of ours, get the icon from the standard MATLAB icons.
iconPath = fullfile(matlabroot, 'toolbox', 'matlab', 'icons', [iconName '.png']);
iconMax = 65536;
end
iconData = double(imread(iconPath, 'BackgroundColor', backgroundColor)) / iconMax;
end
function welcomeUser(obj) %#ok<MANU>
if ~getpref('Tempo', 'UserWasWelcomed', false)
uiwait(msgbox(['Welcome to Tempo!' char(10) char(10) ...
'To get started open a video, audio or annotation file.'], 'Tempo', 'modal'));
setpref('Tempo', 'UserWasWelcomed', true);
end
end
%% Panel management
function panels = panelsOfClass(obj, panelClass)
% Return a cell array of panel instances of the given class.
% A cell array is needed in case a base panel class is specified, e.g. TimelinePanel.
panels = {};
for i = 1:length(obj.videoPanels)
panel = obj.videoPanels{i};
if isa(panel, panelClass)
panels{end + 1} = panel; %#ok<AGROW>
end
end
for i = 1:length(obj.timelinePanels)
panel = obj.timelinePanels{i};
if isa(panel, panelClass)
panels{end + 1} = panel; %#ok<AGROW>
end
end
end
function arrangePanels(obj, panels, parentSize, margin) %#ok<INUSL>
% Arrange a set of panels within the given parent size (W, H) and margins (T, R, B, L).
% Figure out how many panels are hidden.
numPanels = length(panels);
numHidden = 0;
for i = 1:numPanels
if panels{i}.isHidden
numHidden = numHidden + 1;
end
end
numShown = numPanels - numHidden;
% Arrange the panels.
% Open panels share the full height of the area minus room for hidden panels and top and bottom margins.
% TODO: allow left to right
% TODO: allow panels to specify a fixed height/width.
titleBarHeight = 16;
shownPanelsHeight = parentSize(2) - margin(1) - numPanels * (titleBarHeight + 1) - margin(3);
shownPanelHeight = floor(shownPanelsHeight / numShown);
if shownPanelHeight < 32
warning('Tempo:WindowTooSmall', 'The window is too small to display all of the panels');
else
curY = parentSize(2) - margin(1) + 2;
for i = 1:numPanels
if panels{i}.isHidden
% Just need room for the title bar.
panelHeight = titleBarHeight;
curY = curY - panelHeight - 1;
elseif i < numPanels
% Open panels get an even fraction of the available space.
% Leave a one pixel gap between panels so there's a visible line between them.
panelHeight = shownPanelHeight + 1 + titleBarHeight;
curY = curY - panelHeight - 1;
else
% The last open panel gets whatever is left.
panelHeight = curY - (2 + margin(3)) - 1;
curY = 2 + margin(3);
end
set(panels{i}.panel, 'Position', [margin(4), curY, parentSize(1) - margin(2), panelHeight]);
panels{i}.handleResize([], []);
end
end
end
function arrangeVideoPanels(obj, ~, ~)
if isempty(obj.videosPanel) return; end
% Get the size of the top-level panel.
set(obj.videosPanel, 'Units', 'pixels');
videosPos = get(obj.videosPanel, 'Position');
set(obj.videosPanel, 'Units', 'normalized');
obj.arrangePanels(obj.videoPanels, videosPos(3:4), [0 3 16 4]);
iconAxesPos = [videosPos(3) / 2 - 62, videosPos(4) / 2 - 64, 124, 128];
set(obj.videoIconAxes, 'Visible', onOff(isempty(obj.videoPanels)), ...
'Position', iconAxesPos);
set(obj.videoSlider, 'Position', [10, 0, videosPos(3)-20, 16]);
end
function showVideoPanels(obj, doShow)
if nargin < 2
doShow = true;
end
set(obj.videosPanel, 'Visible', onOff(doShow));
end
function to = timelinesAreOpen(obj)
% Get the size of the top-level panel.
set(obj.timelinesPanel, 'Units', 'pixels');
timelinesPos = get(obj.timelinesPanel, 'Position');
set(obj.timelinesPanel, 'Units', 'normalized');
to = timelinesPos(3) > 15;
end
function arrangeTimelinePanels(obj, ~, ~)
if isempty(obj.timelinesPanel) return; end
% Get the size of the top-level panel.
set(obj.timelinesPanel, 'Units', 'pixels');
timelinesPos = get(obj.timelinesPanel, 'Position');
set(obj.timelinesPanel, 'Units', 'normalized');
% Position the time indicator panel at the bottom.
timeIndicatorHeight = 13;
obj.timeIndicatorPanel.setHidden(false);
set(obj.timeIndicatorPanel.panel, 'Position', [2, 18, timelinesPos(3), timeIndicatorHeight + 1]);
obj.timeIndicatorPanel.updateAxes(obj.displayRange(1:2));
obj.arrangePanels(obj.timelinePanels, timelinesPos(3:4), [0, 0, timeIndicatorHeight + 2 + 16, 2]);
iconAxesPos = [timelinesPos(3) / 2 - 172, timelinesPos(4) / 2 - 64, 344, 128];
set(obj.timelineIconAxes, 'Visible', onOff(isempty(obj.timelinePanels)), ...
'Position', iconAxesPos);
axis(obj.timelineIconAxes, 'off');
% Position the timeline slider at the bottom.
set(obj.timelineSlider, 'Position', [10, 0, timelinesPos(3)-20, 16]);
end
function showTimelinePanels(obj, doShow)
if nargin < 2
doShow = true;
end
set(obj.timelinesPanel, 'Visible', onOff(doShow));
end
function hidePanel(obj, panel)
panel.setHidden(true);
if isa(panel, 'VideoPanel')
obj.arrangeVideoPanels();
else
obj.arrangeTimelinePanels();
end
end
function showPanel(obj, panel)
panel.setHidden(false);
if isa(panel, 'VideoPanel')
obj.arrangeVideoPanels();
else
obj.arrangeTimelinePanels();
end
end
function closePanel(obj, panel)
% Let the panel do what it needs to before closing.
panel.close();
% Remove the panel.
if isa(panel, 'VideoPanel')
obj.videoPanels(cellfun(@(x) x == panel, obj.videoPanels)) = [];
obj.arrangeVideoPanels();
else
obj.timelinePanels(cellfun(@(x) x == panel, obj.timelinePanels)) = [];
obj.arrangeTimelinePanels();
end
end
%% File menu callbacks
function handleNewWorkspace(obj, ~, ~) %#ok<INUSD>
% Open a new window with a new controller object.
% TODO: stagger the window so it doesn't land exactly on top of the existing one.
TempoController();
end
function handleOpenFile(obj, ~, ~)
[fileNames, pathName] = uigetfile('*.*', 'Select an audio or video file to open', obj.lastOpenedPath, 'MultiSelect', 'on');
if ischar(fileNames)
fileNames = {fileNames};
elseif isnumeric(fileNames)
fileNames = {};
end
if ~isempty(fileNames)
% Remember this path for the next time the user opens a file.
obj.lastOpenedPath = pathName;
end
somethingOpened = false;
for i = 1:length(fileNames)
fileName = fileNames{i};
fullPath = fullfile(pathName, fileName);
% Handle special characters in the file name.
NFD = javaMethod('valueOf', 'java.text.Normalizer$Form','NFD');
UTF8=java.nio.charset.Charset.forName('UTF-8');
s = java.lang.String(fullPath);
sc = java.text.Normalizer.normalize(s,NFD);
bs = single(sc.getBytes(UTF8)');
bs(bs < 0) = 256 + (bs(bs < 0));
fullPath = char(bs);
[~, ~, ext] = fileparts(fileName);
if strcmp(ext, '.tempo')
% Open a Tempo workspace.
if isempty(obj.recordings) && isempty(obj.reporters)
% Nothing has been done in this controller so load the workspace here.
target = obj;
else
% Open the workspace in a new controller.
target = TempoController();
end
target.openWorkspace(fullPath);
somethingOpened = true;
continue
end
% First check if the file can be imported by one of the feature importers.
try
possibleImporters = [];
for j = 1:length(obj.importerClassNames)
if eval([obj.importerClassNames{j} '.canImportFromPath(''' strrep(fullPath, '''', '''''') ''')'])
possibleImporters(end+1) = j; %#ok<AGROW>
end
end
if ~isempty(possibleImporters)
index = [];
if length(possibleImporters) == 1
index = possibleImporters(1);
else
choice = listdlg('PromptString', 'Choose which importer to use:', ...
'SelectionMode', 'Single', ...
'ListString', handles.importerTypeNames(possibleImporters));
if ~isempty(choice)
index = choice(1);
end
end
if ~isempty(index)
obj.importing = true;
constructor = str2func(obj.importerClassNames{index});
importer = constructor(obj, fullPath);
importer.startProgress();
try
features = importer.importFeatures();
importer.endProgress();
obj.importing = false;
% Open any recordings that the importer queued up.
for recording = obj.recordingsToAdd
if isa(recording, 'AudioRecording')
obj.addAudioRecording(recording);
elseif isa(recording, 'VideoRecording')
obj.addVideoRecording(recording);
end
end
obj.recordingsToAdd = {};
if isempty(features)
waitfor(msgbox('No features were imported.', obj.importerTypeNames{index}, 'warn', 'modal'));
else
importer.addFeatures(features);
obj.reporters{end + 1} = importer;
obj.timelinePanels{end + 1} = FeaturesPanel(importer);
somethingOpened = true;
obj.needsSave = true;
end
catch ME
importer.endProgress();
obj.importing = false;
obj.recordingsToAdd = {};
waitfor(msgbox('An error occurred while importing features. (See the command window for details.)', obj.importerTypeNames{index}, 'error', 'modal'));
rethrow(ME);
end
end
end
catch ME
rethrow(ME);
end
% Next check if it's an audio or video file.
try
for j = 1:length(obj.recordingClassNames)
if eval([obj.recordingClassNames{j} '.canLoadFromPath(''' strrep(fullPath, '''', '''''') ''')'])
set(obj.figure, 'Pointer', 'watch'); drawnow
constructor = str2func(obj.recordingClassNames{j});
recording = constructor(obj, fullPath);
if isa(recording, 'AudioRecording')
obj.addAudioRecording(recording);
somethingOpened = true;
elseif isa(recording, 'VideoRecording')
obj.addVideoRecording(recording);
somethingOpened = true;
end
set(obj.figure, 'Pointer', 'arrow'); drawnow
break
end
end
catch ME
set(obj.figure, 'Pointer', 'arrow'); drawnow
if ~strcmp(ME.identifier, 'Tempo:UserCancelled')
warndlg(sprintf('Error opening recording:\n\n%s', ME.message));
rethrow(ME);
end
end
end
if somethingOpened
obj.updateOverallDuration();
obj.arrangeVideoPanels();