[HOW-TO] Easily switch audio outputs, settings, etc.
#31
(2014-10-08, 10:39)nickr Wrote: @teeedubb if people looked at logs before posting, 90% of the posts here would not be needed LOL.

Sorry about that guys...

Considering that I'm definitively new to python, I think that you at least appreciated my effort to try to solve the issue by myself...sometimes people just ask for questions and lazy wait for someone else to answer them...

Anyway, the trik to solve the problem was in fact to take a look to the log... Smile I know that the "spaces/tabs/indentations" issue it is quite obviuous for you, but was not for me... Wink
Light IMDb Ratings Update - Keep updated the IMDb ratings for your movies and TV shows.
In case you found useful my work, feel free to offer me a cappuccino!
Reply
#32
You have done well. Good job.
If I have helped you or increased your knowledge, click the 'thumbs up' button to give thanks :) (People with less than 20 posts won't see the "thumbs up" button.)
Reply
#33
@tee3dubb - Since updating to kodi, this script has stopped working. I did change the name of the user running kodi from 'xbmc' to 'kodi' but I replaced all references to that users homedir and updated the permissions. Has anything changed?
Code:
# credit to teeedubb
# http://forum.xbmc.org/showthread.php?tid=199579

import xbmc
import os

tempdir = xbmc.translatePath('special://temp/')
tempfile0 = os.path.join(tempdir, 'audiooutput0')

print("CURRENT AUDIO DEVICE:")
print(xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.GetSettingValue", "params":{"setting":"audiooutput.audiodevice"},"id":1}'))

if not os.path.isfile(tempfile0):
    # pulseaudio analog out
        xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice", "value":"alsa_output.pci-0000_00_1b.0.analog-stereo"}, "id":1}')
        xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"Soundbar", "image":"/var/lib/kodi/glossy-speaker-icon.png"}, "id":1}')
        file = open(tempfile0, "a")
        file.close()
else:
    # pulseaudio HDMI out
        xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice", "value":"alsa_output.pci-0000_00_03.0.hdmi-stereo-extra1"}, "id":1}')
        xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"TV Speakers", "image":"/var/lib/kodi/hdtv.png"}, "id":1}')
        os.remove(tempfile0)
Need help programming a Streamzap remote?
Reply
#34
Its not necessary to change xbmc to kodi as nothing has changed in this regard with Helix, the same script works with both versions. Did the log file show any clues?
Reply
#35
@teee- When I change the sources manually in the GUI the resulting log shows:
Code:
10:08:42 T:140682193147648  NOTICE: PulseAudio: Opened device alsa_output.pci-0000_00_03.0.hdmi-stereo-extra1 in pcm mode with Buffersize 150 ms
10:09:15 T:140682193147648  NOTICE: PulseAudio: Opened device alsa_output.pci-0000_00_1b.0.analog-stereo in pcm mode with Buffersize 150 ms

So I updated the lines in the python script accordingly (edited my post above). Pressing the key I had mapped to this action leaves no entries in the log at all and all other buttons on the remote work as expected.

Code:
% grep audio_switch /var/lib/kodi/.kodi/userdata/keymaps/remote.xml
      <display>RunScript("/var/lib/kodi/bin/audio_switch.py")</display>

EDIT: Nevermind... it's working now. Not sure what I did to fix it!
Need help programming a Streamzap remote?
Reply
#36
My setup for switching the audio between hdmi and analog with ctrl+a shortcut:

/home/xbmc/.kodi/userdata/keymaps/my.xml
Code:
<keymap>
  <global>
    <keyboard>
      <a mod="ctrl">RunScript(/home/xbmc/bin/kodi/switch_audio.py)</a>
    </keyboard>
  </global>
</keymap>

/home/xbmc/bin/kodi/switch_audio.py
Code:
import json
import xbmc

jsonGetAudioDevice = '{"jsonrpc":"2.0","method":"Settings.GetSettingValue", "params":{"setting":"audiooutput.audiodevice"},"id":1}'
jsonSetAudioDevice = '{"jsonrpc":"2.0","method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice","value":"%s"},"id":1}'
jsonNotify = '{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"Audio Output","message":"%s","image":"info"},"id":1}'
audioDeviceHDMI = 'ALSA:hdmi:CARD=NVidia,DEV=0'
audioDeviceAnalog = 'ALSA:@'

# get current audio device
audioDeviceCurrent = json.loads(xbmc.executeJSONRPC(jsonGetAudioDevice))['result']['value']

if audioDeviceCurrent == audioDeviceAnalog:
        # set to hdmi
        xbmc.executeJSONRPC(jsonSetAudioDevice % audioDeviceHDMI)
        xbmc.executeJSONRPC(jsonNotify % "TV")
