Friday, October 7, 2011

Enabling HDMI audio-out in Ubuntu 10.04 on GeForce 520GT

Happy Thanksgiving weekend everyone!

Not that long ago, I had a sad day where I booted up my desktop and, despite the whirling of the fan and hard drive, nothing appeared on the screen! Oh noes!

Quickly fearing for my monitor, I plugged in my laptop and was promptly greeting with the laptop's bios booting on the monitor.

No problem with the monitor, then. What else? When turning on the computer, and waiting a few moments, I could clearly hear the Ubuntu start-up noise though the speakers despite having no video.

That would indicate processor, ram, and hard drive were all fine. Only guess remaining was a shot-out video card (or perhaps the PCI slot on the MB). Quite sad really, considering the card was less then a year old.

It also wasn't an easy card to get. Having yet to jump on the High Definition TV band wagon, I was still chucking around with my old, 27 inch, 480i, CRT TV. The only input it had was good old fashioned RCA cables. So, when I went video card shopping, I specifically looked for one with S-video out, having years ago purchased an S-video to RCA adapter for just these occasions.

Alas, I found one! Most cards now-a-days lack S-video, going for the HDMI or DVI connectors instead. But I found one at a local store nonetheless (no having to order on line). Furthermore, it was in fact a 7-pin, S-video cable, meaning that I could use it for not only standard definition S-video, but also for (potentially) high definition component video should I ever decide to upgrade my TV (a 7-pin S-video to component adapter was included with the card).

But I never got the chance to try it. I took my tower down to where I bought the video card and explained the situation. The staff was pretty friendly, they took my tower and let me know what they found. I figured it was either the Video Card (or possibly the MB), so either way I'd probably be getting some new hardware. The called me a short while later.

The video card was toast. Unfortunately, they couldn't tell me much more then that, I figure all they did was either try another card in my tower, or my card in another tower. Good news is, it was still under warranty, so they replaced it for free. Bad news: no more S-video cards.

So I ended up with a new card, a nVidia GeForce 520GX. It had D-Sub, DVI and HDMI outputs. I plugged in the DVI, but alas Ubuntu would only boot in Low-graphics mode. So I went to nVidia's site and downloaded the binary driver for Ubuntu and installed it (a bit of Googling revealed the 520Gx wasn't supported by 10.04 open source driver). My DVI port then worked fine, and I was greeted with the full 1920x1080 Ubuntu desktop on my monitor.

Since my monitor also has HDMI, I tried the HDMI output. It worked! As soon as I plugged it in, nvidia-settings picked it up as another monitor and allowed me to activate it.

But now what? I still couldn't plug it into my TV. So I did the only sane thing a geek would do: upgrade! Yep, I decided it was finally time to get a new TV. I won't bore you with the details, but after a week or two of searching, I ended up with a good deal on a 40-inch Sanyo LCD TV that supported HDMI.

So I plugged in the HDMI and was greeted with yet-another 1920 x 1080 desktop on the TV (I decided to use it as a separate X-session). Alas, I didn't find that resolution was very good for the TV (it looks great on my monitor, but despite the TV's size, things were a little hard to read). So I lowered the resolution to 1280 x 720 (720p) and find it much better and readable.

But what about audio? Sure, I could plug the audio cable from my speakers in, like I used to, but HDMI *is* cable of audio output, was it not? Surely, I could figure out how to activate it!

In fact, nVidia themselves have a guide to do just this. But it's not an easy read and requires some addition steps/research. In the end, it wasn't really that bad. Unfortunately, I was lazy at the time and didn't post any instructions. So when it stopped working the other day (more on that later), I couldn't recall everything I needed to do to get it back! I got everything restored without too much trouble, but decided to turn it into an opportunity to write the steps down and post them.

First, check to see that the audio device is being recognized. Do lspci | grep -i nvidia and you should see:

jason@jason-lucidlynx:~$ lspci | grep -i nvidia
05:00.0 VGA compatible controller: nVidia Corporation Device 1040 (rev a1)
05:00.1 Audio device: nVidia Corporation Device 0e08 (rev a1)

The audio device is what you're looking for. I didn't run into a problem with this, so if you don't see it show up, google-is-your-friend. My thinking though is that it's necessary to install the nVidia binary drivers (at the link I pointed out above) before you can get this far.

