ALSA/ja

この記事ではArticle description::ALSA (Advanced Linux Sound Architecture) を利用したサウンドカードのセットアップについて解説します.

ハードウェアの検出
必要なドライバを見つけるためにはまず、稼働しているオーディオコントローラを検出します. そのためには コマンドを実行します:

コントローラの名前が判明したら、必要なドライバが ALSA SoundCard Matrix で見つけられます.

カーネル
次のカーネルオプションを有効化してください:

コマンドが助けになります.

システムが 8 個以上のサウンド出力を持っている場合（GPU の HDMI 出力はそれぞれ 1 個として数えられます）、サウンドカードの最大数を上げる必要があるでしょう:

ソフトウェア
portage は、他のパッケージで ALSA に対するサポートを有効化するためのグローバル USE フラグ  を知っています. この USE フラグを有効化することで を自動的にインストールします（ と  のデスクトッププロファイルではデフォルトで有効化されています）:

コマンドは に含まれます.

これを設定したら、変更を適用するためにシステムを更新してください:

パッケージはサウンドシステムのトラブルシュートとテストをするためのいくつかのツールを提供します. まだインストールされていなければ、このパッケージをマージするといいでしょう:

ファイル

 * - グローバルな（システム全体の）設定ファイル.
 * - ローカルな（ユーザ毎の）設定ファイル.

~/.asoundrc
ユーザ毎に ALSA デフォルト設定を上書きするためにこのファイルを利用することができます. システムが特殊なハードウェアを持っている場合、カード 0 デバイス 0 （デフォルト）が動作しないことがあるかもしれません.

最も簡単な変更のひとつがカードとデバイスです. Nvidia カード上の HDMI をオンボードの Realtek ALC88* オーディオデバイスに接続した私の場合はこうでした.

If you can't seem to find the card number assigned to the device that is the sound card you intend to use, don't panic — the card number is internally a name, which you can use. If your card is listed like this...

... then it means the file should be like this:

複数のサウンドカードを使用している場合、デバイス番号はブートの度に順番が変わることがあるので、名前を使うほうが便利です.

正しい名前が分からないなら、有効な名前のリストは次のコマンドで簡単に取得できます:

これは複数のサウンドカードを持つある開発者のシステムからの出力です:

ここでは Q1U のマイクが Q1U として、内蔵 HDMI が HDMI として、アナログオーディオジャックが PCH として、ウェブカムの内蔵マイクが C930e として表示されています. どれもカードに対する有効な名前です.

Background
Most sound cards allow passing through audio to an external consumer receiver or DAC using S/PDIF digital coaxial or optical cables. Doing so, preserves sound quality and compressed Dolby/DTS encoded material. Uncompressed Dolby/DTS or Bluray material, require HDMI connections. The more common mono or analog stereo cables cannot carry Dolby/DTS signals.

You may find your S/PDIF or HDMI connection with default installed ALSA works with no file alterations. While only some applications, such as the Web browser Flash plugin will fail playing sound. As such, the below is usually required for most S/PDIF and HDMI connections. Also, any media applications open will need to be restarted for the files to take affect. (i.e. Web browser with Flash plugin)

Basic analog
Basic Analog (ie. RCA) Connections - Basic user. Quality depends on sound card DAC. Look for a sound card with a high SNR db level.

S/PDIF
S/PDIF provides good quality audio for music, videos and DVD quality movies containing Dobly/DTS compressed signals. Most sound cards and motherboards, these days, provide some sort of S/PDIF port. Nowadays, it's more common to see S/PDIF Toslink ports on motherboards. Many computer games provide compressed Dolby/DTS signals.

S/PDIF Digital Coaxial
S/PDIF Digital Coaxial may have problems with voltage cross talk, but is more common as it only requires a simple mini jack or RCA coaxial cable.

S/PDIF optical (Toslink)
S/PDIF Optical (Toslink) Cable completely avoids possible electrical cross talk or interference amongst cables as it's fiber optic, but is susceptible to signal degradation if the cable is bent too much. You'll find audiophiles tend to favor Optical/Toslink.

