Author Topic: switching from laptop display w builtin speakers to HDMI video and audio  (Read 823 times)

Offline AnotherUser

  • Full Member
  • ***
  • Posts: 92
Hi,

I just installed KDE MiniMe 2012.08 on my new Dell Inspiron I5R laptop. Using Synaptic, I then updated everything and after a few minor tweaks to video settings, everything works fine. If I go to a website like YouTube and play a video, I can see it on my laptop screen and the audio comes out of the built-in speakers.

Since this laptop has an HDMI output, I wanted to occasionally use that port to drive a TV for both video and audio. The video part seems to be easy enough. Using xranr, I can find out which modes are supported on my TV using:
Code: [Select]
xrandr
Screen 0: minimum 320 x 200, current 1360 x 765, maximum 8192 x 8192
LVDS1 connected 1360x765+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
   1360x765_60    60.0*
   1280x720_60    60.0 
   1024x768       60.0 
   800x600        60.3     56.2 
   640x480        59.9 
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 connected (normal left inverted right x axis y axis)
   1920x1080      60.0 +
   1600x1200      60.0 
   1400x1050      60.0 
   1280x1024      60.0 
   1360x768       60.0 
   1024x768       75.1     70.1     60.0 
   800x600        72.2     75.0     60.3     56.2 
   640x480        72.8     75.0     60.0 
   720x400        70.1 
DP1 disconnected (normal left inverted right x axis y axis)
Then, to output to the TV I can do
Code: [Select]
xrandr --output HDMI1 --mode 1024x768This duplicates my current screen on the TV. Question 1: how do I tell xrandr to only output to the TV and not on the laptop (and vice versa)?

Now for the audio part. I do not have PulseAudio installed, so I'm using ALSA. after turning on the TV output, if I then play something from YouTube, the sound still comes out of the built-in laptop speakers. Question 2: Is there a way to switch the audio output to HDMI (and back to laptop) on the fly? Ideally, I'd like to write a bash script that can then be tied to a shortcut to switch everything from laptop to TV and vice versa.

Any advice is much appreciated!

P.S. Anticipating your next question, here's more info on my sound configuration
Code: [Select]
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, CONEXANT Analog
    Default Audio Device
front:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    Front speakers
surround40:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=PCH,DEV=0
    HDA Intel PCH, CONEXANT Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
hdmi:CARD=PCH,DEV=0
    HDA Intel PCH, HDMI 0
    HDMI Audio Output

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10664
  • MLUs Forever!
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #1 on: December 01, 2012, 03:43:28 PM »
Quote
$ aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, CONEXANT Analog
    Default Audio Device

Does that not imply that PulseAudio is enabled and has the Analog sound as the default?

I dunno as I have no recollection of the output of aplay -L without PA installed.


If PulseAudio is installed and enabled then the package  pavucontrol  gives on the fly switching of the output sink ....  and remembers the setting from last use of the application.

MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline AnotherUser

  • Full Member
  • ***
  • Posts: 92
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #2 on: December 01, 2012, 03:53:49 PM »
Does that not imply that PulseAudio is enabled and has the Analog sound as the default?
I don't know. Looking for help in this forum.

Quote
If PulseAudio is installed and enabled then the package  pavucontrol  gives on the fly switching of the output sink ....  and remembers the setting from last use of the application.
I do not have pav* anywhere on my system. That is what led me to believe that I do not have PulseAudio installed.

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3738
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #3 on: December 01, 2012, 04:18:44 PM »
Search for task-pulseaudio in our synaptic, and install it.
Then go to 'configure your computer' and enable pulseaudio.
See instruction for it http://www.pclinuxos.com/forum/index.php/topic,83740.0.html too...
« Last Edit: December 01, 2012, 04:31:19 PM by daniel »

Offline bicol_willem

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 2378
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #4 on: December 01, 2012, 04:28:01 PM »
Does that not imply that PulseAudio is enabled and has the Analog sound as the default?
I don't know. Looking for help in this forum.