Once you confirm the hardware is recognized, you *must* make sure your Alsa is up-to-date. Default Alsa with 10.04 is 1.0.21, which is not sufficient for the GeForce 520GX.

Before you upgrade Alsa, you can note your old version:

jason@jason-lucidlynx:~$ cat /proc/asound/version
Advanced Linux Sound Architecture Driver Version 1.0.21.

Also, aplay -L won't list your HDMI audio output.

jason@jason-lucidlynx:~$ aplay -L
pulse
Playback/recording through the PulseAudio sound server
front:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
Front speakers
surround40:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
4.0 Surround output to Front and Rear speakers
surround41:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers

Upgrading Alsa is simple. Just follow the instructions here: https://wiki.ubuntu.com/Audio/InstallingLinuxAlsaDriverModules

After that (don't forget to restart after installing alsa!), you should get:

jason@jason-lucidlynx:~$ cat /proc/asound/version
Advanced Linux Sound Architecture Driver Version 1.0.24.
Compiled on Oct 6 2011 for kernel 2.6.32-34-generic (SMP).

and

jason@jason-lucidlynx:~$ aplay -L
pulse
Playback/recording through the PulseAudio sound server
front:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
Front speakers
surround40:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
4.0 Surround output to Front and Rear speakers
surround41:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=SB,DEV=0
HDA ATI SB, VT1818S Analog
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=SB,DEV=0
HDA ATI SB, VT1818S Digital
IEC958 (S/PDIF) Digital Audio Output
hdmi:CARD=NVidia
HDA NVidia, HDMI 0
HDMI Audio Output

which will make the HDMI Audio output show up! Cool, eh?

Next thing to do is see if you can get sound out of it, and figure out what hardware audio device it's enumerated as.

This is where the speaker-test utility comes in handy:

speaker-test -c 2 -r 48000 -D hw:0,0

This should cause some static to come out of your left and right main speakers.

After that, I just tried different values for the hw device.

Each audio card you have is numbered. 0 is typically your main one...I happen to have a USB audio device which shows up as device 1.

But eventually, I found it:

speaker-test -c 2 -r 48000 -D hw:2,7

This caused static to come out of my TV's speakers over the HDMI. Horray!

You might not have 2,7. In fact, 2,3 appeared to work for me, but didn't produce any sound. However, 2.3 *might* work for you.

At this point, you can go into your Gnome audio configuration (or whatever the equivalent might be in KDE/xfce/etc) and under output, you might be lucky enough to see "HDA NVidia..." in addition to your Internal Audio Analog Stereo. Great!

Play a sound file and try it out. If it works, you're done! If not, read on....

This unfortunately did *not* work for me. But I was closer then I realized.

The problem is called under-enumeration. It's pretty simply really, the video card (for whatever reason) made two devices available to the system: (2,3) and (2,7). But only the second mapped to the HDMI audio output. However, the Linux audio utils, by default, pick the first one (2.3). As far as I know, there's no way to change this.

But, you can add more audio devices, and select among those. To do this, open up the following file:

/etc/pulse/default.pa

and add the following line at the end:

load-module module-alsa-sink device=hw:2,7.

Restart. Now, you'll actually get three output devices. The regular internal stereo one, the HDA NVidia one, and a second HDA NVidia one (the text might look a little different).

So now, trying playing some audio and selecting the new audio device and voilĂ ! Hopefully, you should have audio coming through your TV. To be fair, I believe this is only stereo sound, I haven't done any research on getting a digital, surround-sound signal through, but I imagine it's possible.

Strangely, when I booted up a few days ago after doing a system update, Ubuntu once again kicked me into Low-graphics mode.

Investigating, I found that my nVidia binary driver was no longer being loaded. I was going to reinstall the driver, when I decided to check nVidia's site. Even stranger, nVidia *just* released a new driver for my card yesterday! Not sure if they two things are related or not, but hey, I got a new driver.

Installing the new driver restored all my video functionality. But my HDMI audio wasn't working again. It seemed my Alsa had kicked back to 1.0.21, so the device was no longer being picked. So I started doing the motions necessary to restore it and decided to write them down this time (which you've just finished reading). Afterwards, my HDMI audio is working fine again!

References:

