Difference between revisions of "User:Duggabe"

From GNU Radio
Jump to: navigation, search
Line 1: Line 1:
<!-- ALSAPulseAudio.mediawiki -->
+
<!-- InstallingGRFromSource_on_Raspberry_Pi -->
== Working with ALSA and Pulse Audio ==
+
These instructions have been tested with the <q>Raspberry Pi OS (32-bit) with desktop and recommended software</q> on a Raspberry Pi 3B+. A 32GB microSD card is recommended.
  
* 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.
+
== Set up a swap file ==
* 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 ==
+
A swap file will improve the compile time greatly.
  
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.
+
<pre>
 +
sudo fallocate -l 2G /swapfile
 +
sudo chmod 600 /swapfile
 +
sudo mkswap /swapfile
 +
sudo swapon /swapfile
 +
</pre>
 +
<p>To make the swap file permanent, add this line to /etc/fstab</p>
 +
<pre>
 +
/swapfile  none  swap  sw  0  0
 +
</pre>
  
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.
+
== Load prerequisites ==
  
You can obtain a list the playback devices (for an Audio Sink) using the <code>aplay</code> program.
+
You may have some of these prerequisites already, but it doesn't hurt to get them again and check for the latest versions.
  
* from a terminal window enter:
 
<code>aplay -L</code>
 
* a long list of options will be displayed, such as:
 
 
<pre>
 
<pre>
default
+
sudo apt update --allow-releaseinfo-change
    Playback/recording through the PulseAudio sound server
+
sudo apt upgrade
null
+
sudo apt install git cmake g++ libboost-all-dev libgmp-dev swig python3-numpy \
    Discard all samples (playback) or generate zero samples (capture)
+
python3-mako python3-sphinx python3-lxml doxygen libfftw3-dev \
pulse
+
libsdl1.2-dev libgsl-dev libqwt-qt5-dev libqt5opengl5-dev python3-pyqt5 \
    PulseAudio Sound Server
+
liblog4cpp5-dev libzmq3-dev python3-yaml python3-click python3-click-plugins \
hdmi:CARD=HDMI,DEV=0
+
python3-zmq python3-scipy libpthread-stubs0-dev libusb-1.0-0-dev \
    HDA ATI HDMI, HDMI 0
+
python-docutils build-essential liborc-0.4-0 liborc-0.4-dev
    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
 
...
 
 
</pre>
 
</pre>
* find the entry such as:
+
 
 +
== Install UHD from source ==
 +
 
 +
If you want to use GNU Radio with a USRP, install the UHD package from source using the following instructions.
 +
UHD sits at the same level as GNU Radio as an independent driver, which gr-uhd references.  So if you want gr-uhd enabled, you <b>FIRST</b> must clone and install UHD.
 +
 
 +