Quote
If PulseAudio is installed and enabled then the package  pavucontrol  gives on the fly switching of the output sink ....  and remembers the setting from last use of the application.
I do not have pav* anywhere on my system. That is what led me to believe that I do not have PulseAudio installed.

Look in synaptic for task-pulse audio and see if it is installed. pavcontrol is a separated package.

Offline AnotherUser

  • Full Member
  • ***
  • Posts: 92
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #5 on: December 01, 2012, 05:11:07 PM »
Ok ... I installed task-pulseaudio and pavucontrol packages. Through the control center, I put a check mark to enable PulseAudio and the restarted as prompted. After the restart, sound came out of my laptop speakers (haven't tested HDMI yet). However, when I run pavucontrol I get a "connection failed: connection refused" error, when I run it both as myself and as root.

My knowledge of PulseAudio is already exceeded. What's the troubleshooting path?

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10664
  • MLUs Forever!
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #6 on: December 01, 2012, 05:55:26 PM »
My suggestions in order

1.      Delete the directory   /home/<your user name>/.pulse ......  note this is a hidden directory
       Log out and log in again and test  pavucontrol  connection by launching it.


If 1. does not work then try

2.    Have a read of this thread
         http://www.pclinuxos.com/forum/index.php/topic,105480.0.html
       There are a couple of suggestions there .....  I might be inclined to try the second one first ... replacing /etc/pulse/default.pa  with
         /etc/pulse/default.pa.rpmnew  IF it exists.

Please post back about the results of those.

« Last Edit: December 01, 2012, 05:57:15 PM by Just17 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline AnotherUser

  • Full Member
  • ***
  • Posts: 92
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #7 on: December 01, 2012, 06:18:52 PM »
1.      Delete the directory   /home/<your user name>/.pulse ......  note this is a hidden directory
       Log out and log in again and test  pavucontrol  connection by launching it.

Deleted, logged out/in, same result.

Quote
2.    Have a read of this thread
         http://www.pclinuxos.com/forum/index.php/topic,105480.0.html
       There are a couple of suggestions there .....  I might be inclined to try the second one first ... replacing /etc/pulse/default.pa  with
         /etc/pulse/default.pa.rpmnew  IF it exists.

I replaced /etc/pulse/default.pa and /etc/sysconfig/pulseaudio with their respective .rpmnew equivalents, deleted ~/.pulse and restarted. Now, pavucontrol works. Thanks!

So now, on to my original question. How do I use PulseAudio to switch my audio output from internal speakers to HDMI?
Also, kmix still automatically starts for me. Is it useful (it has different content now). If not how do I remove it? Is there a PulseAudio equivalent for kmix?

Online agmg

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1914
  • Certified Windows Hater
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #8 on: December 02, 2012, 01:44:41 AM »
Using pulseaudio, you can select the output for the audio stream, while the audio stream is active.
Using KMix for example (click on KMix icon -> Mixer -> Playback streams):



You can see there is an audio stream from mplayer. If you right click, you can select to send this stream to HDMI.

For the video part, check these threads:

http://www.pclinuxos.com/forum/index.php/topic,111155.0.html
http://www.pclinuxos.com/forum/index.php/topic,103010.0.html
For the whole world, you are someone.
For someone, you are the whole world.

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10664
  • MLUs Forever!
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #9 on: December 02, 2012, 02:45:10 AM »
1.      Delete the directory   /home/<your user name>/.pulse ......  note this is a hidden directory
       Log out and log in again and test  pavucontrol  connection by launching it.

Deleted, logged out/in, same result.

Quote
2.    Have a read of this thread
         http://www.pclinuxos.com/forum/index.php/topic,105480.0.html
       There are a couple of suggestions there .....  I might be inclined to try the second one first ... replacing /etc/pulse/default.pa  with
         /etc/pulse/default.pa.rpmnew  IF it exists.

I replaced /etc/pulse/default.pa and /etc/sysconfig/pulseaudio with their respective .rpmnew equivalents, deleted ~/.pulse and restarted. Now, pavucontrol works. Thanks!

So now, on to my original question. How do I use PulseAudio to switch my audio output from internal speakers to HDMI?
Also, kmix still automatically starts for me. Is it useful (it has different content now). If not how do I remove it? Is there a PulseAudio equivalent for kmix?


Thanks for reporting back on the 'fix'.  ;)

