Pulseaudio: "Record what you hear"

Moderator: Forum moderators

Post Reply
User avatar
fredx181
Posts: 3110
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 378 times
Been thanked: 1329 times
Contact:

Pulseaudio: "Record what you hear"

Post by fredx181 »

How to capture with alsa is here discussed: https://forum.puppylinux.com/viewtopic.php?t=6083
This is for when running the system with PulseAudio, run below script from terminal to capture "what you hear", a file "record.mp3" @192 will be created (depends on lame) :

Code: Select all

#!/bin/bash

SoundFile=~/record.mp3
#rm -f "$SoundFile"  # remove previous (uncomment) or not
trap 'killall parec' EXIT

# Get sink monitor:
MONITOR=$(pactl list | egrep -A2 '^(\*\*\* )?Source #' | grep 'Name: alsa.*\.monitor$' | awk '{print $NF}' | tail -n1)
echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null

echo "Start the audio player ..."
read -p "Press Enter to start capturing: "
echo "Stop with Ctrl-C ..."
echo "Recording..."
parec -d "$MONITOR" | lame -r -q 3 --lowpass 17 --abr 192 - "$SoundFile"
User avatar
fredx181
Posts: 3110
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 378 times
Been thanked: 1329 times
Contact:

Re: Pulseaudio: "Record what you hear"

Post by fredx181 »

Some newer Puppy builds now use "pipewire", goes for e.g. BookwormPup64 (tested script from previous post and didn't work, fix below).
For pipewire the command "pw-record" should be used for recording.
This script will detect if pipewire is in use, and will make use of pw-record then (but if just pulseaudio is running it will use 'parec' for recording):

Code: Select all

#!/bin/bash

SoundFile=~/record.mp3
trap 'killall parec 2> /dev/null; killall pw-record 2> /dev/null' SIGINT

if [ -z "$(pidof pulseaudio)" ] && [ -z "$(pidof pipewire)" ]; then
    echo "Pulseaudio or Pipewire not running, exiting..."
    sleep 3
exit
fi

# Get sink monitor:
if [ -n "$(pidof pulseaudio)" -a  -z "$(pidof pipewire)" ]; then    # pulseauio running
MONITOR=$(pactl list short sources | grep 'alsa.*\.monitor' | awk '{print $2}')
PA=yes
echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null
elif [ -z "$(pidof pulseaudio)" -a  -n "$(pidof pipewire)" ]; then    # pipewire running
MONITOR=$(pactl list short sources | grep 'alsa.*\.monitor' | awk '{print $1}')
fi

echo "Start the audio player ..."
read -p "Press Enter to start capturing: "
echo "Stop with Ctrl-C ..."
echo "Recording..."

if [ "$PA" = "yes" ]; then  # pulseaudio
parec -d "$MONITOR" | lame -r -q 3 --lowpass 17 --abr 192 - "$SoundFile"
else  # pipewire
pw-record --target="$MONITOR" - | lame -r -s 48  -q 3 --lowpass 17 --abr 192 - "$SoundFile"
fi

EDIT: Had to install "lame" on BookwormPup64 apt install lame .

Post Reply

Return to “Tips & Tweaks”