For this example, we will start in the home directory to parallel the steps in [[InstallingGR#To_install_system_wide]].
 +
 
 +
<pre>cd ~/</pre>
 +
 
 +
Clone the code into your home directory:
 +
 
 
<pre>
 
<pre>
hw:CARD=Generic,DEV=0
+
git clone git://github.com/EttusResearch/uhd.git
    HD-Audio Generic, ALC662 rev3 Analog
+
cd ~/uhd
    Direct hardware device without any conversions
 
 
</pre>
 
</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).
 
  
For audio input devices, use:
+
<b>Note:</b> In the following command, change <code>v3.15.0.0</code> to some other branch or tag if you want to build a different version.
 +
<pre>
 +
git tag -l
 +
git checkout v3.15.0.0
 +
</pre>
  
<pre>arecord -L</pre>
+
<b>Note:</b> Unlike most build processes, UHD builds under the <code>host</code> directory.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to obtain a similar list.
+
<pre>
 +
cd host
 +
mkdir build
 +
cd build
 +
</pre>
  
== Monitoring the audio input of your system with PulseAudio  ==
+
<b>Note:</b> In the following command, we will use <code>-DCMAKE_INSTALL_PREFIX=/usr/local</code> to install UHD into the same prefix as GNU Radio. When compiling for ARM platforms, the assumption is made that NEON extensions are available if the arm_neon.h header is found. However, for platforms such as Raspberry Pi, one must specify -DNEON_SIMD_ENABLE=OFF in the following command.
  
<p><b>IMPORTANT: this procedure only applies to an Audio Source block!</b></p>
+
<pre>
 +
cmake -DNEON_SIMD_ENABLE=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ../
 +
</pre>
  
PulseAudio has its own monitor &quot;ports&quot;. You can list all PulseAudio monitor sources by running:
+
<b>Note:</b> In the following command, if your computer's CPU has multiple cores, you can use the argument <code>-j#</code> to speed compilation; <br> e.g., <code>make -j3</code> will use 3 threads in the build. Specify at least one less than the number of CPU cores so the system does not appear to 'freeze' during the build. If not specified, then a single thread is used for the build; this is not necessarily a bad thing, but it will take roughly 2 times as long to build as using 2 threads, and roughly 3 times as long to build as using 3 threads.<br>
  
<pre>pactl list|grep &quot;Monitor Source&quot;|sed 's/^[[:space:]]*Monitor Source: //g'</pre>
+
<pre>
 +
make -j3
 +
make test
 +
sudo make install
 +
</pre>
  
This will give you one or more lines containing something like
+
If you're running Linux, then always do the following command after installing any library:
  
<pre>alsa_output.pci-0000_00_03.0.hdmi-stereo.monitor
+
<pre>sudo ldconfig</pre>
alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
 
alsa_output.pci-0000_06_00.1.hdmi-stereo.monitor</pre>
 
  
Select the right name; assuming we use the analog input, the second line would be the right.
+
You can now download the UHD FPGA Images for this installation.
 +
<pre>
 +
sudo uhd_images_downloader
 +
</pre>
  
=== Add ALSA Pseudodevice for monitor ===
+
On Linux, udev handles USB plug and unplug events. The following commands install a udev rule so that non-root users may access the device.
 +
<pre>
 +
cd ~/uhd/host/utils
 +
sudo cp uhd-usrp.rules /etc/udev/rules.d/
 +
sudo udevadm control --reload-rules
 +
sudo udevadm trigger
 +
</pre>
  
Now, we need to edit (or create, if it doesn't already exist) <code>~/.asoundrc</code>.<br />
+
The installation of UHD is now complete. At this point, connect the USRP to the host computer and run:
Add
 
 
<pre>
 
<pre>
pcm.pulse_monitor {
+
uhd_usrp_probe
    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
 
}
 
 
</pre>
 
</pre>
of course, replacing the <code>device</code> name with the correct one from the previous step.
 
  
=== Using the newly created device ===
+
== Install GNU Radio ==
  
In the Audio Source block, use <code>pulse_monitor</code> as the device name:<br />
+
<b>The next steps will take two or more hours. Be patient. The terminal does show the progress.</b>
  
[[File:PulseAudio-ALSA-Monitoring.png|PulseAudio-ALSA-Monitoring.png]]
+
Click [[InstallingGR#To_install_system_wide]] to go to the install instructions.

Revision as of 01:08, 7 July 2020

These instructions have been tested with the Raspberry Pi OS (32-bit) with desktop and recommended software on a Raspberry Pi 3B+. A 32GB microSD card is recommended.

Set up a swap file

A swap file will improve the compile time greatly.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

To make the swap file permanent, add this line to /etc/fstab

/swapfile  none  swap  sw  0  0

Load prerequisites

You may have some of these prerequisites already, but it doesn't hurt to get them again and check for the latest versions.

sudo apt update --allow-releaseinfo-change
sudo apt upgrade
sudo apt install git cmake g++ libboost-all-dev libgmp-dev swig python3-numpy \
python3-mako python3-sphinx python3-lxml doxygen libfftw3-dev \
libsdl1.2-dev libgsl-dev libqwt-qt5-dev libqt5opengl5-dev python3-pyqt5 \
liblog4cpp5-dev libzmq3-dev python3-yaml python3-click python3-click-plugins \
python3-zmq python3-scipy libpthread-stubs0-dev libusb-1.0-0-dev \
python-docutils build-essential liborc-0.4-0 liborc-0.4-dev

Install UHD from source

If you want to use GNU Radio with a USRP, install the UHD package from source using the following instructions. UHD sits at the same level as GNU Radio as an independent driver, which gr-uhd references. So if you want gr-uhd enabled, you FIRST must clone and install UHD.

For this example, we will start in the home directory to parallel the steps in InstallingGR#To_install_system_wide.

cd ~/

Clone the code into your home directory:

git clone git://github.com/EttusResearch/uhd.git
cd ~/uhd

Note: In the following command, change v3.15.0.0 to some other branch or tag if you want to build a different version.

git tag -l
git checkout v3.15.0.0

Note: Unlike most build processes, UHD builds under the host directory.

cd host
mkdir build
cd build

Note: In the following command, we will use -DCMAKE_INSTALL_PREFIX=/usr/local to install UHD into the same prefix as GNU Radio. When compiling for ARM platforms, the assumption is made that NEON extensions are available if the arm_neon.h header is found. However, for platforms such as Raspberry Pi, one must specify -DNEON_SIMD_ENABLE=OFF in the following command.

cmake -DNEON_SIMD_ENABLE=OFF -DCMAKE_INSTALL_PREFIX=/usr/local ../

Note: In the following command, if your computer's CPU has multiple cores, you can use the argument -j# to speed compilation;
e.g., make -j3 will use 3 threads in the build. Specify at least one less than the number of CPU cores so the system does not appear to 'freeze' during the build. If not specified, then a single thread is used for the build; this is not necessarily a bad thing, but it will take roughly 2 times as long to build as using 2 threads, and roughly 3 times as long to build as using 3 threads.

make -j3
make test
sudo make install

If you're running Linux, then always do the following command after installing any library:

sudo ldconfig

You can now download the UHD FPGA Images for this installation.

sudo uhd_images_downloader

On Linux, udev handles USB plug and unplug events. The following commands install a udev rule so that non-root users may access the device.

cd ~/uhd/host/utils
sudo cp uhd-usrp.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules
sudo udevadm trigger

The installation of UHD is now complete. At this point, connect the USRP to the host computer and run:

uhd_usrp_probe

Install GNU Radio

The next steps will take two or more hours. Be patient. The terminal does show the progress.

Click InstallingGR#To_install_system_wide to go to the install instructions.