elif audioDeviceCurrent == audioDeviceHDMI:
        # set to analog
        xbmc.executeJSONRPC(jsonSetAudioDevice % audioDeviceAnalog)
        xbmc.executeJSONRPC(jsonNotify % "Veza")
Reply
#37
Hi I've made an addon to switch between 2 audio devices. You can select the 2 audio devices from a list.

Tested on Kodi 14.1 on Linux. Not tested on other operating systems.

This addon uses the json methods that are described in the previous posts.

https://github.com/Helly1206/script.audioswitch
Reply
#38
Is it possible to set volume amplification (in player's audio dialog) with this script? I'd like to map a button to toggle between 0 and 20dB values but I have idea what command to use etc.
This setting:
http://kodi.wiki/view/Video_playback#OSD...e_settings
Reply
#39
'{"jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume":10}, "id": 1}'

Change the value after "volume": to suit
Reply
#40
Does that change the amplification setting (second bar) and not the volume? I noticed this thread that seems to indicate that setting amplification is not possible but maybe it has old info: http://forum.kodi.tv/showthread.php?tid=200129
Reply
#41
Yeah that is only for volume. Not sure if its possible to change amplification via json - I cant find anything in the json-rpc v6 wiki page regarding amplification. If the link you posted is still valid you can use something like whats in this post to stop kodi, change the entry in guisettings.xml and restart kodi:

http://forum.kodi.tv/showthread.php?tid=...pid1468716
Reply
#42
Restart is not very usable in this case. I usually start to watch something late at night and want to reduce dynamic range with quick toggle. I'll check if this is possible to do with other scripts or as a last resort with remote control macro.
Reply
#43
I managed to make a script that does what I want: it sets the volume amplification (dynamic range compression) to 15dB and second press sets it back to 0dB. There is also an on-screen display of the setting. I'm not sure if this is the best way to do it (probably not), but in my system it works fine. It's fast, you don't see any menus or cursor movement, just that OSD notice. I used your script as a base to make the toggle.
Here's the script:

Code:
#audio DRC 15dB toggle script for Kodi
import xbmc
import os

tempdir = xbmc.translatePath('special://temp/')
tempfile0 = os.path.join(tempdir, 'audiooutput0')


if not os.path.isfile(tempfile0):

#settings group 0
#edit below here
    xbmc.executebuiltin("ActivateWindow(osdaudiosettings)")
    xbmc.executebuiltin("SetFocus(-79)")

#each line adds 1dB i.e. 15dB
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Action(Right)")
    xbmc.executebuiltin("Dialog.Close(osdaudiosettings)")
    xbmc.executebuiltin("Notification(DRC:,15dB,2000)")
#edit above here
    file = open(tempfile0, "a")
    file.close()

else:

#settings group 1
#edit below here
    xbmc.executebuiltin("ActivateWindow(osdaudiosettings)")
    xbmc.executebuiltin("SetFocus(-79)")

#each line removes 1dB - use same amount as above in add
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Action(Left)")
    xbmc.executebuiltin("Dialog.Close(osdaudiosettings)")
    xbmc.executebuiltin("Notification(DRC:,0dB,2000)")
#edit above here
    os.remove(tempfile0)
Reply
#44
(2015-02-07, 16:45)Helly1206 Wrote: Hi I've made an addon to switch between 2 audio devices. You can select the 2 audio devices from a list.

Tested on Kodi 14.1 on Linux. Not tested on other operating systems.

This addon uses the json methods that are described in the previous posts.

https://github.com/Helly1206/script.audioswitch

I tried this on my raspberry pi with openelec. I only get "Option 1 is selected" (something like that) while I have both the onboard HDMI and analog connected. Is this how it should work?
Reply
#45
So this works for me, however I'm getting the following error:

13:46:20 T:5512 NOTICE: CURRENT AUDIO DEVICE:
13:46:20 T:5512 NOTICE: {"id":1,"jsonrpc":"2.0","result":{"value":"DIRECTSOUND:{22CF9947-3D93-41FC-8505-26F800E3AD96}"}}
13:46:20 T:5512 ERROR: JSONRPC: Failed to parse '{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"DS", "image":"C:\garden_on.png"}, "id":1}'

As a result, no notification is shown.

The relevant section of the python code is:

xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"WASAPI", "image":"C:\garden_on.png"}, "id":1}')

Anyone have any ideas please?
Kodi 16.1 on main HTPC Win 7 64-bit, 8 GB RAM, Quad Core 2.4 Ghz
3 x Pi2 running Kodi 16.1 (OSMC)
TVHeadend PVR server providing Freeview HD and Freesat HD
Reply



Logout Mark Read Team Forum Stats Members Help
[HOW-TO] Easily switch audio outputs, settings, etc.3
This forum uses Lukasz Tkacz MyBB addons.