{"id":187,"date":"2019-08-25T16:44:44","date_gmt":"2019-08-25T23:44:44","guid":{"rendered":"http:\/\/eequalspi.com\/?p=187"},"modified":"2019-08-25T16:44:44","modified_gmt":"2019-08-25T23:44:44","slug":"mopidy-and-multiple-audio-streams-from-the-command-line-on-raspberry-pi","status":"publish","type":"post","link":"http:\/\/eequalspi.com\/index.php\/2019\/08\/25\/mopidy-and-multiple-audio-streams-from-the-command-line-on-raspberry-pi\/","title":{"rendered":"Mopidy and Multiple Audio Streams From the Command Line on Raspberry Pi"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Background<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I feel like almost everyone who has had to deal with\nanything outside the most mundane audio setup on Linux machines has experienced\nthe pain I&#8217;m about to describe. On Mac and Windows machines, audio seems to\n&#8216;just work&#8217; (TM) (R) (C) &#8212; and on linux desktops like Ubuntu it seems to be\nfine too.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A-1024x1024.jpg\" alt=\"\" class=\"wp-image-188\" width=\"386\" height=\"386\" srcset=\"http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A-1024x1024.jpg 1024w, http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A-150x150.jpg 150w, http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A-300x300.jpg 300w, http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A-768x768.jpg 768w, http:\/\/eequalspi.com\/wp-content\/uploads\/2019\/08\/PCM5102A.jpg 1100w\" sizes=\"auto, (max-width: 386px) 85vw, 386px\" \/><figcaption>PCM5102A Module, product photo from Amazon.com<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">For my UNIAC project however, I went a little off the rails (dun-dun-dun). UNIAC 2.0 uses a Raspberry PI Zero W, which doesn\u2019t come with any onboard analog audio output, forcing me to use a third party sound card. Since the project had a nice backplane motherboard, I wanted a module I could just plop down on the board as if it were a component. Ultimately, I settled on a PCM5102A module <a href=\"https:\/\/www.amazon.com\/HiLetgo-Lossless-Digital-Converter-Raspberry\/dp\/B07Q9K5MT8\/\">like this one<\/a>, which took audio as I2S samples and cost all of $8. For the final, working version of this, I used Raspbian Buster (July 2019).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As an aside, I created an EAGLE Footprint for the module, so you can incorporate one into your own projects if so desired.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To get it all working, we need the OS to do four things:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Recognize the new I2S sound card.<\/li><li>Use it by default.<\/li><li>Provide some software volume control.<\/li><li>Mix multiple audio streams at once<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">PCM5102A Drivers<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">These no-name PCM5102A modules are sketchy at best, and come with no meaningful support and no manual. Fortunately, they use the same IC as the <a href=\"https:\/\/www.hifiberry.com\/products\/dacplus\/\">Hifiberry DAC+<\/a> project. After getting this thing going I feel somewhat bad for not buying their board, but the form factor is just not what I need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In theory you can follow the <a href=\"https:\/\/www.hifiberry.com\/build\/documentation\/hifiberry-software-configuration\/\">Hifiberry DAC+ setup instructions<\/a> to get basic output going \u2013 <strong>but don\u2019t <\/strong>use them if you want software volume control. For me following those instructions resulted in a weird situation where the software volume control didn\u2019t actually stick after reboot, so I recommend staying away.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Volume Control<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">What did work for me, was a modified version of <a href=\"http:\/\/www.customelectronics.co.uk\/rpihq.htm\">these instructions<\/a>. To get basic volume control working, I used the first few steps of those instructions. I&#8217;ll duplicate them here in case the site goes down.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Software setup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First we need to disable the onboard sound by editing alsa-blacklist.conf:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/modprobe.d\/alsa-blacklist.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>blacklist snd_bcm2835<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save and exit (^X, Y, enter).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, to set the IO, edit config.txt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/boot\/config.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dtoverlay=hifiberry-dac<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Remove or comment (#) the line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dtparam=audio=on<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Leave the line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dtparam=i2s=on<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">commented out.<br>\nSave and exit (^X, Y, enter).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next add some alsa (sound) configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/asound.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Paste the text below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pcm.!default {\n  type hw\n  card 0\n}\nctl.!default {\n  type hw\n  card 0\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save and exit (^X, Y, enter).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is no harm to do a reboot. So:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo reboot<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Test if everything is OK:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aplay -l<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This should return:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>**** List of PLAYBACK Hardware Devices **** card 0: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [] Subdevices: 1\/1 Subdevice #0: subdevice #0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have an amplifier you can connect via a phono lead you can now test it via: speaker-test -D -c -twav So:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>speaker-test -D default -c 2 -twav<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a continuous test saying \u2018front left\u2019 and \u2018front right\u2019 alternately from the appropriate speakers. Enter ^C to stop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Software Volume Control<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is an optional step that adds a software volume control into the  sound processing sequence. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/asound.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pcm.sftvol {\n   type softvol \n   slave.pcm \"plughw:0\" \n   control { \n     name \"PCM\" \n     card 0 \n   }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enter or change the pcm.!default section to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pcm.!default { \n   type plug \n   slave.pcm \"sftvol\" \n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save and exit (^X, Y, enter).<br>\n Now we can test it, as before:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>speaker-test -D default -c 2 -twav<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This should give alternating &#8220;front right&#8221; and &#8220;front left&#8221; from the respective speakers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Great, but what about multiple streams?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s where we go off the rails. We need to add a software mixer to this cozy little ALSA setup. Fortunately, <a href=\"https:\/\/alsa.opensrc.org\/Dmix\">DMIX is a thing<\/a>. There are a few dmix examples out there. What we need to do, however, is connect dmix to softvol to a PCM sound device. This I could not find documented clearly anywhere on the internet, so here we are.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pcm.!default {\n  type plug\n  slave.pcm \"softvol\"\n}\n\npcm.softvol {\n  type softvol\n  slave {\n    pcm \"dmix\" #redirect the output to dmix\n  }\n  control {\n    name \"PCM\" #override PCM slider to set softvol lvl globally\n    card 0\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Great, Now What?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To get MOPIDY to work (my ultimate goal here), I had to do two things:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Install mopidy-alsamixer.<\/li><li>Edit ~\/.config\/mopidy\/mopidy.conf<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">In mopidy.conf, I had to change the audio section to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#mixer = software\nmixer = alsamixer\noutput = alsasink device=plug:softvol\n#mixer_volume = \n#output = autoaudiosink\n#buffer_time = <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And then I added an alsamixer config as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[alsamixer]\ncard = 0\ncontrol = PCM\nmin_volume = 0\nmax_volume = 100\nvolume_scale = cubic<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally after weeks of pain, I&#8217;ve arrived at a configuration that works!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background I feel like almost everyone who has had to deal with anything outside the most mundane audio setup on Linux machines has experienced the pain I&#8217;m about to describe. On Mac and Windows machines, audio seems to &#8216;just work&#8217; (TM) (R) (C) &#8212; and on linux desktops like Ubuntu it seems to be fine &hellip; <a href=\"http:\/\/eequalspi.com\/index.php\/2019\/08\/25\/mopidy-and-multiple-audio-streams-from-the-command-line-on-raspberry-pi\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mopidy and Multiple Audio Streams From the Command Line on Raspberry Pi&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[18,22,19,20,16,17,21],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-alsa","tag-linux","tag-mopidy","tag-pcm5102a","tag-raspberrypi","tag-rpi","tag-uniac"],"_links":{"self":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":7,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":195,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions\/195"}],"wp:attachment":[{"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/eequalspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}