emptybee
Posts: 23
Joined: Wed Jun 20, 2012 12:56 pm

Switching Audio Output with a Button

Thu May 12, 2016 8:50 am

I am building a SNES in a box with a 10" HDMI screen. Unfortunately the screen has no audio so I have set the default audio to the 3.5mm jack using raspi-config and have a small speaker connected.

I also have an HDMI splitter that goes to a female HDMI port in the side of the box in case I want to plug the system into TV. In this instance I want to disable the 3.5mm jack and push audio through HDMI. Unfortunately, as both screens run of HDMI, auto switching won't work. Therefore I am including a momentary push button to allow the audio output to be switched easily. I know I could do this in Retropie menu but I plan on removing the menu item once everything is setup correctly so friends to mess it up. Also a button will be quicker and easier than going through the menu system.

Will the below work? I've read that os.system isn't the preferred command anymore so should I change it and if so what should the python script below (that will be added to rc.local) be altered to?

Edit: Below script updated and works for me. If you have any issues it will be due to the spacing of lines. Default audio is set to the speaker and if I plug in the hdmi I simply press the momentary button switch to swap to HDMI out.

Thanks!

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
    input_state = GPIO.input(18)
    if input_state == False and set = 0:
        os.system("amixer -c 0 cset numid=3 1") 
        time.sleep(1)
        set =1
    input_state = GPIO.input(18)
    if input_state == False and set = 1:
         os.system("amixer -c 0 cset numid=3 2")
         time.sleep(1)
         set =0

emptybee
Posts: 23
Joined: Wed Jun 20, 2012 12:56 pm

Re: Switching Audio Output with a Button

Fri May 20, 2016 1:54 pm

I had to add the following to config.txt to prevent the audio being awful (white noise):

Code: Select all

disable_audio_dither=1
Unfortunately my HDMI splitter caused prevented an image appearing on the hdmi cctv screen I've fit in the box so I have had to remove the splitter for the time being until I figure out what is going wrong. Before I pulled it I tried boosting the HDMI signal with no success. This now makes the whole audio out switch a bit redundant until I can fix this.

john.t
Posts: 28
Joined: Fri Sep 23, 2016 10:56 am

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 12:55 pm

Hello,

thank you for posting this code. i tried it and it works.... but not 100%..if i push on the switch it switches from hdmi to analog output but when i push it again sometimes it switches to hdmi and sometime back to analog. ex . : button pushed1 = hdmi , push2=rca , push3=rca,push4=rca,push5=hdmi,push6=hdmi,push7=rca..etc..

User avatar
rpdom
Posts: 15577
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 1:05 pm

You'll need to add some code on the end to wait for the button to be released, otherwise it'll keep flipping between the two inputs for as long as the button is held down.

Let's see, I'll fire up my rusty Python brain...

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
    if GPIO.input(18) == False:
        if set = 0:
            os.system("amixer -c 0 cset numid=3 1")
            set =1
        if set = 1:
            os.system("amixer -c 0 cset numid=3 2")
            set =0
	while GPIO.input(18) == False:
	    sleep 0.1
    sleep 0.1
I think that's how I'd do it, but I don't use Python much.

john.t
Posts: 28
Joined: Fri Sep 23, 2016 10:56 am

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 6:43 pm

hi rpdom

i tried your code but it does exact the same thing as before. :(

john.t
Posts: 28
Joined: Fri Sep 23, 2016 10:56 am

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 6:48 pm

and i always get an error at False and set = 0 or 1. i have to make it False and start == 0 or 1

User avatar
rpdom
Posts: 15577
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 7:07 pm

Yes, you will have to use ==. not =.

= on it's own is an assignment. "set = 0" will make the variable called "set" equal to 0.
== is a conditional test. It returns True if the value of "set" is 0.

john.t
Posts: 28
Joined: Fri Sep 23, 2016 10:56 am

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 7:35 pm

Do you have any idea what's wrong with the code? if i push the button it sends a signal set = 1 and directly after set = 0 everytime

User avatar
rpdom
Posts: 15577
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 7:55 pm

I just looked at my code and saw the problem straight away.

If set is 0, the first action is triggered, and then changes set to 1

Then the second test checks to see if set is 1... well, it is because we just set it to 1 in the previous test. You need to make it so it runs one or the other, not both.

My code updated

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
    if GPIO.input(18) == False:
        if set == 0:
            os.system("amixer -c 0 cset numid=3 1")
            set = 1
        elif set == 1:
            os.system("amixer -c 0 cset numid=3 2")
            set = 0
   while GPIO.input(18) == False:
       sleep 0.1
    sleep 0.1
I think that will work.

john.t
Posts: 28
Joined: Fri Sep 23, 2016 10:56 am

Re: Switching Audio Output with a Button

Sun Mar 12, 2017 8:35 pm

Yes!!
it works :)!!! thank you very much.you have been much helpful..

Return to “Python”