Add a LinearVolume wrapper for mediaElement.Volume (#834)

Co-authored-by: Frank Becker <frank.becker@thoughtexchange.com>
This commit is contained in:
Frank Becker
2021-02-22 12:10:36 -08:00
committed by GitHub
parent 36e9d97509
commit 7377b4f2ab
2 changed files with 34 additions and 9 deletions

View File

@@ -162,7 +162,7 @@
<Button.Style> <Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource ControlButtonStyle}"> <Style TargetType="Button" BasedOn="{StaticResource ControlButtonStyle}">
<Setter Property="Content" <Setter Property="Content"
Value="{Binding ElementName=mediaElement, Path=Volume, Converter={StaticResource VolumeToIconConverter}}" /> Value="{Binding ElementName=viewerPanel, Path=LinearVolume, Converter={StaticResource VolumeToIconConverter}}" />
</Style> </Style>
</Button.Style> </Button.Style>
</Button> </Button>
@@ -198,7 +198,7 @@
<Grid Height="32" Width="130" HorizontalAlignment="Right" VerticalAlignment="Bottom" <Grid Height="32" Width="130" HorizontalAlignment="Right" VerticalAlignment="Bottom"
Background="{DynamicResource CaptionButtonHoverBackground}" Margin="0,0,0,32"> Background="{DynamicResource CaptionButtonHoverBackground}" Margin="0,0,0,32">
<Slider Style="{StaticResource CustomSliderStyle}" Width="110" Maximum="1" <Slider Style="{StaticResource CustomSliderStyle}" Width="110" Maximum="1"
Value="{Binding ElementName=mediaElement, Path=Volume}" /> Value="{Binding ElementName=viewerPanel, Path=LinearVolume}" />
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>

View File

@@ -145,7 +145,7 @@ namespace QuickLook.Plugin.VideoViewer
public void Dispose() public void Dispose()
{ {
// old plugin use an int-typed "Volume" config key ranged from 0 to 100. Let's use a new one here. // old plugin use an int-typed "Volume" config key ranged from 0 to 100. Let's use a new one here.
SettingHelper.Set("VolumeDouble", mediaElement.Volume); SettingHelper.Set("VolumeDouble", LinearVolume);
SettingHelper.Set("ShouldLoop", ShouldLoop); SettingHelper.Set("ShouldLoop", ShouldLoop);
try try
@@ -280,13 +280,38 @@ namespace QuickLook.Plugin.VideoViewer
: Visibility.Visible; : Visibility.Visible;
} }
// Newer .net has Math.Clamp
private T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
// A change in amplitude by a factor of 10 corresponds to a 20 dB change
private const double DecibelAmplitudeMult = 20.0;
public double LinearVolume
{
// mediaElement.Volume returns [0,1] where 0 = -100db, 1 = 0db
// Decibel is logarithmic. See amplitude table https://en.wikipedia.org/wiki/Decibel
get
{
var dbVol = 100.0 * (mediaElement.Volume - 1.0);
var linearVol = Math.Pow(10, dbVol / DecibelAmplitudeMult);
return linearVol;
}
set
{
var linearVol = Clamp(value, 0.00001, 1.0);
var dbVol = DecibelAmplitudeMult * Math.Log10(linearVol);
mediaElement.Volume = dbVol / 100.0 + 1.0;
OnPropertyChanged();
}
}
private void ChangeVolume(double delta) private void ChangeVolume(double delta)
{ {
var newVol = mediaElement.Volume + delta; LinearVolume = LinearVolume + delta; // setter will clamp
newVol = Math.Max(newVol, 0);
newVol = Math.Min(newVol, 1);
mediaElement.Volume = newVol;
} }
private void TogglePlayPause(object sender, EventArgs e) private void TogglePlayPause(object sender, EventArgs e)
@@ -316,7 +341,7 @@ namespace QuickLook.Plugin.VideoViewer
mediaElement.Source = new Uri(path); mediaElement.Source = new Uri(path);
// old plugin use an int-typed "Volume" config key ranged from 0 to 100. Let's use a new one here. // old plugin use an int-typed "Volume" config key ranged from 0 to 100. Let's use a new one here.
mediaElement.Volume = SettingHelper.Get("VolumeDouble", 1d); LinearVolume = SettingHelper.Get("VolumeDouble", 1d);
mediaElement.Play(); mediaElement.Play();
} }