Author Topic: Playtime with Pulse Audio  (Read 5348 times)

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Playtime with Pulse Audio
« on: May 05, 2012, 08:04:20 AM »
I am a bit of a gadget freak ...  I like to buy really cheap gadgets and see how they play with Linux. As a consequence I have various devices through which audio is sent, and I find Pulse Audio the ideal means of controlling and playing with sound streams etc.

I recently resurrected a USB Telbox for instance, and now have it working again - the first time since moving to KDE4.
This device is an ATA which has an input for a land line and a USB connection to the PC.
Calls from Skype - through USB - and the landline are routed to a phone handset plugged into the device.
In fact I have replaced the landline with a SIP VOIP line, so now have both SIP and Skype available on the one phone handset.

Anyway, while playing with this, I wondered about recording calls ....  yes I know this is not legal without notification to the other party in some locales.

I set about attempting this today using PA ....  and had success.

This post is not really about the above ramble, but more about what follows .......  how I set up PA to record not only one sound stream but a combination of two streams, into the one file.
Note that I only used two streams, but PA is not limited to two .....  have fun and record as many streams as you have devices ....  all into the one sound file  :D

***

To combine sound streams, first we create a virtual sound sink (a sink could be speakers or any other device which accepts the sound stream).
This virtual sink is where we will send our combined sound stream.

Next we create a loopback device for each of the streams we wish to include ....  in this case two.

After the loopback devices are created, we assign a stream to each ....  in my case above it would be the phone MIC to one and the phone Earpiece to the other.

So now we have both sound streams assigned to loopback devices which in turn are linked to the Null Sink.

While a call is in progress, we launch our favourite recording application .....  I mostly use gnome-recorder from the gnome-media package.

Start the application, and in the PulseAudio Volume Control Recording Tab, set the application to record from   Monitor of Null Sink

The resulting audio file will have both sound streams mixed together.
For best results you will likely have to alter the relative sound volumes of the two streams to get the best recording.

That's it!

When using Pulse Audio Control please ensure that on each tab (at the bottom) is
show:  All  --- Streams/Output Devices/Input Devices/whatever is applicable to each tab.


Oh yeah ....  the commands to achieve this ......

To set up the Null Sink use the following command

pactl load-module module-null-sink sink_name=<any name you wish>

Code: [Select]
[user@XPS ~]$ pactl load-module module-null-sink sink_name=MySink
31
[user@XPS ~]$

This will return an ID number .....  something like 23 maybe, or in the above case 31

If you check you will also see a new entry in the Output Devices Tab of PA Vol Control  called Null Output, as well as a new entry on the Input Devices Tab called Monitor of Null Output.

Your new Null Sink is set up!

The next command creates a loopback device, so I use it twice for the two I want

pactl load-module module-loopback sink=<previously chosen name>

Code: [Select]
[user@XPS ~]$ pactl load-module module-loopback sink=MySink
32
[user@XPS ~]$ pactl load-module module-loopback sink=MySink
33
[user@XPS ~]$

On the recording tab there are now two new Loopback Devices ......  and a button to select to decide what stream they should connect to.
I connect one to "Monitor of VOIP Phone Analogue Mono" and the other to "VOIP Phone Analogue Mono". The first being the earpiece and the second the MIC.

The loopback devices are now set up and ready for use.
When your preferred app is launched to record, it will also appear on the Recording Tab with a selection button, from where you choose to record from Monitor of Null Sink.

To clean up after play, it might be nice to delete the temporary sink and loopback devices. To do this use the following command

pactl unload-module <id number>

In my case it was the following


Code: [Select]
[user@XPS ~]$ pactl unload-module 31
[user@XPS ~]$ pactl unload-module 32
[user@XPS ~]$ pactl unload-module 33
[user@XPS ~]$

So the terminal I used looks like this after I was finished

Code: [Select]
user@XPS ~]$ pactl load-module module-null-sink sink_name=MySink
31
[user@XPS ~]$ pactl load-module module-loopback sink=MySink
32
[user@XPS ~]$ pactl load-module module-loopback sink=MySink
33
[user@XPS ~]$ pactl unload-module 31
[user@XPS ~]$ pactl unload-module 32
[user@XPS ~]$ pactl unload-module 33
[user@XPS ~]$

