Skip to content

Commit

Permalink
Allow switching volume levels
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgit2k committed Oct 10, 2020
1 parent 1c560e4 commit be0c7c4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 44 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# AudioSwitcher

Quickly switch active audio devices for example from webcam & speakers to headset and back
Quickly switch active audio devices for example from webcam & speakers to headset and back. \
Change output volume levels depending on active audio device.

![Screenshot](screenshot.png)

Expand All @@ -10,6 +11,8 @@ The correct device-IDs have to be configured in audioswitcher.ini.
For the device-IDs start SoundVolumeView in the subdirectory SoundVolumeView, sort entries after column "Type" and go to the entries with "Type" = device,
click on the right mouse button, select "Properties" and then use the entry for "Command-Line Friendly ID".

Volume levels, texts, font and fontsizes can also be configured in audioswitcher.ini.

## Download
Windows: ![audioswitcher.zip](https://github.com/alexgit2k/audioswitcher/releases/latest/download/audioswitcher.zip)

Expand Down
24 changes: 15 additions & 9 deletions audioswitcher-window.pbf
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@

Global WindowMain

Global ButtonDevices1, ButtonDevices2


Procedure OpenWindowMain(x = 0, y = 0, width = 250, height = 150)
WindowMain = OpenWindow(#PB_Any, x, y, width, height, "AudioSwitcher", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
;ButtonDevices1 = ButtonImageGadget(#PB_Any, 10, 10, 230, 60, 0, #PB_Button_Toggle)
;ButtonDevices2 = ButtonImageGadget(#PB_Any, 10, 80, 230, 60, 0, #PB_Button_Toggle)
ButtonDevices1 = BevelButton::New(10, 10, 230, 60, 0, "Webcam", font, #PB_Button_Toggle)
ButtonDevices2 = BevelButton::New(10, 80, 230, 60, 0, "Headset", font, #PB_Button_Toggle)
Global ButtonDevices1, ButtonDevices2, HeadLineDevices, ButtonVolumes1, ButtonVolumes2, HeadlineVolumes


Procedure OpenWindowMain(x = 0, y = 0, width = 400, height = 180)
WindowMain = OpenWindow(#PB_Any, x, y, width, height, "AudioSwitcher", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_Invisible)
;ButtonDevices1 = ButtonImageGadget(#PB_Any, 10, 40, 230, 60, 0, #PB_Button_Toggle)
;ButtonDevices2 = ButtonImageGadget(#PB_Any, 10, 110, 230, 60, 0, #PB_Button_Toggle)
ButtonDevices1 = BevelButton::New(10, 40, 230, 60, 0, "Webcam", font, #PB_Button_Toggle)
ButtonDevices2 = BevelButton::New(10, 110, 230, 60, 0, "Headset", font, #PB_Button_Toggle)
HeadlineDevices = TextGadget(#PB_Any, 10, 10, 230, 25, "Devices", #PB_Text_Center)
;ButtonVolumes1 = ButtonImageGadget(#PB_Any, 250, 40, 140, 60, 0, #PB_Button_Toggle)
;ButtonVolumes2 = ButtonImageGadget(#PB_Any, 250, 110, 140, 60, 0, #PB_Button_Toggle)
ButtonVolumes1 = BevelButton::New(250, 40, 140, 60, 0, "15", font, #PB_Button_Toggle)
ButtonVolumes2 = BevelButton::New(250, 110, 140, 60, 0, "5", font, #PB_Button_Toggle)
HeadlineVolumes = TextGadget(#PB_Any, 250, 10, 140, 25, "Volumes", #PB_Text_Center)
EndProcedure

Procedure WindowMain_Events(event)
Expand Down
14 changes: 13 additions & 1 deletion audioswitcher.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
[base]
font = Segoe UI
fontSize = 19
last = devices1
fontSizeHeadline = 12
lastDevice = devices1
lastVolume = volumes1
devicesHeadline = Devices
volumesHeadline = Volumes

[devices1]
devices1Text = Webcam
devices1Speaker = Logitech Speaker\Device\Speaker\Render
devices1Mic = Logitech Webcam\Device\Microphone\Capture
devices1volumes1Text = Normal
devices1volumes1Value = 20
devices1volumes2Text = Low
devices1volumes2Value = 10

[devices2]
devices2Text = Headset
devices2Speaker = Realtek High Definition Audio\Device\Headset\Render
devices2Mic = Realtek High Definition Audio\Device\Headset Microphone\Capture
devices2volumes1Text = Normal1
devices2volumes1Value = 30
devices2volumes2Text = Low1
devices2volumes2Value = 15
102 changes: 69 additions & 33 deletions audioswitcher.pb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Global NewMap Config$()

; Procedures
Declare ReadPreference(Map PrefMap$())
Declare SoundDevice(Device$, Type = -1, Wait = #False)
Declare WritePreference(Device$)
Declare SoundDevice(Command$, Device$, Param$ = "", Wait = #False)
Declare WritePreference(Key$, Device$)

; Config
ReadPreference(Config$())
Expand All @@ -16,88 +16,124 @@ ReadPreference(Config$())
XIncludeFile "bevelbutton.pb"
XIncludeFile "audioswitcher-window.pbf"

; Font
; Fonts
gadgetFont = LoadFont(#PB_Any, Config$("font"), Val(Config$("fontSize")), #PB_Font_HighQuality)
font = FontID(gadgetFont)
gadgetFont = LoadFont(#PB_Any, Config$("font"), Val(Config$("fontSizeHeadline")), #PB_Font_HighQuality)
fontHeadline = FontID(gadgetFont)

; Window
OpenWindowMain()
; Set text
SetGadgetFont(HeadlineDevices, fontHeadline)
SetGadgetText(HeadlineDevices, Config$("devicesHeadline"))
BevelButton::SetText(ButtonDevices1, Config$("devices1Text"), font)
BevelButton::SetText(ButtonDevices2, Config$("devices2Text"), font)
SetGadgetFont(HeadlineVolumes, fontHeadline)
SetGadgetText(HeadlineVolumes, Config$("volumesHeadline"))
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"), font)
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"), font)

; Set current state
Select Config$("last")
Select Config$("lastDevice")
Case "devices1"
BevelButton::Enable(ButtonDevices1)
BevelButton::Disable(ButtonDevices2)
Case "devices2"
BevelButton::Enable(ButtonDevices2)
BevelButton::Disable(ButtonDevices1)
EndSelect
Select Config$("lastVolume")
Case "volumes1"
BevelButton::Enable(ButtonVolumes1)
BevelButton::Disable(ButtonVolumes2)
Case "volumes2"
BevelButton::Enable(ButtonVolumes2)
BevelButton::Disable(ButtonVolumes1)
EndSelect

; ----------------------------------------------------------------------------------------------------------------------------------

; Loop
HideWindow(WindowMain, #False)
Repeat
Event = WaitWindowEvent()

Select Event
Case #PB_Event_Gadget

; Toogle Buttons
LockWindowUpdate_(WindowID(WindowMain))
Select EventGadget()
Case BevelButton::GetGadgetID(ButtonDevices1)

; Toggle Buttons
Config$("lastDevice") = "devices1"
BevelButton::Enable(ButtonDevices1)
BevelButton::Disable(ButtonDevices2)

; Switch Audio
SoundDevice(Config$("devices1Mic"))
SoundDevice(Config$("devices1Speaker"))
SoundDevice(Config$("devices1Mic"), 2)
SoundDevice(Config$("devices1Speaker"), 2, True)

Config$("last") = "devices1"
; Set Volume-Buttons
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"))
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"))
Case BevelButton::GetGadgetID(ButtonDevices2)

; Toggle Buttons
Config$("lastDevice") = "devices2"
BevelButton::Enable(ButtonDevices2)
BevelButton::Disable(ButtonDevices1)

; Set Volume-Buttons
BevelButton::SetText(ButtonVolumes1, Config$(Config$("lastDevice") + "volumes1Text"))
BevelButton::SetText(ButtonVolumes2, Config$(Config$("lastDevice") + "volumes2Text"))
Case BevelButton::GetGadgetID(ButtonVolumes1)
Config$("lastVolume") = "volumes1"
BevelButton::Enable(ButtonVolumes1)
BevelButton::Disable(ButtonVolumes2)
Case BevelButton::GetGadgetID(ButtonVolumes2)
Config$("lastVolume") = "volumes2"
BevelButton::Enable(ButtonVolumes2)
BevelButton::Disable(ButtonVolumes1)
EndSelect
LockWindowUpdate_(0)
UpdateWindow_(WindowID(WindowMain))

; Set Device/Volume
Select EventGadget()
Case BevelButton::GetGadgetID(ButtonDevices1), BevelButton::GetGadgetID(ButtonDevices2)
; Switch Audio
SoundDevice(Config$("devices2Mic"))
SoundDevice(Config$("devices2Speaker"))
SoundDevice(Config$("devices2Mic"), 2)
SoundDevice(Config$("devices2Speaker"), 2, True)

Config$("last") = "devices2"
SoundDevice("SetDefault", Config$(Config$("lastDevice") + "Mic"))
SoundDevice("SetDefault", Config$(Config$("lastDevice") + "Speaker"))
SoundDevice("SetDefault", Config$(Config$("lastDevice") + "Mic"), "2")
SoundDevice("SetDefault", Config$(Config$("lastDevice") + "Speaker"), "2")

; Set Volume & wait
SoundDevice("SetVolume", Config$(Config$("lastDevice") + "Speaker"), Config$(Config$("lastDevice") + Config$("lastVolume") + "Value"), True)
Case BevelButton::GetGadgetID(ButtonVolumes1), BevelButton::GetGadgetID(ButtonVolumes2)
; Set Volume
SoundDevice("SetVolume", Config$(Config$("lastDevice") + "Speaker"), Config$(Config$("lastDevice") + Config$("lastVolume") + "Value"), True)
EndSelect

EndSelect
Until Event = #PB_Event_CloseWindow
HideWindow(WindowMain, #True)

; Write last state
WritePreference(Config$("last"))
WritePreference("lastDevice", Config$("lastDevice"))
WritePreference("lastVolume", Config$("lastVolume"))

; Free Memory
BevelButton::Delete(ButtonDevices2)
BevelButton::Delete(ButtonDevices1)
BevelButton::Delete(ButtonVolumes2)
BevelButton::Delete(ButtonVolumes1)

End

; ----------------------------------------------------------------------------------------------------------------------------------

Procedure SoundDevice(Device$, Type = -1, Wait = #False)
param$ = "/SetDefault " + Chr(34) + Device$ + Chr(34)
; Add type if available
If Type <> -1
param$ = param$ + " " + Str(Type)
EndIf
Procedure SoundDevice(Command$, Device$, Param$ = "", Wait = #False)
allParams$ = "/" + Command$ + " " + Chr(34) + Device$ + Chr(34) + " " + Param$
; Flags
Flags = 0
If Wait = True
Flags = #PB_Program_Wait
EndIf
; Run SoundVolumeView
RunProgram("SoundVolumeView/SoundVolumeView", param$, "", Flags)
RunProgram("SoundVolumeView/SoundVolumeView", allParams$, "", Flags)
EndProcedure

Procedure ReadPreference(Map PrefMap$())
Expand All @@ -122,7 +158,7 @@ Procedure ReadPreference(Map PrefMap$())
ClosePreferences()
EndProcedure

Procedure WritePreference(Device$)
Procedure WritePreference(Key$, Device$)
; Open Preferences
FileIni.s = GetFilePart(ProgramFilename(),#PB_FileSystem_NoExtension) + ".ini"
If (OpenPreferences(FileIni) = 0)
Expand All @@ -132,7 +168,7 @@ Procedure WritePreference(Device$)

; Write
PreferenceGroup("base")
WritePreferenceString("last", Device$)
WritePreferenceString(Key$, Device$)

; Close
ClosePreferences()
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit be0c7c4

Please sign in to comment.