Difference between revisions of "ALSAPulseAudio"

From GNU Radio
Jump to: navigation, search
(Talking to ALSA)
(reformat; clarify)
(One intermediate revision by one other user not shown)
Line 1: Line 1:
= Working with ALSA and Pulse Audio =
+
<!-- ALSAPulseAudio.mediawiki -->
 +
== Working with ALSA and Pulse Audio ==
  
== General ==
+
* Sound cards don't support arbitrary sampling rates. If your audio is choppy, check the rate of your audio sink or source: 44100 Hz works under all audio cards known, 48000 Hz on most, others may not.
 
 
* Sound cards don't support arbitrary sampling rates. If your audio is choppy, check the rate of your audio sink or source: 44100 Hz works under all audio cards known, 48000 Hz on many, 32000 usually does not work.
 
 
* Don't clip: The maximum amplitude in your signal '''must not''' exceed 1.0.
 
* Don't clip: The maximum amplitude in your signal '''must not''' exceed 1.0.
 +
* Set "OK to Block" to "No" when the flowgraph is throttled by another hardware device.
  
 
== Talking to ALSA ==
 
== Talking to ALSA ==
  
=== Audience ===
+
The GNU Radio Audio Sink and Source blocks use ALSA (unless ALSA support was disabled during build time). ALSA has been the standard sound API under Linux for a decade or more, so basically all programs that produce Audio know how to deal with it. Alternately one can use a ''sound server'' like PulseAudio that takes care of dealing with all the low-level details for the application.
Linux users wanting to get Audio in and/or out of their GNU Radio application, but are having problems ===
 
  
=== Problem ===
+
PulseAudio also provides a &quot;fake&quot; ALSA device to make ALSA applications talk to PulseAudio instead of directly with the hardware driver, allowing one central volume control, etc. However, PulseAudio's device isn't always perfect. PulseAudio is capable of resampling internally, but the results aren't always predictable. For GNU Radio applications, it's often desirable to use the raw device.
  
The GNU Radio Audio sink and source use ALSA (unless ALSA support was disabled during build time). ALSA has been the standard sound API under Linux for a decade or so, so basically all programs that produce Audio know how to deal with it, or use a ''sound server'' like PulseAudio that takes care of dealing with all the low-level details for the application.
+
You can obtain a list the playback devices (for an Audio Sink) using the <code>aplay</code> program.
  
PulseAudio itself also provides a &quot;fake&quot; ALSA device, so to make ALSA applications talk to PulseAudio instead of directly with the hardware driver, allowing one central volume control etc.
+
* from a terminal window enter:
 
+
<code>aplay -L</code>
However, PulseAudio's device isn't always perfect. PulseAudio is internally capable of resampling, but the results aren't always predictable. For GNU Radio applications, it's often desirable to know the raw device.
+
* a long list of options will be displayed, such as:
 
+
<pre>
=== Solution ===
+
default
 
+
    Playback/recording through the PulseAudio sound server
You can figure out the playback devices using the <code>aplay</code> program, that usually ships with modern linux distributions:
+
null
 
 
<pre>aplay -L</pre>
 
yields something like
 
 
 
<pre>null
 
 
     Discard all samples (playback) or generate zero samples (capture)
 
     Discard all samples (playback) or generate zero samples (capture)
 
pulse
 
pulse
 
     PulseAudio Sound Server
 
     PulseAudio Sound Server
default
+
hdmi:CARD=HDMI,DEV=0
    Default ALSA Output (currently PulseAudio Sound Server)
+
     HDA ATI HDMI, HDMI 0
hdmi:CARD=HDMI_1,DEV=0
 
     HDA Intel HDMI, HDMI 0
 
 
     HDMI Audio Output
 
     HDMI Audio Output
hdmi:CARD=HDMI_1,DEV=1
+
hw:CARD=Generic,DEV=0
     HDA Intel HDMI, HDMI 1
+
     HD-Audio Generic, ALC662 rev3 Analog
     HDMI Audio Output
+
     Direct hardware device without any conversions
hdmi:CARD=HDMI_1,DEV=2
+
plughw:CARD=Generic,DEV=0
     HDA Intel HDMI, HDMI 2
+
     HD-Audio Generic, ALC662 rev3 Analog
     HDMI Audio Output
+
     Hardware device with all software conversions
sysdefault:CARD=PCH
+
...
    HDA Intel PCH, ALC887-VD Analog
+
</pre>
    Default Audio Device
+
* find the entry such as:
front:CARD=PCH,DEV=0
+
<pre>
     HDA Intel PCH, ALC887-VD Analog
+
hw:CARD=Generic,DEV=0
     Front speakers
+
     HD-Audio Generic, ALC662 rev3 Analog
...</pre>
+
     Direct hardware device without any conversions
here, using somethig like <code>front</code> or <code>sysdefault</code> in the Audio Sink's &quot;Device&quot; property makes a lot of sense.
+
</pre>
 +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in the list which matches your desired device.
 +
* use the first line of that entry (e.g. "hw:CARD=Generic,DEV=0") as the device name (without the quotes).
  
In analogy
+
For audio input devices (an Audio Source), use:
  
 
<pre>arecord -L</pre>
 
<pre>arecord -L</pre>
lists all recording devices.
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to obtain a similar list.
  
== Monitoring the output of your system ==
+
== Monitoring the audio input of your system with PulseAudio  ==
  
=== Audience ===
+
<p><b>IMPORTANT: this procedure only applies to an Audio Source block!</b></p>
  
Users wanting to process what the Sound system currently is producing
+
PulseAudio has its own monitor &quot;ports&quot;. You can list all PulseAudio monitor sources by running:
  
=== Problem ===
+
<pre>pactl list|grep &quot;Monitor Source&quot;|sed 's/^[[:space:]]*Monitor Source: //g'</pre>
  
ALSA is badly documented, and PulseAudio is mainly documented through fragmented Wiki Entries, mainly done by the ArchLinux community.
+
This will give you one or more lines containing something like
 
 
=== Solution ===
 
 
 
==== Find PulseAudio monitors ====
 
 
 
PulseAudio has its own monitor &quot;ports&quot;; you can list all PulseAudio endpoints by running
 
 
 
<pre>pactl list</pre>
 
We want to find monitors. Because we're too lazy to read through the nautic mile of output that <code>pactl list</code> produces:
 
 
 
<pre>pactl list|grep &quot;Monitor Source&quot;|sed 's/^[[:space:]]*Monitor Source: //g'</pre>
 
Will give you one or more lines containing something like
 
  
 
<pre>alsa_output.pci-0000_00_03.0.hdmi-stereo.monitor
 
<pre>alsa_output.pci-0000_00_03.0.hdmi-stereo.monitor
 
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
 
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
 
alsa_output.pci-0000_06_00.1.hdmi-stereo.monitor</pre>
 
alsa_output.pci-0000_06_00.1.hdmi-stereo.monitor</pre>
Select the right name; assuming we use the analog output, the second line would be the right.
 
  
==== Add ALSA Pseudodevice for monitor ====
+
Select the right name; assuming we use the analog input, the second line would be the right.
  
Now, we need to edit (and create, if it doesn't already exist) <code>~/.asoundrc</code>.<br />
+
=== Add ALSA Pseudodevice for monitor ===
 +
 
 +
Now, we need to edit (or create, if it doesn't already exist) <code>~/.asoundrc</code>.<br />
 
Add
 
Add
 
+
<pre>
<pre>pcm.pulse_monitor {
+
pcm.pulse_monitor {
 
     type pulse
 
     type pulse
 
     device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
 
     device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
Line 95: Line 78:
 
     type pulse
 
     type pulse
 
     device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
 
     device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
}</pre>
+
}
of course, replacing the <code>device</code> name with the correct one from the last step.
+
</pre>
 +
of course, replacing the <code>device</code> name with the correct one from the previous step.
  
==== Using the newly created device ====
+
=== Using the newly created device ===
  
 
In the Audio Source block, use <code>pulse_monitor</code> as the device name:<br />
 
In the Audio Source block, use <code>pulse_monitor</code> as the device name:<br />
 +
 
[[File:PulseAudio-ALSA-Monitoring.png|PulseAudio-ALSA-Monitoring.png]]
 
[[File:PulseAudio-ALSA-Monitoring.png|PulseAudio-ALSA-Monitoring.png]]

Revision as of 15:24, 4 July 2020

Working with ALSA and Pulse Audio

  • Sound cards don't support arbitrary sampling rates. If your audio is choppy, check the rate of your audio sink or source: 44100 Hz works under all audio cards known, 48000 Hz on most, others may not.
  • Don't clip: The maximum amplitude in your signal must not exceed 1.0.
  • Set "OK to Block" to "No" when the flowgraph is throttled by another hardware device.

Talking to ALSA

The GNU Radio Audio Sink and Source blocks use ALSA (unless ALSA support was disabled during build time). ALSA has been the standard sound API under Linux for a decade or more, so basically all programs that produce Audio know how to deal with it. Alternately one can use a sound server like PulseAudio that takes care of dealing with all the low-level details for the application.

PulseAudio also provides a "fake" ALSA device to make ALSA applications talk to PulseAudio instead of directly with the hardware driver, allowing one central volume control, etc. However, PulseAudio's device isn't always perfect. PulseAudio is capable of resampling internally, but the results aren't always predictable. For GNU Radio applications, it's often desirable to use the raw device.

You can obtain a list the playback devices (for an Audio Sink) using the aplay program.

  • from a terminal window enter:

aplay -L

  • a long list of options will be displayed, such as:
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
hdmi:CARD=HDMI,DEV=0
    HDA ATI HDMI, HDMI 0
    HDMI Audio Output
hw:CARD=Generic,DEV=0
    HD-Audio Generic, ALC662 rev3 Analog
    Direct hardware device without any conversions
plughw:CARD=Generic,DEV=0
    HD-Audio Generic, ALC662 rev3 Analog
    Hardware device with all software conversions
...
  • find the entry such as:
hw:CARD=Generic,DEV=0
    HD-Audio Generic, ALC662 rev3 Analog
    Direct hardware device without any conversions

      in the list which matches your desired device.

  • use the first line of that entry (e.g. "hw:CARD=Generic,DEV=0") as the device name (without the quotes).

For audio input devices (an Audio Source), use:

arecord -L

      to obtain a similar list.

Monitoring the audio input of your system with PulseAudio

IMPORTANT: this procedure only applies to an Audio Source block!

PulseAudio has its own monitor "ports". You can list all PulseAudio monitor sources by running:

pactl list|grep "Monitor Source"|sed 's/^[[:space:]]*Monitor Source: //g'

This will give you one or more lines containing something like

alsa_output.pci-0000_00_03.0.hdmi-stereo.monitor
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
alsa_output.pci-0000_06_00.1.hdmi-stereo.monitor

Select the right name; assuming we use the analog input, the second line would be the right.

Add ALSA Pseudodevice for monitor

Now, we need to edit (or create, if it doesn't already exist) ~/.asoundrc.
Add

pcm.pulse_monitor {
    type pulse
    device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
}

ctl.pulse_monitor {
    type pulse
    device alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
}

of course, replacing the device name with the correct one from the previous step.

Using the newly created device

In the Audio Source block, use pulse_monitor as the device name:

PulseAudio-ALSA-Monitoring.png