-
Notifications
You must be signed in to change notification settings - Fork 85
/
StopWatch.version.3.4.lua
1999 lines (1954 loc) · 76.8 KB
/
StopWatch.version.3.4.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 = "3.4"
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 = ""
custom_time_format = ""
font_normal = "#ffffff"
font_dimmed = "#bfbbbf"
timer_source = ""
countdown_type = ""
backup_folder = ""
import_list = ""
output_file_name = "-backup($date_stamp).json";
longtimetext_s = ""
longtimetext_p = ""
last_split_data = ""
split_type = ""
split_source = ""
active_source = ""
next_scene = ""
stop_text = ""
cur_seconds = 0
def_seconds = 0
split = 0
timer_year = 0
timer_month = 0
timer_day = 0
timer_hours = 0
timer_minutes = 0
timer_seconds = 0
timer_type = 0
start_recording = 0
recording_type = 0
trigger_text = 1
orig_time = 0
time_frequency = 0
completed_cycles = 0
ns_last = 0
cycle_index = 0
timer_cycle = 10 --milliseconds
split_itm = {}
split_data = nil
script_settings = nil
props = nil
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 = "",
caution_note_source = "",
warning_note_source = "",
caution_note = "",
warning_note = "",
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
----------------------------------------------------------
]]
function log( name, msg )
if msg ~= nil then
msg = " > " .. tostring( msg )
else
msg = ""
end
obs.script_log( obs.LOG_DEBUG, name .. msg )
end
--[[
----------------------------------------------------------
-- Get the name of this script
----------------------------------------------------------
]]
function filename()
local str = debug.getinfo(2).source:sub(2)
return str:match("^.*/(.*).lua$") or str
end
--[[
----------------------------------------------------------
A function named script_description returns the description shown to
the user
----------------------------------------------------------
]]
function script_description()
return string.format( desc, tostring( gversion ) )
end
--[[
----------------------------------------------------------
--
----------------------------------------------------------
]]
function get_filenames( path )
local filenames = {}
local dir = obs.os_opendir( path )
local entry
repeat
entry = obs.os_readdir(dir)
if entry then
local ext = obs.os_get_path_extension( entry.d_name )
if ext == ".json" then
local filename = string.gsub( entry.d_name, ext, "" )
table.insert( filenames, filename )
end
end
until not entry
obs.os_closedir( dir )
return filenames
end
--[[
----------------------------------------------------------
--
----------------------------------------------------------
]]
function write_to_json( data )
output_folder = backup_folder
-- convert Windows path to UNIX path
local file_name = filename() .. output_file_name:gsub("$date_stamp", os.date("%Y-%m-%d-%H%M"))
-- set output path as the script path by default
local script_path = script_path();
local output_path = script_path .. file_name;
-- if specified output path exists, then set this as the new output path
if (output_folder ~= "") then
output_path = output_folder .. "/" .. file_name
else
output_path = script_path .. file_name;
end
output_path = output_path:gsub([[\]], "/");
obs.obs_data_erase( data, "backup_folder" )
obs.obs_data_erase( data, "backup_mode" )
return obs.obs_data_save_json( data, output_path )
end
--[[
----------------------------------------------------------
Assign a default Frequency based on the Frame Rate
video_info.base_width
video_info.base_height
video_info.fps_den
video_info.output_width
video_info.output_height
video_info.range
video_info.colorspace
----------------------------------------------------------
]]
function assign_default_frequency()
local fps = 60 -- 60 is the maximum supported frame rate
local video_info = obs.obs_video_info()
if obs.obs_get_video_info(video_info) then
fps = video_info.fps_num
end
time_frequency = ( 1/fps )
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 debug
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 delta_time(year, month, day, hour, minute, second)
local now = os.time()
if (year == -1) then
year = os.date("%Y", now)
end
if (month == -1) then
month = os.date("%m", now)
end
if (day == -1) then
day = os.date("%d", now)
end
local future = os.time{year=year, month=month, day=day, hour=hour, min=minute, sec=second}
local seconds = os.difftime(future, now)
if (seconds < 0) then
seconds = 0
end
return seconds
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 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" and next_scene ~= "Source List" and next_scene ~= "Scene List" 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 == "Source List" then
reset( true )
cycle_list_activate( 'source' )
on_pause( true )
end
if next_scene == "Scene List" then
reset( true )
cycle_list_activate( 'scene' )
on_pause( true )
end
if next_scene == "TIMER END TEXT" then
local text = stop_text
set_text( source_name, text )
end
end
--[[
----------------------------------------------------------
Function
----------------------------------------------------------
]]
function cycle_list_activate( source_type )
local list = {}
if source_type ~= "source" then
local scenes = obs.obs_frontend_get_scene_names( )
if scenes ~= nil then
for _, scene in pairs( scenes ) do
if name ~= scene then
list[scene] = scene
end
end
obs.bfree( scene )
end
else
list = {}
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, source in pairs( sources ) do
local name = obs.obs_source_get_name( source )
list[name] = name
end
end
obs.source_list_release( sources )
end
cycle_list = obs.obs_data_get_array( script_settings, "cycle_list" )
local count = obs.obs_data_array_count(cycle_list) - 1;
for i = 0, count do
local item = obs.obs_data_array_item(cycle_list, cycle_index);
local value = obs.obs_data_get_string(item, "value");
cycle_index = cycle_index + 1
if cycle_index > count then
cycle_index = 0
end
if list[value] ~= nil then
if source_type ~= "source" then
local scene_source = obs.obs_frontend_get_current_scene()
local name = obs.obs_source_get_name( scene_source )
obs.obs_source_release( scene_source )
if name == list[value] then
-- goto next
else
local source = obs.obs_get_source_by_name( list[value] )
if source ~= nil then
obs.obs_frontend_set_current_scene( source )
end
obs.obs_source_release( source )
set_text( active_source, list[value] )
break
end
else
if is_visible(list[value]) then
-- goto next
else
set_visible( list[value], true )
set_text( active_source, list[value] )
break
end
end
end
end
obs.obs_data_array_release(cycle_list)
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
--[[
If there is more than 24 hours in the time value
we need to remove the additional time value to leave only a 23:59:59
value. We will do this by calculating days
]]
-- If there is more than 24 hours, remove 23:59:59 as it will be in the clock
if time > 86399 then -- 23:59:59
local c_time = ( math.floor( (time ) / 86400 ) * 86400 )
time = time - c_time
end
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 or ( timer_trim == 4 ) then
return trim_time( hour, minutes, seconds, nil, true )
end
if ( timer_trim == 5 ) then
return trim_time( hour, minutes, seconds, mili, true )
end
return trim_time( hour, minutes, seconds, ( ( timer_trim ~= 3 ) and mili or nil ), trim )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function LongTimeFormat( time )
local c_time = time
-- If there is more than 24 hours, remove 23:59:59 as it will be in the clock
if time > 86399 then -- 23:59:59
c_time = math.floor( ( time ) / 86400)
end
if time < 86400 then
c_time = 0
end
return c_time
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function explode(str,delim)
local t, ll
t={}
ll=0
if(#str == 1) then
return {str}
end
while true do
l = string.find(str, delim, ll, true) -- find the next d in the string
if l ~= nil then -- if "not not" found then..
table.insert(t, string.sub(str,ll,l-1)) -- Save it in our array.
ll = l + 1 -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(str,ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function format_time_stamp( timestamp, format )
local table1 = explode( timestamp, ':' )
if table1 == nil then return timestamp end
local c = tablelength( table1 )
local _, d = timestamp:gsub(":","")
local _, t = format:gsub("$T","")
local day, hour, minute, seconds, mili = 0, 0, 0, 0, 0
if d == 3 then
if tableHasKey( table1, 1 ) then
day = table1[1]
end
if tableHasKey( table1, 2 ) then
hour = table1[2]
end
if tableHasKey( table1, 3 ) then
minute = table1[3]
end
if tableHasKey( table1, 4 ) then
seconds = table1[4]
local table2 = explode( table1[4], ',' )
if tableHasKey( table2, 1 ) and tableHasKey( table2, 2 ) then
seconds = table2[1]
mili = table2[2]
end
end
end
if d == 2 then
if tableHasKey( table1, 1 ) then
hour = table1[1]
end
if tableHasKey( table1, 2 ) then
minute = table1[2]
end
if tableHasKey( table1, 3 ) then
seconds = table1[3]
local table2 = explode( table1[3], ',' )
if tableHasKey( table2, 1 ) and tableHasKey( table2, 2 ) then
seconds = table2[1]
mili = table2[2]
end
end
end
if d == 1 then
if tableHasKey( table1, 1 ) then
minute = table1[1]
end
if tableHasKey( table1, 2 ) then
seconds = table1[2]
local table2 = explode( table1[2], ',' )
if tableHasKey( table2, 1 ) and tableHasKey( table2, 2 ) then
seconds = table2[1]
mili = table2[2]
end
end
end
if tonumber(day) < 10 then
day = "0"..day
end
timestamp = format:gsub("$T", ""):gsub("$D", day):gsub( "$H", hour):gsub("$M", minute):gsub("$S", seconds):gsub("$F", mili)
if t ~= 0 and cur_seconds > 0.01 then
--local reg = '^[0]+[:]?[0]+[:]?[0]+[:]?[0]?'
local reg = "^[0:,]*" -- close, but misses 1 instance
timestamp = timestamp:gsub(reg,"")
end
if cur_seconds < 0.01 then -- else it will show 00:00:00
timestamp = ""
end
return timestamp
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 ) )
if timer_trim ~= 5 then
--[[
Format the Text 'Day/Days'
]]
if timer_type == 2 and countdown_type == 1 and cur_seconds ~= 0 then
local longtimetext = longtimetext_p
if math.floor( cur_seconds / 86400 ) <= 1 then
longtimetext = longtimetext_s
end
if math.floor( cur_seconds / 86400 ) <= 0 then
longtimetext = longtimetext_p
end
text = string.gsub(longtimetext .. text, "[#]", LongTimeFormat( cur_seconds ))
end
else
local l_time = LongTimeFormat( cur_seconds )
local t_time = TimeFormat( cur_seconds )
text = format_time_stamp( ( l_time ~= 0 ) and string.format("%s:%s", l_time, t_time ) or string.format("%s", t_time ), custom_time_format )
end
if timer_type ~= 2 then
--text_prefix = ""
-- text_suffix = ""
end
text = text_prefix .. text .. text_suffix
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, true )
--[[
Timer Ended
]]--
if trigger_text ~= 1 then
set_visible( media["caution_note_source"], false )
set_visible( media["warning_note_source"], false )
end
timer_ended( source_name )
end
end
--[[
----------------------------------------------------------
Function to set the split 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
set_text( source_name, text )
end
last_split_data = text
end
--[[
----------------------------------------------------------
Function to set the source text
----------------------------------------------------------
]]
function set_text( source_name, text )
if source_name == 'Select' or source_name == 'select'then
return
end
--[[
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
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function set_text_note_color( ref )
if media[ref .. "_note_source"] == 'Select' then return end
local source = obs.obs_get_source_by_name( media[ref .. "_note_source"] )
if source ~= nil then
local settings = obs.obs_source_get_settings( source )
obs.obs_data_set_string( settings, "text", media[ref..'_note'] )
obs.obs_data_set_int( settings, "color", media[ref..'_color'] )
end
obs.obs_source_update( source, settings )
obs.obs_data_release( settings )
obs.obs_source_release( source )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function media_activate( settings, ref )
if TimeFormat( cur_seconds, true ) == media[ref..'_text'] then
if trigger_text ~= 1 and ref == 'caution' then
set_visible( media[ref .. "_note_source"], true )
set_visible( media["warning_note_source"], false )
set_text_note_color( ref )
end
if trigger_text ~= 1 and ref == 'warning' then
set_visible( media[ref .. "_note_source"], true )
set_visible( media["caution_note_source"], false )
set_text_note_color( ref )
end
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
-- The source is looping, it will never stop
if source_name == media['source_name_audio_'..ref] then
local time_remaining = math.floor( media['cur_seconds_'..ref] ) + math.floor( media[ref..'_duration'] ) - math.floor( cur_seconds )
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
----------------------------------------------------------
]]
function set_visible( target_name, visible )
if in_table( {'','none', 'select'}, target_name ) then return end
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
--[[
----------------------------------------------------------
check source visibility
----------------------------------------------------------
]]
function is_visible( target_name )
local isvisible = false
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
isvisible = obs.obs_sceneitem_visible( sceneitem )
break
end
end --end for
obs.bfree( scn )
obs.source_list_release( scenes )
end
return isvisible
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 timer_type == 2 and countdown_type == 1 then
cur_seconds = delta_time( timer_year, timer_month, timer_day, timer_hours, timer_minutes, timer_seconds)
def_seconds = cur_seconds
end
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()
set_visible( media["caution_note_source"], false )
set_visible( media["warning_note_source"], false )
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function timer_caution_media_end_callback()
set_visible( media['source_name_audio_caution'], false )
obs.remove_current_callback()
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function timer_warning_media_end_callback()
set_visible( media['source_name_audio_warning'], false )
obs.remove_current_callback()
end
--[[
----------------------------------------------------------
----------------------------------------------------------
]]
function disconnect_after_media_end( ref )
local source_name = media['source_name_audio_'..ref]