****

Unless you find it annoying and useless I would suggest leaving Kmix available in the tray .....  it is handy for changing volume quickly.

***

Using Pulse Audio Volume Control familiarise yourself with its tabs.
For instance on the Configuration tab you can set the audio sink status ...... and can disable it there. Check the options from the drop-down boxes.

On the Playback tab the currently playing applications are listed with the destination of their audio streams and volume.
You can change the audio sink and volume level for each -  individually - and those settings for that application will be remembered the next time it is launched.

This is what you want I believe .....  switching audio sink for a playing stream.

**

In addition to PA Vol Control and Kmix, there is also available Veromix, a plasma widget, which allows you to drag & drop audio streams from one sink to another.

**

HTH

EDIT:
          Depending on your usage, there are a few ways to set up the priority of the audio devices.

For instance in one situation here I have the HDMI as the first audio device - default - but I have it disabled in the config.
PulseAudio will fail back to the next audio device if the first is not available.
When I want to use the HDMI out, I enable it and it gets used for everything.

Another use might be having the HDMI on, but not the default ......  and set some streams (such as the video player) to use it.

You should find, with a little playing about, what suits your situation best.

« Last Edit: December 02, 2012, 02:54:55 AM by Just17 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline AnotherUser

  • Full Member
  • ***
  • Posts: 92
Thanks for everyone's help. I have completed what I wanted to accomplish, which, to recap, was to be able to go between the laptop and HDMI for both video and audio. I wrote a (relatively) simple Perl script to automate and fool-proof the actions and then bound it to a KDE shortcut. Now, with a simple button press I can easily go back and forth.

In case anyone else may find this useful, I am posting my script here. I wrote it relatively generically so that the only thing others may need to tweak is the contents two lines near the top. Your questions and comments are welcome!

Code: [Select]
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

# this script will do the best that it can to switch between using laptop as a
# standalone device and a TV entertainment center. The NAME, WIDTH, and HEIGHT
# fields were found by running 'xrandr'. This script assumes you have PulseAudio
# installed and thus changes between sound profile to one of the choices
# listed by 'pactl list'.
########################
# TUNE THIS SECTION FOR YOUR LAPTOP/TV SETTINGS
my %laptop = (name => 'LVDS1', width => '1360', height => '765_60', soundCard => 0, soundProfile => 'analog-stereo+input:analog-stereo');
my %tv =     (name => 'HDMI1', width => '1360', height => '768',    soundCard => 0, soundProfile => 'hdmi-stereo');
# END OF TUNABLE PARAMETERS
#######################

my %outputs;
my $currentOutput;
my @cmd;
my $error;

my @xrandrOutput = `xrandr`;
if ($? >> 8) {
    $error = "problem execuring 'xrandr'";
}

