Server-less way to record what you hear
Preamble
This is an instruction on how to record the sound that you hear from your own speaker, using ALSA, without using any audio server.
If you already run a audio-server (pulseaudio, pipewire, jack, aloopd, alsa-loop, etc), leave now and read the documentation of your audio server. The information you see here will not help you.
Before you begin
ALSA is a very sophisticated sound system, found in Linux.
Most if end-user components can be configured using ALSA configuration files, located either in /etc/asound.conf
and/or $HOME/.asoundrc
(that would be /root/.asoundrc
if you're running as root, as most puppies are).
The file $HOME/.asoundrc
takes precedence over what you put in /etc/asound.conf
, so if you edit /etc/asound.conf
and it doesn't seem to take any effect, please check that if you have $HOME/.asoundrc
that overrides it. Most Puppy utilities works by editing /etc/asound.conf
but please be aware that the same instructions works as well if you work with $HOME/.asoundrc
instead.
Principle of operation
It's very simple actually. You configure ALSA to do the followig:
1. Configure ALSA to split the sound data into two streams.
2. Configure one of the streams to go into the actual soundcard, so you hear the sound.
3. Configure the other one to go into a virtual loopback device.
4. The virtual loopback device has two ends - whatever comes into one end, goes out from the other.
5. You record from the other end of this virtual loopback.
Code: Select all
|---> soundcard
Sound data -->|
|---> virtual loopback ---> RECORDER program.
Requirement
1. The virtual loopback device comes from the kernel module called snd-aloop
. To be able to use this, your kernel must support this module, and this module must be loaded. To do this, open terminal and run modprobe snd-aloop
; if you don't get an error, you're good to go. It is good to notice that most kernels supports snd-aloop out of the box.
2. That's it. You don't need any other binary or program. You don't even need "alsa-loop" program.
The configuration files
Here is the working configuration file that output sounds to hw:0,0
(the first soundcard in your system), and allows you to record from a "mixout
" PCM device.
Code: Select all
pcm.!default fdplug
ctl.!default { type hw card 0 }
pcm.fdplug {
type plug
slave {
pcm fdasym
}
}
pcm.fdasym {
type asym
playback.pcm {
@func getenv
vars [ ALSA_PCMOUT ]
default fdmixout
}
capture.pcm {
@func getenv
vars [ ALSA_PCMIN ]
default fdhw
}
}
pcm.fdmixoutdmix {
type dmix
ipc_key 11793603
ipc_gid audio
ipc_perm 0660
slave {
pcm "hw:Loopback,0,7"
period_time 0
period_size 2048
buffer_size 32768
}
}
pcm.fdmixout {
type plug
route_policy "duplicate"
slave.pcm {
type multi
slaves.a.pcm fddmix
slaves.a.channels 2
slaves.b.pcm "fdmixoutdmix"
slaves.b.channels 2
bindings.0.slave a
bindings.0.channel 0
bindings.1.slave a
bindings.1.channel 1
bindings.2.slave b
bindings.2.channel 0
bindings.3.slave b
bindings.3.channel 1
}
}
pcm.mixout "plughw:Loopback,1,7"
pcm.fddmix {
type dmix
ipc_key 9957191
ipc_gid audio
ipc_perm 0660
slave {
pcm fdhw
period_time 0
period_size 2048
buffer_size 32768
}
}
pcm.fdhw { type hw card 0 device 0 }
If you need to change the output (not to hw:0,0
but for example, to hw:3,0
), you only need to edit and change the above configuration in two places:
a) Change pcm.fdhw { type hw card 0 device 0 }
to pcm.fdhw { type hw card 3 device 0 }
and
b) Change ctl.!default { type hw card 0 }
to ctl.!default { type hw card 3 }
How to use this?
Copy and paste the above configuration file to a text editor, and save the file into either /etc/asound.conf
or $HOME/.asoundrc
(edit as necessary if you're not outputting to hw:0,0).
That's it!
After you have done that:
1. Start playing whatever sound source you want to play (live audio stream, youtube, etc), and
2. Activate your recorded program and tell it to record from "mixout" device.
To clarify step (2), here are some examples.
A. Use "arecord" program to display the sound-level input from the "mixout" device. If the "mixout" device records that you hear, you should see a level-meter which roughly correspond to the music/audio that you hear.
To do this:
- open terminal
- run arecord -vvv -D mixout > /dev/null
- when done, press Ctrl-C to stop it.
B. Use "ffmpeg" to record the sound to an MP3 file
To do this:
- open terminal
- run ffmpeg -f alsa -i mixout /tmp/test.mp3
- when done, press Ctrl-C to stop it.
C. Use "mhhwaveedit" to record the sound to the editor
To do this:
- Launch mhwaveedit.
- Choose "Edit" menu entry, then "Preferences", the click the "Sound" tab, and click the "Settings" button next to ALSA, and change the "recording device" from "default" to "mixout". Then close the preference window.
- Click the "record" button. A VU meter should show up, and the meters should move corresponding to the music/audio that you hear.
- Click "Start recording" once you want to record it.
D. Other programs
All other bonafide recording programs would have some sort of settings to do this to the specify the recording source. Specify "mixout" in this case.
That's it!
Limitations and restrictions, and Q&A
1. The above configuration only works for stereo output. For any other formats (mono, 5.1 surround, 7.1. surround), the principle is the same, but you will need to edit the "bindings" section of the "duplicate" section of the block to bind the appropriate number of streams.
2. There can only be one recorder program in use. You cannot record using mhwaveedit and ffmpeg at the same time (or any other programs for that matter).
3. The configuration file can be left as is and it will continue to work (without any appreciable overhead) even if you are not recording.
What does it mean? It means, if you leave the configuration as it is, you can listen to your favourite radio; and if you want to record it, you can just launch your favorite recording program and click "record". There is no need to start/stop/re-start your radio program. The "mixout" function is always available and on-standby for when you need it; but it will not have any side effects if you are not recording anything - you will still continue to hear the audio.
This settings persists across reboots until you modify the configuration file again.
4. Q: I tried this and it doesn't work! Now I hear nothing at all!
A: Easy. Just delete the configuration file, and things should go back to normal.
5. Q: This is a bit difficult for me, is there a program that automate all this stuff?
A: Yes, the ALSA configuration snippet you see comes from Fatdog's "Set Default Soundcard" program (which is the equivalent / outgrowth of what Puppy's MSCW). With it you can configure this stuff and a lot more. Can this program be used in Puppy, or is it specific to Fatdog? Yes, it can be used in Puppy too. Bother your favorite puppy developer to get it included. Don't bother me, please.
Finally
Good luck and enjoy.