Chapter 10. Audio

Table of Contents

10.1. Configuring the default audio device
10.2. Configuring the mixer and volume
10.2.1. Setting default mixer settings on boot
10.3. Pseudo audio devices
10.4. Recording and playback commands
10.4.1. audioplay(1)
10.4.2. audiorecord(1)
10.4.3. audioctl(1)
10.5. MIDI support
10.5.1. midirecord(1)
10.5.2. midiplay(1)
10.6. Intel HD Audio devices
10.6.1. Built-in and jacks: DACs/ADCs
10.6.2. HDMI/DisplayPort audio

10.1. Configuring the default audio device

audiocfg(1) can be used to list, test and set default audio devices.

All available audio devices can be listed with audiocfg list:

$ audiocfg list
0: [*] audio0 @ hdafg0: Realtek ALC292
       playback: 2ch, 48000Hz
       record:   2ch, 48000Hz
       (PR) slinear_le 16/16, 2ch, { 32000, 44100, 48000, 88200, 96000, 192000 }
       (PR) slinear_le 20/32, 2ch, { 32000, 44100, 48000, 88200, 96000, 192000 }
       (PR) slinear_le 24/32, 2ch, { 32000, 44100, 48000, 88200, 96000, 192000 }
       (  ) ac3 16/16, 2ch, { 32000, 44100, 48000, 88200, 96000, 192000 }
1: [ ] audio1 @ uaudio0: USB audio
       playback: 2ch, 48000Hz
       record:   1ch, 48000Hz
       (P-) slinear_le 16/16, 2ch, { 48000, 44100 }
       (-R) slinear_le 16/16, 1ch, { 48000, 44100 }

The asterisk next to the Realtek ALC292 device indicates it is currently the default device, so if any application writes or reads to /dev/audio it will play or record from it. It is also available as /dev/audio0, and for mixer commands, /dev/mixer0.

The other device, USB audio, is a secondary device that has been plugged in. Since it isn't the default, it is only used if specifically selected in an application. It is available as /dev/audio1, and for mixer commands, /dev/mixer1.

The playback: and record: rows indicate the currently selected hardware audio format. Below this, the other supported formats are listed. Some devices set the playback and recording formats separately, while others set both at the same time. This is indicated by PR.

audiocfg test index can be used to test playback, and plays a tone of 2 seconds for each channel of the index device:

$ audiocfg test 0
0: [*] audio0 @ hdafg0: Realtek ALC292
  testing channel 0... done
  testing channel 1... done

If more than one audio device is available, audiocfg default index can be used to change the default one. This persists between reboots. Please note that unlike other audiocfg(1) commands, audiocfg default needs to be run as root.

10.2. Configuring the mixer and volume

In NetBSD, mixerctl(1) is used to adjust audio mixing, e.g. volume for recording and playback, and the sources and sinks currently in use.

Set the current playback volume:

$ mixerctl -w outputs.master=50

List the available controls and settings:

$ mixerctl -av
outputs.master=255,255 volume
inputs.dac=255,255 volume
outputs.auto=255,255 volume delta=13
outputs.headphones=0,0 volume delta=13
outputs.hdmi=255,255 volume delta=13
outputs.select=headphones  [ auto headphones hdmi ]

Secondary devices can also be configured using mixerctl(1). For example, if you've just plugged in an USB audio device, it may have attached as /dev/audio1 and /dev/mixer1 - this is visible using audiocfg(1). You would therefore configure it with mixerctl -d /dev/mixer1.

10.2.1. Setting default mixer settings on boot

Default mixer device settings can be applied on boot by setting mixerctl=YES in /etc/rc.conf, then providing arguments in /etc/mixerctl.conf. For example, this /etc/mixerctl.conf sets the playback volume and playback sink:

outputs.master=120,120
outputs.select=headphones

To automatically load and save the settings of mixer devices on boot and shutdown, you can specify each device to save individually in /etc/rc.conf:

mixerctl=YES
mixerctl_mixers="mixer0 mixer1"

10.3. Pseudo audio devices

