It is currently April 20th, 2024, 2:55 am

Problem with Win7Audio Plugin

Share and get help with Plugins and Addons
Bishop149
Posts: 25
Joined: March 23rd, 2016, 3:07 pm

Problem with Win7Audio Plugin

Post by Bishop149 »

I am trying to make a button that will turn on my secondary monitor (which is my TV).
I have pretty much managed it save one final detail, namely getting it to switch the audio output to the TV as well.

This is my code

Code: Select all

[MeasureWin7Audio]
Measure=Plugin
Plugin=Win7AudioPlugin

[measureDisplay]
Measure=Calc
Formula= #switch#
DynamicVariables=1
UpdateDivider=-1 
IfCondition=measureDisplay = 0
IfCondition2=measureDisplay = 1
IfTrueAction=[C:\Windows\System32\DisplaySwitch.exe /external][!CommandMeasure "MeasureWin7Audio" "SetOutPutIndex 1"][!SetOption MeasureLoop EndValue 10][!SetOption MeasureLoop Increment 2][!SetOption MeasureLoop InvertMeasure 1][!UpdateMeasure "MeasureLoop"][!CommandMeasure MeasureLoop "Reset"]
IfTrueAction2=[C:\Windows\System32\DisplaySwitch.exe /extend][!CommandMeasure "MeasureWin7Audio" "SetOutPutIndex 7"][!SetOption MeasureLoop EndValue 10][!SetOption MeasureLoop Increment 2][!UpdateMeasure "MeasureLoop"][!SetOption MeasureLoop InvertMeasure 0][!CommandMeasure MeasureLoop "Reset"]
The relevant sections being
[!CommandMeasure "MeasureWin7Audio" "SetOutPutIndex 1"] for the first IfTrueAction
[!CommandMeasure "MeasureWin7Audio" "SetOutPutIndex 7"] for the second
The other stuff relates to it playing an animation and actually switching the screen over.

Here is a picture of my Audio Devices window:
Image

As you can see the 1st item is my computer speakers and the 7th is SAMSUNG-4 which is my TV.
In the picture the SAMSUNG-4 is "Not Plugged in" but when I activate the 2nd display it becomes "Ready" and the audio switches to item 3 which is "Digital Audio (S/PDIF)" (I must admit I dunno what that is!) NOT item 7 as requested.

I originally thought that this might be happening because when the switch occurred in rainmeter the screen was still not active, so I programmed a 5-10 second delay on the Win7Audio switch to give the screen time to register as connected. Which made no difference.
In fact if I "SetOutPutIndex" to anything greater than 1 it ALWAYS selects Item 3, "Digital Audio (S/PDIF)".
So as a test I disabled this device. It then wouldn't switch to anything, remaining on the speakers . . . . even after I re-enabled "Digital Audio (S/PDIF)".

I get the feeling that Win7Audio populates its list of devices when it first starts up and from then on does not ever add new ones (although it does appear to take them away when disabled!), so unless the screen is on when windows starts (which I definitely don't want) it will never work.
Is this correct?

If so is there any other way of getting rainmeter to switch the default audio device?
I saw something about doing it using AudioLevels? I went with the Win7Audio option as it seemed easier and more fit for purpose! :confused:
Bishop149
Posts: 25
Joined: March 23rd, 2016, 3:07 pm

Re: Problem with Win7Audio Plugin

Post by Bishop149 »

The plot thickens

I made a quick little skin to test the Win7Audio Plugin pretty much just using the example code from here:
https://docs.rainmeter.net/manual/plugins/win7audio/

I then enabled the second screen "SAMSUNG-4" so that all three devices were "Ready".
Using the "ToggleNext" command simply switched between devices 1 and 3 (Speakers and Digital Audio (S/PDIF))
SAMSUNG-4 is totally ignored. I then manually set the default audio device to SAMSUNG-4 and tried the toggle again, this time it stays resolutely on SAMSUNG-4 and does not switch to the other two.

Then I tried refreshing the skin and lo and behold it started working.

Basically WIn7Audio must take an inventory of attached audio devices when the skin is loaded which is not ever updated until the skin is refreshed. So new audio devices will not work until such a refresh is performed.
I wondered if t was just the Win7Audio Measure that needed refreshing and so tried adding [!UpdateMeasure "MeasureWin7Audio"] to the toggle button. No Joy it seems the WHOLE skin must be refreshed.

This is going to be a royal pain in the butt. I can't refresh the whole skin every time it switches, it will reset all the variables to their default states, which are all centered around second screen "off".
I'll try and think of a way round it but it isn't going to be easy. . . . . maybe separate out each audio state into a separate skin and use !ActivateConfig / !DeactivateConfig bangs to turn the whole skin on/off?
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Problem with Win7Audio Plugin

Post by jsmorley »

Did you try adding DynamicVariables=1 to the measure?
Bishop149
Posts: 25
Joined: March 23rd, 2016, 3:07 pm

Re: Problem with Win7Audio Plugin

Post by Bishop149 »

Hmmm good idea.

I added it to both the Win7Audio Measure and the Button meter doing the switching.
Nope doesn't fix it.

I'm going to explore the idea of making a couple of invisible skins solely to do each audio switch over and then load / unload each one as required.
Bishop149
Posts: 25
Joined: March 23rd, 2016, 3:07 pm

Re: Problem with Win7Audio Plugin

Post by Bishop149 »

Bishop149 wrote:I'm going to explore the idea of making a couple of invisible skins solely to do each audio switch over and then load / unload each one as required.
Yeah this approach worked.
Some code for reference:

This is the code on the skin containing the button that switches.

Code: Select all

;This is the button that switches, there was other stuff on the IFTrueActions but I trimmed it to the relevant

Measure=Calc
Formula= #switch#
DynamicVariables=1
UpdateDivider=-1 
IfCondition=measureDisplay = 0
IfCondition2=measureDisplay = 1
IfTrueAction=[C:\Windows\System32\DisplaySwitch.exe /external][!ActivateConfig "Bishop149\AudioSwitch" "Speakers.ini"]
IfTrueAction2=[C:\Windows\System32\DisplaySwitch.exe /extend][!EnableMeasure cCounter]

;This counter implements a delay to allow the newly activated device to become active. Length defined by variable #delay#, a few seconds is enough

[cCounter]
Measure = Calc
Formula = cCounter+1
Disabled=1
IfAboveValue = #delay#
IfAboveAction = [!DisableMeasure #CURRENTSECTION#][!ActivateConfig "Bishop149\AudioSwitch" "Samsung4.ini"]
"Speakers.ini" and "Samsung4.ini" are two almost indentical variants of the same config "AudioSwitch", I found this quite handy because only one varient can be active at a time you don't have to worry about turning off the unwanted one.
Below is the code for one of them.

Code: Select all

[Variables]
a=2

[MeasureWin7Audio]
Measure=Plugin
Plugin=Win7AudioPlugin

;Important bit here, just match SetOutputIndex to whichever device you want to switch to!
;I dunno why I had to use a variable, just setting Formula = 2 didn't seem to work?!

[measureDoSwitch]
Measure=Calc
Formula=#a#
IfAboveValue=1
IfAboveAction=[!CommandMeasure "MeasureWin7Audio" "SetOutPutIndex 1"]
UpdateDivider=-1

;Rainmeter doesn't allow you to not have any meters so below is a blank one

[MeterDefault]
Meter=String
FontSize=16
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
Text=
Hope that might help anyone who hits the same issue!