diff --git a/examples/selectAudioDevice.js b/examples/selectAudioDevice.js new file mode 100644 index 0000000000..882aec87cc --- /dev/null +++ b/examples/selectAudioDevice.js @@ -0,0 +1,118 @@ +// +// audioDeviceExample.js +// hifi +// +// Created by Brad Hefta-Gaub on 3/22/14 +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. +// +// This is an example script that demonstrates use of the Menu object +// + +if (typeof String.prototype.startsWith != 'function') { + String.prototype.startsWith = function (str){ + return this.slice(0, str.length) == str; + }; +} + +if (typeof String.prototype.endsWith != 'function') { + String.prototype.endsWith = function (str){ + return this.slice(-str.length) == str; + }; +} + +if (typeof String.prototype.trimStartsWith != 'function') { + String.prototype.trimStartsWith = function (str){ + if (this.startsWith(str)) { + return this.substr(str.length); + } + return this; + }; +} + +if (typeof String.prototype.trimEndsWith != 'function') { + String.prototype.trimEndsWith = function (str){ + if (this.endsWith(str)) { + return this.substr(0,this.length - str.length); + } + return this; + }; +} + +var selectedInputMenu = ""; +var selectedOutputMenu = ""; + +function setupAudioMenus() { + Menu.addMenu("Tools > Audio"); + Menu.addSeparator("Tools > Audio","Output Audio Device"); + + var outputDevices = AudioDevice.getOutputDevices(); + var selectedOutputDevice = AudioDevice.getOutputDevice(); + + for(var i = 0; i < outputDevices.length; i++) { + var thisDeviceSelected = (outputDevices[i] == selectedOutputDevice); + var menuItem = "Use " + outputDevices[i] + " for Output"; + Menu.addMenuItem({ + menuName: "Tools > Audio", + menuItemName: menuItem, + isCheckable: true, + isChecked: thisDeviceSelected + }); + if (thisDeviceSelected) { + selectedOutputMenu = menuItem; + } + } + + Menu.addSeparator("Tools > Audio","Input Audio Device"); + + var inputDevices = AudioDevice.getInputDevices(); + var selectedInputDevice = AudioDevice.getInputDevice(); + + for(var i = 0; i < inputDevices.length; i++) { + var thisDeviceSelected = (inputDevices[i] == selectedInputDevice); + var menuItem = "Use " + inputDevices[i] + " for Input"; + Menu.addMenuItem({ + menuName: "Tools > Audio", + menuItemName: menuItem, + isCheckable: true, + isChecked: thisDeviceSelected + }); + if (thisDeviceSelected) { + selectedInputMenu = menuItem; + } + } +} + +setupAudioMenus(); + +function scriptEnding() { + print("scriptEnding()"); + //Menu.removeMenuItem("Tools > Audio"); + Menu.removeMenu("Tools > Audio"); + //Menu.removeMenu("Tools"); + print("DONE... scriptEnding()"); +} +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.setIsOptionChecked(selectedOutputMenu, false); + selectedOutputMenu = menuItem; + Menu.setIsOptionChecked(selectedOutputMenu, true); + AudioDevice.setOutputDevice(selectedDevice); + + } else if (menuItem.endsWith(" for Input")) { + var selectedDevice = menuItem.trimStartsWith("Use ").trimEndsWith(" for Input"); + print("input audio selection..." + selectedDevice); + Menu.setIsOptionChecked(selectedInputMenu, false); + selectedInputMenu = menuItem; + Menu.setIsOptionChecked(selectedInputMenu, true); + AudioDevice.setInputDevice(selectedDevice); + } + } +} + +Menu.menuItemEvent.connect(menuItemEvent);