Skip to content

Commit 65dcf61

Browse files
committed
independent VHF1 Control when GSX disabled / Handling GSX Restarts during Session
1 parent 31ec71c commit 65dcf61

File tree

3 files changed

+102
-73
lines changed

3 files changed

+102
-73
lines changed

Fenix2GSX/GsxController.cs

Lines changed: 100 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public GsxController(ServiceModel model)
123123

124124
private void GetAudioSessions()
125125
{
126-
if (gsxAudioSession == null)
126+
if (Model.GsxVolumeControl && gsxAudioSession == null)
127127
{
128128
MMDeviceEnumerator deviceEnumerator = new(Guid.NewGuid());
129129
var devices = deviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
@@ -146,7 +146,7 @@ private void GetAudioSessions()
146146
}
147147
}
148148

149-
if (vhf1AudioSession == null)
149+
if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession == null)
150150
{
151151
MMDeviceEnumerator deviceEnumerator = new(Guid.NewGuid());
152152
var devices = deviceEnumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
@@ -189,95 +189,114 @@ public void ResetAudio()
189189

190190
public void ControlAudio()
191191
{
192-
if (SimConnect.ReadLvar("I_FCU_TRACK_FPA_MODE") == 0 && SimConnect.ReadLvar("I_FCU_HEADING_VS_MODE") == 0)
192+
try
193193
{
194-
if (Model.GsxVolumeControl || !string.IsNullOrEmpty(Model.Vhf1VolumeApp))
195-
ResetAudio();
196-
return;
197-
}
194+
if (SimConnect.ReadLvar("I_FCU_TRACK_FPA_MODE") == 0 && SimConnect.ReadLvar("I_FCU_HEADING_VS_MODE") == 0)
195+
{
196+
if (Model.GsxVolumeControl || !string.IsNullOrEmpty(Model.Vhf1VolumeApp))
197+
ResetAudio();
198+
return;
199+
}
198200

199-
if (Model.GsxVolumeControl && gsxAudioSession != null)
200-
{
201-
float volume = SimConnect.ReadLvar("A_ASP_INT_VOLUME");
202-
int muted = (int)SimConnect.ReadLvar("I_ASP_INT_REC");
203-
if (volume >= 0 && volume != gsxAudioVolume)
201+
if (Model.GsxVolumeControl && gsxAudioSession != null)
202+
{
203+
float volume = SimConnect.ReadLvar("A_ASP_INT_VOLUME");
204+
int muted = (int)SimConnect.ReadLvar("I_ASP_INT_REC");
205+
if (volume >= 0 && volume != gsxAudioVolume)
206+
{
207+
gsxAudioSession.SimpleAudioVolume.MasterVolume = volume;
208+
gsxAudioVolume = volume;
209+
}
210+
211+
if (muted >= 0 && muted != gsxAudioMute)
212+
{
213+
gsxAudioSession.SimpleAudioVolume.Mute = muted == 0;
214+
gsxAudioMute = muted;
215+
}
216+
}
217+
else if (Model.GsxVolumeControl && gsxAudioSession == null)
204218
{
205-
gsxAudioSession.SimpleAudioVolume.MasterVolume = volume;
206-
gsxAudioVolume = volume;
219+
GetAudioSessions();
220+
gsxAudioVolume = -1;
221+
gsxAudioMute = -1;
222+
}
223+
else if (!Model.GsxVolumeControl && gsxAudioSession != null)
224+
{
225+
gsxAudioSession.SimpleAudioVolume.MasterVolume = 1.0f;
226+
gsxAudioSession.SimpleAudioVolume.Mute = false;
227+
gsxAudioSession = null;
228+
gsxAudioVolume = -1;
229+
gsxAudioMute = -1;
230+
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for GSX (Setting disabled)");
207231
}
208232

209-
if (muted >= 0 && muted != gsxAudioMute)
233+
if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession != null)
210234
{
211-
gsxAudioSession.SimpleAudioVolume.Mute = muted == 0;
212-
gsxAudioMute = muted;
235+
float volume = SimConnect.ReadLvar("A_ASP_VHF_1_VOLUME");
236+
int muted = (int)SimConnect.ReadLvar("I_ASP_VHF_1_REC");
237+
if (volume >= 0 && volume != vhf1AudioVolume)
238+
{
239+
vhf1AudioSession.SimpleAudioVolume.MasterVolume = volume;
240+
vhf1AudioVolume = volume;
241+
}
242+
243+
if (muted >= 0 && muted != vhf1AudioMute)
244+
{
245+
vhf1AudioSession.SimpleAudioVolume.Mute = muted == 0;
246+
vhf1AudioMute = muted;
247+
}
248+
}
249+
else if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession == null)
250+
{
251+
GetAudioSessions();
252+
vhf1AudioVolume = -1;
253+
vhf1AudioMute = -1;
213254
}
214-
}
215-
else if (Model.GsxVolumeControl && gsxAudioSession == null)
216-
{
217-
GetAudioSessions();
218-
gsxAudioVolume = -1;
219-
gsxAudioMute = -1;
220-
}
221-
else if (!Model.GsxVolumeControl && gsxAudioSession != null)
222-
{
223-
gsxAudioSession = null;
224-
gsxAudioVolume = -1;
225-
gsxAudioMute = -1;
226-
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for GSX (Setting disabled)");
227-
}
228255

