10

I have a USB mic I keep plugging into my laptop dock. When I drop into the dock I'd like the mic setup as my current audio input automatically. I do have a "I'm docked" script I run which sets xrandr/etc items, I'd just need some method perform the action of:

gnome-volume-control, input tab, choose mic

but from the cmd line/script.

Any hints on how I can automate/script this?

13

If you are using ALSA, amixer can be helpful for your script programming.

When dropping the amixer --help command in a terminal you will see something like this:

enter image description here

Depending on your soundcard, levels may be different than mine, but you can use alsamixer in the terminal in order to check which levels and which features in your sound card you can call in a command to set the volume as you wish.

enter image description here

In my example, with my principal sound card (I have 2: the embedded and a PCI audio card), levels are from 0 to 100, this way I can change the volume of a desired input/output in my soundcard by dropping in a terminal the next command:

amixer -c 0 set Front 50DB 
amixer -c 0 set Front 64DB 

In the first command, the result will set the Front panel output to 78% level and the second one will set the Front panel output to 100% level.

In order to gather information related to your mixer controls, drop the amixer command with no parameters and you will get a list. Or indicate which audio device you wish to see a list of controls with amixer -c X (where "X" is the number of your audio device).

BTW: Remember that DB values are calculated logarithmically and not linearly.

2
  • From where can I install the amixer? sudo apt-get install amixer on ubuntu 13.04 does not do the trick – vegard torvund Oct 25 '13 at 13:52
  • You may need to install (or reinstall if needed) alsa-lib and/or alsa-utils by dropping in a terminal sudo apt-get install alsa-utils --reinstall Please note that the --reinstall is only needed if you wish to reinstall. Good luck! – Geppettvs D'Constanzo Oct 25 '13 at 15:04
7

Possibly pacmd set-default-source works if you are using pulseaudio, for example:

pacmd set-default-source alsa_input.usb-046d_0819_9F13DC90-02-U0x46d0x819.analog-mono

Find the source name using the command: pacmd list-sources.

1
  • Linux Mint 18.1, here. My "Sound" settings would not change input device from Acer's built-in mic, but using this command finally worked: pacmd set-default-source "alsa_input.usb-Samson_Technologies_Samson_Meteor_Mic-00.analog-stereo" – user207863 May 16 '17 at 13:33
6

I have found a better answer by looking in the docs (probably is a recent addition to pacmd...)

Use pacmd list-sources to see the different input devices, the one with the * index: ... is the one active.

You can then use pacmd set-source-volume <index> <volume> to set the volume, In my case using 50% as an arguments was failing, so I just tried different numbers until it showed as 50% in the UI.

For me pacmd set-source-volume 1 30000 was the line that worked

Cheers!

0

In relation to #4, I wrote a program that finds the index, and then sets the level to 250000:

#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <sstream>


using namespace std;

void exec(string str) {
const char *command = str.c_str();
system(command);
}


int main() {

exec("pacmd list-sources | grep \"*\" > tmp");
fstream file ("tmp");
string output;
if (file.is_open() && !file.eof()) {
getline(file, output, '\n');
}
file.close();

char id;

id = output.back();

exec("rm tmp");

string command = (string) "pacmd set-source-volume " + id + " 250000";

exec(command);

return 0;
}

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.