forked from midnight-studios/obs-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopWatch.lua
1168 lines (1117 loc) · 43.8 KB
/
StopWatch.lua
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
--[[
----------------------------------------------------------
Open Broadcaster Software®️
OBS > Tools > Scripts
@midnight-studios
Stopwatch
----------------------------------------------------------
]]
--Globals
obs = obslua
gversion = 2.6
luafile = "StopWatch.lua"
obsurl = "simple-stopwatch.1364/"
icon="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAVCAYAAACpF6WWAAAENElEQVQ4jY1UTUgjZxh+ksl/JuMkMYb4F40bNZqK0KJFqBZqS9ddyl76dyhdKPRQShH2sNDSnnopCz11D10KS/dSKNiDoD2I7KXFQ0XSSGpM1llFMYn5mZiMY2IymfIOhgazXfaDj5n53u975vme531fnaqqeMHxJYCvAOgAlABcAyA1jxLO1tYW1tbWoL+Kd3x8jGg0imw2C0VRWkMEYgNgBeAFYKTFRqOh7aVnE9xwFTSZTGJ7exszMzPQ6XSQZRk8z9P7YrVa/Y5hmKLBYHCpqirW63Wcn5/j7OwMHo9HA6bvNqY2mw1Op1N70qaTkxPkcjmbLMsDZrN5hOO4NxuNhlMUxTFiSCA0FEW5GQ6H/wmHwzfamDavUKlUYDKZAoFA4Gue52/r9f/9v6OjQ5uKojwpFAr3RFF8UCwWjW63OzQ/P/9yGyiBnZ6eEtN3eZ7/9XJZrlQqP2cymcf5fL4QDAbHdTrd2yzLXvd4PD9yHHdLEISFXC7nsdvtuTb3c7kcEokEJiYmhliWtaiqWs5ms4f1el0lE2lOTU0hn8/DYrF09vb23jebze9JkvRXNBqdMpvNaIJaLh1tHScAzpvsSd+joyOkUimEQiFNa4vFAlEU4Xa7HwYCgduFQuHRxsbGx5p+qqq+o/7/SF7uQSaTwcHBgZYdgiBMqKqa2dnZ8S8tLaFcLicIIR6PjzU13Qew+gzPKNEj9JJOp5tag+O41/v7+x/v7u7+sLOzc8BxHN1icXR0dMXlcn3xQhW1v7+PSCSC6enptxwOx3WWZRcbjcbTjY2NAJ1nWRYGgwHj4+OqoigFYnr/UlPlClYFwJ1arVYjU8bGxhZ8Pt9KMxiLxd5gGEbTlTSv1WqQJOmJw+G4RqCfPYfkN4qiFDs7O9HT0/Nqa4BhmKd2u10DrFaruLi4oJmncibQSUCrLHJabDlHzItGo1E7FIvFvg+FQjMmkykkCMK9eDwOivl8PvqhBspxXJAOEujfz2HazzBMdXh4OJNMJoupVGre7/cbBEGor6+vY2RkROsLlwY6jUajS5KkSGvtf0oVemUeAPiDgsFgUHMeQJ3MmZycxNzcnMZWkiT4/f67FJRl+UFrmcYB/N7y3UyLSHOBzNjb20MgEMDg4CC6urqwublJZo12d3ffVRRFEQTh4TNTqlQqaawoTShOVdOsqMPDQ8zOzmqFQK3PZrO91NPTs2U0GkmWG4lEYrWt9cViMSwvL1Ntvw9gRafT/aTX6z8AwFKcuhU5zjDMkNfr/XZgYCBKgMfHx3eSyeSqw+Fob9LEipxMp9MRp9P5uclkWuB5/hOKWa3Wvb6+vjLP8wNer5fXUkRRLkql0ofZbPY3ug019TZQ6jKU0AzD7Iqi+Josy6+4XK6P7Hb7LbvdPkS5SXpXKpU/ZVn+5ezs7FG9Xi9brVZNLr1ej38BVDs6EbSfFQsAAAAASUVORK5CYII="
desc = [[
<hr/><center><h2>Advanced Stopwatch</h2>( Version: %s )</center>
<br><center><img width=38 height=42 src=']] .. icon .. [['/></center>
<br><center><a href="https://github.com/midnight-studios/obs-lua/blob/main/]] .. luafile ..[[">Find it on GitHub</a></center>
<br><p>The Properties for this script will adjust visibility as needed. Some advanced properties will only be visible if the Configuration is set to "Advanced". If the Configuration is set to "Basic" the defined values will still be used, so ensure you define those correctly.</p><p>Find help on the <a href="https://obsproject.com/forum/resources/]] .. obsurl ..[[">OBS Forum Thread</a>.</p><hr/>]]
last_text = ""
font_normal = "#ffffff"
font_dimmed = "#bfbbbf"
timer_source = ""
cur_seconds = 0
def_seconds = 0
split = 0
split_itm = {}
split_data = nil
p_settings = nil
last_split_data = ""
split_type = ""
split_source = ""
timer_type = 0
next_scene = ""
start_recording = 0
recording_type = 0
stop_text = ""
orig_time = 0
time_frequency = 0
completed_cycles = 0
ns_last = 0
timer_cycle = 10 --milliseconds
activated = false
timer_active = false
reset_activated = false
start_on_visible = false
disable_script = false
media = {
warning_text = "",
caution_text = "",
source_name_audio_warning = "",
source_name_audio_caution = "",
warning_activated = false,
caution_activated = false,
cur_seconds_caution = 0,
cur_seconds_warning = 0,
caution_duration = 0,
warning_duration = 0,
normal_color = 0xFFFFFFFF,
caution_color = 0x40f3ed,
warning_color = 0x05055a,
last_state_caution = obs.OBS_MEDIA_STATE_NONE,
last_state_warning = obs.OBS_MEDIA_STATE_NONE
}
hotkey_id_reset = obs.OBS_INVALID_HOTKEY_ID
hotkey_id_pause = obs.OBS_INVALID_HOTKEY_ID
hotkey_id_split = obs.OBS_INVALID_HOTKEY_ID
--[[
----------------------------------------------------------
-- Use this to create a Script Log Output used in testing
----------------------------------------------------------
]]
local function log( name, msg )
if msg ~= nil then
msg = " > " .. tostring( msg )
else
msg = ""
end
obs.script_log( obs.LOG_DEBUG, name .. msg )
end
--[[
----------------------------------------------------------
A function named script_description returns the description shown to
the user
----------------------------------------------------------
]]
function script_description()
return string.format( desc, tostring( gversion ) )
end
--[[
----------------------------------------------------------
Assign a default Frequency based on the Frame Rate
----------------------------------------------------------
]]
function assign_default_frequency()
local fps = obs.obs_get_active_fps()
local f = 60 -- 60 is the maximum supported frame rate
if fps ~= 'inf' then f = fps end
time_frequency = ( 1/f )
end
--[[
----------------------------------------------------------
Used this in testing to measure accuracy
The Text Source and the Log should produce the same value
The Text source is updated by the time function while the dedug
uses start and end time stamps to get a value
----------------------------------------------------------
]]
function get_time_lapsed()
local ns = obs.os_gettime_ns()
local delta = ( ns/1000000000.0 ) - ( orig_time/1000000000.0 )
return TimeFormat( delta )
end
--[[
----------------------------------------------------------
The true frequency between cycles varies due to script
and system task processing, therefore a static frequency
will produce inaccuarte results over time.
Start with a default frequency of 1 second devided by
the assigned active fps and then update the frequency
calculated from the difference between cycles for the
previous and current cycle using high-precision system
time, in nanoseconds.
It should be noted, the frequency is based on the
script defined cycle time, which in this case is
10 miliseconds. Based on testing 10 Miliseconds is the
fastest cycle supported in OBS lua.
----------------------------------------------------------
]]
function get_frequency( previous )
local ns = obs.os_gettime_ns()
ns_last = ns
local f = ( ns/1000000000.0 ) - ( previous/1000000000.0 )
if f > 1 then f = time_frequency end
return f
end
--[[
----------------------------------------------------------
Convert Seconds to hours:minutes:seconds:miliseconds
----------------------------------------------------------
]]
function TimeFormat( time, notrim )
if time == nil then
return
end
local trim = ( timer_trim == 1 )
local hour, minutes, seconds, mili = 0, 0, 0, 0
hour = math.floor( time/3600 )
if hour < 10 and trim then
hour = "0"..hour
end
minutes = math.floor( ( time - math.floor( time/3600 )*3600 )/60 )
if minutes < 10 and trim then
minutes = "0"..minutes
end
seconds = math.floor( time - math.floor( time/3600 )*3600 - math.floor( ( time - math.floor( time/3600 )*3600 )/60 )*60 )
if seconds < 10 and trim then
seconds = "0"..seconds
end
mili = math.floor( ( time - math.floor( time/3600 )*3600 - math.floor( ( time - math.floor( time/3600 )*3600 )/60 )*60 - math.floor( time - math.floor( time/3600 )*3600 - math.floor( ( time - math.floor( time/3600 )*3600 )/60 )*60 ) )*100 )
if mili < 10 and trim then
mili = "0"..mili
end
if notrim then
return trim_time( hour, minutes, seconds, nil, true )
end
return trim_time( hour, minutes, seconds, ( ( timer_trim ~= 3 ) and mili or nil ), trim )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function trim_time( hour, minutes, seconds, mili, trim )
local format_hour,
format_minutes,
format_seconds,
format_mili =
( hour and "%02d" or "" ),
( minutes and ":%02d" or "" ),
( seconds and ":%02d" or "" ),
( mili and ",%02d" or "" )
local time = string.format( format_hour..format_minutes..format_seconds..format_mili, hour, minutes, seconds, mili )
if trim then
return time
end
if hour == 0 then
time = string.format( "%02d:%02d"..format_mili, minutes, seconds, mili )
end
if hour == 0 and minutes == 0 then
time = string.format( "%02d"..format_mili, seconds, mili )
end
if hour == 0 and minutes == 0 and seconds == 0 then
format_mili = ( mili and "%02d" or "" )
time = string.format( format_mili, mili )
end
return trim_zero( time )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function trim_zero( int )
while true do
if int:sub( 1,1 ) == '0' then
int = int:sub( 2 )
else
break
end
end
return int
end
--[[
----------------------------------------------------------
Function to set the time text
----------------------------------------------------------
]]
function set_split_text( source_name )
if source_name == 'Select' then
return
end
if reset_activated then
reset_activated = false
fresh_start( true )
end
local text = split_data
if text ~= last_split_data then
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
]]
local source = obs.obs_get_source_by_name( source_name )
if source ~= nil then
local settings = obs.obs_source_get_settings( source )
obs.obs_data_set_string( settings, "text", text )
end
obs.obs_source_update( source, settings )
obs.obs_data_release( settings )
obs.obs_source_release( source )
end
last_split_data = text
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function wait( ms )
if ms ~= nil then
local start = math.floor( ( obs.os_gettime_ns()/1000000 ) )
repeat until ( math.floor( ( obs.os_gettime_ns()/1000000 ) )-start ) >= ms
end
end
--[[
----------------------------------------------------------
"Timer Expires" = 1
"Caution Time" = 2
"Warning Time" = 3
"Timer Visible" = 4
"Timer Start" = 5
----------------------------------------------------------
]]
function record( mark, ms )
if timer_type ~= 2 then return end
if start_recording == 1 and mark == recording_type then
obs.obs_frontend_recording_start()
end
if ms ~= nil then wait( ms ) end
end
--[[
----------------------------------------------------------
Only used in Countdown mode
----------------------------------------------------------
]]
function timer_ended( source_name )
delayed_recording()
if next_scene == "" or next_scene == "Select" then
return
end
if next_scene ~= "TIMER END TEXT" then
set_visible( timer_source, false ) -- last thing, set visibility of timer to hide
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
]]
local source = obs.obs_get_source_by_name( next_scene )
obs.obs_frontend_set_current_scene( source )
obs.obs_source_release( source )
fresh_start( true )
--obs.remove_current_callback()
end
if next_scene == "TIMER END TEXT" then
local text = stop_text
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
]]
local source = obs.obs_get_source_by_name( source_name )
if source ~= nil then
local settings = obs.obs_source_get_settings( source )
obs.obs_data_set_string( settings, "text", text )
end
obs.obs_source_update( source, settings )
obs.obs_data_release( settings )
obs.obs_source_release( source )
end
end
--[[
----------------------------------------------------------
Function to set the time text
----------------------------------------------------------
]]
function set_time_text( source_name )
if reset_activated then
reset_activated = false
fresh_start( true )
end
if cur_seconds <= 0.01 and timer_type ~= 1 then cur_seconds = 0 end
local text = tostring( TimeFormat( cur_seconds ) )
text = text_prefix .. text
if text ~= last_text then
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
]]
local source = obs.obs_get_source_by_name( source_name )
if source ~= nil then
local settings = obs.obs_source_get_settings( source )
obs.obs_data_set_string( settings, "text", text )
if not media['caution_activated'] and not media['warning_activated'] then
obs.obs_data_set_int( settings, "color", media['normal_color'] )
end
media_activate( settings, 'caution' )
media_activate( settings, 'warning' )
end
obs.obs_source_update( source, settings )
obs.obs_data_release( settings )
obs.obs_source_release( source )
end
stop_media( 'caution' )
stop_media( 'warning' )
last_text = text
if cur_seconds <= 0.01 and timer_type ~= 1 then
activate( false )
--[[
Timer Ended
]]--
timer_ended( source_name )
end
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function media_activate( settings, ref )
if TimeFormat( cur_seconds, true ) == media[ref..'_text'] then
obs.obs_data_set_int( settings, "color", media[ref..'_color'] )
media['cur_seconds_'..ref] = cur_seconds
media[ref..'_activated'] = true
start_media( media['source_name_audio_'..ref], ref )
if ref == 'caution' then record( 2 ) end
if ref == 'warning' then record( 3 ) end
end
return settings
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function start_media( source_name, ref )
start_media_action( source_name, ref )
end
--[[
----------------------------------------------------------
Set source visble = true
----------------------------------------------------------
]]
function start_media_action( source_name, ref )
if source_name == nil or source_name == "None" then
return
end
if media[ref..'_activated'] then set_visible( source_name, true ) end
end
--[[
----------------------------------------------------------
Stop Media is designed to reset the Source to the
starting state. In other words, make the source
invisible again. This sould only happen if the media
ended, or if it is looped, end the media after a
defined time.
----------------------------------------------------------
]]
function stop_media( ref, bypass )
if bypass then -- No checks, just stop it
set_visible( media['source_name_audio_'..ref], false )
else -- do some checks
stop_media_action( ref )
end
end
--[[
----------------------------------------------------------
Check if the source state changed,
if so, set source visble = false
----------------------------------------------------------
]]
function stop_media_action( ref )
local source_name = media['source_name_audio_'..ref]
if source_name == nil or source_name == "None" then
return
end
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
we got a source name, let's see if it exist...
]]
local source = obs.obs_get_source_by_name( source_name )
if source ~= nil then -- source is valid
local state = obs.obs_source_media_get_state( source ) -- get the current state for the source
if media['last_state_'..ref] ~= state then -- The state has changed
if get_source_looping( source_name ) then
--log( 'is looped', source_name )
if state == obs.OBS_MEDIA_STATE_PLAYING then
local time_remaining = math.floor( media['cur_seconds_'..ref] ) + math.floor( media[ref..'_duration'] ) - math.floor( cur_seconds )
-- The source is looping, it will never stop
if source_name == media['source_name_audio_'..ref] then
local time_end = ( time_remaining == 0 )
if time_end then
media['last_state_'..ref] = state
set_visible( source_name, false )
end
end
end
else
--log( 'not looped', source_name )
media['last_state_'..ref] = state
if state == obs.OBS_MEDIA_STATE_STOPPED or state == obs.OBS_MEDIA_STATE_ENDED then
set_visible( source_name, false )
end
end
end
end
obs.obs_source_release( source )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function get_source_looping( source_name )
local property = "looping"
--[[
Increments the source reference counter,
use obs_source_release() to release it when complete.
we got a source name, let's see if it exist...
]]
local source = obs.obs_get_source_by_name( source_name )
local enabled = false
if source ~= nil then
local source_id = obs.obs_source_get_unversioned_id( source )
if source_id == "ffmpeg_source" then
local s = obs.obs_source_get_settings( source )
--local prop = obs.obs_data_get_string( s, property )
enabled = obs.obs_data_get_bool( s, property )
obs.obs_data_release( s )
end
end
obs.obs_source_release( source )
return enabled
end
--[[
----------------------------------------------------------
set source visibility
----------------------------------------------------------
]]
--[[
----------------------------------------------------------
set source visibility
----------------------------------------------------------
]]
function set_visible( target_name, visible )
local scenes = obs.obs_frontend_get_scenes()
if scenes ~= nil then
for i, scn in ipairs( scenes ) do
local scene = obs.obs_scene_from_source( scn )
local sceneitem = obs.obs_scene_find_source_recursive( scene, target_name )
if sceneitem ~= nil then
obs.obs_sceneitem_set_visible( sceneitem, visible )
break
end
end --end for
obs.bfree( scn )
obs.source_list_release( scenes )
end
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function delayed_recording()
obs.timer_add( recording_callback, 100 ) --<- milliseconds
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function recording_callback()
obs.timer_remove( recording_callback )
record( 1 )
end
--[[
----------------------------------------------------------
Add timer here so we have a global setting
----------------------------------------------------------
]]
function start_timer()
record( 5, 100 ) -- wait 100 miliseconds
timer_active = true
fresh_start( false )
obs.timer_add( timer_callback, timer_cycle ) --<- milliseconds
end
--[[
----------------------------------------------------------
Add timer here so we have a global setting
----------------------------------------------------------
]]
function timer_callback()
time_frequency = get_frequency( ns_last )
calculate()
completed_cycles = completed_cycles + 1
set_time_text( timer_source )
--log( 'Applied frequency', time_frequency )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function calculate()
if timer_type ~= 2 then
cur_seconds = cur_seconds + time_frequency
else
cur_seconds = cur_seconds - time_frequency
end
end
--[[
----------------------------------------------------------
Called if the counter is starting fresh
----------------------------------------------------------
]]
function fresh_start( reset_curent )
if reset_curent ~= nil then
if reset_curent then
cur_seconds = def_seconds
completed_cycles = 0
split = 0
split_itm = {}
split_data = nil
media['caution_activated'] = false
media['warning_activated'] = false
end
end
orig_time = obs.os_gettime_ns()
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function activate( activating )
if disable_script then
return
end
activated = activating
if activating then
--obs.obs_frontend_recording_start()
start_timer()
else
timer_active = false
obs.timer_remove( timer_callback )
stop_media( 'caution',true )
stop_media( 'warning',true )
end
end
--[[
----------------------------------------------------------
Called when a source is activated/deactivated
----------------------------------------------------------
]]
function activate_signal( cd, activating )
local source = obs.calldata_source( cd, "source" )
if source ~= nil then
local name = obs.obs_source_get_name( source )
if ( name == timer_source ) then
if activating then record( 4, 300 ) end
if start_on_visible then
fresh_start( true )
activate( activating )
end
end
end
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function source_activated( cd )
if disable_script then
return
end
activate_signal( cd, true )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function source_deactivated( cd )
activate_signal( cd, false )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function reset( pressed )
if not pressed then
return
end
reset_activated = true
set_time_text( timer_source )
activate( false )
set_split_text( split_source )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function reset_button_clicked( props, p )
reset( true )
return false
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function pause_button_clicked( props, p )
on_pause( true )
return false
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function on_pause( pressed )
if not pressed then
return
end
set_visible( timer_source, true )
if timer_active then
timer_active = false
activate( false )
split_unpack()
set_split_text( split_source )
--log( 'OBS Video Frame Time', obs.obs_get_video_frame_time() )
--log( completed_cycles..' Cycles', get_time_lapsed() )
else
if activated == false then
activate( true )
end
end
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function split_button_clicked( props, p )
on_split( true )
return false
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function on_split( pressed )
if not pressed then
return
end
if timer_active then
split = split + 1
split_itm[split] = cur_seconds
split_unpack()
set_split_text( split_source )
end
end
--[[
----------------------------------------------------------
This captures the split times and unpack it in the
correct format.
The text source only permits linebreaks ( '\n' ) this
limitation affects how the data can be formated ):
----------------------------------------------------------
]]
function split_unpack()
local data = nil
local c = table.getn( split_itm )
local text = ''
local title = ''
local subtitle = ''
local line = '______________________________'
for i = 1,c do
local mark = split_itm[i]
local interval = mark
if i > 1 then
local j = i - 1
interval = split_itm[i] - split_itm[j]
end
if split_type == 'Interval' then
title = 'Interval'
--subtitle = ''
text = tostring( TimeFormat( interval ) )
elseif split_type == 'Mark' then
title = 'Mark'
--subtitle = ''
text = tostring( TimeFormat( mark ) )
elseif split_type == 'Mark Interval' then
title = 'Mark '
subtitle = 'Interval'
text = tostring( TimeFormat( mark )..' '..TimeFormat( interval ) )
else -- "Interval Mark"
title = 'Interval '
subtitle = 'Mark'
text = tostring( TimeFormat( interval )..' '..TimeFormat( mark ) )
end
-- data collection here
local n = i --formatting the index number
if i < 10 then n = '0'..tostring( i ) end
if data ~= nil then
data = data .. '\n' .. n..' ) '..text
else
data = '# '..title..subtitle..'\n'..line..'\n\n'..n..' ) '..text
end
end -- end for
split_data = data
end
--[[
--------------------------------------------------------------------
--------------------------------------------------------------------
]]
function pairsByKeys(t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
--[[
--------------------------------------------------------------------
custom function: helper
----------------------------------------------------------
]]
function in_table( tbl, value )
--if debug > 1 then print( "function: in_table()" ) end
local found = false
for k, v in pairs( tbl ) do
if value == v then
found = true
break
end
end
return found
end
--[[
----------------------------------------------------------
Callback on list modification
----------------------------------------------------------
]]
function property_visibility( props, property, settings )
-- Retrieves value selected in list
local config = obs.obs_data_get_int( settings, "config" )
local mode = obs.obs_data_get_int( settings, "timer_type" )
local rec = obs.obs_data_get_int( settings, "start_recording" )
local scene = obs.obs_data_get_string( settings, "next_scene" )
obs.obs_property_set_visible( obs.obs_properties_get( props, "stop_text" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "text_prefix" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "recording_type" ), false )
if scene == 'TIMER END TEXT' and mode == 2 then
obs.obs_property_set_visible( obs.obs_properties_get( props, "stop_text" ), true )
end
-- Preset parameters
if rec == 1 and mode == 2 then
obs.obs_property_set_visible( obs.obs_properties_get( props, "recording_type" ), config == 2 )
end
if mode == 2 then
obs.obs_property_set_visible( obs.obs_properties_get( props, "text_prefix" ), config == 2 )
obs.obs_property_set_description( obs.obs_properties_get( props, "pause_button" ), "Start/Pause Countdown" )
obs.obs_property_set_description( obs.obs_properties_get( props, "reset_button" ), "Reset Countdown" )
obs.obs_property_set_visible( obs.obs_properties_get( props, "start_recording" ), config == 2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "next_scene" ), config == 2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "hours" ), config == 2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "minutes" ), config == 2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "seconds" ), config == 2 )
else
obs.obs_property_set_description( obs.obs_properties_get( props, "pause_button" ), "Start/Pause Stopwatch" )
obs.obs_property_set_description( obs.obs_properties_get( props, "reset_button" ), "Reset Stopwatch" )
obs.obs_property_set_visible( obs.obs_properties_get( props, "start_recording" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "next_scene" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "hours" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "minutes" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "seconds" ), false )
end
obs.obs_property_set_visible( obs.obs_properties_get( props, "split_button" ), mode==1 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "split_type" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "split_source" ), false )
obs.obs_property_set_visible( obs.obs_properties_get( props, "timer_trim" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "audio_caution" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "audio_warning" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "caution_duration" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "warning_duration" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "normal_color" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "caution_color" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "warning_color" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "caution_text" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "warning_text" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "start_on_visible" ), config==2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "disable_script" ), config==2 )
if mode == 1 then
obs.obs_property_set_visible( obs.obs_properties_get( props, "split_type" ), config == 2 )
obs.obs_property_set_visible( obs.obs_properties_get( props, "split_source" ), config == 2 )
end
-- IMPORTANT: returns true to trigger refresh of the properties
return true
end
--[[
----------------------------------------------------------
A function named script_properties defines the properties that the user
can change for the entire script module itself
----------------------------------------------------------
]]
function script_properties()
local props = obs.obs_properties_create()
local p_a = obs.obs_properties_add_list( props, "timer_type", "<b>Timer Type</b>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
t_type = {"Stopwatch", "Countdown"}
for i,v in ipairs( t_type ) do obs.obs_property_list_add_int( p_a, v, i ) end
local p_b = obs.obs_properties_add_list( props, "config", "<font color=".. font_dimmed ..">Configuration</font>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
t_type = {"Basic", "Advanced"}
for i,v in ipairs( t_type ) do obs.obs_property_list_add_int( p_b, v, i ) end
--[[
Returns an array of reference-incremented sources.
Release with source_list_release().
]]
local sources = obs.obs_enum_sources()
local p_c = obs.obs_properties_add_list( props, "timer_source", "<i>Timer Source</i>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
obs.obs_property_list_add_string( p_c, "Select", "select" )
local list = {}
if sources ~= nil then
for _, source in ipairs( sources ) do
source_id = obs.obs_source_get_unversioned_id( source )
if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
local name = obs.obs_source_get_name( source )
if name ~= split_source then
--[[
add it to list so that it can be reordered
]]
list[name] = name
else
--continue
end
end
end
obs.bfree(source)
for key, value in pairsByKeys( list ) do
--[[
add item to property list
]]
obs.obs_property_list_add_string( p_c, value, value )
end
end
local p_n = obs.obs_properties_add_int( props, "hours", "<font color=".. font_dimmed ..">Hours</font>", 0, 23, 1 )
obs.obs_property_int_set_suffix( p_n, " Hours" )
local p_o = obs.obs_properties_add_int( props, "minutes", "<font color=".. font_dimmed ..">Minutes</font>", 0, 59, 1 )
obs.obs_property_int_set_suffix( p_o, " Minutes" );
local p_p = obs.obs_properties_add_int( props, "seconds", "<font color=".. font_dimmed ..">Seconds</font>", 0, 59, 1 )
obs.obs_property_int_set_suffix( p_p, " Seconds" );
local p_m = obs.obs_properties_add_list( props, "timer_trim", "Timer Format", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
t_type = {"Display full format", "Remove leading zeros", "No leading zeros, no split seconds"}
for i,v in ipairs( t_type ) do obs.obs_property_list_add_int( p_m, v, i ) end
local p_d = obs.obs_properties_add_list( props, "split_source", "<i>Split Source</i>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
obs.obs_property_list_add_string( p_d, "Select", "select" )
list = {}
if sources ~= nil then
for _, source in ipairs( sources ) do
source_id = obs.obs_source_get_unversioned_id( source )
if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
local name = obs.obs_source_get_name( source )
if name ~= timer_source then
--[[
add it to list so that it can be reordered
]]
list[name] = name
else
--continue
end
end
end
obs.bfree(source)
for key, value in pairsByKeys(list) do
--[[
add item to property list
]]
obs.obs_property_list_add_string( p_d, value, value )
end
end
local p_e = obs.obs_properties_add_list( props, "split_type", "Split Type", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
t_type = {"Interval", "Mark", "Mark Interval", "Interval Mark"}
for i,v in ipairs( t_type ) do obs.obs_property_list_add_int( p_e, v, i ) end
obs.obs_property_set_long_description( p_e, "\nInterval = Time between current and previous split.\n\nMark = Time of split\n" )
local p_f = obs.obs_properties_add_list( props, "audio_caution", "<font color=".. font_dimmed ..">Caution Audio</font>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
obs.obs_property_list_add_string( p_f, "None", "none" )
if sources ~= nil then
for _, source in ipairs( sources ) do
source_id = obs.obs_source_get_unversioned_id( source )
if source_id == "ffmpeg_source" then
local name = obs.obs_source_get_name( source )
obs.obs_property_list_add_string( p_f, name, name )
end
end
obs.bfree(source)
end
local p_g = obs.obs_properties_add_list( props, "audio_warning", "<font color=".. font_dimmed ..">Warning Audio</font>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
obs.obs_property_list_add_string( p_g, "None", "none" )
if sources ~= nil then
for _, source in ipairs( sources ) do
source_id = obs.obs_source_get_unversioned_id( source )
if source_id == "ffmpeg_source" then
local name = obs.obs_source_get_name( source )
obs.obs_property_list_add_string( p_g, name, name )
end
end
obs.bfree(source)
end
obs.source_list_release( sources )
obs.obs_properties_add_color( props, "normal_color", "Normal Color" )
obs.obs_properties_add_color( props, "caution_color", "Caution Color" )
obs.obs_properties_add_color( props, "warning_color", "Warning Color" )
local p_h = obs.obs_properties_add_text( props, "caution_text", "<font color=".. font_dimmed ..">Caution Time</font>", obs.OBS_TEXT_DEFAULT )
obs.obs_property_set_long_description( p_h, "\nUse format 00:00:00 ( hoursa:minutes:seconds )\n" )
local p_i = obs.obs_properties_add_text( props, "warning_text", "<font color=".. font_dimmed ..">Warning Time</font>", obs.OBS_TEXT_DEFAULT )
obs.obs_property_set_long_description( p_i, "\nUse format 00:00:00 ( hoursa:minutes:seconds )\n" )
--*props, *name, *description, min, max, step
obs.obs_properties_add_int_slider( props, "caution_duration", "Caution Duration", 1, 100, 1 )
obs.obs_properties_add_int_slider( props, "warning_duration", "Warning Duration", 1, 100, 1 )
local p_j = obs.obs_properties_add_list( props, "start_recording", "Auto Recording", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_INT )
t_type = {"Yes", "No"}
for i,v in ipairs( t_type ) do obs.obs_property_list_add_int( p_j, v, i ) end