-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from t3knomanzer/mode-master-volume
Mode master volume
- Loading branch information
Showing
9 changed files
with
219 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Desktop/Application/MaxMix/Services/Audio/AudioEndpointVolumeWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using CSCore.CoreAudioAPI; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MaxMix.Services.Audio | ||
{ | ||
/// <summary> | ||
/// Provides a facade with a simpler interface over the AudioSessionControl | ||
/// CSCore class. | ||
/// </summary> | ||
public class AudioEndpointVolumeWrapper : IDisposable | ||
{ | ||
#region Constructor | ||
public AudioEndpointVolumeWrapper(AudioEndpointVolume endpointVolume) | ||
{ | ||
_endpointVolume = endpointVolume; | ||
|
||
_callback = new AudioEndpointVolumeCallback(); | ||
_callback.NotifyRecived += OnEndpointNotifyReceived; | ||
_endpointVolume.RegisterControlChangeNotify(_callback); | ||
|
||
_isNotifyEnabled = true; | ||
} | ||
#endregion | ||
|
||
#region Events | ||
/// <summary> | ||
/// Raised when the volume of this session has changed. | ||
/// </summary> | ||
public event EventHandler<AudioEndpointVolumeCallbackEventArgs> VolumeChanged; | ||
#endregion | ||
|
||
#region Fields | ||
private AudioEndpointVolume _endpointVolume; | ||
private AudioEndpointVolumeCallback _callback; | ||
private bool _isNotifyEnabled; | ||
#endregion | ||
|
||
#region Properties | ||
/// <summary> | ||
/// The display name of the process that created this session. | ||
/// </summary> | ||
public string DisplayName | ||
{ | ||
get => "Master"; | ||
} | ||
|
||
/// <summary> | ||
/// Current volume of this session (0-100). | ||
/// </summary> | ||
public int Volume | ||
{ | ||
get => (int)(_endpointVolume.MasterVolumeLevelScalar * 100); | ||
set | ||
{ | ||
_isNotifyEnabled = false; | ||
_endpointVolume.MasterVolumeLevelScalar = value / 100f; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Current mute state of this session. | ||
/// </summary> | ||
public bool IsMuted | ||
{ | ||
get => _endpointVolume.IsMuted; | ||
set | ||
{ | ||
_isNotifyEnabled = false; | ||
_endpointVolume.IsMuted = value; | ||
} | ||
} | ||
#endregion | ||
|
||
#region Event Handlers | ||
private void OnEndpointNotifyReceived(object sender, AudioEndpointVolumeCallbackEventArgs e) | ||
{ | ||
if (!_isNotifyEnabled) | ||
{ | ||
_isNotifyEnabled = true; | ||
return; | ||
} | ||
|
||
VolumeChanged?.Invoke(this, e); | ||
} | ||
#endregion | ||
|
||
#region IDisposable | ||
public void Dispose() | ||
{ | ||
if (_callback != null) | ||
_endpointVolume.UnregisterControlChangeNotify(_callback); | ||
|
||
_endpointVolume = null; | ||
_callback = null; | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.