229-
if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession != null)
230-
{
231-
float volume = SimConnect.ReadLvar("A_ASP_VHF_1_VOLUME");
232-
int muted = (int)SimConnect.ReadLvar("I_ASP_VHF_1_REC");
233-
if (volume >= 0 && volume != vhf1AudioVolume)
256+
if (lastVhf1App != Model.Vhf1VolumeApp)
234257
{
235-
vhf1AudioSession.SimpleAudioVolume.MasterVolume = volume;
236-
vhf1AudioVolume = volume;
258+
if (vhf1AudioSession != null)
259+
{
260+
vhf1AudioSession.SimpleAudioVolume.MasterVolume = 1.0f;
261+
vhf1AudioSession.SimpleAudioVolume.Mute = false;
262+
vhf1AudioSession = null;
263+
vhf1AudioVolume = -1;
264+
vhf1AudioMute = -1;
265+
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for {lastVhf1App} (App changed)");
266+
}
267+
GetAudioSessions();
237268
}
269+
lastVhf1App = Model.Vhf1VolumeApp;
238270

239-
if (muted >= 0 && muted != vhf1AudioMute)
271+
if (Model.GsxVolumeControl && gsxAudioSession != null && !IPCManager.IsProcessRunning(gsxProcess))
240272
{
241-
vhf1AudioSession.SimpleAudioVolume.Mute = muted == 0;
242-
vhf1AudioMute = muted;
273+
gsxAudioSession = null;
274+
gsxAudioVolume = -1;
275+
gsxAudioMute = -1;
276+
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for GSX (App not running)");
243277
}
244-
}
245-
else if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession == null)
246-
{
247-
GetAudioSessions();
248-
vhf1AudioVolume = -1;
249-
vhf1AudioMute = -1;
250-
}
251-
252-
if (lastVhf1App != Model.Vhf1VolumeApp)
253-
{
254-
if (vhf1AudioSession != null)
278+
279+
if (Model.GsxVolumeControl && gsxAudioSession != null && SimConnect.ReadLvar("FSDT_GSX_COUATL_STARTED") != 1)
280+
{
281+
gsxAudioSession.SimpleAudioVolume.MasterVolume = 1.0f;
282+
gsxAudioSession.SimpleAudioVolume.Mute = false;
283+
gsxAudioSession = null;
284+
gsxAudioVolume = -1;
285+
gsxAudioMute = -1;
286+
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for GSX (Couatl Engine not started)");
287+
}
288+
289+
if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession != null && !IPCManager.IsProcessRunning(Model.Vhf1VolumeApp))
255290
{
256-
vhf1AudioSession.SimpleAudioVolume.MasterVolume = 1.0f;
257-
vhf1AudioSession.SimpleAudioVolume.Mute = false;
258291
vhf1AudioSession = null;
259292
vhf1AudioVolume = -1;
260293
vhf1AudioMute = -1;
261-
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for {lastVhf1App} (App changed)");
294+
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for {Model.Vhf1VolumeApp} (App not running)");
262295
}
263-
GetAudioSessions();
264-
}
265-
lastVhf1App = Model.Vhf1VolumeApp;
266-
267-
if (Model.GsxVolumeControl && gsxAudioSession != null && !IPCManager.IsProcessRunning(gsxProcess))
268-
{
269-
gsxAudioSession = null;
270-
gsxAudioVolume = -1;
271-
gsxAudioMute = -1;
272-
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for GSX (App not running)");
273296
}
274-
275-
if (!string.IsNullOrEmpty(Model.Vhf1VolumeApp) && vhf1AudioSession != null && !IPCManager.IsProcessRunning(Model.Vhf1VolumeApp))
297+
catch (Exception ex)
276298
{
277-
vhf1AudioSession = null;
278-
vhf1AudioVolume = -1;
279-
vhf1AudioMute = -1;
280-
Logger.Log(LogLevel.Information, "GsxController:ControlAudio", $"Disabled Audio Session for {Model.Vhf1VolumeApp} (App not running)");
299+
Logger.Log(LogLevel.Debug, "GsxController:ControlAudio", $"Exception {ex.GetType()} during Audio Control: {ex.Message}");
281300
}
282301
}
283302