HDMI
HDMI can carry compressed Dolby/DTS and uncompressed (i.e. Blu-ray) Dolby/DTS signals. The audio market has favored this connection, but still preserves S/PDIF connections. One concern being, HDMI cables are copper wire, still susceptible to electrical cross talk or interference, similar to S/PDIF Digital Coaxial connections. There are HDMI Optical cables, but too expensive for the consumer market. If audiophiles have S/PDIF Optical ports, they will use the S/PDIF connections for other media such as music and DVD movies, while only using HDMI when needed for processing uncompressed Dolby/DTS encoded material such as Blu-ray media. Again, the basic user will likely just use HDMI, avoiding the fuss of changing configuration files around.

Configuration
Find the digital output device:

Adjust the below file to use the card/device number.

A/52, AC3, Dolby, DTS
Soundcards providing S/PDIF output can pass through lossy compressed multichannel audio. To my ears, the high bitrate compressed media has little loss in comparison to two channel CD or DVD audio. I surmise this is because we now have multichannels feeding multiple speakers. Hence, the more hardware, the equivelant or better sounding?

If a soundcard states it has S/PDIF, it will likely pass through Dolby or DTS even though it does not specifically show a Dolby or DTS icon or listed within it's features. This is because, when they do list Dolby or DTS, it's because they're providing software for upmixing, or providing the decoded signal through analog output.

HDMI will pass through uncompressed multichannel audio, but a video feed is interleaved as required by the HDMI specification. In comparison to DVD video and audio, I see and hear very little quality difference, if any! About the only thing I noticed between S/PDIF Toslink and HDMI, HDMI seems to amplify the signal by a few watts or 5db. (I conclude this is because of the higher bitrate?)

As such, S/PDIF is still quite popular, even today.

Decoding or encoding to Dolby or DTS requires a license. If you have already purchased a sound card with this multichannel support, then you likely already have a license. See this article for more informations.

ALSA package requires recompilation to include the ffmpeg USE Flag with the A/52 (pcm.a52encode) plugin.

Decode or playback
Recompile or another other favorite software player to include the ALSA libraries.

Add the multichannel codecs to, so media attempting to be played has first been provided the option of hardware passthrough rather then down mixing.

You should now be able to pass through (and enjoy) almost any Dolby or DTS signal through S/PDIF to the receiver.

Upmix two channel audio to a A/52 multi-channel audio stream
To simulate A/52 encoded audio from normal one or two channel audio streams or files, ALSA can upmix using its A/52 plugin. (A/52 is also known as AC-3 or Dolby Digital encoding.)

A/52 upmixing preferred when playing computer games or watching video without Dolby/DTS encoded material, such as older Movies. Many sound card manufacturers provide their applications with a feature for software upmixing to Dolby Digital, and recommend enabling this upmixing when listening to such media, except for Music.

Upmixing to A/52 is frowned upon by audiophiles. As such, it's user preference if you want to upmix all the sounds to A/52 before sending the stream to the consumer stereo receiver or DAC. Matter of fact, audiophiles prefer simple stereo, and further state stereo is still better quality than Dolby/DTS material or other audio encoded with gimmicks. Also, the consumer stereo or DAC likely possibly includes a feature for upmixing audio into five channel audio.

Encode PCM 5.1 24-bit audio into a A/52 16-bit audio stream (for streaming via S/PDIF)
Also known as, encoding PCM 5.1 audio stream into a compressed Dolby Digital stream for playing over S/PDIF. No real need to use this if you're using HDMI, as HDMI can handle the bandwith of a PCM 5.1 audio stream. S/PDIF Toslink has also supposedly been upgrade to do so also, but most hardware has yet to be upgraded to the new specifications, and some doubt it will ever happen. (Personally, I think HDMI is evil and only gives me problems, with little benefit.)

For some reason, the previous section previously mentioned doesn't work with PCM 5.1 streams. Another issue I run into with 24 bit PCM 5.1 audio streams, they require downmixing from S24_LE to S16_LE bit format. Reason being, 24 bit is a common format for most media, however receivers can sometimes only decode 16 or 32 bit audio. Also, the S/PDIF can only handle 16 bit audio when encoding to multi-channel formats such as A/52, for the time being until S/PDIF TosLink hardware specifications are upgraded.

The following excerpt will encode a PCM 5.1 24 bit stream into a 16 bit A/52 stream, for streaming over S/PDIF.

Clone audio for 2 or more devices
Sometimes you need to clone audio (example using 2 cloned screens with diferent inputs (first screen HDMI and second screen DVI + Analog audio)). This is a simple script that you can use as global or local configuration for clone 2 (or more) channels. You must adjust to your system the lines below comented lines. More information of the autor of the script and forum discussion here.

