Javascript and CSS3 Fun – Rotating the Screen

Info for Nerds (ie: How to rotate the browser’s display)

See http://dotslash.ca/javascript-fun-what-my-browser-looks-like-on-a-boat/ for the example/demo.


Through the magic of CSS2 and some basic Javascript, this is actually pretty simple! Here’s the basic description of the code:

  1. Set up a global variable for the document body’s style element, as well as for a few other things like "angle"
  2. Check to see if we’re on the page that is only showing this post so we don’t mess up the home page when a blurb of this page is shown. (I just did it a quick and dirty way by checking if the page’s title matches this page’s title)
  3. Increase the angle but a small random amount, and if the angle is more than twice PI (in Radians) subract twice PI
  4. Use the sine of the angle divided by a somewhat arbitrary factor to adjust the CSS3 transform’s "rotate()" parameter. (If I didn’t divided it down by the factor it would spin all the way around, just like Google search does when you type “do a barrel roll” in the search!)
  5. Set a timer to do it again in a moment

The Code

<script lang="javascript">
var pi = Math.PI;
var timer;
var factor = 20;
var angle = 0;
var docBody = document.getElementsByTagName("body")[0];
var s = docBody.style;
function stopTimer()
{
    clearTimeout(timer);
    document.getElementById('stopTimerButton').style.display = "none";
    document.getElementById('straightenUp').style.display = "block";
}
function rollit()
{
    angle += Math.random() / 5;
    if (angle > pi)
    {
        angle -= (2 * pi);
    }
    s.transform = "rotate(" + (Math.sin(angle) / factor) + "rad)";
    timer = setTimeout("rollit()", 35);
}
if (document.title.indexOf('Javascript fun') === -1)
{
    document.getElementById("viewThisPost").style.display = "block";
    document.getElementById("stopTimerButton").style.display = "none";
}
else
{
    s.transform = "rotate(" + angle + "rad)";
    rollit();
}
</script>

I haven’t tested this on any browsers other than Firefox… but it’s pretty cool, huh?

Share Button

Javascript fun: What my browser looks like on my boat.

Note: If you’re using Internet Explorer or a mobile device this may not work… try Firefox!



Info for Nerds (ie: How it’s done) is in the next post

Share Button

Changing permalink for a single WordPress Page

On WordPress 3.6 or 3.7, when you have the permalink type on the default /?page=33 style, you cannot edit the permalink for an individual page. If you change the global setting for permalinks to something like /page-title, the “Edit” button will now appear beside the permalink (Near the page title) when you edit the page.

Share Button

Connecting multiple sound devices with JACK and ALSA

The problem: JACK connects to one (generally the default) sound device, but you may have a second (third, fourth, etc) device that you want to record from or playback on.

Example situation (Actually, my situation):
Device #1: The NVidia sound card built in to the PC/Laptop.
Device #2: Guitar amplifier connected via USB
Device #3: USB headset/microphone.

Software I use:
(Note that this is all free software for Linux, versions may exist for other operating systems)

  • QJackCtl – essentially to start/stop JACK
  • Patchage – Used for making the connections between devices
  • alsa_in/alsa_out – Used to add the extra sound devices for JACK. If you installed JACK, you should have these command line programs
  • Ardour – for recording the tracks
  • aplay and arecord – part of the alsa-utils package

First Steps

Before starting anything, connect the devices, and use aplay and arecord to list what our devices are:
To list the playback devices:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

And the capture devices:

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: NVidia [HDA NVidia], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: LX3000 [Microsoft LifeChat LX-3000], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Amplifier [Mustang Amplifier], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Now, to translate this into standard ALSA lingo, the NVidia built-in device is card0, so we will refer to it as HW0, The M$ LX-3000 headset is card 1, thus referred to as HW1, and the Fender Mustang Amp is HW2.

Okay, so now running QJackCtl and clicking “Start” should get JACK running on HW0, the built-in sound device (Assuming it’s still at default settings). If you now start Patchage, you should see something like the following:
patchage_ss1
Now for the fun part; let’s get the input from the amplifier connected. Open up a terminal and run “alsa_in -j Amplifier -d HW:2″ and you should see something like:

$ alsa_in -j Amplifier -d hw:2
selected sample format: 16bit
delay = 4094
delay = 1019

Don’t worry if you get a couple of those “delay” messages when it first starts up.
Now, let’s get the mic on the headset connected:

