Thicket data repository for the EEG
1{
2 "id": "https://www.tunbury.org/2021/07/27/audio-stream",
3 "title": "Audio Stream from a Raspberry PI",
4 "link": "https://www.tunbury.org/2021/07/27/audio-stream/",
5 "updated": "2021-07-27T19:41:29",
6 "published": "2021-07-27T19:41:29",
7 "summary": "Now singing has returned to churches I need to add an additional microphone to pickup the choir. I’d like this to be completely separate to the Church PA system to avoid playing this sound out through the speakers. A Raspberry PI Zero W with a USB sound card looks to be a good option to capture the audio and stream it to OBS.",
8 "content": "<p>Now singing has returned to churches I need to add an additional microphone to pickup the choir. I’d like this to be completely separate to the Church PA system to avoid playing this sound out through the speakers. A Raspberry PI Zero W with a USB sound card looks to be a good option to capture the audio and stream it to OBS.</p>\n\n<p>Run <code>arecord -l</code> to get a list of available mixer devices. In my case my USB audio device is #2.</p>\n\n<p>Set the mixer level for the microphone:</p>\n\n<div><div><pre><code>amixer -c 2 -q set 'Mic',0 100%\n</code></pre></div></div>\n\n<p>Install <code>ffmpeg</code> which pulls down around 750MB on a lite installation.</p>\n\n<div><div><pre><code>apt install ffmpeg\n</code></pre></div></div>\n\n<p>Run <code>ffmpeg</code> to create the stream specifying the mixer device name as the input <code>-i</code></p>\n\n<div><div><pre><code>ffmpeg -ar 44100 -ac 1 -f alsa -i plughw:2,0 -f wav -listen 1 tcp://0.0.0.0:5002\n</code></pre></div></div>\n\n<p>You can play this stream using VideoLAN’s VLC using <em>Open Network Stream</em> <code>tcp/wav://192.168.1.104:5002</code> where 192.168.1.104 is the IP address of the PI.</p>\n\n<p>In OBS create a new Media Source and set the network buffer to zero (to avoid excessive delay) and turn off <em>Restart playback when source becomes active</em> which keeps the stream alive even when it’s not the active scene:</p>\n\n<div><div><pre><code>tcp://192.162.1.104:5002\n</code></pre></div></div>\n\n<p>Wrap the ffmpeg command as a service by creating <code>/etc/systemd/system/stream.service</code> containing</p>\n\n<div><div><pre><code>[Unit]\nDescription=auto start stream\nAfter=multi-user.target\n\n[Service]\nType=simple\nExecStartPre=/usr/bin/amixer -c 2 -q set 'Mic',0 100%\nExecStart=/usr/bin/ffmpeg -ar 44100 -ac 1 -f alsa -i plughw:2,0 -f wav -listen 1 tcp://0.0.0.0:5002\nUser=pi\nWorkingDirectory=/home/pi\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n</code></pre></div></div>\n\n<p>Enable and start the service as follows:</p>\n\n<div><div><pre><code>systemctl enable stream\nservice stream start\n</code></pre></div></div>\n\n<h2>Practical Issues</h2>\n\n<p>After successfully testing using a Raspberry PI Zero W using USB audio dongle with WiFi connecting over a distance of 30m in an empty church I decided to use it as a secondary device in live broadcast. This was immediately scuppered on the day as I was unable to maintain the WiFi link. I put this down to the interference created by the in house PA system, induction loop, and the mobile phones of the congregation.</p>\n\n<p>I added a UFL connector the Pi Zero W as described by <a href=\"https://www.briandorey.com/post/raspberry-pi-zero-w-external-antenna-mod\">Briain Dorey</a>. Using this with a 5dB D-Link antenna did marginally increase the antenna signal level and quality of most networks but not sufficiently to make the difference.</p>\n\n<h3>Internal antenna</h3>\n\n<div><div><pre><code>pi@raspberrypi:~ $ sudo iwlist wlan0 scan | grep 'Cell\\|Signal' | sed '$!N;s/\\n/ /'\n Cell 01 - Address: 6C:xx:xx:xx:xx:10 Quality=69/70 Signal level=-41 dBm \n Cell 02 - Address: 5C:xx:xx:xx:xx:9E Quality=26/70 Signal level=-84 dBm \n Cell 03 - Address: 5E:xx:xx:xx:xx:9F Quality=27/70 Signal level=-83 dBm \n Cell 04 - Address: 9C:xx:xx:xx:xx:62 Quality=35/70 Signal level=-75 dBm \n Cell 05 - Address: 78:xx:xx:xx:xx:8E Quality=21/70 Signal level=-89 dBm \n Cell 06 - Address: 9C:xx:xx:xx:xx:72 Quality=37/70 Signal level=-73 dBm \n Cell 07 - Address: 80:xx:xx:xx:xx:6A Quality=17/70 Signal level=-93 dBm \n</code></pre></div></div>\n\n<h3>External antenna</h3>\n\n<div><div><pre><code>pi@raspberrypi:~ $ sudo iwlist wlan0 scan | grep 'Cell\\|Signal' | sed '$!N;s/\\n/ /'\n Cell 01 - Address: 6C:xx:xx:xx:xx:10 Quality=70/70 Signal level=-29 dBm \n Cell 02 - Address: 5C:xx:xx:xx:xx:9E Quality=22/70 Signal level=-88 dBm \n Cell 03 - Address: 5E:xx:xx:xx:xx:9F Quality=23/70 Signal level=-87 dBm \n Cell 04 - Address: 9C:xx:xx:xx:xx:62 Quality=41/70 Signal level=-69 dBm \n Cell 05 - Address: 78:xx:xx:xx:xx:8E Quality=30/70 Signal level=-80 dBm \n Cell 06 - Address: 9C:xx:xx:xx:xx:72 Quality=41/70 Signal level=-69 dBm \n Cell 07 - Address: 80:xx:xx:xx:xx:6A Quality=24/70 Signal level=-86 dBm \n</code></pre></div></div>\n\n<p>Switching to a Raspberry PI 3 gave easy access to an Ethernet port without resorting to a USB hub. Following that there were no further connection issues!</p>\n\n<p><code>FFMPEG</code> can also create an MP3 stream rather than a WAV stream by simply changing the output format <code>-f mp3</code></p>\n\n<div><div><pre><code>/usr/bin/ffmpeg -ar 44100 -ac 1 -f alsa -i plughw:2,0 -f mp3 -listen 1 tcp://0.0.0.0:5002\n</code></pre></div></div>\n\n<p>The Raspberry PI 3 didn’t really have sufficient processing capacity to keep up with the MP3 encoding. Switch to MP2, <code>-f mp2</code>, reduced the processor requirement significantly with no noticeable change in quality.</p>",
9 "content_type": "html",
10 "author": {
11 "name": "Mark Elvers",
12 "email": "mark.elvers@tunbury.org",
13 "uri": null
14 },
15 "categories": [
16 "raspberrypi",
17 "obs"
18 ],
19 "source": "https://www.tunbury.org/atom.xml"
20}