How can I record audio?

I have tried to record audio in many different ways. I have tried recording with obs-studio, simplescreenrecorder, and via ffmpeg. None of these methods work. Usually I tried to record from /dev/dsp3 but I also tried to record from /dev/dsp4 and all other outputs, but nothing works. Playing sound works very well, it even sounds better than on macos, windows and linux (alsa). So it's only with recording that I have a problem.


mixer
Code:
Mixer vol      is currently set to 100:100
Mixer pcm      is currently set to 100:100
Mixer speaker  is currently set to 100:100
Mixer line     is currently set to   0:0
Mixer mic      is currently set to   0:0
Mixer mix      is currently set to   0:0
Mixer rec      is currently set to 100:100
Mixer igain    is currently set to 100:100
Mixer ogain    is currently set to 100:100
Recording source: speaker


dmesg | egrep "hda|pcm"
Code:
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1
hdac0: <NVIDIA (0x0fb9) HDA Controller> mem 0xf7080000-0xf7083fff irq 17 at device 0.1 on pci1
hdac1: <Intel Cougar Point HDA Controller> mem 0xf7800000-0xf7803fff irq 22 at device 27.0 on pci0
hdacc0: <NVIDIA (0x0080) HDA CODEC> at cad 0 on hdac0
hdaa0: <NVIDIA (0x0080) Audio Function Group> at nid 1 on hdacc0
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 5 on hdaa0
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> at nid 6 on hdaa0
hdacc1: <Realtek ALC887 HDA CODEC> at cad 0 on hdac1
hdaa1: <Realtek ALC887 Audio Function Group> at nid 1 on hdacc1
pcm3: <Realtek ALC887 (Rear Analog)> at nid 20 and 24,26 on hdaa1
pcm4: <Realtek ALC887 (Front Analog)> at nid 27 and 25 on hdaa1


cat /dev/sndstat
Code:
Installed devices:
pcm0: <NVIDIA (0x0080) (HDMI/DP 8ch)> (play)
pcm1: <NVIDIA (0x0080) (HDMI/DP 8ch)> (play)
pcm2: <NVIDIA (0x0080) (HDMI/DP 8ch)> (play)
pcm3: <Realtek ALC887 (Rear Analog)> (play/rec) default
pcm4: <Realtek ALC887 (Front Analog)> (play/rec)
No devices installed from userspace.

Any help would be greatly appreciated.
 
In case you got a microphone pluged into the pink Mic rear connector for recording, do:

mixer mic 67:67
ffmpeg -thread_queue_size 1024 -f oss -i /dev/dsp3 test.m4a

In case you got a audio device plugged into the blue Line-in rear connector for recording, do:
mixer line 67:67
ffmpeg -thread_queue_size 1024 -f oss -i /dev/dsp3 test.m4a
 
In case you got a microphone pluged into the pink Mic rear connector for recording, do:

mixer mic 67:67
ffmpeg -thread_queue_size 1024 -f oss -i /dev/dsp3 test.m4a

In case you got a audio device plugged into the blue Line-in rear connector for recording, do:
mixer line 67:67
ffmpeg -thread_queue_size 1024 -f oss -i /dev/dsp3 test.m4a

In principle, nothing is connected to the blue and pink connections. My speakers are connected to the green connection.

I have followed your instructions and connected my micro to the pink connection, but it has recorded an audio file with no audio, although cmus played music. This was the output of the command: https://i.imgur.com/2omOad1.png
 
Long long ago I found a port named audio/xwave, which was tested to be basically able to do recording. That was not pursued further, and the port is now very outdated.
Then, recently I found a port called ardour, which actually appears to dwarf my 11" openreel machine and seems to almost play in the realm of the Digidesign desks... thats now postponed for study when I'm in the mood... It doesn't make things easier, it built upon just another layer of indirection.

There are two basic layers:
1. the "nid" stuff, done in /boot/device.hints. That configures the wiring between the physical plugs and the audio chip(s). There is a color code for the meaning of the physical plugs, but that doesn't mean much, because the wiring on the board can be arbitrarily changed.
2. The "dsp" stuff, controlled with the mixer command(s). That seems to control what the dsp(s) do with the signals. The mixer(8) command has switches to select which source to use for recording, see the rec and recsrc option.

Both may be more or less hardware dependent. Both are not perfectly good documented. It can be quite a session to systematically figure out what might work.
 
What's the exact use case?

OSS doesn't allow output capture. You need to either use a physical loop or try your luck with virtual_oss, which effectively emulates dsp devices in userspace.
 