$ alsa_in -j Mic -d hw:1
WARNING: chennel count does not match (requested 2 got 1)
selected sample format: 16bit
delay = 3255

Note the message about channel count – that’s just because it defaults to 2, if you don’t like seeing the message you can add a “-c 1″ to the command.

Now the last thing, the headset speakers:

$ alsa_out -j Headphones -d hw:1 -r 44100
selected sample format: 16bit

Note that I added a “-r 44100″ – in my situation everything else defaults to 44100, but the headset defaults to 48000 and sounds like crap.

Now, let’s check on what it looks like in Patchage now:

patchage_ss2

Now we’re getting somewhere!

So now, I’ll fire up Ardour and add a couple tracks – 1 stereo track for the guitar and one mono track for the vocals. (I normally also use Hydrogen for a drum track as well, but I’ll skip that in the interest of keeping this shorter) Once that’s done, we’ll make the connections in Patchage by right-clicking on items that are connected automatically that we don’t want and selecting “Disconnect All”, then dragging the mouse from one device to another to “wire them up”.

Once it’s done, you should see something like the below:
patchage_ss3

I know the connections in Patchage might look a bit confusing, but play around with it a bit and you’ll find it’s pretty intuitive.

And now, you’re ready to Rock!

Share Button

Convert time from UNIX Epoch to Human Readable

Here’s a simple online form to convert a UNIX timestamp to a human readable date

Enter the timestamp:

Share Button

Automatically Enable USB Headset

I, along with many others, would like a USB headset to be the default sound device in ALSA whenever it’s connected, and it seems to be a tough thing to find out how to do.

Short answer: Create UDEV rules to invoke a script that will modify your ~/.asoundrc file.

Long answer:

Assumptions:

  1. You’re using Linux kernel >=2.6
  2. You’re using ALSA.
  3. You’re using UDEV
  4. You already have the headset working

First, we need to know what device we are looking for, so with the headset unplugged, fire up a terminal and type:
ls /dev/snd/
You’ll see something similar to the below:

localhost ~ # ls /dev/snd
by-path  controlC0  hwC0D0  hwC0D3  pcmC0D0c  pcmC0D0p  pcmC0D3p  seq  timer

Now, plug in the headset and check it again, ie:

localhost ~ # ls /dev/snd
by-id    controlC0  hwC0D0  pcmC0D0c  pcmC0D3p  pcmC1D0p  timer
by-path  controlC1  hwC0D3  pcmC0D0p  pcmC1D0c  seq

Notice the new items, in my case, the ones we are interested in are pcmC1D0p and pcmC1D0c. Note: The suffixes "p" and "c" indicate "Playback" and "Capture". For the purposes of this article, I’ll ignore the capture device, but the same process applies for it as for the playback.

Knowing the device name, we can now create the UDEV rules. Rules files are kept in /etc/udev/rules.d on my Gentoo system – other distros should be similar. You may have no files in there, or there may be many. I made a new file called /etc/udev/rules.d/99-audio.rules. the 99- prefix indicates the order in which the rules files will be applied, and I simply chose 99 to make this one run last.

My /etc/udev/rules.d/99-audio.rules file:

KERNEL=="pcmC1D0p", ACTION=="add", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"
KERNEL=="pcmC1D0p", ACTION=="remove", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"

Rough explanation: When UDEV sees a kernel message about the device "pcmC1D0p" via USB, and the action that occurred was that it was "add", it runs the script specified with the ACTION as a parameter (in this case, "add". Note that you’ll need to be root to do this.

Now for the script that actually changes the device, I should put it somewhere better, but I created /home/pvint/headsetToggle.sh with the following contents:

#!/bin/sh
# Set default ALSA device appropriately when USB  headset is plugged/unplugged
if [ "$1" = "add" ]
then
        echo 'defaults.pcm.card 1' > /home/pvint/.asoundrc
else
        echo 'defaults.pcm.card 0' > /home/pvint/.asoundrc
fi

Note that if you had anything in the .asoundrc file, it’ll be clobbered. This could easily be avoided by creating two dummy files like .asoundrc_headset and .asoundrc_normal and having the script copy the appropriate one to .asoundrc. The above was simple and suited my purpose just fine.

That should do it, so restart UDEV (ie: /etc/init.d udevd restart) and try it out.

Notes:

  • Check your /var/log/messages to ensure there were no errors with the UDEV rules
  • Some applications need to be restarted before this takes effect.
  • The script will be run as root, so be careful
Share Button