Update SRS.lua

This commit is contained in:
Frank 2023-11-24 23:59:31 +01:00
parent f87126f22c
commit 1b1f8e0d2c

View File

@ -65,6 +65,7 @@
-- ## Prerequisites -- ## Prerequisites
-- --
-- This script needs SRS version >= 1.9.6. -- This script needs SRS version >= 1.9.6.
-- Optional: DCS-gRPC as backend to communicate with SRS (vide infra)
-- --
-- ## Knwon Issues -- ## Knwon Issues
-- --
@ -130,6 +131,7 @@
-- ## Set Coordinate -- ## Set Coordinate
-- --
-- Use @{#MSRS.SetCoordinate} to define the origin from where the transmission is broadcasted. -- Use @{#MSRS.SetCoordinate} to define the origin from where the transmission is broadcasted.
-- Note that this is only a factor if SRS server has line-of-sight and/or distance limit enabled.
-- --
-- ## Set SRS Port -- ## Set SRS Port
-- --
@ -193,6 +195,7 @@ MSRS = {
lid = nil, lid = nil,
port = 5002, port = 5002,
name = "MSRS", name = "MSRS",
backend = "srsexe",
frequencies = {}, frequencies = {},
modulations = {}, modulations = {},
coalition = 0, coalition = 0,
@ -202,12 +205,12 @@ MSRS = {
volume = 1, volume = 1,
speed = 1, speed = 1,
coordinate = nil, coordinate = nil,
provider = "win",
Label = "ROBOT", Label = "ROBOT",
AltBackend = nil,
ConfigFileName = "Moose_MSRS.lua", ConfigFileName = "Moose_MSRS.lua",
ConfigFilePath = "Config\\", ConfigFilePath = "Config\\",
ConfigLoaded = false, ConfigLoaded = false,
ttsprovider = "Microsoft", poptions = {},
} }
--- MSRS class version. --- MSRS class version.
@ -329,8 +332,8 @@ MSRS.Backend = {
-- @type MSRS.Provider -- @type MSRS.Provider
-- @field #string WINDOWS Microsoft windows (`win`). -- @field #string WINDOWS Microsoft windows (`win`).
-- @field #string GOOGLE Google (`gcloud`). -- @field #string GOOGLE Google (`gcloud`).
-- @field #string AZURE Microsoft Azure (`azure`). -- @field #string AZURE Microsoft Azure (`azure`). Only possible with DCS-gRPC backend.
-- @field #string AMAZON Amazon Web Service (`asw`). -- @field #string AMAZON Amazon Web Service (`asw`). Only possible with DCS-gRPC backend.
MSRS.Provider = { MSRS.Provider = {
WINDOWS = "win", WINDOWS = "win",
GOOGLE = "gcloud", GOOGLE = "gcloud",
@ -340,10 +343,10 @@ MSRS.Provider = {
--- Provider options. --- Provider options.
-- @type MSRS.ProviderOptions -- @type MSRS.ProviderOptions
-- @field #string key -- @field #string key Access key (DCS-gRPC with Google, ASW, AZURE as provider).
-- @field #string secret -- @field #string secret Secret key (DCS-gRPC with ASW as provider)
-- @field #string region -- @field #string region Region.
-- @field #string defaultVoice -- @field #string defaultVoice Default voice.
-- @field #string voice -- @field #string voice
--- GRPC options. --- GRPC options.
@ -390,13 +393,11 @@ MSRS.GRPCOptions.DefaultProvider = "win"
-- set the path to the exe file via @{#MSRS.SetPath}. -- set the path to the exe file via @{#MSRS.SetPath}.
-- --
-- @param #MSRS self -- @param #MSRS self
-- @param #string PathToSRS Path to the directory, where SRS is located.
-- @param #number Frequency Radio frequency in MHz. Default 143.00 MHz. Can also be given as a #table of multiple frequencies. -- @param #number Frequency Radio frequency in MHz. Default 143.00 MHz. Can also be given as a #table of multiple frequencies.
-- @param #number Modulation Radio modulation: 0=AM (default), 1=FM. See `radio.modulation.AM` and `radio.modulation.FM` enumerators. Can also be given as a #table of multiple modulations. -- @param #number Modulation Radio modulation: 0=AM (default), 1=FM. See `radio.modulation.AM` and `radio.modulation.FM` enumerators. Can also be given as a #table of multiple modulations.
-- @param #number Volume Volume - 1.0 is max, 0.0 is silence. -- @param #string Backend Backend used: `MSRS.Backend.SRSEXE` (default) or `MSRS.Backend.GRPC`.
-- @param #table AltBackend Optional table containing tables 'Functions' and 'Vars' which add/replace functions and variables for the MSRS instance to allow alternate backends for transmitting to SRS.
-- @return #MSRS self -- @return #MSRS self
function MSRS:New(Frequency, Modulation, Volume, AltBackend) function MSRS:New(Frequency, Modulation, Backend)
-- Defaults. -- Defaults.
Frequency = Frequency or 143 Frequency = Frequency or 143
@ -407,52 +408,27 @@ function MSRS:New(Frequency, Modulation, Volume, AltBackend)
self.lid = string.format("%s-%s | ", "unknown", self.version) self.lid = string.format("%s-%s | ", "unknown", self.version)
-- If AltBackend is supplied, initialize it, which will add/replace functions and variables in this MSRS instance.
if type( AltBackend ) == "table" or type( self.AltBackend ) == "table" then
local Backend = UTILS.DeepCopy(AltBackend) or UTILS.DeepCopy(self.AltBackend)
-- Add parameters to vars so alternate backends can use them if applicable
Backend.Vars = Backend.Vars or {}
Backend.Vars.PathToSRS = PathToSRS
Backend.Vars.Frequency = UTILS.DeepCopy(Frequency)
Backend.Vars.Modulation = UTILS.DeepCopy(Modulation)
Backend.Vars.Volume = Volume
Backend.Functions = Backend.Functions or {}
return self:_NewAltBackend(Backend)
end
if not self.ConfigLoaded then if not self.ConfigLoaded then
-- If no AltBackend table, the proceed with default initialisation -- Defaults.
self:SetPath(PathToSRS) self:SetPath()
self:SetPort() self:SetPort()
self:SetFrequencies(Frequency) self:SetFrequencies(Frequency)
self:SetModulations(Modulation) self:SetModulations(Modulation)
self:SetGender() self:SetGender()
self:SetCoalition() self:SetCoalition()
self:SetLabel() self:SetLabel()
self:SetVolume(Volume) self:SetVolume()
else else
-- there might be some overwrites from :New() -- there might be some overwrites from :New()
if PathToSRS then
self:SetPath(PathToSRS)
end
if Frequency then if Frequency then
self:SetFrequencies(Frequency) self:SetFrequencies(Frequency)
self:SetModulations(Modulation) self:SetModulations(Modulation)
end end
if Volume then
self:SetVolume(Volume)
end
end end
self.lid = string.format("%s-%s | ", self.name, self.version) self.lid = string.format("%s-%s | ", self.name, self.version)
@ -484,9 +460,37 @@ function MSRS:SetBackend(Backend)
return self return self
end end
--- Set DCS-gRPC as backend to communicate with SRS.
-- @param #MSRS self
-- @return #MSRS self
function MSRS:SetBackendGRPC()
self:SetBackend(MSRS.Backend.GRPC)
return self
end
--- Set SRS-TTS.exe as backend to communicate with SRS.
-- @param #MSRS self
-- @return #MSRS self
function MSRS:SetBackendSRSEXE(Backend)
self:SetBackend(MSRS.Backend.SRSEXE)
return self
end
--- Get currently set backend.
-- @param #MSRS self
-- @return #string Backend.
function MSRS:GetBackend()
return self.backend
end
--- Set path to SRS install directory. More precisely, path to where the DCS- --- Set path to SRS install directory. More precisely, path to where the DCS-
-- @param #MSRS self -- @param #MSRS self
-- @param #string Path Path to the directory, where the sound file is located. This does **not** contain a final backslash or slash. -- @param #string Path Path to the directory, where the sound file is located.
-- @return #MSRS self -- @return #MSRS self
function MSRS:SetPath(Path) function MSRS:SetPath(Path)
@ -613,7 +617,7 @@ end
--- Get frequencies. --- Get frequencies.
-- @param #MSRS self -- @param #MSRS self
-- @param #table Frequencies in MHz. -- @return #table Frequencies in MHz.
function MSRS:GetFrequencies() function MSRS:GetFrequencies()
return self.frequencies return self.frequencies
end end
@ -672,7 +676,7 @@ end
--- Set culture. --- Set culture.
-- @param #MSRS self -- @param #MSRS self
-- @param #string Culture Culture, e.g. "en-GB" (default). -- @param #string Culture Culture, e.g. "en-GB".
-- @return #MSRS self -- @return #MSRS self
function MSRS:SetCulture(Culture) function MSRS:SetCulture(Culture)
@ -681,7 +685,7 @@ function MSRS:SetCulture(Culture)
return self return self
end end
--- Set to use a specific voice. Will override any gender and culture settings. --- Set to use a specific voice. Note that this will override any gender and culture settings as a voice already has a certain gender/culture.
-- @param #MSRS self -- @param #MSRS self
-- @param #string Voice Voice. -- @param #string Voice Voice.
-- @return #MSRS self -- @return #MSRS self
@ -689,10 +693,6 @@ function MSRS:SetVoice(Voice)
self.voice=Voice self.voice=Voice
--local defaultprovider = self.provider or self.GRPCOptions.DefaultProvider or MSRS.GRPCOptions.DefaultProvider or "win"
--self.GRPCOptions[defaultprovider].voice = Voice
return self return self
end end
@ -703,13 +703,14 @@ end
function MSRS:SetDefaultVoice(Voice) function MSRS:SetDefaultVoice(Voice)
self.defaultVoice=Voice self.defaultVoice=Voice
local provider = self.provider or self.GRPCOptions.DefaultProvider or MSRS.GRPCOptions.DefaultProvider or "win" local provider = self.provider or self.GRPCOptions.DefaultProvider or MSRS.GRPCOptions.DefaultProvider or "win"
self.GRPCOptions[provider].defaultVoice = Voice self.GRPCOptions[provider].defaultVoice = Voice
return self return self
end end
--- Set the coordinate from which the transmissions will be broadcasted. --- Set the coordinate from which the transmissions will be broadcasted. Note that this is only a factor if SRS has line-of-sight or distance enabled.
-- @param #MSRS self -- @param #MSRS self
-- @param Core.Point#COORDINATE Coordinate Origin of the transmission. -- @param Core.Point#COORDINATE Coordinate Origin of the transmission.
-- @return #MSRS self -- @return #MSRS self
@ -755,6 +756,77 @@ function MSRS:SetGoogleAPIKey(APIKey)
return self return self
end end
--- Set provider used to generate text-to-speech.
-- These options are available:
--
-- - `MSRS.Provider.WINDOWS`: Microsoft Windows (default)
-- - `MSRS.Provider.GOOGLE`: Google Cloud
-- - `MSRS.Provider.AZURE`: Microsoft Azure (only with DCS-gRPC backend)
-- - `MSRS.Provier.AMAZON`: Amazone Web Service (only with DCS-gRPC backend)
--
-- Note that all providers except Microsoft Windows need as additonal information the credentials of your account.
--
-- @param #MSRS self
-- @param #string Provider
-- @return #MSRS self
function MSRS:SetProvider(Provider)
self.provider = Provider or MSRS.Provider.WINDOWS
return self
end
--- Get provider.
-- @param #MSRS self
-- @return #MSRS self
function MSRS:GetProvider()
return self.provider or MSRS.Provider.WINDOWS
end
--- Set provider options and credentials.
-- @param #MSRS self
-- @param #string Provider Provider.
-- @param #string CredentialsFile Full path to your credentials file. For Google this is the path to a JSON file.
-- @param #string AccessKey Your API access key.
-- @param #string SecretKey Your secret key.
-- @param #string Region Region to use.
-- @return #MSRS.ProviderOptions Provider optionas table.
function MSRS:SetProviderOptions(Provider, CredentialsFile, AccessKey, SecretKey, Region)
local option={} --#MSRS.ProviderOptions
option.provider=Provider
option.credentials=CredentialsFile
option.key=AccessKey
option.secret=SecretKey
option.region=Region
self.poptions[Provider]=option
return option
end
--- Set provider options and credentials for Google Cloud.
-- @param #MSRS self
-- @param #string CredentialsFile Full path to your credentials file. For Google this is the path to a JSON file. This is used if SRS-TTS.exe is used as backend.
-- @param #string AccessKey Your API access key. This is necessary if DCS-gRPC is used as backend.
-- @return #MSRS self
function MSRS:SetProviderOptionsGoogle(CredentialsFile, AccessKey)
self:SetProviderOptions(MSRS.Provider.GOOGLE, CredentialsFile, AccessKey)
return self
end
--- Set provider options and credentials for Google Cloud.
-- @param #MSRS self
-- @param #string Provider Provider. Default is as set via @{#MSRS.SetProvider}.
-- @return #MSRS.ProviderOptions Provider options.
function MSRS:GetProviderOptions(Provider)
return self.poptions[Provider or self.provider]
end
--- Use Google text-to-speech as default. --- Use Google text-to-speech as default.
-- @param #MSRS self -- @param #MSRS self
-- @return #MSRS self -- @return #MSRS self
@ -895,6 +967,8 @@ function MSRS:PlayText(Text, Delay, Coordinate)
self:ScheduleOnce(Delay, MSRS.PlayText, self, Text, nil, Coordinate) self:ScheduleOnce(Delay, MSRS.PlayText, self, Text, nil, Coordinate)
else else
if self.backend==MSRS.Backend.SRSEXE then
-- Get command line. -- Get command line.
local command=self:_GetCommand(nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,Coordinate) local command=self:_GetCommand(nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,Coordinate)
@ -904,6 +978,13 @@ function MSRS:PlayText(Text, Delay, Coordinate)
-- Execute command. -- Execute command.
self:_ExecCommand(command) self:_ExecCommand(command)
else
self:T(self.lid.."Transmitting")
self:_DCSgRPCtts(Text, nil, nil , nil, nil, nil, nil, Coordinate)
end
end end
return self return self
@ -928,18 +1009,10 @@ function MSRS:PlayTextExt(Text, Delay, Frequencies, Modulations, Gender, Culture
self:ScheduleOnce(Delay, MSRS.PlayTextExt, self, Text, 0, Frequencies, Modulations, Gender, Culture, Voice, Volume, Label, Coordinate) self:ScheduleOnce(Delay, MSRS.PlayTextExt, self, Text, 0, Frequencies, Modulations, Gender, Culture, Voice, Volume, Label, Coordinate)
else else
-- Ensure table. if self.backend==MSRS.Backend.SRSEXE then
if Frequencies and type(Frequencies)~="table" then
Frequencies={Frequencies}
end
-- Ensure table.
if Modulations and type(Modulations)~="table" then
Modulations={Modulations}
end
-- Get command line. -- Get command line.
local command=self:_GetCommand(Frequencies, Modulations, nil, Gender, Voice, Culture, Volume, nil, nil, Label, Coordinate) local command=self:_GetCommand(UTILS.EnsureTable(Frequencies, false), UTILS.EnsureTable(Modulations, false), nil, Gender, Voice, Culture, Volume, nil, nil, Label, Coordinate)
-- Append text. -- Append text.
command=command..string.format(" --text=\"%s\"", tostring(Text)) command=command..string.format(" --text=\"%s\"", tostring(Text))
@ -947,6 +1020,12 @@ function MSRS:PlayTextExt(Text, Delay, Frequencies, Modulations, Gender, Culture
-- Execute command. -- Execute command.
self:_ExecCommand(command) self:_ExecCommand(command)
elseif self.backend==MSRS.Backend.GRPC then
self:_DCSgRPCtts(Text,Optional,Frequencies,Voice,Label,Plaintext)
end
end end
return self return self
@ -1029,12 +1108,18 @@ end
--- Get lat, long and alt from coordinate. --- Get lat, long and alt from coordinate.
-- @param #MSRS self -- @param #MSRS self
-- @param Core.Point#Coordinate Coordinate Coordinate. Can also be a DCS#Vec3. -- @param Core.Point#Coordinate Coordinate Coordinate. Can also be a DCS#Vec3.
-- @return #number Latitude. -- @return #number Latitude (or 0 if no input coordinate was given).
-- @return #number Longitude. -- @return #number Longitude (or 0 if no input coordinate was given).
-- @return #number Altitude. -- @return #number Altitude (or 0 if no input coordinate was given).
function MSRS:_GetLatLongAlt(Coordinate) function MSRS:_GetLatLongAlt(Coordinate)
local lat, lon, alt=coord.LOtoLL(Coordinate) local lat=0.0
local lon=0.0
local alt=0.0
if Coordinate then
lat, lon, alt=coord.LOtoLL(Coordinate)
end
return lat, lon, math.floor(alt) return lat, lon, math.floor(alt)
end end
@ -1239,35 +1324,39 @@ end
--- Make DCS-gRPC API call to transmit text-to-speech over SRS. --- Make DCS-gRPC API call to transmit text-to-speech over SRS.
-- @param #MSRS self -- @param #MSRS self
-- @param #string Text Text of message to transmit (can also be SSML). -- @param #string Text Text of message to transmit (can also be SSML).
-- @param #string Optional plaintext version of message (for accessiblity).
-- @param #table Frequencies Radio frequencies to transmit on. Can also accept a number in MHz. -- @param #table Frequencies Radio frequencies to transmit on. Can also accept a number in MHz.
-- @param #string Voice Voice for the TTS provider to user. -- @param #string Gender Gender.
-- @param #string Label Label (SRS diplays as name of the transmitter). -- @param #string Culture Culture.
-- @param #string Voice Voice.
-- @param #number Volume Volume.
-- @param #string Label Label.
-- @param Core.Point#COORDINATE Coordinate Coordinate.
-- @return #MSRS self -- @return #MSRS self
function MSRS:_DCSgRPCtts(Text, Plaintext, Frequencies, Voice, Label) function MSRS:_DCSgRPCtts(Text, Frequencies, Gender, Culture, Voice, Volume, Label, Coordinate)
BASE:T("MSRS_BACKEND_DCSGRPC:_DCSgRPCtts()") --Frequencies, Modulations, Gender, Culture, Voice, Volume, Label, Coordinate)
BASE:T({Text, Plaintext, Frequencies, Voice, Label})
self:I("MSRS_BACKEND_DCSGRPC:_DCSgRPCtts()")
self:I({Text, Frequencies, Gender, Culture, Voice, Volume, Label, Coordinate})
--local options = self.ProviderOptions or MSRS.ProviderOptions or {} -- #MSRS.GRPCOptions
local options = {} -- #MSRS.GRPCOptions
local options = self.ProviderOptions or MSRS.ProviderOptions or {} -- #MSRS.GRPCOptions
local ssml = Text or '' local ssml = Text or ''
local XmitFrequencies = Frequencies or self.Frequency Frequencies = UTILS.EnsureTable(Frequencies, true) or self:GetFrequencies()
if type(XmitFrequencies)~="table" then
XmitFrequencies={XmitFrequencies}
end
options.plaintext = Plaintext options.plaintext = Plaintext
options.srsClientName = Label or self.Label options.srsClientName = Label or self.Label
if false then
options.position = {} options.position = {}
if self.coordinate then if self.coordinate then
options.position.lat, options.position.lon, options.position.alt = self:_GetLatLongAlt(self.coordinate) options.position.lat, options.position.lon, options.position.alt = self:_GetLatLongAlt(self.coordinate)
end end
options.position.lat = options.position.lat or 0.0
options.position.lon = options.position.lon or 0.0
options.position.alt = options.position.alt or 0.0
if UTILS.GetCoalitionName(self.coalition) == 'Blue' then if UTILS.GetCoalitionName(self.coalition) == 'Blue' then
options.coalition = 'blue' options.coalition = 'blue'
elseif UTILS.GetCoalitionName(self.coalition) == 'Red' then elseif UTILS.GetCoalitionName(self.coalition) == 'Red' then
@ -1277,8 +1366,8 @@ function MSRS:_DCSgRPCtts(Text, Plaintext, Frequencies, Voice, Label)
local provider = self.provider or self.GRPCOptions.DefaultProvider or MSRS.GRPCOptions.DefaultProvider local provider = self.provider or self.GRPCOptions.DefaultProvider or MSRS.GRPCOptions.DefaultProvider
options.provider = {} options.provider = {}
--options.provider[provider] = {}
options.provider[provider] = {} options.provider[provider] = self:GetProviderOptions(provider)
if self.APIKey then if self.APIKey then
options.provider[provider].key = self.APIKey options.provider[provider].key = self.APIKey
@ -1288,8 +1377,10 @@ function MSRS:_DCSgRPCtts(Text, Plaintext, Frequencies, Voice, Label)
options.provider[provider].defaultVoice = self.defaultVoice options.provider[provider].defaultVoice = self.defaultVoice
end end
end
if self.voice then if self.voice then
options.provider[provider].voice = Voice or self.voice or self.defaultVoice --options.provider[provider].voice = Voice or self.voice or self.defaultVoice
elseif ssml then elseif ssml then
-- DCS-gRPC doesn't directly support language/gender, but can use SSML -- DCS-gRPC doesn't directly support language/gender, but can use SSML
-- Only use if a voice isn't explicitly set -- Only use if a voice isn't explicitly set
@ -1307,16 +1398,17 @@ function MSRS:_DCSgRPCtts(Text, Plaintext, Frequencies, Voice, Label)
postTag = '</voice>' postTag = '</voice>'
ssml = preTag .. Text .. postTag ssml = preTag .. Text .. postTag
end end
end end
for _,_freq in ipairs(XmitFrequencies) do env.info("FF freq")
local freq = _freq*1000000 for _,freq in pairs(Frequencies) do
BASE:T("GRPC.tts") env.info("FF freq1")
BASE:T(ssml) self:T("GRPC.tts")
BASE:T(freq) self:T(ssml)
BASE:T({options}) self:T(freq)
GRPC.tts(ssml, freq, options) self:T({options})
UTILS.PrintTableToLog(options)
--GRPC.tts(ssml, freq*1e6, options)
end end
end end
@ -1778,10 +1870,15 @@ MSRS_BACKEND_DCSGRPC.Functions._DCSgRPCtts = function (self, Text, Plaintext, Fr
end end
if self.voice then if self.voice then
-- Set voice.
options.provider[provider].voice = Voice or self.voice or self.defaultVoice options.provider[provider].voice = Voice or self.voice or self.defaultVoice
elseif ssml then elseif ssml then
-- DCS-gRPC doesn't directly support language/gender, but can use SSML -- DCS-gRPC doesn't directly support language/gender, but can use SSML
-- Only use if a voice isn't explicitly set -- Only use if a voice isn't explicitly set
local preTag, genderProp, langProp, postTag = '', '', '', '' local preTag, genderProp, langProp, postTag = '', '', '', ''
if self.gender then if self.gender then
@ -2200,7 +2297,7 @@ function MSRSQUEUE:_CheckRadioQueue(delay)
end end
MSRS.LoadConfigFile() --MSRS.LoadConfigFile()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------