Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
L3-NR authored Apr 30, 2023
1 parent 2e70875 commit a191e22
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Buffers;
using System.Net;
using System.Net.Sockets;
using Windows.Media.Control;

namespace PotatOSC
{
class Program
{
static async Task Main(string[] args)
{
UdpClient client = new UdpClient();
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 9000;
IPEndPoint endPoint = new IPEndPoint(ip, port);

var smtc = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
var session = smtc.GetCurrentSession();

if (session != null)
{
session.MediaPropertiesChanged += async (sender, args) =>
{
var mediaProperties = await sender.TryGetMediaPropertiesAsync();
await SendOSCMessage(client, endPoint, mediaProperties);
};

var mediaProperties = await session.TryGetMediaPropertiesAsync();
await SendOSCMessage(client, endPoint, mediaProperties);
}

Console.WriteLine("Press any key on this console window to quit...");
Console.ReadLine();
}

private static async Task SendOSCMessage(UdpClient client, IPEndPoint endPoint, GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties)
{
string address = "/chatbox/input";
string typeTag = ",s";
string argument = $"Now playing: {mediaProperties.Title} by {mediaProperties.Artist}";

using (var ms = new MemoryStream())
{
WritePadded(ms, System.Text.Encoding.ASCII.GetBytes(address));
WritePadded(ms, System.Text.Encoding.ASCII.GetBytes(typeTag));
WritePadded(ms, System.Text.Encoding.ASCII.GetBytes(argument));

await client.SendAsync(ms.ToArray(), (int)ms.Length, endPoint);
}
}

private static void WritePadded(MemoryStream ms, byte[] input)
{
ms.Write(input, 0, input.Length);

int padding = 4 - (input.Length % 4);
if (padding == 4) return;

byte[] buffer = ArrayPool<byte>.Shared.Rent(padding);
Array.Clear(buffer, 0, padding);
ms.Write(buffer, 0, padding);
ArrayPool<byte>.Shared.Return(buffer);
}
}
}



//haha now it's 69 lines long
Binary file added potatosc.ico
Binary file not shown.

0 comments on commit a191e22

Please sign in to comment.