Skip to content

Commit

Permalink
Add PressToTalkCommand class for PTT functionality
Browse files Browse the repository at this point in the history
Introduce PressToTalkCommand class in Loupedeck.Steinerd.AudioSwitcherPlugin.Actions namespace. This class inherits from PluginDynamicCommand and implements INotifyPropertyChanged. It manages PTT functionality, allowing users to hold a button to enable communication and release it to mute. Added PTTState property to track PTT state. Implemented OnPropertyChange to handle property changes and mute/unmute the communication device. Overrode ProcessButtonEvent and ProcessTouchEvent to handle events and update PTTState. Overrode GetCommandImage to provide visual representation of PTT state.
  • Loading branch information
steinerd committed Oct 19, 2024
1 parent 8ddbd55 commit c5d1b7f
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/Steinerd.AudioSwitcherPlugin/Actions/PressToTalkCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
namespace Loupedeck.Steinerd.AudioSwitcherPlugin.Actions
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AudioSwitcher.AudioApi.CoreAudio;

internal class PressToTalkCommand : PluginDynamicCommand, INotifyPropertyChanged

{
public bool PTTState
{
get => this._pttState;
set
{
this._pttState = value;
this.OnPropertyChange(nameof(this.PTTState));

}
}
private bool _pttState = false;

public CoreAudioDevice DefaultCommunicationDevice =>
AudioSwitcherPlugin.Instance.AudioController.GetDefaultDevice(AudioSwitcher.AudioApi.DeviceType.Capture, AudioSwitcher.AudioApi.Role.Communications);
public PressToTalkCommand() : base("PTT", "Hold to PTT", "Controls") { }

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChange(String propertyName)
{
if (propertyName == nameof(this.PTTState))
{
this.DefaultCommunicationDevice.SetMuteAsync(!this.PTTState);
}

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected override Boolean ProcessButtonEvent(String actionParameter, DeviceButtonEvent buttonEvent)
{
if (buttonEvent.IsPressed)
{
this.PTTState = true;
this.ActionImageChanged();
return true;
}
else if (!buttonEvent.IsPressed)
{
this.PTTState = false;
this.ActionImageChanged();
return true;
}

return true;
}

protected override Boolean ProcessTouchEvent(String actionParameter, DeviceTouchEvent touchEvent)
{

if (touchEvent.EventType == DeviceTouchEventType.TouchDown)
{
this.PTTState = true;
this.ActionImageChanged();
return true;
}
else if (touchEvent.EventType == DeviceTouchEventType.TouchUp)
{
this.PTTState = false;
this.ActionImageChanged();
return true;
}

return true;
}

protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
{
var builder = new BitmapBuilder(imageSize);
builder.Clear(this.PTTState ? new BitmapColor(0, 255, 0) : new BitmapColor(0, 0, 0));

builder.DrawText("PTT", BitmapColor.White);


return builder.ToImage();
}
}

}

0 comments on commit c5d1b7f

Please sign in to comment.