http://www.nvidia.com/Download/index.aspx?lang=en-us
https://wiki.ubuntu.com/Audio/InstallingLinuxAlsaDriverModules
ftp://download.nvidia.com/XFree86/gpu-hdmi-audio-document/gpu-hdmi-audio.html

Thursday, October 6, 2011

How not to solve a Rubik's Cube

The Rubik's Cube is one of my favorite puzzles. Upon first glace at a sufficiently scrambled cube, one might feel an instant feeling of dismay. With so many combinations, how could anyone hope to figure out how to unscramble it in a reasonable amount of time?

Many people would give up right then and there. But Rubik's Cubes have been solved countless times by countless people. It's far from an unsolvable problem, and in fact many of us take our fun from the thrill in the challenge.

I have successfully solved a Rubik's Cube on a number of occasions. I'm far from an expert solver, or a speed solver, generally it'll take me a couple of hours to solve an arbitrarily scrambled cube, not because it takes that long, but because I can only do it in stages and spending too long on a single stage get's my brain a little wonky :)

I remember the first time I solved the cube was right around the time of my thirteen birthday. Being proud, I continued to play with the solved cube until I accidentally scrambled it again. To my initial frustration, I then had to solve it a second time, which I did. It was promptly buried in the backyard after that.

(Aside: It was buried not because it frustrated me to the point I wanted to get rid of it, but we also happened to be making a Time Capsule at the same time and I thought it would make a good addition. That Time Capsule I believe is due to be unearthed next summer actually.)

Nevertheless, puzzle solved, I prompt forgot about the device for a number of years. Fast forward to several months back when I happened to see one at a local department store and decided to buy it.

I've solved this cube a couple of times since then, this time things are a little different however. Instead of just solving the cube once and forgetting about it, over time I'm practicing on it, trying to become better at solving it so that I can solve it in general for any specific cube without help.

To be fair, I've never actually solved a cube, from scratch, without some sort of assistance. Even when I solved the first cube all those years ago, I had a sort of guide that came with the cube. Now, this guide was far from a defacto "here are step by step solving instructions", but rather just a general set up "helper" steps to get where you wanted to go.

These helper steps are called algorithms, and are basically a short (or long) set of instructions on how to take the cube in one configuration and get it to another configuration. For example, how to move a block on the bottom, to the top, without changing a series of surrounding cubes (but not all cubes). I've always used these to help me, though again, even with this set of helper steps solving the cube is still fairly difficult. You still need to get it the right stage to do perform the algorithm, and could make other unwanted changes as well. Assembling them all together to solve the cube is still the hard part.

Some might label using the algorithms as "cheating", but to me, cheating at a cube would be more akin to rearranging the stickers or taking the cube apart (as many, including my original, are capable of being dismantled) and reassembling it correctly. Even expert speed solvers use these algorithms, likely taught to them by a book or someone else, or perhaps learned from scratch in some cases. Using algorithms is just a natural part of learning how to solve the cube. I suppose you could invest many hours in addition to actual solving time to develop the algorithms, or it may just be the software engineer in me which has taught me to use what's already been developed and not waste time reinventing the wheel :)

But in fact, this blog post is not about how to solve a Rubik's Cube. There are many other sites for that, one of my favorites is linked here: http://www.chessandpoker.com/rubiks-cube-solution.html

No, rather this post is about how *not* to solve a Rubik's Cube. But wait, you cry, why would anyone care about how not to solve the cube?

The answer is that there is an oft-quoted, but misguided, way that people often use to try and solve the cube. When someone first starts trying to solve it, they might start by randomly twisting sides in the hope they'll get somewhere. This procedure is doomed to fail (unless maybe you happen to be a computer program with dozens of gigabytes of memory). But rather, the misconception among novice cubers seems to be that there exists a fixed sequence of moves (moves, which I'll define below) which will solve the cube regardless of it's starting configuration (we'll call this the Magic Sequence). So a cuber with this mindset will spend all of his or her time searching for the Magic Sequence, without actually really paying attention to the mechanics of the cube.

In fact, searching for the Magic Sequence is at least equally (in fact more so) doomed to fail then twisting the cube randomly. I suppose even random twists could conceivably solve the cube eventually even if it takes eons. But trying to use a single set to solve the cube regardless of it's starting configuration is simply impossible, as I will demonstrate here. And so, it's important to explain why this method can't work and that a cuber trying to use it will ultimately never succeed.

