mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
Checkpoint
This commit is contained in:
parent
20bc32fc89
commit
665b21f2f7
2 changed files with 90 additions and 71 deletions
|
@ -80,6 +80,7 @@ public:
|
|||
|
||||
signals:
|
||||
bool shouldShowHandControllersChanged();
|
||||
void mountedChanged();
|
||||
|
||||
public:
|
||||
HMDScriptingInterface();
|
||||
|
|
|
@ -45,26 +45,43 @@ if (typeof String.prototype.trimEndsWith != 'function') {
|
|||
};
|
||||
}
|
||||
|
||||
/****************************************
|
||||
VAR DEFINITIONS
|
||||
****************************************/
|
||||
const INPUT_DEVICE_SETTING = "audio_input_device";
|
||||
const OUTPUT_DEVICE_SETTING = "audio_output_device";
|
||||
|
||||
var selectedInputMenu = "";
|
||||
var selectedOutputMenu = "";
|
||||
var audioDevicesList = [];
|
||||
// Some HMDs (like Oculus CV1) have a built in audio device. If they
|
||||
// do, then this function will handle switching to that device automatically
|
||||
// when you goActive with the HMD active.
|
||||
var wasHmdActive = false; // assume it's not active to start
|
||||
var switchedAudioInputToHMD = false;
|
||||
var switchedAudioOutputToHMD = false;
|
||||
var previousSelectedInputAudioDevice = "";
|
||||
var previousSelectedOutputAudioDevice = "";
|
||||
|
||||
var audioDevicesList = [];
|
||||
/****************************************
|
||||
BEGIN FUNCTION DEFINITIONS
|
||||
****************************************/
|
||||
function setupAudioMenus() {
|
||||
removeAudioMenus();
|
||||
Menu.addSeparator("Audio", "Input Audio Device");
|
||||
|
||||
/* Setup audio input devices */
|
||||
Menu.addSeparator("Audio", "Input Audio Device");
|
||||
var inputDeviceSetting = Settings.getValue(INPUT_DEVICE_SETTING);
|
||||
var inputDevices = AudioDevice.getInputDevices();
|
||||
var selectedInputDevice = AudioDevice.getInputDevice();
|
||||
if (inputDevices.indexOf(inputDeviceSetting) != -1 && selectedInputDevice != inputDeviceSetting) {
|
||||
print ("Audio input device SETTING does not match Input AudioDevice. Attempting to change Input AudioDevice...")
|
||||
if (AudioDevice.setInputDevice(inputDeviceSetting)) {
|
||||
selectedInputDevice = inputDeviceSetting;
|
||||
} else {
|
||||
print("Error setting audio input device!")
|
||||
}
|
||||
}
|
||||
print("audio input devices: " + inputDevices);
|
||||
print("Audio input devices: " + inputDevices);
|
||||
for(var i = 0; i < inputDevices.length; i++) {
|
||||
var thisDeviceSelected = (inputDevices[i] == selectedInputDevice);
|
||||
var menuItem = "Use " + inputDevices[i] + " for Input";
|
||||
|
@ -80,17 +97,20 @@ function setupAudioMenus() {
|
|||
}
|
||||
}
|
||||
|
||||
/* Setup audio output devices */
|
||||
Menu.addSeparator("Audio", "Output Audio Device");
|
||||
|
||||
var outputDeviceSetting = Settings.getValue(OUTPUT_DEVICE_SETTING);
|
||||
var outputDevices = AudioDevice.getOutputDevices();
|
||||
var selectedOutputDevice = AudioDevice.getOutputDevice();
|
||||
if (outputDevices.indexOf(outputDeviceSetting) != -1 && selectedOutputDevice != outputDeviceSetting) {
|
||||
print("Audio output device SETTING does not match Output AudioDevice. Attempting to change Output AudioDevice...")
|
||||
if (AudioDevice.setOutputDevice(outputDeviceSetting)) {
|
||||
selectedOutputDevice = outputDeviceSetting;
|
||||
} else {
|
||||
print("Error setting audio output device!")
|
||||
}
|
||||
}
|
||||
print("audio output devices: " + outputDevices);
|
||||
print("Audio output devices: " + outputDevices);
|
||||
for (var i = 0; i < outputDevices.length; i++) {
|
||||
var thisDeviceSelected = (outputDevices[i] == selectedOutputDevice);
|
||||
var menuItem = "Use " + outputDevices[i] + " for Output";
|
||||
|
@ -115,127 +135,125 @@ function removeAudioMenus() {
|
|||
Menu.removeMenuItem("Audio", audioDevicesList[index]);
|
||||
}
|
||||
|
||||
Menu.removeMenu("Audio > Devices");
|
||||
|
||||
audioDevicesList = [];
|
||||
}
|
||||
|
||||
function onDevicechanged() {
|
||||
print("audio devices changed, removing Audio > Devices menu...");
|
||||
Menu.removeMenu("Audio > Devices");
|
||||
print("now setting up Audio > Devices menu");
|
||||
print("System audio device changed. Removing and replacing Audio Menus...");
|
||||
setupAudioMenus();
|
||||
}
|
||||
|
||||
// Have a small delay before the menu's get setup and the audio devices can switch to the last selected ones
|
||||
Script.setTimeout(function () {
|
||||
print("connecting deviceChanged");
|
||||
AudioDevice.deviceChanged.connect(onDevicechanged);
|
||||
print("setting up Audio > Devices menu for first time");
|
||||
setupAudioMenus();
|
||||
}, 5000);
|
||||
|
||||
function scriptEnding() {
|
||||
Menu.removeMenu("Audio > Devices");
|
||||
}
|
||||
Script.scriptEnding.connect(scriptEnding);
|
||||
|
||||
|
||||
function menuItemEvent(menuItem) {
|
||||
if (menuItem.startsWith("Use ")) {
|
||||
if (menuItem.endsWith(" for Output")) {
|
||||
var selectedDevice = menuItem.trimStartsWith("Use ").trimEndsWith(" for Output");
|
||||
print("output audio selection..." + selectedDevice);
|
||||
Menu.menuItemEvent.disconnect(menuItemEvent);
|
||||
Menu.setIsOptionChecked(selectedOutputMenu, false);
|
||||
selectedOutputMenu = menuItem;
|
||||
Menu.setIsOptionChecked(selectedOutputMenu, true);
|
||||
if (AudioDevice.setOutputDevice(selectedDevice)) {
|
||||
Settings.setValue(OUTPUT_DEVICE_SETTING, selectedDevice);
|
||||
}
|
||||
Menu.menuItemEvent.connect(menuItemEvent);
|
||||
} else if (menuItem.endsWith(" for Input")) {
|
||||
if (menuItem.endsWith(" for Input")) {
|
||||
var selectedDevice = menuItem.trimStartsWith("Use ").trimEndsWith(" for Input");
|
||||
print("input audio selection..." + selectedDevice);
|
||||
print("User selected a new Audio Input Device: " + selectedDevice);
|
||||
Menu.menuItemEvent.disconnect(menuItemEvent);
|
||||
Menu.setIsOptionChecked(selectedInputMenu, false);
|
||||
selectedInputMenu = menuItem;
|
||||
Menu.setIsOptionChecked(selectedInputMenu, true);
|
||||
if (AudioDevice.setInputDevice(selectedDevice)) {
|
||||
Settings.setValue(INPUT_DEVICE_SETTING, selectedDevice);
|
||||
} else {
|
||||
print("Error setting audio input device!")
|
||||
}
|
||||
Menu.menuItemEvent.connect(menuItemEvent);
|
||||
} else if (menuItem.endsWith(" for Output")) {
|
||||
var selectedDevice = menuItem.trimStartsWith("Use ").trimEndsWith(" for Output");
|
||||
print("User selected a new Audio Output Device: " + selectedDevice);
|
||||
Menu.menuItemEvent.disconnect(menuItemEvent);
|
||||
Menu.setIsOptionChecked(selectedOutputMenu, false);
|
||||
selectedOutputMenu = menuItem;
|
||||
Menu.setIsOptionChecked(selectedOutputMenu, true);
|
||||
if (AudioDevice.setOutputDevice(selectedDevice)) {
|
||||
Settings.setValue(OUTPUT_DEVICE_SETTING, selectedDevice);
|
||||
} else {
|
||||
print("Error setting audio output device!")
|
||||
}
|
||||
Menu.menuItemEvent.connect(menuItemEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Menu.menuItemEvent.connect(menuItemEvent);
|
||||
|
||||
// Some HMDs (like Oculus CV1) have a built in audio device. If they
|
||||
// do, then this function will handle switching to that device automatically
|
||||
// when you goActive with the HMD active.
|
||||
var wasHmdMounted = false; // assume it's un-mounted to start
|
||||
var switchedAudioInputToHMD = false;
|
||||
var switchedAudioOutputToHMD = false;
|
||||
var previousSelectedInputAudioDevice = "";
|
||||
var previousSelectedOutputAudioDevice = "";
|
||||
|
||||
function restoreAudio() {
|
||||
if (switchedAudioInputToHMD) {
|
||||
print("switching back from HMD preferred audio input to:" + previousSelectedInputAudioDevice);
|
||||
print("Switching back from HMD preferred audio input to: " + previousSelectedInputAudioDevice);
|
||||
menuItemEvent("Use " + previousSelectedInputAudioDevice + " for Input");
|
||||
switchedAudioInputToHMD = false;
|
||||
}
|
||||
if (switchedAudioOutputToHMD) {
|
||||
print("switching back from HMD preferred audio output to:" + previousSelectedOutputAudioDevice);
|
||||
print("Switching back from HMD preferred audio output to: " + previousSelectedOutputAudioDevice);
|
||||
menuItemEvent("Use " + previousSelectedOutputAudioDevice + " for Output");
|
||||
switchedAudioOutputToHMD = false;
|
||||
}
|
||||
}
|
||||
|
||||
function checkHMDAudio() {
|
||||
// Mounted state is changing... handle switching
|
||||
if (HMD.mounted != wasHmdMounted) {
|
||||
print("HMD mounted changed...");
|
||||
// HMD Active state is changing; handle switching
|
||||
if (HMD.active != wasHmdActive) {
|
||||
print("HMD Active state changed!");
|
||||
|
||||
// We're putting the HMD on... switch to those devices
|
||||
if (HMD.mounted) {
|
||||
print("NOW mounted...");
|
||||
// We're putting the HMD on; switch to those devices
|
||||
if (HMD.active) {
|
||||
print("HMD is now Active.");
|
||||
var hmdPreferredAudioInput = HMD.preferredAudioInput();
|
||||
var hmdPreferredAudioOutput = HMD.preferredAudioOutput();
|
||||
print("hmdPreferredAudioInput:" + hmdPreferredAudioInput);
|
||||
print("hmdPreferredAudioOutput:" + hmdPreferredAudioOutput);
|
||||
print("hmdPreferredAudioInput: " + hmdPreferredAudioInput);
|
||||
print("hmdPreferredAudioOutput: " + hmdPreferredAudioOutput);
|
||||
|
||||
|
||||
var hmdHasPreferredAudio = (hmdPreferredAudioInput !== "") || (hmdPreferredAudioOutput !== "");
|
||||
if (hmdHasPreferredAudio) {
|
||||
print("HMD has preferred audio!");
|
||||
if (hmdPreferredAudioInput !== "") {
|
||||
print("HMD has preferred audio input device.");
|
||||
previousSelectedInputAudioDevice = Settings.getValue(INPUT_DEVICE_SETTING);
|
||||
previousSelectedOutputAudioDevice = Settings.getValue(OUTPUT_DEVICE_SETTING);
|
||||
print("previousSelectedInputAudioDevice:" + previousSelectedInputAudioDevice);
|
||||
print("previousSelectedOutputAudioDevice:" + previousSelectedOutputAudioDevice);
|
||||
if (hmdPreferredAudioInput != previousSelectedInputAudioDevice && hmdPreferredAudioInput !== "") {
|
||||
print("switching to HMD preferred audio input to:" + hmdPreferredAudioInput);
|
||||
print("previousSelectedInputAudioDevice: " + previousSelectedInputAudioDevice);
|
||||
if (hmdPreferredAudioInput != previousSelectedInputAudioDevice) {
|
||||
print("Switching Audio Input device to HMD preferred device: " + hmdPreferredAudioInput);
|
||||
switchedAudioInputToHMD = true;
|
||||
menuItemEvent("Use " + hmdPreferredAudioInput + " for Input");
|
||||
}
|
||||
if (hmdPreferredAudioOutput != previousSelectedOutputAudioDevice && hmdPreferredAudioOutput !== "") {
|
||||
print("switching to HMD preferred audio output to:" + hmdPreferredAudioOutput);
|
||||
}
|
||||
if (hmdPreferredAudioOutput !== "") {
|
||||
print("HMD has preferred audio output device.");
|
||||
previousSelectedOutputAudioDevice = Settings.getValue(OUTPUT_DEVICE_SETTING);
|
||||
print("previousSelectedOutputAudioDevice: " + previousSelectedOutputAudioDevice);
|
||||
if (hmdPreferredAudioOutput != previousSelectedOutputAudioDevice) {
|
||||
print("Switching Audio Output device to HMD preferred device: " + hmdPreferredAudioOutput);
|
||||
switchedAudioOutputToHMD = true;
|
||||
menuItemEvent("Use " + hmdPreferredAudioOutput + " for Output");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print("HMD NOW un-mounted...");
|
||||
print("HMD no longer active. Restoring audio I/O devices...");
|
||||
restoreAudio();
|
||||
}
|
||||
}
|
||||
wasHmdMounted = HMD.mounted;
|
||||
wasHmdActive = HMD.active;
|
||||
}
|
||||
/****************************************
|
||||
END FUNCTION DEFINITIONS
|
||||
****************************************/
|
||||
|
||||
Script.update.connect(checkHMDAudio);
|
||||
/****************************************
|
||||
BEGIN SCRIPT BODY
|
||||
****************************************/
|
||||
// Have a small delay before the menus get setup so the audio devices can switch to the last selected ones
|
||||
Script.setTimeout(function () {
|
||||
print("Connecting deviceChanged() and displayModeChanged()");
|
||||
AudioDevice.deviceChanged.connect(onDevicechanged);
|
||||
HMD.displayModeChanged.connect(checkHMDAudio);
|
||||
print("Setting up Audio > Devices menu for the first time");
|
||||
setupAudioMenus();
|
||||
}, 2000);
|
||||
|
||||
print("Connecting menuItemEvent() and scriptEnding()");
|
||||
Menu.menuItemEvent.connect(menuItemEvent);
|
||||
Script.scriptEnding.connect(function () {
|
||||
restoreAudio();
|
||||
removeAudioMenus();
|
||||
Menu.menuItemEvent.disconnect(menuItemEvent);
|
||||
Script.update.disconnect(checkHMDAudio);
|
||||
HMD.displayModeChanged.disconnect(checkHMDAudio);
|
||||
});
|
||||
|
||||
}()); // END LOCAL_SCOPE
|
||||
|
|
Loading…
Reference in a new issue