3

I often use a headset, to select if using the headset or the normal soundcard I use a environment variable thanks to this code in ~/.asound.rc:

@args.CARD {
  type string
  default {
    @func getenv
    vars {
      0 ALSA_CARD
    }
    default {
      @func refer
      name 'defaults.pcm.card'
    }
  }
}

Unfortunately the new headset (hw:Set) has reversed channels so I created a pcm to fix, adding those lines to ~/.asoundrc:

pcm.swapped {
    type  route
    slave {
        pcm "hw:Set"
    }
    ttable.0.1   1
    ttable.1.0   1
}


pcm.HeadsetSwapped {
  type plug
  slave {
    pcm "pcm.swapped"
  }
}

The pcm works, but how can I decide to use it via environment variable? At the moment the env variable only accepts card names. It is probably something around the -- name 'defaults.pcm.card' -- line, but I cannot fix it.

I know I can change pcm.HeadsetSwapped to pcm.!default, but I would like to avoid to edit the file each time I want to start a program...

5

Use something like this, and set the environment variable MY_DEVICE to either hw or swapped:

pcm.!default {
    type plug
    slave.pcm {
        @func getenv
        vars [ MY_DEVICE ]
        default "hw"
    }
}
pcm.swapped {
    type route
    slave.pcm "hw:Set"
    ttable.0.1 1
    ttable.1.0 1
}
2
  • Thanks, works perfect. And it is well readable, uncommon for alsa related stuff. Jun 14 '13 at 12:57
  • Hint: If you hit "Device or resource busy" with default "hw", try default "dmix". I just felt into that trap. Jun 3 '20 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.