@@ -301,6 +320,11 @@ public void RunServices()
301320
if (SimConnect.ReadLvar("FSDT_GSX_COUATL_STARTED") != 1)
302321
{
303322
Logger.Log(LogLevel.Information, "GsxController:RunServices", $"Couatl Engine not running");
323+
if (Model.GsxVolumeControl && gsxAudioSession != null)
324+
{
325+
Logger.Log(LogLevel.Information, "GsxController:RunServices", $"Resetting GSX Audio (Engine not running)");
326+
gsxAudioSession = null;
327+
}
304328
return;
305329
}
306330

@@ -687,6 +711,11 @@ private void RunArrivalServices(int deboard_state)
687711
if (SimConnect.ReadLvar("FSDT_GSX_COUATL_STARTED") != 1)
688712
{
689713
Logger.Log(LogLevel.Information, "GsxController:RunArrivalServices", $"Couatl Engine not running");
714+
if (Model.GsxVolumeControl && gsxAudioSession != null)
715+
{
716+
Logger.Log(LogLevel.Information, "GsxController:RunServices", $"Resetting GSX Audio (Engine not running)");
717+
gsxAudioSession = null;
718+
}
690719
return;
691720
}
692721

Fenix2GSX/ServiceController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected void ServiceLoop()
103103
elapsedMS = 0;
104104
}
105105

106-
if (Model.GsxVolumeControl)
106+
if (Model.GsxVolumeControl || !string.IsNullOrEmpty(Model.Vhf1VolumeApp))
107107
gsxController.ControlAudio();
108108

109109
Thread.Sleep(delay);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The Configuration is done through the UI, open it by clicking on the System-Tray
4343
All Settings can be changed dynamically on the Fly if needed (except "Wait until MSFS..."). But do that before a Service/Feature starts or after it has ended. For example, don't disable "Automatic Jetway/Stair Operation" (autoConnect) while the Jetway is connected. Do it before the Tool calls the Jetway or after it was disconnected by the Tool.<br/><br/>
4444
In general, it is up to your Preference how much Automation you want. I you want to keep Control of when Services are Called and/or the Jetway is connected, you can still enjoy the (De-)Boarding and Refueling Syncronization when the Automation-Options are disabled. The only Automation which can not be disabled: The Removal of the Ground-Equipment and Jetway-Disconnection (if still connected) is always active on Depature.<br/><br/>
4545
Tip for VATSIM / IVAO: Disable the Auto-Connect Option before loading the Session in MSFS, in Case you need to move to another Gate. If the Gate is free (or you have moved to a free one) you can renable Auto-Connect and the Jetway/Stairs will still connect then (unless the Flightplan was already loaded in the EFB). Repositioning through GSX normaly keeps GPU/Chocks/PCA connected - but a quick Check won't hurt :wink:<br/><br/>
46-
A Note on the Audio-Control: The Tool does not control the Audio until the Plane is powered (=FCU is On). When you end your Session, it will try to reset the Application-Audio to unmuted and 100% Volume. But that does not really work on GSX because it is restarting at the same Time. So GSX can stay muted - keep that in Mind.
46+
A Note on the Audio-Control: The Tool does not control Audio until the Plane is powered (=FCU is On). When you end your Session, it will try to reset the Application-Audio to unmuted and 100% Volume. But that does not really work on GSX because it is resetting at the same Time. So GSX can stay muted when switching to another Plane - keep that in Mind.
4747
<br/><br/>
4848

4949
* **waitForConnect** - The Binary will wait until MSFS is started and SimConnect is available. Default *"true"*

0 commit comments

Comments
 (0)