how to switch audio output between the speakers and earphone jack on freebsd

this info was gathered on:

$ date +%m/%d/%Y
$ uname -smr
FreeBSD 11.1-RELEASE-p1 amd64

get system audio information:

$ sysctl hw.snd
hw.snd.maxautovchans: 16
hw.snd.default_unit: 2
hw.snd.version: 2009061500/amd64
hw.snd.default_auto: 1
hw.snd.verbose: 0
hw.snd.vpc_mixer_bypass: 1
hw.snd.feeder_rate_quality: 1
hw.snd.feeder_rate_round: 25
hw.snd.feeder_rate_max: 2016000
hw.snd.feeder_rate_min: 1
hw.snd.feeder_rate_polyphase_max: 183040
hw.snd.feeder_rate_presets: 100:8:0.85 100:36:0.92 100:164:0.97
hw.snd.feeder_eq_exact_rate: 0
hw.snd.feeder_eq_presets: PEQ:16000,0.2500,62,0.2500:-9,9,1.0:44100,48000,88200,96000,176400,192000
hw.snd.basename_clone: 1
hw.snd.compat_linux_mmap: 0
hw.snd.syncdelay: -1
hw.snd.usefrags: 0
hw.snd.vpc_reset: 0
hw.snd.vpc_0db: 45
hw.snd.vpc_autoreset: 1
hw.snd.timeout: 5
hw.snd.latency_profile: 1
hw.snd.latency: 5
hw.snd.report_soft_matrix: 1
hw.snd.report_soft_formats: 1

$ cat /dev/sndstat
Installed devices:
pcm0: <NVIDIA (0x001c) (HDMI/DP 8ch)> (play)
pcm1: <NVIDIA (0x001c) (HDMI/DP 8ch)> (play)
pcm2: <Realtek ALC662 (Rear Analog)> (play/rec) default
pcm3: <Realtek ALC662 (Front Analog)> (play/rec)

$ egrep '^(hda|pcm)' /var/run/dmesg.boot
hdac0: <NVIDIA GF119 HDA Controller> mem 0xdf080000-0xdf083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Sunrise Point HDA Controller> mem 0xdf320000-0xdf323fff,0xdf300000-0xdf30ffff irq 16 at device 31.3 on pci0
hdacc0: <NVIDIA (0x001c) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x001c) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x001c) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x001c) (HDMI/DP 8ch)> at nid 5 on hdaa0
hdacc1: <Realtek ALC662 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC662 Audio Function Group> at nid 1 on hdacc1
pcm2: <Realtek ALC662 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm3: <Realtek ALC662 (Front Analog)> at nid 27 and 25 on hdaa1

output is currently speakers. get default sound output:

sysctl hw.snd.default_unit
hw.snd.default_unit: 1

change to earphones:

sysctl hw.snd.default_unit=2
hw.snd.default_unit: 1 -> 2

that’s all folks!
Sun Nov 12 17:52:17 PST 2017