JACK audio connection kit
Jack provides an additional layer, to the already low latency ALSA drivers and applications, and further it guarantees a constant sound latency and synchronous operation of all its clients. Jack can only use one sound card at a time, and will provide separated outputs and inputs for each audio channel of the sound card. Jack further provides the ability to view, manipulate or pipe audio streams, both hardware and software streams, in a similar manner we use cables to interface different audio equipments. As example, JAMin can intercept an audio stream before exporting the stream to another audio application and/or through analog or S/PDIF outputs. The Jack user owned daemon, is usually started using qjackctl. The qjackctl utility provides many other functions including audio stream connections.

What Jack does not do, is handle A/52 encoded (ie. AC-3 or Dolby/DTS) material on input; it is just a sound server that deal with separated channels of audio streams. And it has the capability to connect simultaneously and synchronously any output stream on any input, and any input to any output stream, that on any hardware and software audio component, and with a constant latency. This make JACK an outstanding tool for audio production and creation. The ac3jack tool can be used for encoding multiple separated audio channels to AC-3 streams.

When playing an A/52 encoded media, the player will request as many outputs from JACK than audio channels in the media. This will fail if your sound card doesn't have the requested amount of audio outputs, but will be fine otherwise. As example, you can use smplayer as usual, and configure it to output a 5.1 stream on a 7.1 output configuration, or a 7.1 stream on a 2 channels stereo output.

One solution is to configure your kernel and ALSA according to Jack installation instructions for providing low latency audio. Since ALSA is said to be already very efficient and low latency, providing very good quality playback with no additional mixing when stated within its file, unlike MS Windows, Jack probably isn't needed for most. The main audience of JACK is audio producers and musicians (in studio, live performances, DJ, ...). (The ac3jack package is currently hosted by the pro-audio overlay available with layman.)

It is possible to interconnect JACK and ALSA. At that time of writing, the best solution is via the snd-loopback module and zita-*2* bridge. For that, see Using an ALSA Loopback device and zita-ajbridge on the ProAudio Gentoo overlay wiki.

Using zita-ajbridge, it is also possible to add additional real sound cards into JACK. (Example needed)

Those who want to try JACK must know one thing: if you are happy with pulseaudio, you don't need to setup a realtime kernel for JACK. You will need rt operations only if you want to make a Digital Audio Workstation (DAW) with your computer, which necessitate something pulseaudio is not able to achieve: constant sound latencies as low or lower than 20 msec (pulseaudio is not able to provide constant sound latency at all), and synchronous operations. But if you want to make a DAW, JACK is definitely the way to go, and you will never look back.

Hopefully this small brief section about Jack will give some quick insight about Jack's features, while reserving the details of Jack to a Wiki page specifically focused on Jack.

In the meantime, you can take a look at the Pro-Audio Gentoo Overlay Wiki.

パーミッション
USE フラグがグローバルに有効化されていてConsoleKit が使用されている（つまり、システムが desktop プロファイルを使用している）場合、サウンドカードに対するパーミッションは自動的に取り扱われます. を利用することでパーミッションを確認できます:

より広い解決策は、サウンドカードにアクセスできるようにしたいユーザを audio グループに追加することです:

OpenRC
OpenRC を利用している場合は コマンドを使って ALSA を開始してください:

ALSA をブート時に開始させるには、 を使ってこれを boot ランレベルに追加してください.

systemd
systemd を利用している場合は、ALSA の状態は保存され、システム再起動時に自動で復元されます.

ALSA の状態は コマンドを使って確認できます:

ミキサー
何も聞こえないという場合、出力チャンネルがミュートされているかもしれません. デスクトップ環境のミキサーでチャンネルのミュートを解除するか、 で適切なチャンネルを選択して キーを押すことでミュートとアンミュートを切り替えてください:

スピーカーをテストする
If everything above is perfect, it should now be able to test the sound card and the speakers. The command-line tool from package  (this should already be installed as per the previous recommendation).

5.1 サラウンドサウンドシステムを利用した設定では:

Press + to interrupt the test.

マイクをテストする
If needed, select the audio device if no default one is provided and add the  to the incantation of, substituting the CardNumber,DeviceNumber in the integer fields.

The following will list possible devices.

The following will record indefinitely until + is depressed and provide a default 8000 Hz mono quality recording:

The following will provide will record for two seconds (--duration=2) using DAT 48000 Hz quality and display the curses vumeter in stereo  and save to.

Or optionally:

Notice, cannot record in S24_LE, and is only capable of recording using S16_LE or S32_LE formats. For the human ear, any audio resolutions greater than 24 bit (S24_LE) or 48000Hz are said to be indistinguishable to differentiate using the human ear. Reference Sampling (signal processing) Wikipedia, Audio sampling. Users should also specify channels=1, as all recordings are performed in mono/monaural when typically using the Microphone Input unless recordings are using the stereo Line Input. In order to further encourage to record monaural or only one channel, using the device=plughw:0,0 is further specified. (Alleviates a common problem, microphone/mic playback only occurs on the right or left channels.)

Use or  to playback the saved file. I usually record in "dat" or atleast "cd" quality formats. DAT is best when benchmarking.

Tips
Try one of the many configuration options in PaulBredbury's asoundrc file.

Firefox and YouTube have no audio with custom .asoundrc but other apps do
Browsers are sometimes picky about settings. If you're specifying the default audio device (likely given that most computers nowadays have an HDMI A/V output and the analog and HDMI audio outputs are viewed as two separate sound cards), try doing this specifically as follows:

Soundcard only available for one application
Sometimes one app essentially takes over all sound devices. This might even be for performance reasons.

Force the use of dmix instead of direct audio output (which is what most things use by default, such as Adobe Flash [which really means the browser] and Wine).

If the device is card 1 and device 7 (which is the case for Nvidia HDMI for me):

Use of is immediate and as long as you are not forcing the use of specific devices in any applications, they either will require a restart or will begin working immediately. One of the best tests is to run Chrome, go to YouTube, open a terminal, run mplayer with an audio or video file and see that you do not get an error about audio (such an error might be 'Device or resource busy').

Missing dialogue (sounds) with 4.0 speakers
If you're using a 4.0 sound card (like an old SB Live!) or 4.0 speakers in general, you may notice that in some games or movies the dialogues are very quiet or even missing. This is because most of those applications/movies support only either 2.0 (stereo) or 5.1 output. In order to achieve surround sound, the 5.1 audio track is used but the two excessive channels are discarded — the center channel (which usually carries dialogues) and subwoofer channel.

The above issue can be circumvented through creating a virtual device which will downmix 5.1 to 4.0, mixing the center and subwoofer channels with other audio channels.

HDMI/SPDIF 5.1 and 7.1 speaker testing
It's stated ALSA's speaker-test utility will not test more then two channel audio over HDMI or SPDIF. For this, you need to use mplayer, as quoted from ALSA's documentation.

Get the Microsoft's 8 Channel Speaker Test and unzip the file. (The file creates a sub directory named "8 Channel/", with files named "8_Channel_ID.wav" and "8_Channel_ID.wma".)

Play the provided sound file; specifying your HDMI or SPDIF device, PCM file, and number of channels which will likely be either 6 or 8.

APlay SPDIF/HDMI output has incorrect speaker channels
Now that you know how to use MPlayer to play a speaker test PCM WAV file per "HDMI/SPDIF 5.1 and 7.1 Speaker Testing" above, you now find APlay doesn't output to the speaker channels properly when using SPDIF/HDMI. With HDMI, this is likely caused by the snd_hda_intel HDMI audio module/driver which is used by other manufacturers such as NVidia HDMI aside from just Intel's HDMI hardware. As to why MPlayer does use the correct channels, MPlayer is usually under constant development and manually corrects this issue?

Another problem this solution pertains to, trying to play a 24 bit PCM 2.0 or PCM5.1 WAV files and finding APlay constantly refuses to play the stream, due to incorrect bit rate, etc.

To correct this for ALSA (APlay) with minimal alterations to the PCM streams, we'll need to remap the speaker channels within a file. Add the following to the bottom of your file. Also note, the below configuration is for both 5.1 and 7.1 audio, or you could further map/copy the two extra channels to your 5.1 channels incase you do not want to omit the audio from a 7.1 stream.

It seems that most HDMI to Stereo Receiver connections only stream 16 and 32 bit formats, skipping 24 bit. The above configuration up-mixes any PCM stream to 32 bit when using the pcm.myHDMI profile, as it's quite common to see PCM 2.0 and 5.1 24 bit audio files. It just doesn't seem right to down-mix everything to 16 bit, or use Float as the latter uses more processing power. (Users can also set an alias within their bashrc file for 16 bit or 24 bit incantations as well.)

