Sunday, July 21, 2013

Two Sound Outputs to Two Channels of The Same SoundCard Using ALSA

          Linux comes in handy when the necessities are very specific. I had installed Mixxx in my computer to do some beginner level mixing of songs. The software is free, and if you have a good media library and internet connection to help you out the only problem that comes in the way is the problem of having only one sound output. The headphone and master outputs have to be channeled to two sound cards. This is the basic requirement to start mixing songs. But I for one was not ready to invest on the simplest of sound card, mostly because I am that cheap.

        The Only solution I could think of to this problem was to use my to channels as two sound cards, If one has a head phone, and is not particular about panning effects in your mixes, getting the master output as mono in your left ear and the Headphone output of the console (Mixxx) in your right ear is good enough for entry level mixing, good enough if you want to not pay and get things done.

      Two do this I had to get two sound cards emulated so that I can give the master in one and the Headphone in the other soundcard in the Mixxx interface. And afterwards I have to make the output I recieve in these sound cards mono, and send the to the left and right channel of my real sound card. Now this right here is a very specific configuration that I can't imagine myself doing anywhere else but here, in Linux.

      Though it started out with me thinking all the wild and complicated things I would have to do, like writing a code in C or if lucky in python, I soon found that this can be easily done with the help of this wonderful "thing" called ALSA (Advanced Linux Sound Architecture ). Now with ALSA all I have to do was add a config file in my home file called ".asoundrc". Once I had done that, that is add the required config which will emulate two sound cards that will 'mono-fy' their received inputs and then send to their alloted channels in my real sound card (Howto is below), I could simply go to mixxx, and when I checked in the lists of sound cards, there they were. The two new sound cards that I emulated. Now I assigned the two outputs of Mixxx to these two cards and now I have the crowd output on my left ear and the headphone output on my right. :)

    Now going to the code.


     The code (more like config) has to be written in the location ~/.asoundrc. So take the terminal and type

"nano ~/.asoundrc"

 This should take you to the text editor, now copy this text given below to that file and save it.

#The handle to the hardware "hw:0", the only sound card in my computer
#The hardware details can be found out by running command 'aplay -l' in console
pcm.Scard{
    type hw   
    card 0
    device 0
    subdevice 0
}

#t8k is the simple use of sound card without any additions to its properties
pcm.t8k {
    type plug
    slave{
        pcm Scard
    }   
   
}


#this is where the mixing is done
pcm.dmix2 {
     type dmix
        ipc_key 1024
        ipc_key_add_uid false # let multiple users share
        ipc_perm 0660 # IPC permissions (octal, default 0600)
        slave {
                pcm Scard # see below
                channels 2
                period_time 0
                period_size 2048 # try 2048 against skipping
                buffer_time 0
                buffer_size 1024 # in case of problems reduce this
                                 # in case of skipping, try increasing
        }
}

#The emulated left sound card, routes half the sound from left and right to
#the left channel of the dmix soundcard
pcm.left {
        type route
        slave.pcm dmix2
        slave.channels 2
        ttable.0.0 0.5
        ttable.1.0 0.5
}

#Same thing, output mono to right
pcm.right {
        type route
        slave.pcm dmix2
        slave.channels 2
        ttable.0.1 0.5
        ttable.1.1 0.5
}
ctl.left{
    type hw
    card 0
}
ctl.right{
    type hw
    card 0
}

#These are bitrate compatible interface of left and right soundcards.
#Try only if the 'right' and 'left' soundcards are skipping.
pcm.rightGood{
    type plug
    slave.pcm right
}
pcm.leftGood{
    type plug
    slave.pcm left
}


      Now, once this file is created, the sound cards can be checked in the terminal thus:

#reload the alsa with command:
     sudo /sbin/alsa force-reload
#try the left sound card with some wave file you have, here test.wav
      aplay -D left test.wav
#if this is giving you a squeaky sound as it is played fast go for the 
#bitrate compatible plugin     
      aplay -D leftGood  test2.wav
#now you should be getting a left only output. 

It is that easy.