The above is of course just a brief visit to this subject, and shows a temporary method of creating sinks and loopback devices and deleting them again after use.

You can if you wish, by editing a config file, make them permanent and available at all times, should you have the need.

OK, that enough writing for now.

Go play!
« Last Edit: May 05, 2012, 08:05:54 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #1 on: May 05, 2012, 08:12:20 AM »
This is a simple recorder making use of Pulse Audio.

It can record from any device in your system.

It is by no means finalised ......  only something to give an idea of how to record sound streams when there are multiple devices attached.

Have a play with it!

:D

Code: [Select]
#!/bin/bash

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# 3rd Dec. 2011
# clareoldie@gmail.com GPLv2 or later
# Depends on Pulse Audio being installed and properly working. In addition it makes use of sox and Zenity.

set -x

set_variables () {
##### set some variables #######
mypid=$$                        # PID of this script
TITLE="Sound Recording"
ICON="/usr/share/icons/default.kde4/64x64/devices/audio-input-microphone.png"
SoundFile=""
# The script can be run from the command line with the target file as an option.
# Set SoundFile to the file option - if given.
SoundFile="$1"

}

Shut-Down () {
sleep 2              # This to allow sox to save whatever it may have in cache or such
kill $(pgrep parec)
kill $mypid
}

EXIT () {
if [ $? == 1 ]
then
exit
fi
}

INTRO () {
# What this utility is supposed to do!
 zenity  --info --no-wrap --window-icon=$ICON --title="                   $TITLE                 " --text=$"
                                                 <b>Please Read the following Information Carefully</b>                          


This utility allows you to record audio from any of the available devices managed by Pulse Audio (Pulse Audio must be installed).  

You can either select an existing file or name a new file to record to. The file must be one of .wav, .mp3 or .flac, and its      

<b>extension type must be given as part of the file name</b>.

For best results it is recommended that you record in <b>wav</b> and when satisfied recode the file to whichever format you desire.                  


You will be able to select the source of the audio you wish to record from. The devices are made available through Pulse

Audio. Please read carefully when selecting the device, as very similar names exist for input (microphone) and output (speakers).

"
EXIT
}

get_file () {
# Ask the user for the filename and extension  ... wav, mp3, flac ...
while [ -z "$SoundFile" ]
do
SoundFile=$(zenity --window-icon=$ICON --file-selection --save --confirm-overwrite --filename=~/ --file-filter="*.wav *.mp3 *.flac" --title "  Select or enter a file to record to  ")

EXIT

# Check for the existence of a correct extension. If the check fails set $SoundFile=""
Extension=$(basename "$SoundFile" |  cut -d . -f2)
if [ "$Extension" != "wav" ] &&  [ "$Extension" != "mp3" ] && [ "$Extension" != "flac" ]
then
SoundFile=""
fi
done
# Delete the existing file if it exists to give a clean start.
rm -f "$SoundFile"
}

get_source () {
# Get source for recording:
MONITOR=""
#
while [ -z $MONITOR ]
do
MONITOR=$(pactl list | grep -A3 -E "Source #" | grep -A1 -E "Name: " | cut -d ' ' -f2-30 | zenity --window-icon=$ICON --list --separator "--" --title="    $TITLE    " --text=$"                    <b>Select the source you wish to record</b>    \n    Please select the sound source you wish to record from    
"  --height=320 --width=450 --hide-column=1,3  --column  $"Sound Source Name" --column $"Sound Source Description" --column "separator")
EXIT # see function of this name
done
# Ensure the source is not muted ..... not yet properly checked!
echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null
}

record_file () {
# Zenity progress wrapper
(
        echo
parec -d "$MONITOR" | sox -V3 -t raw --norm -r 44.1k -sLb 16 -c 2 - "$SoundFile" &
) |
zenity --window-icon=$ICON --progress --pulsate --title="   $TITLE   " --text $"  
<b>Recording</b>  

 Sound is being recorded to the  <b>$SoundFile</b>  file.

                  Press <b>Cancel</b>  to stop recording." ;
Shut-Down
}

main_prog () {
set_variables
INTRO
get_file
get_source
record_file
}

#######

main_prog

exit 0
« Last Edit: May 05, 2012, 08:14:18 AM by Just18 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #2 on: May 05, 2012, 10:22:05 AM »
I meant to ask .......  if anyone tries that recording script please drop a post back about your experience. 

Thanks  ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #3 on: May 05, 2012, 01:47:35 PM »
Oops ......  we got moved  :D

For anyone following, this is a pic of the Configuration Tab of PA Vol Control in my set up, with a few audio capable devices attached ........




....  and this is a pic of the recording choices presented by the above script





Those entries called 'Monitor of' refer to sound sink devices, whereas the others are sound source devices such as MICs.

Hope that helps to clarify a little ......

MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #4 on: May 06, 2012, 05:07:26 AM »
Have you ever wanted to play your music from your laptop with crappy speakers, but are connected to your LAN which has a PC with decent speakers? It happens  :D

There is a simple way to play sounds across the LAN using PA.

On the sender side create an RTP sink:

pactl load-module module-null-sink sink_name=rtp
pactl load-module module-rtp-send source=rtp.monitor

Now on PA Vol Control, you can select the Null Output on the Playback Tab for whatever application is playing the sound.

On the client sides just load the reciever module:

pactl load-module module-rtp-recv

It will automatically play whatever is being played on the sending machine.

Now you can play your favourite music on the sender side and all clients will output it simultaneously.

BTW: You can have more than one sender machine set up like this. The audio data will be mixed on the receiving PC(s). That could be fun!  ;D

Be sure to take note of the module IDs when you load them so you can unload them when finished as in previous post.  ;)