foreach my $line (@xrandrOutput) {
    ################ if a line is like
    # Screen 0: minimum 320 x 200, current 1360 x 765, maximum 8192 x 8192
    ################
    if ($line =~ /^Screen (\d+):/) {
        unless ($1 == 0) {
            $error = "Cannot handle multi-screen setups";
            last;
        }
        
        # we can skip this because we don't care
        next;
    }

    ########### if a line is like
    # LVDS1 connected 1360x765+0+0 (normal left inverted right x axis y axis) 344mm x 194mm
    # HDMI1 connected (normal left inverted right x axis y axis)
    # VGA1 disconnected (normal left inverted right x axis y axis)
    ###########
    if ($line =~ /^(\S+)\s+(dis)?connected\s+(\d+x\d+)?/) {
        # if it's disconnected, then we don't care about it
        next if defined $2;
        $currentOutput = $1;
        
        # assume it's inactive, unless there is a resolution specified for it
        $outputs{$currentOutput}{active} = 0;
        if (defined $3) {
            $outputs{$currentOutput}{active} = 1;
        }

        # we are done with all we need to extract from here.
        next;
    }

    # If its a line describing the legal modes for the current display, then
    # capture each of them
    ################## if a line is like
    #    1360x765_60    60.0*
    #    1280x720_60    60.0  
    #    1024x768       60.0  
    #    800x600        60.3     56.2  
    #    640x480        59.9  
    ##################
    if ($line =~ /^\s+(\d+)x([_\d]+)/) {
        unless (defined($currentOutput)) {
            $error = "We don't have a current output. That can't be!";
            last;
        }
        push @{$outputs{$currentOutput}{res}}, {width => $1, height => $2};
    }
}

# prepare to log whatever we are about to do
chdir(dirname($0));
open(FIL, ">>$0.log") || die "Cannot open $0.log for writing: $!";

# make sure we can safely proceed
unless (defined($error)) {
    if ( exists($outputs{$tv{name}}) &&      # tv is connected
         !$outputs{$tv{name}}{active}        # tv is not active
    ){
        # turn off laptop and turn on tv
        if (IsValidMode($outputs{$tv{name}}{res}, \%tv)) {
            push @cmd, "xrandr --output $laptop{name} --off --output $tv{name} --mode $tv{width}x$tv{height}";
            push @cmd, "pactl set-card-profile $tv{soundCard} output:$tv{soundProfile}";
        } else {
            $error = "Desired tv mode is not a valid choice";
        }
    }
    elsif ( exists($outputs{$tv{name}}) &&     # tv is connected
            $outputs{$tv{name}}{active}        # tv is active
    ){
        # turn off tv and turn on laptop
        if (IsValidMode($outputs{$laptop{name}}{res}, \%laptop)) {
            push @cmd, "xrandr --output $laptop{name} --mode $laptop{width}x$laptop{height} --output $tv{name} --off";
            push @cmd, "pactl set-card-profile $laptop{soundCard} output:$laptop{soundProfile}";
        } else {
            $error = "Desired laptop mode1 is not a valid choice";
        }
    }
    else
    {
        # default to a safe state of display only to laptop
        if (IsValidMode($outputs{$laptop{name}}{res}, \%laptop)) {
            push @cmd, "xrandr --output $laptop{name} --mode $laptop{width}x$laptop{height}";
            push @cmd, "pactl set-card-profile $laptop{soundCard} output:$laptop{soundProfile}";
        } else {
            $error = "Desired laptop mode2 is not a valid choice";
        }
    }
}

# make sure we can proceed, or else log our error
if (defined($error)) {
    print FIL localtime() . ": ERROR: $error\n";
    close(FIL);
    exit;
}

# finally, we can do something useful
foreach my $cmd (@cmd) {
    print FIL localtime() . ": $cmd\n";
    `$cmd`;
    if ($? >> 8) {
        print FIL localtime() . ": ERROR: problem running '$cmd'\n";
    }
}

# finished
close(FIL);
exit;

sub IsValidMode {
    my $rRes = shift;
    my $rTarget = shift;

    foreach (@{$rRes}) {
        if ($$_{width} eq $$rTarget{width} && $$_{height} eq $$rTarget{height}) {
            return 1;
        }
    }
    return 0;
}

Online agmg

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1914
  • Certified Windows Hater
Re: switching from laptop display w builtin speakers to HDMI video and audio
« Reply #11 on: December 05, 2012, 02:13:59 AM »
Thank you for sharing this script with us. I will definitely check it out. :)
Although I prefer to use PulseAudio which gives better control over audio streams...
For the whole world, you are someone.
For someone, you are the whole world.