Skip to content

Commit

Permalink
Add a LinearVolume wrapper for mediaElement.Volume (#834)
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Becker <frank.becker@thoughtexchange.com>
  • Loading branch information
mooflu and fb-te authored Feb 22, 2021
1 parent 36e9d97 commit 7377b4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource ControlButtonStyle}">
<Setter Property="Content"
Value="{Binding ElementName=mediaElement, Path=Volume, Converter={StaticResource VolumeToIconConverter}}" />
Value="{Binding ElementName=viewerPanel, Path=LinearVolume, Converter={StaticResource VolumeToIconConverter}}" />
</Style>
</Button.Style>
</Button>
Expand Down Expand Up @@ -198,7 +198,7 @@
<Grid Height="32" Width="130" HorizontalAlignment="Right" VerticalAlignment="Bottom"
Background="{DynamicResource CaptionButtonHoverBackground}" Margin="0,0,0,32">
<Slider Style="{StaticResource CustomSliderStyle}" Width="110" Maximum="1"
Value="{Binding ElementName=mediaElement, Path=Volume}" />
Value="{Binding ElementName=viewerPanel, Path=LinearVolume}" />
</Grid>
</Grid>
</Grid>
Expand Down
39 changes: 32 additions & 7 deletions QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private set
public void Dispose()
{
// 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);

try
Expand Down Expand Up @@ -280,13 +280,38 @@ private void UpdateMeta(string path, MediaInfo.MediaInfo info)
: Visibility.Visible;
}

private void ChangeVolume(double delta)
// 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
{
var newVol = mediaElement.Volume + delta;
newVol = Math.Max(newVol, 0);
newVol = Math.Min(newVol, 1);
// 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();
}
}

mediaElement.Volume = newVol;
private void ChangeVolume(double delta)
{
LinearVolume = LinearVolume + delta; // setter will clamp
}

private void TogglePlayPause(object sender, EventArgs e)
Expand Down Expand Up @@ -316,7 +341,7 @@ public void LoadAndPlay(string path, MediaInfo.MediaInfo info)

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.
mediaElement.Volume = SettingHelper.Get("VolumeDouble", 1d);
LinearVolume = SettingHelper.Get("VolumeDouble", 1d);

mediaElement.Play();
}
Expand Down

0 comments on commit 7377b4f

Please # to comment.