Option Explicit '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' Event attaching '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sub OnStartup SDB.UI.AddOptionSheet "Song Announcer", Script.ScriptPath, "InitSheet", "SaveSheet", GetScriptsPanel Script.RegisterEvent SDB, "OnPlay", "SDB_OnPlay" End Sub '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' When a song starts playing... '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sub SDB_OnPlay If SDB.IniFile.StringValue("SongAnnouncer","Enabled") = "True" Then Dim TextToSayArray(5) TextToSayArray(0) = "Here's %artist% with one of their greatest songs: %title%" TextToSayArray(1) = "This is %title% ... from %artist%" TextToSayArray(2) = "And another greatest hit from %artist%. This is... %title%" TextToSayArray(3) = "And yet another one from %artist%. This is... %title%" TextToSayArray(4) = "Oh.. This is a great song... %title% by %artist%" TextToSayArray(5) = "This is %artist%, with %title%" Dim TextToSay : TextToSay = InsertProps(GetRandom(TextToSayArray)) Dim VoiceIndex : VoiceIndex = CInt(SDB.IniFile.StringValue("SongAnnouncer","VoiceIndex")) Dim PreviousVolume : PreviousVolume = SDB.Player.Volume SDB.Player.Volume = PreviousVolume / 4 Speak TextToSay, VoiceIndex SDB.Player.Volume = PreviousVolume End If End Sub Function GetRandom(Arr) Randomize Dim Count : Count = UBound(Arr) + 1 Dim Index : Index = Int(Rnd * Count) GetRandom = Arr(Index) End Function Function InsertProps(Text) Text = Replace(Text, "%artist%", SDB.Player.CurrentSong.ArtistName, vbTextCompare) Text = Replace(Text, "%title%", SDB.Player.CurrentSong.Title, vbTextCompare) InsertProps = Text End Function Sub Speak(Message, VoiceIndex) On Error Resume Next Dim TTS : Set TTS = CreateObject("Sapi.SpVoice") If TTS Is Nothing Then Exit Sub If VoiceIndex >= TTS.GetVoices.Count Then Exit Sub Err.Clear Set TTS.Voice = TTS.GetVoices.Item(VoiceIndex) If Err.Number = 0 Then TTS.Speak Message, 1 Do While Not TTS.WaitUntilDone(100) SDB.ProcessMessages Loop End If End Sub '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' When the option sheets are viewed... '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sub InitSheet(Sheet) ' Checkbox "chkEnabled": enable/disable SongAnnouncer Dim chkEnabled : Set chkEnabled = CreateCheckBox(Sheet, "Announce songs with Windows TextToSpeech with", spLeftMargin, spTopMargin + 1, 300, 20, "chkEnabled") If SDB.IniFile.StringValue("SongAnnouncer","Enabled") = "True" Then chkEnabled.Checked = True End If ' DropDown "ddnVoices": select voice Dim ddnVoices : Set ddnVoices = CreateDropDown(Sheet, spLeftMargin + 270, spTopMargin, 150, 20, "ddnVoices") ddnVoices.Style = csDropDownList Dim VoiceIndex : VoiceIndex = SDB.IniFile.StringValue("SongAnnouncer","VoiceIndex") If VoiceIndex = "" Then VoiceIndex = 0 Else VoiceIndex = CInt(VoiceIndex) FillDropdownAndSelect ddnVoices, VoiceIndex If ddnVoices.ItemCount = 0 Then chkEnabled.Checked = False chkEnabled.Common.Enabled = False ddnVoices.Common.Enabled = False End If End Sub Sub SaveSheet(Sheet) SDB.IniFile.StringValue("SongAnnouncer","Enabled") = Sheet.Common.ChildControl("chkEnabled").Checked SDB.IniFile.StringValue("SongAnnouncer","VoiceIndex") = Sheet.Common.ChildControl("ddnVoices").ItemIndex End Sub Sub FillDropdownAndSelect(ddnVoices, VoiceIndex) On Error Resume Next Dim TTS : Set TTS = CreateObject("Sapi.SpVoice") If TTS Is Nothing Then Exit Sub Dim VoiceCount : VoiceCount = TTS.GetVoices.Count If VoiceCount = 0 Then Exit Sub Dim i For i = 0 To (VoiceCount - 1) ddnVoices.AddItem TTS.GetVoices.Item(i).GetDescription Next If VoiceIndex >= VoiceCount Then ddnVoices.ItemIndex = 0 Else ddnVoices.ItemIndex = VoiceIndex End If End Sub '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' Stuff copied from the Utility_Controls file... '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ' Script panel margin constants Const spLeftMargin = 20 Const spTopMargin = 20 ' DropDown Style constants Const csDropDown = 0 'DropDown can be edited Const csDropDownList = 2 'DropDown cannot be edited (user can only select from a list of values) ' Returns the ID of the Scripts option sheet (or panel) Function GetScriptsPanel() If SDB.Objects("ScriptsOptionsSheet") Is Nothing Then GetScriptsPanel = SDB.UI.AddOptionSheet("Scripts", Script.ScriptPath, "InitScriptsSheet", "SaveScriptsSheet", 0) Set SDB.Objects("ScriptsOptionsSheet") = CreateObject("Scripting.Dictionary") SDB.Objects("ScriptsOptionsSheet").Add "SheetID", GetScriptsPanel Else GetScriptsPanel = SDB.Objects("ScriptsOptionsSheet").Item("SheetID") End If End Function Sub InitScriptsSheet(Sheet) SDB.Objects("ScriptsOptionsSheet").Add "Sheet", Sheet End Sub Sub SaveScriptsSheet(Sheet) End Sub Function CreateCheckBox(Owner, Caption, X, Y, Width, Height, ControlName) Set CreateCheckBox = SDB.UI.NewCheckBox(Owner) CreateCheckBox.Caption = Caption CreateCheckBox.Common.SetRect X, Y, Width, Height CreateCheckBox.Common.ControlName = ControlName End Function Function CreateDropDown(Owner, X, Y, Width, Height, ControlName) Set CreateDropDown = SDB.UI.NewDropDown(Owner) CreateDropDown.Common.SetRect X, Y, Width, Height CreateDropDown.Common.ControlName = ControlName End Function