NetBSD's pad(4) device allows feeding back data from an application using a virtual audio device. It can be used to redirect playback elsewhere, or record an application's playback.

/dev/padN devices produce a raw stream of audio in a fixed format when opened for reading. At the time of opening, they also create a /dev/audioN device for an application to use for output. You can observe the device creation happening with dmesg(8).

The following example records the output of a game, games/jumpnbump, using the program multimedia/ffmpeg4 for encoding the data from the pad device to a file and writing it back to the real audio device simultaneously. Both are available from the NetBSD Packages Collection.

$ ffmpeg4 -f s16le -ar 44100 -ac 2 -i /dev/pad0 \
    -f wav output.wav -f oss /dev/audio
$ SDL_PATH_DSP=/dev/audio1 jumpnbump

10.4. Recording and playback commands

NetBSD comes with a number of commands that allow users to play and record audio from scripts or the command-line interface.

10.4.1. audioplay(1)

With this command you can play audio files in simple formats like ULAW and WAVE. For more sophisticated needs you might want to install one of the many programs available in the package system which let you play audio files in different formats (e.g. MP3, etc.)

10.4.2. audiorecord(1)

Allows recording audio from a microphone or other input to the same simple or raw formats that audioplay(1) supports.

The following command records CD quality audio to a WAVE file from the default audio device. Recording will stop when the process is terminated:

$ audiorecord -d /dev/audio -F wav -e linear -c 2 -P 16 -s 44100 recording.wav

Play the recording back (its format is inferred from the WAVE headers):

$ audioplay recording.wav

10.4.3. audioctl(1)

audioctl(1) is used to manually set some variables regarding audio I/O, like the frequencies for playing and recording. This is useful when writing raw samples to /dev/sound without access to the full audio(4) API, e.g. from a shell script, but otherwise is not used during regular operation.

10.5. MIDI support

NetBSD includes built-in MIDI support through the machine-independent midi(4) system. This includes support for USB MIDI devices.

Access to MIDI devices is supported through raw access to the /dev/rmidiX devices, or through the sequencer device, /dev/music.

Digital Audio Workstations and other software with support for NetBSD MIDI in the Packages Collection include audio/lmms and audio/fluidsynth. Several MIDI programs are also included with NetBSD:

10.5.1. midirecord(1)

A program that allows recording MIDI events from a device to files in the Standard MIDI (SMF) format. It can also be used to test a device and verify it works as expected with the -D and -V options.

10.5.2. midiplay(1)

A program that allows playing Standard MIDI and RMID files to the MIDI sequencer device.

10.6. Intel HD Audio devices

Since the 2010s, most x86 machines have hardware compliant with the Intel HD Audio specification. These use NetBSD's hdaudio(4) driver and require some special consideration.

10.6.1. Built-in and jacks: DACs/ADCs

For hdaudio(4) devices, the currently selected playback ports (or, e.g. internal speaker and headphone jack on a laptop) are controlled by selecting a DAC/ADC. The available DACs and ADCs can be seen in /var/run/dmesg.boot:

hdafg0 at hdaudio1: Realtek ALC292
hdafg0: DAC00 2ch: Speaker [Built-In], HP Out [Jack]
hdafg0: ADC01 2ch: Mic In [Jack]
hdafg0: ADC02 2ch: Mic In [Built-In]

Therefore, to use only the built-in mic for recording:

$ mixerctl -w record.source=ADC02

Use all available sources:

$ mixerctl -w record.source=ADC01,ADC02

Some laptops may need outputs.dacsel changed to only play audio from the headphone jack, others have hardware speaker mute and there's no need for this.

10.6.2. HDMI/DisplayPort audio

Currently, HDMI/DisplayPort output with hdaudio(4) is not enabled in the default kernel because it interferes with default device selection. This can be changed by enabling these options in the kernel configuration, and rebuilding. See Chapter 33, Compiling the kernel for more information.

options HDAUDIO_ENABLE_DISPLAYPORT
options HDAUDIO_ENABLE_HDMI

With other drivers (e.g. on ARM boards) this is not necessary.