pactl unload-module <ID>

It is not the optimum way to do this of course, but this does show that it is possible.

More fun!  :D
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #5 on: May 06, 2012, 01:55:34 PM »
It seems Archie has managed to package PAPrefs which will give a GUI for managing most of this!

Yeah!

Thanks Archie .....  I look forward to installing the GUI package.  ;D
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8590
  • Aurum nostrum non est aurum vulgi.
Re: Playtime with Pulse Audio
« Reply #6 on: May 06, 2012, 02:24:54 PM »
You're most welcome. :)
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #7 on: May 16, 2012, 02:03:23 PM »
OK, I finally got around to checking out PAPrefs.
At this time it is in testing.

To play sound across the LAN to a specified sound sink (speakers connected to another PC) is very simple when using PAPrefs.
It does all the configuration mentioned in my previous post on the subject, requiring the user to only tick a few boxes.

PAPrefs needs to be installed on both PCs.
For initial tests it is best to disable any firewall on the PCs.
They can be enabled again, with a rule to allow the connection ....  but that is beyond what is explained here.
Avahi (Bonjour) is used for discovery of the remote sound sinks, so you must have a working Avahi setup.
You also need, of course, a working PulseAudio setup.

First the PC which will play the sound:

Open PAPrefs, and on the Network Access tab, tick the topmost box =>
 Make discoverable PulseAudio network sound devices available locally

This causes the PC to go looking for sound devices which are on the LAN.





Second the PC whose speakers will reproduce the sound:

Open PAPrefs, and on the Network Server tab, tick the top box =>
 Enable network access to local sound devices

... and tick the two boxes directly underneath this one =>
 Allow other machines on the LAN to discover local sound devices
   and 
 Don't require authentication.

This allows access to the sound devices on this PC, from other PCs on the LAN. No authentication is required, but can be introduced later if required.





That sets up everything.

Now using VLC or other player of choice, play a sound file, or playlist.

With the application started, open PulseAudio Volume Control, and on the Playback tab, select to where the playing application's sound should be sent.
Select the remote sound card from the drop-down list of the playing application.

That is it. The sound is now playing through the speakers of the other PC on the LAN.

Have fun!  :D

****

For this test I used a desktop PC running the 64 bit test install of PCLOS, and a Laptop running a 32 bit KDE install of PCLOS.

Both had working Pulse Audio set ups.

I did not look into this, but believe there will be a slight delay before the sound appears ...  probably due to caching or such. That is possibly editable, but was not something that concerned me tonight.

If I remember anything else pertinent I will add it here .... 



regards .......  Thanks again Archie  :D

« Last Edit: May 18, 2013, 09:22:51 AM by Just17 »
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Offline blackhole

  • Full Member
  • ***
  • Posts: 66
Re: Playtime with Pulse Audio
« Reply #8 on: May 16, 2012, 02:46:19 PM »
thank you very much for sharing this, leprechaun.

