Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ALSA PCM softvol plugin #290

Closed
joerg-krause opened this issue May 4, 2016 · 21 comments
Closed

Support for ALSA PCM softvol plugin #290

joerg-krause opened this issue May 4, 2016 · 21 comments

Comments

@joerg-krause
Copy link

I am trying to setup shairport-sync with an ALSA PCM softvol plugin.

asound.conf:

pcm.!default {
      type plug
      slave.pcm "softvol"
  }

pcm.softvol {
     type softvol
      slave {
          pcm "hw:0"
      }
      control {
          name "Master"
          card 0
      }
     min_dB -57.2
     max_dB -6.2
}
ctl.!default {
        type hw
        card 0
}
ctl.softvol {
        type hw
        card 0
}

shairport-sync.conf:

alsa =
{
  output_device = "softvol";
  mixer_control_name = "Master";
};

Running shairport-sync -vvv:

Version: "2.8.3-openssl-Avahi-ALSA-stdout-pipe-metadata"
[...]
Output device name is "softvol".
Open Mixer
Mixer device name is "softvol".
Mixer Control name is "Master".
note: the hardware mixer specified -- "Master" -- does not have a dB volume scale, so it can't be used.
[...]

Looking into audio_alsa shows that snd_mixer_selem_get_playback_dB_range() returns an error code. This is all fine when using a hardware mixer.

However, with an ALSA software mixer, I can get the dB range using snd_ctl_get_dB_range(). e.g.:

min dB: -57.2
max dB: -6.2

What do you think about an option like mixer_type as the MPD audio output configuration has?

Is there any drawback using the softvol plugin for volume setting?

@mikebrady
Copy link
Owner

mikebrady commented May 4, 2016

Hi Jörg-Krause. Thanks for the post. TBH, I have not considered the use of any plug-ins in Shairport Sync. The reason is that i don't know enough about them. In particular, I envisage them as standing between the output of Shair[port Sync and the DAC. Shairport Sync ask the output device for its latency every 8 milliseconds or so, and i don't know if a plug-in will report latency correctly or at all. I've been too concerned with improving the reliability and ease-of-use of Shairport Sync in other areas to look at the plug-ins world.

If you don't specify a mixer name, then Shairport Sync uses the software volume control in Shairport Sync itself. In any case, you could disable the volume control in the Shairport Sync setting and have its output directed to the plug-in at 100% level.

After that, it really depends on the plug-in. If it reports latency correctly, then we are in business; otherwise it's going to lose sync.

@joerg-krause
Copy link
Author

Many thanks for the feedback! What do you mean by:

If you don't specify a mixer name, then shairport-sync assumes So, you could disable the volume control in the Shairport Sync setting...

@joerg-krause
Copy link
Author

@mikebrady I managed to get shairport-sync using the softvol control. Unfortunately, I have a problem with vol2attn. For an airplay volume of -6.2dB to 0.0dB the scaled attenuation is fixed to -12.4dB and the amixer volume slider is not set above 88%. Note, that my max dB value is set to -6.2:

Volume min -5720dB, max -620dB, range 5100dB.
On line 0, end point of -2550.000000, input vol -5.625000 yields output vol -478.125000.
On line 1, end point of -3825.000000, input vol -5.625000 yields output vol -95.625000.
returning an attenuation of -1240.000000.

Volume min -5720dB, max -620dB, range 5100dB.
On line 0, end point of -2550.000000, input vol -3.750000 yields output vol -318.750000.
returning an attenuation of -1240.000000.

Volume min -5720dB, max -620dB, range 5100dB.
On line 0, end point of -2550.000000, input vol -1.875000 yields output vol -159.375000.
returning an attenuation of -1240.000000.

@mikebrady
Copy link
Owner

Very interesting. Let me check that out.

@joerg-krause
Copy link
Author

I opened a pull request for testing: #293.

@mikebrady
Copy link
Owner

Yep, thanks for that!

@joerg-krause
Copy link
Author

Many thanks for merging and making a release! Did you had the chance to look at the scaled attenuation problem with vol2attn? I am suspicious about this line, as it sets vol_setting = (-6.2) + (-6.2) = -14.2. However, you are the expert here 😄

@mikebrady
Copy link
Owner

You're welcome! I haven't had a look at the vol2attn thing – there were more pressing issues to examine, IMHO. Real Soon Now, promise!

@joerg-krause
Copy link
Author

I think I have found the issue. The transfer functions start and end points does not take the max dB value as an offset into account. The y-axes ranges always from 0 to (max - min) dB, where it should range from max to max - (max - min) dB.

I make some tests now and propose a PR later.

@mikebrady
Copy link
Owner

That sounds plausible alright, Jörg-Krause.

@joerg-krause
Copy link
Author

joerg-krause commented May 9, 2016

Yes, it does. However, I was wrong as the max_dB offset is added to the volume value later in https://github.com/mikebrady/shairport-sync/blob/development/common.c#L464. The only mistake is to initialize vol_setting with not zero. Now, I got a correct volume attenuation within -6.2 and -57.2 dB.

image

joerg-krause added a commit to joerg-krause/shairport-sync that referenced this issue May 9, 2016
This fixes calculation of the scaled attenuation if `max_dB` is different from
zero.

Closes: mikebrady#290
@joerg-krause
Copy link
Author

