Enabling ALSA dmix - Multiple Audio Apps

Guides and tutorials for various stuff. Posted by DietPi users.
Post Reply
rothchild
Posts: 74
Joined: Sat Jun 27, 2015 10:39 am

Enabling ALSA dmix - Multiple Audio Apps

Post by rothchild »

Just got this working so thought I'd share.

I wanted to install mopidy https://www.mopidy.com/ on my dietpi so I can have Spotify (albeit via a premium acct), but I still prefer to use Subsonic for my local file playback and for remote streaming online.

Mopidy is a really easy install (and I think it should be added to DietPi as another audio option) but the two apps weren't playing nicely together in terms of sharing my DAC soundcard (which doesn't have any mixing function) basically Subsonic wasn't 'letting go' of the card so once it had played anything mopidy refused to play anymore.

The solution is to use ALSA's Dmix plugin.

First I edited /DietPi/config.txt and added:

Code: Select all

dtoverlay=i2s-mmap
I then edited /etc/asound.conf to be:

Code: Select all

pcm.dmixed {
        type asym
        playback.pcm {
                type dmix
                ipc_key 5678293
                ipc_perm 0660
                ipc_gid audio

                slave {
                        channels 2 # make 6 or 5.1 channel
                        pcm {
                                format S16_LE # S32_LE
                                rate 44100 # can also be 44100
                                type hw
                                card 1 # your card
                                device 7 # your device
                                subdevice 0 #important?
                        }

                        period_size 1024
                        buffer_size 8192
                }

                bindings {
                        0 0
                        1 1
# Uncomment below if using 6 channel
#                       2 2
#                       3 3
#                       4 4
#                       5 5
                }
        }
        capture.pcm "hw:0"
}

pcm.!default {
        type plug
        slave.pcm "dmixed"
}
(from https://wiki.gentoo.org/wiki/ALSA - you'll note I've changed the sample rate to 44100 from 48000 as all my files are 44100 and I didn't want them resampled)

Rebooted and now I can play tracks from mopidy, close it, open subsonic play tracks, close it, go back again etc etc


For completeness:
To install mopidy I simple followed the guide on their site: Add their repository, add their key, install mopidy, mopidy-spotify_tunigo and mopidy-sqlite I also installed python-pip so I could get some of the other add-ons that aren't in the repository using pip-install (the 'spotmop' web interface is pretty natty). I then set mopidy to run as a service using: dpkg-reconfigure mopidy and edited /etc/mopidy/mopidy.conf as below:

Code: Select all

[http]
enabled = true
hostname = 192.168.1.10
port = 6680

[spotify]
username = name
password = secret
bitrate = 320

[spotify_tunigo]
enabled = true

[core]
cache_dir = /var/cache/mopidy
config_dir = /etc/mopidy
data_dir = /var/lib/mopidy

[logging]
config_file = /etc/mopidy/logging.conf
debug_file = /var/log/mopidy/mopidy-debug.log

[local]
enabled = true
library = images
data_dir = /var/lib/mopidy/local
media_dir = /mnt/usb_1/
scan_timeout = 1000
scan_flush_threshold = 100
scan_follow_symlinks = false
excluded_file_extensions =
  .directory
  .html
  .jpeg
  .jpg
  .log
  .nfo
  .png
  .txt

[local-sqlite]
enabled = true

[local-images]
library = sqlite

[file]
enabled = false
media_dirs = /mnt/usb_1
show_dotfiles = false
follow_symlinks = false
metadata_timeout = 1000

[m3u]
playlists_dir = /var/lib/mopidy/playlists

[mpd]
enabled = false

[audio]
mixer = software
mixer_volume =
output = autoaudiosink
buffer_time =
Last edited by rothchild on Mon Oct 17, 2016 7:30 pm, edited 1 time in total.
rothchild
Posts: 74
Joined: Sat Jun 27, 2015 10:39 am

Re: Enabling ALSA dmix - Multiple Audio Apps

Post by rothchild »

Small edit/addition,

Whey you edit the asound.conf file you need to know what card and device numbers to use (mine were actually 0 and 0) you can find these by doing:

Code: Select all

aplay -l
Last edited by rothchild on Mon Oct 17, 2016 7:30 pm, edited 1 time in total.
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Enabling ALSA dmix - Multiple Audio Apps

Post by Fourdee »

Excellent post Roth and thanks for sharing, stickied! :)
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
rothchild
Posts: 74
Joined: Sat Jun 27, 2015 10:39 am

Re: Enabling ALSA dmix - Multiple Audio Apps

Post by rothchild »

Small update.

I wanted to run alsa eq on one of my setups, it was quite straightforward to getting it working with mpd but it's taken a bit longer to also have it work with mopidy, so here are the ingredients to enable both dmix and alsaequal so you can have both mpd and mopidy play nicely together on the same machine and have an eq for each of them.

Curently my /etc/asound.conf file looks like this:

Code: Select all

ctl.equal {
    type equal;
}

pcm.plugequal {
    type equal;
    # Modify the line below if you do not
    # want to use sound card 0.
    #slave.pcm "plughw:0,0";
    # by default we want to play from more sources at time:
    slave.pcm "plug:dmix";
}

# pcm.equal {
# If you do not want the equalizer to be your
# default soundcard comment the following
# line and uncomment the above line. (You can
# choose it as the output device by addressing
# it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)
pcm.!default {
    type plug;
    slave.pcm plugequal;
}

And the relevant section of /etc/mopidy/mopidy.conf now looks like this (I've also installed the package mopidy-alsamixer)

Code: Select all

[audio]
mixer = alsamixer
output = alsasink device=plug:plugequal

[softwaremixer]
enabled = false

[alsamixer]
enabled = true
card = 0
control = Digital
The bit that took a bit longer to work out is that there are different commands to get the right eq for the app you want to adjust so to tweak mpd I use:

Code: Select all

alsamixer -D equal
and for mopidy:

Code: Select all

sudo -H -u mopidy alsamixer -D equal
Obviously these are a pita to remember each time so a quick nano .bashrc in the root folder and add the following lines at the bottom

Code: Select all

alias spoteq='sudo -H -u mopidy alsamixer -D equal'
alias mpdeq='alsamixer -D equal'
So if I want to eq mopidy I can just type: spoteq or for mpd it's mpdeq

Now I'm going to try and marry up my new asound.conf with the old one to ensure there's minimal sample conversion or any other interference with the audio stream.
Post Reply