First, let's consider what I mean by a "move". Among cubers, "moves" can have a variety of meanings, anything from a single turn to an entire algorithm. However, a common definition for a move often used by newer cubers and people searching for the Magic Sequence is a single turn, either clockwise or counter clockwise, of one of the cube's six sides (sometimes called a Quarter Turn by cubers).

Now, if you'll consider it, what is the cube actually made of? In fact, a Rubik's Cube is not a singular cube, but rather 26 smaller cubes (9 on each end, plus 8 going in a ring down the middle. There is no dead center cube, as that's where the rotation mechanism lives).

We can easily enumerate each of of these smaller 1-26 cubes and keep track of their orientation as we work the cube. Keep in mind that this enumeration would exist regardless of the color of each "face" of the cube.



Thus, from any starting configuration, there are only 12 possible unique moves, by this definition, that we can make to get the cube in a new configuration:

1. Top layer goes clockwise or counter clockwise
2. Bottom layer goes clockwise or counter clockwise
3. Right layer goes clockwise or counter clockwise
4. Left later goes clockwise or counter clockwise
5. Front later goes clockwise or counter clockwise
6. Back layer goes clockwise or counter clockwise

Here is an illustration of 6 of the 12 possible moves. The other 6 are just mirrors on the non-visible sides.







Some people might try to argue that there are more moves then this. For example, turning the bottom two layers clockwise. But, this is really equivalent to turning the top later counter-clockwise. Likewise, turning the right layer clockwise twice is really just two applications of the same unique move. Each move will have a fixed, predetermined affect on the cube that can't be altered.

A solved cube, with all sides showing the same color, will live as one, and only one, possible configuration of the individual cubes. The cube can only exist in a single configuration at a time, it cannot exist in two different states simultaneously. Agreed? This is Rubik's cube, not Schrodinger's cube, after all.

Let's say that I start with the cube in a given configuration, called Configuration A. I then perform a sequence of moves on the cube (where a sequence is an ordered set of the twelve moves listed above). We'll call this Sequence A. Upon applying Sequence A, to Configuration A, we end up in another Configuration B (which could possibly be equal to Configuration A). The important thing to remember is that every time I start the cube in Configuration A, and apply sequence A, I'll *always* end up in Configuration B. Likewise, if I start the cube in Configuration B, and perform Sequence A backwards, I'll always end up with Configuration A. This is true for all possible Configurations and Sequences of the cube. There is no possible sequence that I can perform on a configuration such that applying the same sequence to the same configuration will result in multiple configurations. Make sense? The diagram below shows the symmetric nature of applying a sequence to a configuration.



Given that, to disprove the "Magic Sequence", we can use what's called a proof by contradiction. The basic premise is that if we can show that the existence of a magic sequence would lead to a contradiction given the rules of the cube we've described (cannot exist in more than one state, and configuration + sequence = a single new configuration), then it can't exist.

Let's call a cube's solved state Configuration Z.
First, let's imagine that the "Magic Sequence" did exist. This would mean that there exists a sequence such that I can apply the sequence to Configuration A and end up with Configuration Z, and that I can apply the identical sequence to Configuration B, and end up with Configuration Z, (where A and B are *not* the same configuration, and represent any configuration the cube could be in).

But then, what would happen to Configuration Z if I performed the "Magic Sequence" backwards? Remember that we stated the cube could only exist in one configuration at any one time.

If the "Magic Sequence" existed, then I'd be able to perform it backwards from the solved state (Configuration Z) and end up with *both* Configuration A AND Configuration B, which is impossible, since the cube can only be in one configuration at a time. In other words, I can't apply the "Magic Sequence" to Configuration Z, and wind up with A, then later apply the same sequence to Configuration Z, and wind up with B, since a fixed sequence of moves will always have the same result on the cube. Therefore, the "Magic Sequence" does not exist.



Make sense?

Now, there are plenty of other ways to solve a cube. Many of them involve the application of multiple algorithms in order, which is sort of like a Magic Sequence. But the important distinction is that the algorithms involve decision-making, e.g. applying a different algorithm next based on the result of the last. If such a "Magic Sequence" like the one disproved existed, you'd be able to solve the cube without any decisions.

Feel free to leave any questions or comments you might have below and I'll do my best to answer them!

Happy cubing!