Note, that the ALSA softvol plugin in version 1.1.1 and below is affected by a bug which may result in wrong dB values. This bug has been fixed: http://git.alsa-project.org/?p=alsa-lib.git;a=commit;h=85bf9915989e0a338632739684c75192c1753239.

@ntwallace
Copy link

@joerg-krause can you provide any advice on how you got softvol working? I'm still stuck with the dB vol scale error, despite setting a min_dB and max_dB

@joerg-krause
Copy link
Author

Hi @ntwallace, basically I am using the configuration settings as said in the first post of this thread.

What is exactly the error message?

What are your settings for asound.conf and shairport-sync.conf?

What ALSA version does you're system provide?

@ntwallace
Copy link

ntwallace commented Oct 2, 2017

Thanks for the quick response @joerg-krause. I'm getting the similar error about my mixer not having a dB volume scale.

FYI my hardware setup is a Rpi Zero with a Phat DAC attached (which doesn't do hardware volume), so I am using a combo of dmix and softvol to control volume via software.

The ALSA version is 1.1.3 -- I guess at some point they made some changes to the architecture so that softvol is implemented by the .asoundrc file in root instead of asound.conf in /etc/.

shairport-sync -v error:

...
alsa output device name is "hw:0".
note: the hardware mixer specified -- "Master" -- does not have a dB volume scale, so it can't be used. Trying software volume control.
Volume control "Master" has dB volume from -50.500000 to -7.150000.
...

shairport-sync.conf code:

general =
{
        name = "Pi Music Box";
        volume_range_db = 60 ;
};
...
alsa =
{
    output_device = "hw:0";
    mixer_control_name = "Master";
};
...

asound.conf code:

pcm.!default  {
 type hw card 0
}

ctl.!default {
 type hw card 0
}

.asoundrc code:

pcm.!default {
    type           hw card 0
    slave.pcm      "softvol"   #make use of softvol
}

pcm.softvol {
    type            softvol
    slave {
        pcm        "dmix"      #redirect the output to dmix (instead of "hw:0,0")
    }
    control {
        name       "Master"       #override the PCM slider to set the softvol volume level globally
        card        0
    }
    min_dB -50.5
    max_dB -7.5
}

@mikebrady
Copy link
Owner

mikebrady commented Oct 2, 2017

Just wondering if the very latest fix on the development branch is relevant at all — perhaps @joerg-krause has a view?

@ntwallace
Copy link

@mikebrady Can you share the 2 missing lines of code for the v3.2d8 of audio_alsa.c and I can test? Or should I compile the dev branch and update to test?

@joerg-krause
Copy link
Author

@ntwallace You're configuration is a bit messy. First, shairport-sync is not configured to use the softvol plugin, but the hardware device. Second, your softvol plugin uses a dmix plugin as pcm slave, but no dmix is configured in your ALSA config.

For the beginning, please try this:

Replace the alsa stanza in shairport-sync.conf:

alsa =
{
    output_device = "softvol";
    mixer_control_name = "Master";
};

Replace the .asoundrc code:

pcm.softvol {
    type            softvol
    slave {
        pcm        "hw:0"
    }
    control {
        name       "Master"
        card        0
    }
}
ctl.softvol {
    type hw
    card 0
}

If it works, you can add the min_dB and max_dB settings. Note, that ALSA disables dB settings for soft-float targets.

@ntwallace
Copy link

ntwallace commented Oct 2, 2017

Thanks @joerg-krause, good point.

I actually tried that at first, but was getting an error. When I update both to those, shairport-sync -v gives me:

alsa output device name is "softvol".
ALSA lib control.c:1373:(snd_ctl_open_noupdate) Invalid CTL softvol
fatal error: Failed to attach mixer
Request to shut down all rtsp conversation threads
asking playing threads to stop

I was able to get shairport-sync to load by updating asound.conf to include a ctl.softvol:

pcm.!default  {
 type hw card 0
 slave.pcm "softvol"  #added softvol as slave 
}

ctl.!default {
 type hw card 0
}

ctl.softvol {  #added ctl for softvol
    type hw
    card 0
}

However, shairport-sync -v still gives me the same dB error:

alsa output device name is "softvol".
note: the hardware mixer specified -- "Master" -- does not have a dB volume scale, so it can't be used. Trying software volume control.
Volume control "Master" has dB volume from -50.500000 to -7.150000.

Adding code for pcm.softvol directly into to asound.conf doesn't change the above error:

pcm.softvol {
    type            softvol
    slave {
        pcm         "hw:0"      #redirect the output to dmix (instead of "hw:0,0")
    }
    control {
        name        "Master"       #override the PCM slider to set the softvol volume level globally
        card        0
    }
    min_dB -50.5
    max_dB -7.5
}

@mikebrady
Copy link
Owner

Nick, I'd say it's probably best to move to the development branch. For one thing, in the event that any changes are needed, they'll go into the development branch.

@joerg-krause
Copy link
Author

@ntwallace Actually, it isn't an error message, but a note that softvol plugin is been used by shairport-sync. My guess is that your ALSA config is still wrong.

This stanza at leadt is still wrong:

pcm.!default  {
 type hw card 0
 slave.pcm "softvol"  #added softvol as slave 
}

Replace it by:

pcm.!default  {
 type plug
 slave.pcm "softvol"  #added softvol as slave 
}

Furthermore, maybe some config options in your system wide alsa config file /etc/asound.conf does collide with your user alsa config file .asoundrc. Please post the content of both and a full log of shairport-sync.

Does aplay -D softvol <somewavfile> work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants