-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.php
More file actions
1375 lines (1265 loc) · 72.3 KB
/
content.php
File metadata and controls
1375 lines (1265 loc) · 72.3 KB
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
<?php
include_once("/opt/fpp/www/common.php");
$pluginName = "fpp-plugin-BackgroundMusic";
$pluginConfigFile = $settings['configDirectory'] . "/plugin." . $pluginName;
// Load current settings
if (file_exists($pluginConfigFile)){
if (is_readable($pluginConfigFile)) {
$pluginSettings = parse_ini_file($pluginConfigFile);
if ($pluginSettings === false) {
error_log("BackgroundMusic: parse_ini_file failed for $pluginConfigFile");
$pluginSettings = array();
} else {
error_log("BackgroundMusic: Successfully loaded config with " . count($pluginSettings) . " settings");
// Trim whitespace from all values
$pluginSettings = array_map('trim', $pluginSettings);
}
} else {
error_log("BackgroundMusic: Config file exists but is not readable: $pluginConfigFile");
$pluginSettings = array();
}
} else {
error_log("BackgroundMusic: Config file does not exist: $pluginConfigFile");
$pluginSettings = array();
}
// Get audio files from music directory
function getAudioFiles() {
$musicDir = '/home/fpp/media/music';
$audioFiles = array();
if (is_dir($musicDir)) {
$files = scandir($musicDir);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, array('mp3', 'wav', 'ogg', 'm4a', 'flac'))) {
$audioFiles[] = $file;
}
}
}
sort($audioFiles);
}
return $audioFiles;
}
// Get playlists
function getPlaylists() {
$ch = curl_init('http://localhost/api/playlists');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data, true);
// FPP API returns a simple array of playlist names
if (is_array($result)) {
return $result;
}
return array();
}
// Check if a playlist contains only media items (no sequences) and is not empty
function isMediaOnlyPlaylist($playlistName) {
$ch = curl_init('http://localhost/api/playlist/' . rawurlencode($playlistName));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$playlist = json_decode($data, true);
if (!isset($playlist['mainPlaylist']) || !is_array($playlist['mainPlaylist'])) {
return false;
}
// Check if playlist has items (don't rely on 'empty' flag - it can be incorrect for m4a files)
if (count($playlist['mainPlaylist']) == 0) {
return false;
}
// Check if all items are media type
foreach ($playlist['mainPlaylist'] as $item) {
if (!isset($item['type']) || $item['type'] !== 'media') {
return false;
}
}
return true;
}
// Check if a playlist has any items
function hasPlaylistItems($playlistName) {
$ch = curl_init('http://localhost/api/playlist/' . rawurlencode($playlistName));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$playlist = json_decode($data, true);
// Check mainPlaylist array directly (don't rely on 'empty' flag - it can be incorrect for m4a files)
if (!isset($playlist['mainPlaylist']) || !is_array($playlist['mainPlaylist'])) {
return false;
}
return count($playlist['mainPlaylist']) > 0;
}
// Filter playlists - get media-only for background music, all for others
function getMediaOnlyPlaylists($allPlaylists) {
$mediaPlaylists = array();
foreach ($allPlaylists as $playlistName) {
if (isMediaOnlyPlaylist($playlistName)) {
$mediaPlaylists[] = $playlistName;
}
}
return $mediaPlaylists;
}
// Filter out empty playlists
function filterNonEmptyPlaylists($playlists) {
$nonEmptyPlaylists = array();
foreach ($playlists as $playlistName) {
if (hasPlaylistItems($playlistName)) {
$nonEmptyPlaylists[] = $playlistName;
}
}
return $nonEmptyPlaylists;
}
$allPlaylists = getPlaylists();
// Filter out empty playlists from all playlists first
$allPlaylists = filterNonEmptyPlaylists($allPlaylists);
// Then get media-only playlists (which already excludes empty ones)
$mediaOnlyPlaylists = getMediaOnlyPlaylists($allPlaylists);
// Get audio files for PSA configuration
$audioFiles = getAudioFiles();
?>
<style>
.settingsTable {
width: 100%;
max-width: 800px;
margin: 20px auto;
}
.settingsTable td {
padding: 10px;
}
.settingsTable .label {
width: 40%;
font-weight: bold;
text-align: right;
}
.settingsTable .value {
width: 60%;
}
.settingsTable select, .settingsTable input[type="number"], .settingsTable input[type="text"] {
width: 100%;
padding: 5px;
}
.settingsTable input[type="checkbox"] {
width: 18px;
height: 18px;
margin: 0;
vertical-align: middle;
cursor: pointer;
transform: scale(1.3);
margin-right: 8px;
}
.settingsTable label {
margin-left: 8px;
vertical-align: middle;
cursor: pointer;
}
.buttonRow {
text-align: center;
margin-top: 20px;
}
.description {
max-width: 800px;
margin: 20px auto;
padding: 15px;
background-color: #f0f8ff;
border: 1px solid #b0d4f1;
border-radius: 5px;
}
.config-header {
position: sticky;
top: 60px;
z-index: 100;
background-color: #fff;
padding: 15px 0;
margin-bottom: 20px;
border-bottom: 2px solid #e0e0e0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
</style>
<div id="global" class="settings">
<div class="config-header">
<div style="display: flex; justify-content: space-between; align-items: center;">
<h1 style="margin: 0;">Background Music Configuration</h1>
<div>
<button type="submit" form="settingsForm" class="btn btn-success" style="margin-right: 5px;">
<i class="fas fa-save"></i> Save Settings
</button>
<a href="plugin.php?_menu=status&plugin=fpp-plugin-BackgroundMusic&page=backgroundmusic.php" class="btn btn-outline-secondary" style="margin-right: 5px;">
<i class="fas fa-arrow-left"></i> Controller
</a>
<a href="plugin.php?plugin=fpp-plugin-BackgroundMusic&page=help%2Fbackgroundmusic-help.php" class="btn btn-outline-info">
<i class="fas fa-question-circle"></i> Help
</a>
</div>
</div>
</div>
<!-- Brightness Plugin Warning -->
<div style="background-color: #fff3cd; border: 2px solid #ffc107; border-radius: 5px; padding: 15px; margin: 20px auto; max-width: 800px;">
<h3 style="margin-top: 0; color: #856404;"><i class="fas fa-exclamation-triangle"></i> Required: fpp-brightness Plugin</h3>
<p style="margin-bottom: 10px;">
This plugin requires the <strong>fpp-brightness</strong> plugin to be installed on <strong>ALL controllers</strong>
in your setup for brightness transitions and MultiSync to work properly.
</p>
<p style="margin-bottom: 0;">
<strong>Installation:</strong> On each controller (master and remotes), go to <em>Plugin Manager → Install Plugins</em>
and search for "brightness". Install on <strong>every controller</strong> in your show, then restart FPPd.
</p>
</div>
<div class="description">
<p><strong>How it works:</strong></p>
<ul>
<li><strong>Background Music Source:</strong> Choose between a local FPP Playlist (audio files you upload) or an Internet Radio Stream (continuous online audio). The selected source plays over your existing pre-show sequence (already running via FPP scheduler).</li>
<li><strong>Show Playlist:</strong> Main show playlist to start after the fade transition.</li>
<li><strong>Fade Time:</strong> Duration to gradually fade out both brightness and background music before the show.</li>
<li><strong>Blackout Time:</strong> Silent period after fade before starting the show (for dramatic effect).</li>
<li><strong>Return to Pre-Show:</strong> After the show completes, background music (playlist or stream) automatically restarts. Your scheduler will handle restarting the pre-show sequence.</li>
<li><strong>Playlist Mode:</strong> Supports shuffle, track control, and playlist management.</li>
<li><strong>Stream Mode:</strong> Plays a continuous audio stream (no shuffle or track control). Auto-reconnects if the stream drops.</li>
<li><strong>Seamless Track Transitions:</strong> Enable crossfading to eliminate silence between tracks. The next track starts playing before the current one ends, creating smooth, continuous playback perfect for maintaining atmosphere.</li>
</ul>
<p><strong>Expected Setup:</strong> Your pre-show sequence (or non media playlist) should already be running (looping) via FPP's scheduler. This plugin adds background music on top.</p>
</div> <form id="settingsForm" onsubmit="return saveSettings();">
<!-- Background Music Source Selection -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #2196F3; border-bottom: 2px solid #2196F3; padding-bottom: 5px;">
<i class="fas fa-list"></i> Background Music Source Selection
</h3>
<table class="settingsTable">
<tr>
<td class="label">Background Music Source:</td>
<td class="value">
<select name="BackgroundMusicSource" id="BackgroundMusicSource" onchange="toggleBackgroundSource()">
<?php
$bgSource = isset($pluginSettings['BackgroundMusicSource']) ? $pluginSettings['BackgroundMusicSource'] : 'playlist';
?>
<option value="playlist" <?php echo ($bgSource == 'playlist') ? 'selected' : ''; ?>>FPP Playlist</option>
<option value="stream" <?php echo ($bgSource == 'stream') ? 'selected' : ''; ?>>Internet Stream</option>
</select>
<small>Choose between local playlist or internet audio stream</small>
</td>
</tr>
<tr id="playlistRow" style="<?php echo ($bgSource == 'stream') ? 'display: none;' : ''; ?>">
<td class="label">Background Music Playlist:</td>
<td class="value">
<select name="BackgroundMusicPlaylist" id="BackgroundMusicPlaylist">
<option value="">-- Select Playlist --</option>
<?php
$savedBgPlaylist = isset($pluginSettings['BackgroundMusicPlaylist']) ? $pluginSettings['BackgroundMusicPlaylist'] : '';
error_log("BackgroundMusic: Saved BG Playlist: '" . $savedBgPlaylist . "'");
foreach ($mediaOnlyPlaylists as $playlist) {
$selected = '';
if ($savedBgPlaylist != '' && $savedBgPlaylist == $playlist) {
$selected = 'selected';
error_log("BackgroundMusic: Matched BG playlist: '" . $playlist . "'");
}
echo '<option value="' . htmlspecialchars($playlist) . '" ' . $selected . '>' .
htmlspecialchars($playlist) . '</option>';
}
?>
</select>
<small>Audio playlist to play as background music (media-only playlists)</small>
</td>
</tr>
<tr id="streamRow" style="<?php echo ($bgSource == 'playlist') ? 'display: none;' : ''; ?>">
<td class="label">Stream URL:</td>
<td class="value">
<select name="BackgroundMusicStreamPreset" id="BackgroundMusicStreamPreset"
onchange="handleStreamPresetChange()" style="width: 100%; max-width: 600px; margin-bottom: 5px;">
<?php
$currentStreamURL = isset($pluginSettings['BackgroundMusicStreamURL']) ? $pluginSettings['BackgroundMusicStreamURL'] : '';
$isCustom = true;
// Load preset streams from JSON file
$presetsFile = __DIR__ . '/stream_presets.json';
$presets = array();
if (file_exists($presetsFile)) {
$jsonContent = file_get_contents($presetsFile);
$presetsData = json_decode($jsonContent, true);
if ($presetsData && isset($presetsData['presets'])) {
foreach ($presetsData['presets'] as $preset) {
if (isset($preset['url']) && isset($preset['name'])) {
$presets[$preset['url']] = $preset['name'];
}
}
}
}
// Fallback to hardcoded preset if JSON file doesn't exist or is invalid
if (empty($presets)) {
$presets = array(
'https://radio.themillerlights.com:8000/radio.mp3' => 'The Miller Lights Holiday Radio'
);
}
// Check if current URL matches a preset
foreach ($presets as $url => $name) {
if ($currentStreamURL === $url) {
$isCustom = false;
break;
}
}
?>
<option value="">-- Select Stream or Enter Custom --</option>
<?php foreach ($presets as $url => $name): ?>
<option value="<?php echo htmlspecialchars($url); ?>"
<?php echo ($currentStreamURL === $url) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($name); ?>
</option>
<?php endforeach; ?>
<option value="custom" <?php echo $isCustom && !empty($currentStreamURL) ? 'selected' : ''; ?>>
Custom URL...
</option>
</select>
<div id="customStreamURLContainer" style="<?php echo (!$isCustom || empty($currentStreamURL)) ? 'display: none;' : ''; ?> margin-top: 5px;">
<input type="text" id="BackgroundMusicStreamURLInput"
value="<?php echo $isCustom ? htmlspecialchars($currentStreamURL) : ''; ?>"
style="width: 100%; max-width: 600px;"
placeholder="https://example.com:8000/stream.mp3">
</div>
<small>Select a preset stream or enter your own URL (HTTP/HTTPS)</small>
</td>
</tr>
<tr id="shuffleRow" style="<?php echo ($bgSource == 'stream') ? 'display: none;' : ''; ?>">
<td class="label">Shuffle Music Playlist:</td>
<td class="value">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<input type="checkbox" name="ShuffleMusic" id="ShuffleMusic" value="1"
<?php echo (isset($pluginSettings['ShuffleMusic']) && $pluginSettings['ShuffleMusic'] == '1') ? 'checked' : ''; ?>>
<label for="ShuffleMusic" style="margin: 0; line-height: 1.4;">
Randomly shuffle background music tracks for variety
</label>
</div>
<small>Playlist is reshuffled each time it loops to avoid gaps</small>
</td>
</tr>
<tr id="crossfadeRow" style="<?php echo ($bgSource == 'stream') ? 'display: none;' : ''; ?>">
<td class="label">Seamless Track Transitions:</td>
<td class="value">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<input type="checkbox" name="EnableCrossfade" id="EnableCrossfade" value="1"
onchange="toggleCrossfadeOptions()"
<?php echo (isset($pluginSettings['EnableCrossfade']) && $pluginSettings['EnableCrossfade'] == '1') ? 'checked' : ''; ?>>
<label for="EnableCrossfade" style="margin: 0; line-height: 1.4;">
Enable crossfading to eliminate silence between tracks
</label>
</div>
<small>Smoothly blend tracks together for continuous playback</small>
</td>
</tr>
<tr id="crossfadeDurationRow" style="<?php echo ($bgSource == 'stream' || !isset($pluginSettings['EnableCrossfade']) || $pluginSettings['EnableCrossfade'] != '1') ? 'display: none;' : ''; ?>">
<td class="label">Crossfade Duration (seconds):</td>
<td class="value">
<input type="number" name="CrossfadeDuration" id="CrossfadeDuration" min="1" max="10" step="0.5"
value="<?php echo isset($pluginSettings['CrossfadeDuration']) ? $pluginSettings['CrossfadeDuration'] : '3'; ?>">
<small>Length of overlap between tracks (1-10 seconds). Recommended: 3-4 seconds</small>
</td>
</tr>
</table>
<!-- Main Show Configuration -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #ff9800; border-bottom: 2px solid #ff9800; padding-bottom: 5px;">
<i class="fas fa-star"></i> Main Show Configuration
</h3>
<table class="settingsTable">
<tr>
<td class="label">Main Show Playlist:</td>
<td class="value">
<select name="ShowPlaylist" id="ShowPlaylist">
<option value="">-- Select Playlist --</option>
<?php
$savedShowPlaylist = isset($pluginSettings['ShowPlaylist']) ? $pluginSettings['ShowPlaylist'] : '';
error_log("BackgroundMusic: Saved Show Playlist: '" . $savedShowPlaylist . "'");
foreach ($allPlaylists as $playlist) {
$selected = '';
if ($savedShowPlaylist != '' && $savedShowPlaylist == $playlist) {
$selected = 'selected';
error_log("BackgroundMusic: Matched Show playlist: '" . $playlist . "'");
}
echo '<option value="' . htmlspecialchars($playlist) . '" ' . $selected . '>' .
htmlspecialchars($playlist) . '</option>';
}
?>
</select>
<small>Main show playlist to start after fade and blackout</small>
</td>
</tr>
</table>
<!-- Volume Settings -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #4CAF50; border-bottom: 2px solid #4CAF50; padding-bottom: 5px;">
<i class="fas fa-volume-up"></i> Volume Settings
</h3>
<table class="settingsTable">
<tr>
<td class="label">Background Music Volume (%):</td>
<td class="value">
<input type="number" name="BackgroundMusicVolume" id="BackgroundMusicVolume" min="0" max="100"
value="<?php echo isset($pluginSettings['BackgroundMusicVolume']) ? $pluginSettings['BackgroundMusicVolume'] : '70'; ?>">
<small>Volume level for background music during pre-show (0-100%). Use the main control page to adjust in real-time.</small>
</td>
</tr>
<tr>
<td class="label">Show Playlist Volume (%):</td>
<td class="value">
<input type="number" name="ShowPlaylistVolume" id="ShowPlaylistVolume" min="0" max="100"
value="<?php echo isset($pluginSettings['ShowPlaylistVolume']) ? $pluginSettings['ShowPlaylistVolume'] : '100'; ?>">
<small>System (ALSA) volume for main show sequences (0-100%). FPP's volume slider will still work at this level.</small>
</td>
</tr>
<tr>
<td class="label">Post-Show Background Volume (%):</td>
<td class="value">
<input type="number" name="PostShowBackgroundVolume" id="PostShowBackgroundVolume" min="0" max="100"
value="<?php echo isset($pluginSettings['PostShowBackgroundVolume']) ? $pluginSettings['PostShowBackgroundVolume'] : '70'; ?>">
<small>Volume level for background music after show ends (often lower to wind down)</small>
</td>
</tr>
</table>
<!-- Show Transition Settings -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #FF9800; border-bottom: 2px solid #FF9800; padding-bottom: 5px;">
<i class="fas fa-adjust"></i> Show Transition Settings
</h3>
<table class="settingsTable">
<tr>
<td class="label">Fade Out Time (seconds):</td>
<td class="value">
<input type="number" name="FadeTime" id="FadeTime" min="1" max="60"
value="<?php echo isset($pluginSettings['FadeTime']) ? $pluginSettings['FadeTime'] : '5'; ?>">
<small>Time in seconds to fade out brightness and volume</small>
</td>
</tr>
<tr>
<td class="label">Blackout Duration (seconds):</td>
<td class="value">
<input type="number" name="BlackoutTime" id="BlackoutTime" min="0" max="30"
value="<?php echo isset($pluginSettings['BlackoutTime']) ? $pluginSettings['BlackoutTime'] : '2'; ?>">
<small>Time in seconds to wait in blackout before starting show</small>
</td>
</tr>
</table>
<!-- Plugin Behavior Settings -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #9C27B0; border-bottom: 2px solid #9C27B0; padding-bottom: 5px;">
<i class="fas fa-redo"></i> Plugin Behavior Settings
</h3>
<table class="settingsTable">
<tr>
<td class="label">Return to Pre-Show After Show:</td>
<td class="value">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<input type="checkbox" name="ReturnToPreShow" id="ReturnToPreShow" value="1"
<?php echo (isset($pluginSettings['ReturnToPreShow']) && $pluginSettings['ReturnToPreShow'] == '1') ? 'checked' : ''; ?>>
<label for="ReturnToPreShow" style="margin: 0; line-height: 1.4;">
Automatically restart background music when show playlist ends
</label>
</div>
<small>Your FPP scheduler should handle restarting the pre-show light sequence</small>
</td>
</tr>
<tr>
<td class="label">Resume Playlist Mode:</td>
<td class="value">
<select name="ResumeMode" id="ResumeMode">
<option value="continue" <?php echo (!isset($pluginSettings['ResumeMode']) || $pluginSettings['ResumeMode'] == 'continue') ? 'selected' : ''; ?>>
Continue from Next Track
</option>
<option value="restart" <?php echo (isset($pluginSettings['ResumeMode']) && $pluginSettings['ResumeMode'] == 'restart') ? 'selected' : ''; ?>>
Restart from Beginning
</option>
</select>
<small>Choose whether to continue from the next track or restart the playlist from the beginning after the show ends</small>
</td>
</tr>
<tr>
<td class="label">Post-Show Delay (seconds):</td>
<td class="value">
<input type="number" name="PostShowDelay" id="PostShowDelay" min="0" max="300"
value="<?php echo isset($pluginSettings['PostShowDelay']) ? $pluginSettings['PostShowDelay'] : '0'; ?>">
<small>Time in seconds to wait after show ends before restarting background music</small>
</td>
</tr>
<tr>
<td class="label">Autostart on FPP Boot:</td>
<td class="value">
<div style="display: flex; align-items: flex-start; gap: 10px;">
<input type="checkbox" name="AutostartEnabled" id="AutostartEnabled" value="1"
<?php echo (isset($pluginSettings['AutostartEnabled']) && $pluginSettings['AutostartEnabled'] == '1') ? 'checked' : ''; ?>>
<label for="AutostartEnabled" style="margin: 0; line-height: 1.4;">
Automatically start background music when FPP starts or restarts
</label>
</div>
<small>When enabled, background music will resume automatically from where it left off (or start fresh if never played)</small>
</td>
</tr>
</table>
<!-- Public Service Announcement Settings -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #e91e63; border-bottom: 2px solid #e91e63; padding-bottom: 5px;">
<i class="fas fa-bullhorn"></i> Public Service Announcements (PSA)
</h3>
<div class="description" style="margin-top: 15px;">
<p><strong>About PSA:</strong> Play pre-recorded announcements over background music. The music volume will be
"ducked" (lowered) during announcements, then restored afterward. Announcements play through a separate audio
stream that mixes with the background music.</p>
</div>
<table class="settingsTable">
<tr>
<td class="label">Announcement Volume (%):</td>
<td class="value">
<input type="number" name="PSAAnnouncementVolume" id="PSAAnnouncementVolume" min="0" max="100"
value="<?php echo isset($pluginSettings['PSAAnnouncementVolume']) ? $pluginSettings['PSAAnnouncementVolume'] : '90'; ?>">
<small>Volume level for playing announcements (0-100%)</small>
</td>
</tr>
<tr>
<td class="label">Ducked Music Volume (%):</td>
<td class="value">
<input type="number" name="PSADuckVolume" id="PSADuckVolume" min="0" max="100"
value="<?php echo isset($pluginSettings['PSADuckVolume']) ? $pluginSettings['PSADuckVolume'] : '30'; ?>">
<small>Volume level to lower background music to during announcements (0-100%)</small>
</td>
</tr>
</table>
<h4 style="margin: 20px auto 10px; max-width: 800px; color: #555;">
Configure Announcement Buttons:
<button type="button" onclick="addPSAButton()" style="float: right; padding: 5px 15px; background: #4caf50; color: white; border: none; border-radius: 4px; cursor: pointer;">
<i class="fas fa-plus"></i> Add Button
</button>
</h4>
<table class="settingsTable" id="psaButtonsTable">
<?php
// Determine how many buttons are configured (default to 1)
$buttonCount = 1;
for ($i = 1; $i <= 20; $i++) {
if (isset($pluginSettings['PSAButton'.$i.'Label']) || isset($pluginSettings['PSAButton'.$i.'File'])) {
$buttonCount = $i;
}
}
if ($buttonCount < 1) $buttonCount = 1; // Always show at least 1
for ($i = 1; $i <= $buttonCount; $i++):
?>
<tr class="psa-button-row" data-button-num="<?php echo $i; ?>">
<td class="label">
Button <?php echo $i; ?> Label:
<?php if ($i > 1): ?>
<button type="button" onclick="removePSAButton(<?php echo $i; ?>)"
style="margin-left: 10px; padding: 2px 8px; background: #f44336; color: white; border: none; border-radius: 3px; cursor: pointer; font-size: 11px;">
<i class="fas fa-trash"></i>
</button>
<?php endif; ?>
</td>
<td class="value">
<input type="text" name="PSAButton<?php echo $i; ?>Label" id="PSAButton<?php echo $i; ?>Label"
placeholder="e.g. Welcome Message"
value="<?php echo isset($pluginSettings['PSAButton'.$i.'Label']) ? htmlspecialchars($pluginSettings['PSAButton'.$i.'Label']) : ''; ?>">
</td>
</tr>
<tr class="psa-button-row" data-button-num="<?php echo $i; ?>">
<td class="label">Button <?php echo $i; ?> MP3 File:</td>
<td class="value">
<select name="PSAButton<?php echo $i; ?>File" id="PSAButton<?php echo $i; ?>File">
<option value="">-- Select Audio File --</option>
<?php
$currentFile = isset($pluginSettings['PSAButton'.$i.'File']) ? $pluginSettings['PSAButton'.$i.'File'] : '';
// Extract just the filename if it's a full path
$currentFileName = basename($currentFile);
foreach ($audioFiles as $audioFile) {
$fullPath = '/home/fpp/media/music/' . $audioFile;
$selected = '';
// Check if this file matches the saved setting (by filename or full path)
if ($currentFile === $fullPath || $currentFileName === $audioFile) {
$selected = 'selected';
}
echo '<option value="' . htmlspecialchars($fullPath) . '" ' . $selected . '>' .
htmlspecialchars($audioFile) . '</option>';
}
?>
</select>
<small>Select audio file from /home/fpp/media/music/</small>
</td>
</tr>
<tr class="psa-button-row psa-spacer" data-button-num="<?php echo $i; ?>"><td colspan="2" style="height: 10px;"></td></tr>
<?php endfor; ?>
</table>
<script>
// Track available audio files for dynamic button creation
var availableAudioFiles = <?php echo json_encode($audioFiles); ?>;
var nextButtonNum = <?php echo $buttonCount + 1; ?>;
function addPSAButton() {
var buttonNum = nextButtonNum++;
var table = document.getElementById('psaButtonsTable');
// Create label row
var labelRow = table.insertRow(-1);
labelRow.className = 'psa-button-row';
labelRow.setAttribute('data-button-num', buttonNum);
labelRow.innerHTML =
'<td class="label">' +
'Button ' + buttonNum + ' Label:' +
'<button type="button" onclick="removePSAButton(' + buttonNum + ')" ' +
'style="margin-left: 10px; padding: 2px 8px; background: #f44336; color: white; border: none; border-radius: 3px; cursor: pointer; font-size: 11px;">' +
'<i class="fas fa-trash"></i>' +
'</button>' +
'</td>' +
'<td class="value">' +
'<input type="text" name="PSAButton' + buttonNum + 'Label" id="PSAButton' + buttonNum + 'Label" ' +
'placeholder="e.g. Welcome Message" value="">' +
'</td>';
// Create file row
var fileRow = table.insertRow(-1);
fileRow.className = 'psa-button-row';
fileRow.setAttribute('data-button-num', buttonNum);
var options = '<option value="">-- Select Audio File --</option>';
availableAudioFiles.forEach(function(audioFile) {
var fullPath = '/home/fpp/media/music/' + audioFile;
options += '<option value="' + fullPath.replace(/"/g, '"') + '">' +
audioFile.replace(/</g, '<').replace(/>/g, '>') + '</option>';
});
fileRow.innerHTML =
'<td class="label">Button ' + buttonNum + ' MP3 File:</td>' +
'<td class="value">' +
'<select name="PSAButton' + buttonNum + 'File" id="PSAButton' + buttonNum + 'File">' +
options +
'</select>' +
'<small>Select audio file from /home/fpp/media/music/</small>' +
'</td>';
// Create spacer row
var spacerRow = table.insertRow(-1);
spacerRow.className = 'psa-button-row psa-spacer';
spacerRow.setAttribute('data-button-num', buttonNum);
spacerRow.innerHTML = '<td colspan="2" style="height: 10px;"></td>';
$.jGrowl('Button ' + buttonNum + ' added', {themeState: 'success'});
}
function removePSAButton(buttonNum) {
if (buttonNum === 1) {
$.jGrowl('Cannot remove button 1', {themeState: 'danger'});
return;
}
if (confirm('Remove button ' + buttonNum + '? This will delete the configuration.')) {
// Remove all rows with this button number
var rows = document.querySelectorAll('.psa-button-row[data-button-num="' + buttonNum + '"]');
rows.forEach(function(row) {
row.remove();
});
$.jGrowl('Button ' + buttonNum + ' removed', {themeState: 'success'});
}
}
</script>
<!-- Text-to-Speech (TTS) Settings -->
<h3 style="margin: 30px auto 10px; max-width: 800px; color: #3f51b5; border-bottom: 2px solid #3f51b5; padding-bottom: 5px;">
<i class="fas fa-robot"></i> Text-to-Speech (TTS) Announcements
</h3>
<div class="description" style="margin-top: 15px;">
<p><strong>About TTS:</strong> Generate AI-powered voice announcements using either Piper TTS (local, free)
or ElevenLabs (cloud-based, premium quality). Create MP3 files from text for future use, or play announcements in real-time.</p>
</div>
<!-- TTS Engine Selection -->
<div style="max-width: 800px; margin: 20px auto; padding: 15px; border: 2px solid #2196F3; border-radius: 8px; background-color: #E3F2FD;">
<h4 style="margin-top: 0; color: #2196F3;"><i class="fas fa-cogs"></i> TTS Engine Configuration</h4>
<table class="settingsTable" style="margin: 0;">
<tr>
<td class="label" style="width: 250px;">TTS Engine:</td>
<td class="value">
<?php
$ttsEngine = isset($pluginSettings['TTSEngine']) ? $pluginSettings['TTSEngine'] : 'piper';
?>
<select id="TTSEngine" name="TTSEngine" class="form-control" style="width: 300px;" onchange="toggleTTSEngineSettings()">
<option value="piper" <?php echo ($ttsEngine == 'piper') ? 'selected' : ''; ?>>Piper (Local - Free)</option>
<option value="elevenlabs" <?php echo ($ttsEngine == 'elevenlabs') ? 'selected' : ''; ?>>ElevenLabs (Cloud - Premium)</option>
</select>
<small>Choose between local Piper TTS or cloud-based ElevenLabs</small>
</td>
</tr>
</table>
<!-- ElevenLabs Settings (hidden by default) -->
<div id="elevenLabsSettings" style="<?php echo ($ttsEngine == 'elevenlabs') ? '' : 'display: none;'; ?> margin-top: 15px; padding-top: 15px; border-top: 1px solid #2196F3;">
<table class="settingsTable" style="margin: 0;">
<tr>
<td class="label" style="width: 250px;">
ElevenLabs API Key:
<a href="https://elevenlabs.io/api" target="_blank" style="font-size: 11px;">
<i class="fas fa-external-link-alt"></i> Get API Key
</a>
</td>
<td class="value">
<input type="text" id="ElevenLabsAPIKey" name="ElevenLabsAPIKey"
value="<?php echo isset($pluginSettings['ElevenLabsAPIKey']) ? htmlspecialchars($pluginSettings['ElevenLabsAPIKey']) : ''; ?>"
placeholder="sk_..." style="width: 400px;" class="form-control" onblur="loadElevenLabsDefaultVoices()">
<small>Your ElevenLabs API key (required for cloud TTS)</small>
</td>
</tr>
<tr>
<td class="label">Default Voice:</td>
<td class="value">
<select id="ElevenLabsVoiceID" name="ElevenLabsVoiceID" style="width: 400px;" class="form-control" data-saved-value="<?php echo isset($pluginSettings['ElevenLabsVoiceID']) ? htmlspecialchars($pluginSettings['ElevenLabsVoiceID']) : '21m00Tcm4TlvDq8ikWAM'; ?>">
<option value="21m00Tcm4TlvDq8ikWAM" <?php echo (!isset($pluginSettings['ElevenLabsVoiceID']) || $pluginSettings['ElevenLabsVoiceID'] == '21m00Tcm4TlvDq8ikWAM') ? 'selected' : ''; ?>>
Rachel (American - Female)
</option>
</select>
<span id="elevenLabsVoiceLoadStatus" style="margin-left: 10px;"></span>
<br><small>Default voice for TTS generation (will load available voices when API key is provided)</small>
</td>
</tr>
</table>
</div>
</div>
<div id="ttsStatusPanel" style="max-width: 800px; margin: 20px auto; padding: 15px; border: 2px solid #ddd; border-radius: 8px; background-color: #f9f9f9;">
<h4 style="margin-top: 0;"><i class="fas fa-info-circle"></i> TTS Engine Status</h4>
<div id="ttsStatus">
<p><i class="fas fa-spinner fa-spin"></i> Checking TTS engine status...</p>
</div>
</div>
<div id="ttsGeneratorPanel" style="max-width: 800px; margin: 20px auto; padding: 15px; border: 2px solid #3f51b5; border-radius: 8px; background-color: #f0f4ff; display: none;">
<h4 style="margin-top: 0; color: #3f51b5;"><i class="fas fa-magic"></i> Generate TTS Audio File</h4>
<table class="settingsTable" style="margin: 0;">
<tr>
<td class="label" style="width: 200px;">Voice:</td>
<td class="value">
<select id="ttsVoiceSelect" style="width: 100%; padding: 8px;">
<option value="">Loading voices...</option>
</select>
<small>Select the voice to use for generation</small>
</td>
</tr>
<tr>
<td class="label" style="width: 200px;">Text to Speak:</td>
<td class="value">
<textarea id="ttsText" rows="3" style="width: 100%; font-family: sans-serif; font-size: 14px; padding: 8px; border: 1px solid #ccc; border-radius: 4px;"
placeholder="Enter text to convert to speech, e.g., 'Welcome to our holiday light show! Please stay in your vehicle and tune to 87.9 FM.'"></textarea>
<small>Enter the text you want to convert to speech</small>
</td>
</tr>
<tr>
<td class="label">Filename:</td>
<td class="value">
<input type="text" id="ttsFilename" placeholder="e.g., welcome_message" style="width: 300px;">
<small>Filename (without extension) - will be saved as MP3 in /home/fpp/media/music/</small>
</td>
</tr>
<tr>
<td class="label"></td>
<td class="value">
<button type="button" class="btn btn-primary" onclick="generateTTS()" style="background-color: #3f51b5;">
<i class="fas fa-magic"></i> Generate MP3 File
</button>
<span id="ttsGenerateStatus" style="margin-left: 10px;"></span>
</td>
</tr>
</table>
</div>
<!-- Voice Management Panel -->
<div id="voiceManagementPanel" style="max-width: 800px; margin: 20px auto; padding: 15px; border: 2px solid #673ab7; border-radius: 8px; background-color: #f3e5f5; display: none;">
<h4 style="margin-top: 0; color: #673ab7;"><i class="fas fa-microphone"></i> <span id="voiceManagementTitle">Voice Management</span></h4>
<p id="voiceManagementDescription" style="margin-bottom: 15px; font-size: 13px;">Loading voices...</p>
<div id="voicesList" style="margin-top: 10px;">
<p><i class="fas fa-spinner fa-spin"></i> Loading available voices...</p>
</div>
</div>
</form>
</div>
<script>
function toggleBackgroundSource() {
var source = $('#BackgroundMusicSource').val();
if (source === 'playlist') {
$('#playlistRow').show();
$('#streamRow').hide();
$('#shuffleRow').show();
$('#crossfadeRow').show();
toggleCrossfadeOptions();
} else {
$('#playlistRow').hide();
$('#streamRow').show();
$('#shuffleRow').hide();
$('#crossfadeRow').hide();
$('#crossfadeDurationRow').hide();
}
}
function toggleCrossfadeOptions() {
if ($('#EnableCrossfade').is(':checked')) {
$('#crossfadeDurationRow').show();
} else {
$('#crossfadeDurationRow').hide();
}
}
function toggleTTSEngineSettings() {
var engine = $('#TTSEngine').val();
if (engine === 'elevenlabs') {
$('#elevenLabsSettings').slideDown();
// For ElevenLabs, hide Piper-specific panels
$('#ttsStatusPanel').hide();
$('#voiceManagementPanel').hide();
// Load ElevenLabs voices into generator panel and default voice dropdown
loadVoices(engine);
loadElevenLabsDefaultVoices();
$('#ttsGeneratorPanel').show();
} else {
$('#elevenLabsSettings').slideUp();
// For Piper, show status panel and check if installed
$('#ttsStatusPanel').show();
$('#ttsGeneratorPanel').hide();
checkTTSStatus();
}
}
function loadElevenLabsDefaultVoices() {
var apiKey = $('#ElevenLabsAPIKey').val().trim();
if (!apiKey) {
return;
}
$('#elevenLabsVoiceLoadStatus').html('<i class="fas fa-spinner fa-spin"></i>');
$.ajax({
url: '/api/plugin/fpp-plugin-BackgroundMusic/tts-voices',
type: 'GET',
data: { engine: 'elevenlabs' },
success: function(data) {
if (data.status === 'OK' && data.voices) {
var select = $('#ElevenLabsVoiceID');
var savedValue = select.data('saved-value') || select.val();
select.empty();
// Group voices by gender
var grouped = {};
data.voices.forEach(function(voice) {
var gender = voice.gender || 'Other';
if (!grouped[gender]) {
grouped[gender] = [];
}
grouped[gender].push(voice);
});
// Add voices grouped by gender
Object.keys(grouped).sort().forEach(function(gender) {
var optgroup = $('<optgroup label="' + gender + '"></optgroup>');
grouped[gender].forEach(function(voice) {
var label = voice.name;
if (voice.accent && voice.accent !== 'unknown') {
label += ' (' + voice.accent + ')';
}
var option = $('<option></option>')
.val(voice.id)
.text(label);
if (voice.id === savedValue) {
option.prop('selected', true);
}
optgroup.append(option);
});
select.append(optgroup);
});
// Set the saved value
select.val(savedValue);
$('#elevenLabsVoiceLoadStatus').html('<i class="fas fa-check-circle" style="color: green;"></i>');
} else {
$('#elevenLabsVoiceLoadStatus').html('<i class="fas fa-exclamation-triangle" style="color: orange;"></i>');
}
},
error: function() {
$('#elevenLabsVoiceLoadStatus').html('<i class="fas fa-times-circle" style="color: red;"></i>');
}
});
}
function handleStreamPresetChange() {
var preset = $('#BackgroundMusicStreamPreset').val();
if (preset === 'custom') {
$('#customStreamURLContainer').show();
$('#BackgroundMusicStreamURLInput').focus();
} else if (preset === '') {
$('#customStreamURLContainer').hide();
$('#BackgroundMusicStreamURLInput').val('');
} else {
$('#customStreamURLContainer').hide();
$('#BackgroundMusicStreamURLInput').val(preset);
}
}
function saveSettings() {
// Get the actual stream URL value
var streamURL = '';
var preset = $('#BackgroundMusicStreamPreset').val();
if (preset === 'custom') {
streamURL = $('#BackgroundMusicStreamURLInput').val();
} else if (preset !== '') {
streamURL = preset;
}
var formData = {
'BackgroundMusicSource': $('#BackgroundMusicSource').val(),
'BackgroundMusicPlaylist': $('#BackgroundMusicPlaylist').val(),
'BackgroundMusicStreamURL': streamURL,
'ShowPlaylist': $('#ShowPlaylist').val(),
'FadeTime': $('#FadeTime').val(),
'BlackoutTime': $('#BlackoutTime').val(),
'ReturnToPreShow': $('#ReturnToPreShow').is(':checked') ? '1' : '0',
'ResumeMode': $('#ResumeMode').val(),
'PostShowDelay': $('#PostShowDelay').val(),
'PostShowBackgroundVolume': $('#PostShowBackgroundVolume').val(),
'AutostartEnabled': $('#AutostartEnabled').is(':checked') ? '1' : '0',
'ShuffleMusic': $('#ShuffleMusic').is(':checked') ? '1' : '0',
'EnableCrossfade': $('#EnableCrossfade').is(':checked') ? '1' : '0',
'CrossfadeDuration': $('#CrossfadeDuration').val(),
'BackgroundMusicVolume': $('#BackgroundMusicVolume').val(),
'ShowPlaylistVolume': $('#ShowPlaylistVolume').val(),
'VolumeLevel': $('#BackgroundMusicVolume').val() || '70', // Maintain backward compatibility
// PSA settings
'PSAAnnouncementVolume': $('#PSAAnnouncementVolume').val(),
'PSADuckVolume': $('#PSADuckVolume').val(),
// TTS settings
'TTSEngine': $('#TTSEngine').val(),
'ElevenLabsAPIKey': $('#ElevenLabsAPIKey').val(),
'ElevenLabsVoiceID': $('#ElevenLabsVoiceID').val()
};
// Dynamically collect all PSA button settings
for (var i = 1; i <= 20; i++) {
var labelInput = $('#PSAButton' + i + 'Label');
var fileInput = $('#PSAButton' + i + 'File');
if (labelInput.length > 0) {
formData['PSAButton' + i + 'Label'] = labelInput.val();
}
if (fileInput.length > 0) {
formData['PSAButton' + i + 'File'] = fileInput.val();
}
}
$.ajax({
url: '/api/plugin/fpp-plugin-BackgroundMusic/save-settings',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(formData),
dataType: 'json',
success: function(data) {
if (data.status === 'OK') {
$.jGrowl('Settings saved successfully', {themeState: 'success'});
} else {
$.jGrowl('Error: ' + (data.message || 'Unknown error'), {themeState: 'danger'});
}
},
error: function() {
$.jGrowl('Failed to save settings', {themeState: 'danger'});
}
});
return false;
}
// TTS Functions
function checkTTSStatus() {
var engine = $('#TTSEngine').val();
// If ElevenLabs is selected, show appropriate message
if (engine === 'elevenlabs') {
$('#ttsStatus').html(