Sometime ago in similar situation I used exmples from snd_hda() to get correct configuration.
If you have not yet looked at those, they might give some help.
 
audio/audacity is made for recording, but if other applications don't work, this may have the same issues. I'm not certain if audio/sox can record from the command line.
As others have mentioned, your mixer: mic, record or igain, settings need to be higher than 0, for it to record. Igain usually works at a setting of 1, while other variables need to be much higher. You may also have to use the microphone input in the back.

Code:
No devices installed from userspace.
I've seen this warning before, but don't remember about it. Installing something from ports seems to fix it, that gets installed with larger applications.
 
What's the exact use case?

Suppose I want to record both what I see on my screen and the audio that is played on my speakers. Simplescreenrecorder can normally do this, but on FreeBSD I am unable to record the audio played on my speakers in a file. The file being recorded has only video, not audio.

Sometime ago in similar situation I used exmples from snd_hda() to get correct configuration.
If you have not yet looked at those, they might give some help.
I also think that there may be the solution. By changing these types of settings: hint.hdac.0.cad0.nid21.config="as=2"
But I do not understand how it works.
I'm not certain if audio/sox can record from the command line.
As others have mentioned, your mixer: mic, record or igain, settings need to be higher than 0, for it to record. Igain usually works at a setting of 1, while other variables need to be much higher. You may also have to use the microphone input in the back.
I don't think my mixer settings are the problem.
I tried to record with sox via this command: sox -c 2 -r 32000 -t ossdsp /dev/dsp3 -t ossdsp -r 32000 /dev/dsp3
And then I get this output:
Code:
/dev/dsp3: (ossdsp)
 File Size: 0         
  Encoding: Signed PCM   
  Channels: 2 @ 16-bit   
Samplerate: 32000Hz     
Replaygain: off         
  Duration: unknown     
In:0.00% 00:03:14.05 [00:00:00.00] Out:6.21M [      |      ]        Clip:0
The ascending megabyte indication gives the impression that the sound is being recorded to a file but I cannot find this file anywhere.
 
Bash:
FreeBSD system76 15.1-RELEASE-p6 FreeBSD 15.1-RELEASE-p6 releng/15.1-n283643-95719d8a895b GENERIC amd64

I'm listening to sound on my headphones....so I know I have sound. Following the docs from here:


Bash:
root@system76 ~ $ dmesg | grep pcm
pcm0: <NVIDIA (0x00a7) (HDMI/DP 8ch)> at nid 4 on hdaa0
pcm1: <Realtek ALC245 (Internal Analog)> at nid 20 and 18 on hdaa1
pcm2: <Realtek ALC245 (Right Analog Headphones)> at nid 33 on hdaa1

Bash:
root@system76 ~ $ cat /dev/sndstat
Installed devices:
pcm0: <NVIDIA (0x00a7) (HDMI/DP 8ch)> (play)
pcm1: <Realtek ALC245 (Internal Analog)> (play/rec)
pcm2: <Realtek ALC245 (Right Analog Headphones)> (play) default
No devices installed from userspace.

What does this mean? Why does pcm0 only play whereas pcm1 can play or record? I get that pcm0 is my video card...so does this mean that card cannot record? Ok... then what's the difference between pcm1 and 2? What is internal analog and it can play and record? Can it only play record on the physical audio jack on my laptop or does this refer to something else internal? Why does pcm2 say it is 'Right Analog Headphones'? Is there a 'Left Analog Headphones'? Why is it default? What does no devices installed from userspace imply? I understand what it says, but what's the context, what are the ramifications?


beep works
Bash:
root@system76 ~ $ beep

The next set of instructions are not clear (to me anyway):
"""
The graphics card (NVIDIA®) has been enumerated before the sound card (Realtek®), with the sound card appearing as pcm4.The system can be configured to use the sound card as the default device by executing the following command:
# sysctl hw.snd.default_unit=4
"""

What does this mean? Do I want the sound from my graphics card or my sound card? What's the difference? When is one better than the other?

The next set of instructions are also not clean (again, to me...):
"""

9.2.4. Automatically Switching to Headphones​

Some systems may struggle with switching between audio outputs, but fortunately FreeBSD allows automatic switchover to be configured in device.hints.
Identify how the system is enumerating the audio outputs by executing the following command:
% dmesg | grep pcm

The output looks something like this:
pcm0: <Realtek ALC892 Analog> at nid 23 and 26 on hdaa0
pcm1: <Realtek ALC892 Right Analog Headphones> at nid 22 on hdaa0
Add the following lines to /boot/device.hints:
hint.hdac.0.cad0.nid22.config="as=1 seq=15 device=Headphones"
hint.hdac.0.cad0.nid26.config="as=2 seq=0 device=speakers"
"""
There's some real magic going on here, and I recall (but cannot find) a blog describing how to decipher the dmesg output into the device.hints.