Test the speaker routing using a surround test PCM file. (See the above previously mentioned "8 Channel Speaker Test".)

Weak center channel on PCM 5.1 live music
If you have a multi-channel sound track or music with an apparently weak center channel, and the sound track is a live recording; it is possible to map the center channel to the rear channels. Note, the rear channels on live recordings usually contain only the audiance screaming with very little music.

The following incantation of mplayer specifies; your HDMI device of hw:1.7, your PCM 5.1 file, number of channels, the format (not needed if your receiver can natively handle 24 bit and in my case I need to upmix as my Yamaha HDMI receiver can only natively handle 16 or 32 bit audio), and then the mapping. Mapping as follows; specify this is a 6 channel audio stream, with 6 mappings immediately following, copy left front channel to left speaker, copy right channel to right speaker, copy center channel to left rear speaker, copy center channel to right rear speaker, copy center channel to center speaker, and finally copy the subwoofer channel to the subwoofer speaker.

See MPlayer's Channel manipulation.

Need to view more detailed information per ALSA stream?
If you need to look at an in-depth illustration of how your program is connecting to ALSA, use the interface. You will need to substitute your relevant card/device details into the command below.

You should see a dump of information regarding the program and useful things like its sample rate(rate) and PID(owner_pid). access: RW_INTERLEAVED format: S16_LE subformat: STD channels: 2 rate: 44100 (44100/1) period_size: 5513 buffer_size: 22050 card: 2 device: 0 subdevice: 0 stream: PLAYBACK id: USB Audio name: USB Audio subname: subdevice #0 class: 0 subclass: 0 subdevices_count: 1 subdevices_avail: 0 state: RUNNING owner_pid  : 934 trigger_time: 86393.193574796 tstamp     : 86540.250594985 delay      : 17714 avail      : 4602 avail_max  : 7379 - hw_ptr     : 6485052 appl_ptr   : 6502500 tstamp_mode: NONE period_step: 1 avail_min: 5513 start_threshold: 2147483647 stop_threshold: 22050 silence_threshold: 0 silence_size: 0 boundary: 6206523236469964800

HTML5 does not play in a browser
If there is no sound in any browser used (Firefox, SeaMonkey, Otter Browser, etc.) and ALSA generally works, there is a workaround that might solve this particular issue: try removing the file.

Restart the browser and test the sound output for HTML5. It might just work now after applying this workaround.

HTML5 does not play in the Firefox browser
Some system motherboards (i.e. Asus Z87-EXPERT) cause Card 0 to be a MID device instead of a PCM device. The same driver module snd_hda_intel is used for both the MID and PCM cards on this motherboard. For some reason Firefox HTML 5 requires Card 0 of the snd_hda_intel no matter how you change it with asoundrc. You can make flash work using the asoundrc file, but HTML 5 audio is silent.

You must remap the PCM device in Linux as card 0 and remove the changes to asoundrc that were added to make Flash work. This wiki page indicates that you should compile the snd_hda_intel driver into the kernel. Using this configuration you must remap the card 0 and card 1 devices using boot parameters instead of a file. For example the following kernel command line option will swap the MID and PCM card indicies so that the default card 0 is the PCM card:

Laptops with HDMI audio output
Some laptops with an HDMI audio output will map /proc/asound/card0 as HDMI and therefore makes it default output device for applications as stated above in the Firefox section. Another way to remap is to add these two lines to

options snd cards_limit=2
 * 1) Set this to the correct number of cards.

options snd-hda-intel id=PCH index=0 options snd-hda-intel id=HDMI index=1

Verify the order change by checking this command

0 [PCH           ]: HDA-Intel - HDA Intel PCH HDA Intel PCH at 0xf2534000 irq 45 1 [HDMI          ]: HDA-Intel - HDA Intel HDMI HDA Intel HDMI at 0xf2530000 irq 46

This method only works if snd-hda-intel is compiled as a module not built-in

IPC semaphore - function not implemented
If you try to run the test procedure, and speaker-test returns an error about a function IPC semphore not implemented, that is the option to activate in your Kernel :

Headphones jack not working
Sometimes to get headset jack working, additional model information is needed for audio driver. For example, in case of Dell Latitude E7470 laptop with snd-hda-intel driver, this needs to be added to :

options snd-hda-intel model=headset-mic

More setup information can be found in Linux kernel documentation.

External resources

 * http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html - Official ALSA PCM (digital audio) plugin descriptions and configuration file examples.