Installation and configuration SVXBRIDGE 
========================================
by SP2ONG and edited by SM5GXQ

The svxbridge.py base on USRPAudio.py and was rewritten by SP2ONG to show the possibility how to create DMR <> Analog SVXLink gate using USRP protocol. 

Adaptation by SM5GXQ
--------------------
This is still not plug-and-play. My production setup contains many refinements, that are omitted here.
Also, I do things a bit different from the original config by SP2ONG. 
He also discribes the build of SvxLink, which I have omitted here.

In my implementation, SvxLink and svxbridge runs as separate services. 
The important thing is that SvxLink is started first, and svxbridge a bit afterwards. 
The PTYs in SvxLink must be created first for svxbridge to work.

You can link to SvxLink by a local SvxReflector or direct to your RepeaterLogic. That's up to you.
As a minimum, you will need a SimplexlogicDMR with Rx and Tx, using ALSA loopback.
The most tricky part is the audio device setup with ALSA loopback. 
You need some trial-and-error, and a great amount of patience.
The index number of the audio devices may vary from system to system.
I am not using the MD380-emu. I use a DV3000 stick instead, directly connected to Analog Bridge. Both alternatives will work fine.


Install the necessary packages:
---------------------------

apt install build-essential
apt install git cmake libsigc++-2.0-dev libasound2-dev libpopt-dev libgcrypt11-dev tk-dev 
apt install libgsm1-dev libspeex-dev libcurl4-openssl-dev libjsoncpp-dev groff curl


Add svxlink user:
------------------

sudo useradd -c "SVXLink user" -G dialout,audio -d /home/svxlink -m -s /bin/bash svxlink

Add to /etc/sudores svxlink user to run svxbridge.py in su command:

svxlink ALL=(ALL)       NOPASSWD: ALL


Add ALSA loop to load modules in /etc/modules add the line:


snd_aloop


and create file /etc/modprobe.d/alsa.conf which will be force use by sn_aloop index 0 for number cards:


options snd-aloop index=0			# or the number of your choosing (*).


Next restart computer and check that alsa loop has number 0 (*) in list of sound devices:

cat /proc/asound/cards

If required, install the Equalizer.

sudo apt install libasound2-plugin-equal


.................


Install DVSwitch packages:
==========================

Install packages necessary to DVSwitch:
--------------------------------------

apt install git python-pip python3-pip python-dev python3-dev libffi-dev libssl-dev

cd /opt/src
wget https://dvswitch.org/install-dvswitch-repo
chmod +x install-dvswitch-repo
./install-dvswitch-repo
apt update
apt install analog-bridge
apt install md380-emu
apt install mmdvm-bridge

Next, configure Analog_Bridge.ini and MMDVM_Bridge.ini
Run dvswitch packages:

systemctl start md380-emu		# If needed. 
systemctl start analog_bridge
systemctl start mmdvm_bridge

We use md380-emu AMBE software emulator vocoder with Analog_Bridge to convert audio analog to dmr and opposite

Install pyserial and pyaudio for svxbridge.py:
---------------------------------------------

pip install pyserial
apt install python-pyaudio

Copy /opt/svxbridge/utils/asound.conf to /etc/asound.conf 

The file contain necessary configuration to work alsa loop sound card
Comment out Equalizer if not needed.


Run /opt/svxbridge/utils/index-audio.py to find number of index for "plug_Loopback_1_2" (?) and put this number 
in svxbridge.py in:
 
in_index=

below exmaple result of run index-audio.py:

(0, u'Loopback: PCM (hw:0,0)', 32L)
(1, u'Loopback: PCM (hw:0,1)', 32L)
(2, u'sysdefault', 128L)
(3, u'front', 0L)
(4, u'surround21', 32L)
(5, u'surround40', 0L)
(6, u'surround41', 0L)
(7, u'surround50', 0L)
(8, u'surround51', 0L)
(9, u'surround71', 0L)
(10, u'plug_Loopback_1_1', 128L)
(11, u'plug_Loopback_1_2', 0L)
(12, u'default', 128L)

Important! Start SvxLink but not svxbridge service before running the index-audio.py script.
The index number will change, depending on devices already in use!


Next copy files:
 
from /opt/svxbridge/utils to their destinations (comment in the files).


Enable and start svxbridge:
-------------------------

systemctl enable svxbridge
systemctl start svxbridge

Important! Svxbridge must be run as user svxlink (same as SvxLink), otherwise access to PTYs will fail.

Check status
------------

systemctl status svxbridge


Tune levels audio in /opt/Analog_Bridge/Analog_Bridge.ini (SP2ONG)
---------------------------------------------------------

The audio from DMR to SVXlink:
------------------------------
usrpAudio = AUDIO_USE_AGC                
usrpGain = -16                        

Try to adjust from -10 to -20

Instead above you can use the following:
-------------------------------------
usrpAudio = AUDIO_UNITY                
usrpGain = 3.8                        

Try adjust from 2.0 to 4.0


The audio from SVXLink to DMR:
------------------------------
tlvAudio = AUDIO_UNITY                 
tlvGain = 0.3                          

Try to adjust from 0.1 to 1.0 but before this, you must set correct level audio
from svxlink (mic gain in alsamixer). I recommended use Echolink PC version
application and connect to svxlink via echolink and next observe the audio level
the meter in Echolink application adjusts mic gain in alsamixer to set gain audio that on the audiometer will time to time appear in yellow color.

SM5GXQ: I use a different approach. See below.

[USRP]
address = 127.0.0.1                     ; IP address of USRP partner (Allstar/Asterisk or another Analog_Bridge)
txPort = 12345                          ; Transmit USRP frames on this port
rxPort = 12346                          ; Listen for USRP frames on this port
usrpAudio = AUDIO_USE_AGC               ; Digital -> Analog (AUDIO_UNITY, AUDIO_USE_GAIN, AUDIO_USE_AGC)
usrpGain = 3.8                          ; Gain factor when usrpAudio = AUDIO_USE_GAIN (0.0 to 5.0) (1.0 = AUDIO_UNITY)
usrpAgc = -30,5,5000                    ; Set the agc threshold (db), slope (db) and decay (ms)
tlvAudio = AUDIO_USE_GAIN               ; Analog -> Digital (AUDIO_UNITY, AUDIO_USE_GAIN, AUDIO_BPF)
tlvGain = 0.27                          ; Gain factor when tlvAudio = AUDIO_USE_GAIN (0.0 to 5.0) (1.0 = AUDIO_UNITY)

I also use Equalizer to remove some lower frequency bands.


After changes you must restart Analog_Bridge:

systemctl restart analog_bridge


Known issue: delay audio between analog / DMR.


Waldek SP2ONG 2020
Peter SM5GXQ 2021