Sound, in general and recording in specific, looks like a tarpit with footguns floating in it.

Anyway, thx for reading. If y'all have sound & recording working, then please post your magic.
 
Code:
/etc/rc.conf

change dsp1 to your device if need

Code:
#=========================================================================#
# virtual oss
#=========================================================================#

virtual_oss_enable="YES"
virtual_oss_configs="dsp"
virtual_oss_dsp="-S -Q 0 -C 2 -c 2 -r 48000 -b 16 -s 1024 -P /dev/dsp1 -R /dev/null -w vdsp.wav -l dsp -t vdsp.ctl"

reboot if needed

install sox

Code:
doas pkg install sox

create a script called redord-desktop-audio
with the following code

Code:
#!/bin/sh

# record desktop audio with sox and virtual oss

sox -r 48k -b 16 -v 0.99 -t oss /dev/dsp -t wav "sox-$(date +%Y-%m-%d-%H-%M-%S).wav"

make it executable

Code:
chmod +x record-desktop-audio

move the script to your ~/bin directory if you have one

Code:
mv record-desktop-audio ~/bin

or /usr/local/bin

Code:
doas mv record-desktop-audio /usr/local/bin

run the script

Code:
record-desktop-audio

ctrl c to quit
 
Thank you NapoleonWils0n . This almost works. I've tried dsp0, dsp1, and dsp2. I restarted the virtual_oss service in between each change to /etc/rc.conf and then verified the dsp that service was using. After recording each test, I opened the resulting .wav file in audacity and tried to play it. In each case, nothing was actually recorded.

Here's an example where I'm trying /dev/dsp0 (I tried /dev/dsp[0|1|2])

Bash:
# rc.conf
virtual_oss_enable="YES"
virtual_oss_configs="dsp"
virtual_oss_dsp="-S -Q 0 -C 2 -c 2 -r 48000 -b 16 -s 1024 -P /dev/dsp0 -R /dev/null -w vdsp.wav -l dsp -t vdsp.ctl"

Bash:
toddg@system76 ~/music/kexp $ ps aux|grep virtual
root       12612    0.0  0.0    17384    6476  -  Ss   08:06      0:00.03 /usr/sbin/virtual_oss -B -D /var/run/virtual_oss/dsp.pid -S -Q 0 -C 2 -c 2 -r 48000 -b 16 -s 1024 -P /dev/dsp0 -R /dev/null -w vdsp.wav -l dsp -t vdsp.ctl
toddg      12617    0.0  0.0    14180    2696  4  S+   08:06      0:00.00 grep virtual

toddg@system76 ~/music/kexp $ record-audio.sh TEST03

Input File     : '/dev/dsp' (ossdsp)
Channels       : 2
Sample Rate    : 48000
Precision      : 16-bit
Sample Encoding: 16-bit Signed Integer PCM
Level adjust   : 0.99 (linear gain)

In:0.00% 00:00:08.87 [00:00:00.00] Out:422k  [      |      ]        Clip:0    ^C
Aborted.

I modified your script to take a param:

Bash:
toddg@system76 ~ $ cat ~/bin/record-audio.sh
#!/bin/sh
# record desktop audio with sox and virtual oss
NAME=$1
sox -r 48k -b 16 -v 0.99 -t oss /dev/dsp -t wav "${NAME}-$(date +%Y-%m-%d-%H-%M-%S).wav"
 
hi mate

the -P option is just the playback device

have you got cuse set up

add cuse_load="YES" loader.conf

Code:
sudo vi /boot/loader.conf

Code:
cuse_load="YES"

reboot

you can also change the audio playback like this

Change the default device to pcm3,
and hot-swap to it for playback in virtual_oss(8):

Code:
mixer -d pcm3 -V /dev/vdsp.ctl:play

check your audio device

Code:
cat /dev/sndstat

Code:
Installed devices:
pcm0: <Realtek ALC298 (Internal Analog)> (play/rec)
pcm1: <Realtek ALC298 (Left Analog Headphones)> (play) default
pcm2: <Intel Kaby Lake (HDMI/DP 8ch)> (play)
pcm3: <BLUE MICROPHONE Blue Snowball> (rec)
Installed devices from userspace:
dsp: <virtual_oss device> (play/rec)

and make sure you are trying to capture sndiod audio and not pulse audio
 
Back
Top