Offline Archie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8590
  • Aurum nostrum non est aurum vulgi.
Re: Playtime with Pulse Audio
« Reply #9 on: May 16, 2012, 03:53:11 PM »
I'm glad it worked out good for you, Just18. You're welcome.

P.S. Thanks for the short howto. I'm pretty sure others will find that very useful.

Peace and much respect,
Archie
Since 2006 | LiCo 401868 | Bare Metal | What is necessary is never unwise. --Sarek, 2258.42


Offline zerocool

  • Sr. Member
  • ****
  • Posts: 375
Re: Playtime with Pulse Audio
« Reply #10 on: May 16, 2012, 04:07:35 PM »

Would make a good writeup for PCLOS magazine. I'm sure parnote is always looking for good stuff (and this fits the bill).

 8)


Offline dixonpete

  • Hero Member
  • *****
  • Posts: 897
Re: Playtime with Pulse Audio
« Reply #11 on: May 16, 2012, 05:06:29 PM »
Are you expecting much new with Pulse Audio 2.0?

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #12 on: May 17, 2012, 09:32:38 AM »
Everything is not yet perfect according to the tests I am running on PA ...  in both 64 & 32 bit.

For instance I had to reinstall    pulse  module-rtp-recv  as it was not working. The reinstall did the job.

I note that there are more of these modules built into the 64 bit pulseaudio package than the 32 bit.

There are still some eight modules for Pulse which are separated .... and wonder if these could possibly be incorporated into the main package with the rest of the modules.
It would make life a lot easier knowing that all the modules were available rather than having to go looking for them to install to get some functionality or other.

****

In the meantime I came across this page which might be of interest

http://tinyurl.com/7xn7gxy

It details sending sound across the LAN ...  one using auto discovery of remote devices and the other using multicast to send the sound stream.
Be aware that using multicast into a router that does not deal with it properly will overload the router and make any other use almost impossible.

In my tests through a router that did not deal correctly with the multicast stream, the audio arrived at the receiving PC very distorted. I might try it through a suitable router if I get the inclination.

Oh yes ...  if you want to monitor the multicast data stream it is on
224.0.0.56  Port 46298  with  MTU of 1280

MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #13 on: May 29, 2012, 05:11:31 AM »
Spent a few minutes today testing some other devices and how PA deals with them.

Mainly I wanted to know how PA dealt with Bluetooth devices for audio.

I have a Logitech earpiece which is very useful for phone calls while moving about.
Mostly it would be used for hands-free operation with a mobile phone.
In this case I tested it connecting to my PC and making a Skype test call.

There is little to say really ....  after the BT earpiece was paired with the PC it popped up on the Config tab of PA Vol Control

Everything worked as one might hope.
I even played an internet music radio station using VLC, to it.

Being bored with the ease of operation I decided to see about using the option to send a sound stream to ALL connected sound sinks.

So, PAPreferences - Simultaneous Output tab ......  and ticked the box (add virtual output device for simultaneous output on all local sound cards).
After that it was a matter of selecting that output device for VLC, on the PA Volume Control Output tab.
All sound sinks received the radio station .....

Devices connected during today's playtime  :D




This is getting boring ......  it just works!   :D

The only item I have used for which there is no GUI is the setting up of null sink & loopback device as explained in the first post of this thread.
I suppose it is not too much hassle to set it up and leave it permanently in place (by editing a config file), or maybe this will be added to PAPrefs or other GUI in the future.

That's it for now.

If I come across anything else of interest I will post again ....  but there cannot be much left that I can try  :D

Later ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10675
  • MLUs Forever!
Re: Playtime with Pulse Audio
« Reply #14 on: May 29, 2012, 05:22:00 AM »
Are you expecting much new with Pulse Audio 2.0?

I'm not ...  but there is nothing I can think of that I would want it to do that it doesn't presently.

Do you have anything in mind that would make things easier for you?



So, I guess, from this user's point of view, 2.0 will be mostly bug fixes.

The big differences for me are the GUI tools which are available, and becoming available in PCLOS.

PA Vol Control
PAPrefs
and of course Veromix which is only available in 64 bit PCLOS presently due to dependency issues.

For me, control of sound streams and devices is now where I hoped it could & would be, a few years ago.

happy chappy!  :D
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT