-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
137 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/Project/Sucrose.Backgroundog/Extension/AudioSession.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,100 @@ | ||
using NPSMLib; | ||
using System.IO; | ||
using SBET = Sucrose.Backgroundog.Extension.Thumbnail; | ||
using SBMI = Sucrose.Backgroundog.Manage.Internal; | ||
|
||
namespace Sucrose.Backgroundog.Extension | ||
{ | ||
internal class AudioSession | ||
{ | ||
private static void SetupEvents() | ||
{ | ||
if (SBMI.PlayingSession != null) | ||
{ | ||
SBMI.DataSource = SBMI.PlayingSession.ActivateMediaPlaybackDataSource(); | ||
SBMI.DataSource.MediaPlaybackDataChanged += (s, e) => SetCurrentSession(); | ||
} | ||
} | ||
|
||
public static void SessionListChanged() | ||
{ | ||
SBMI.PlayingSession = SBMI.SessionManager.CurrentSession; | ||
SetupEvents(); | ||
SetCurrentSession(); | ||
} | ||
|
||
private static void SetCurrentSession() | ||
{ | ||
if (SBMI.PlayingSession != null) | ||
{ | ||
lock (SBMI.LockObject) | ||
{ | ||
MediaObjectInfo MediaDetails = SBMI.DataSource.GetMediaObjectInfo(); | ||
MediaPlaybackInfo MediaPlaybackInfo = SBMI.DataSource.GetMediaPlaybackInfo(); | ||
MediaTimelineProperties MediaTimeline = SBMI.DataSource.GetMediaTimelineProperties(); | ||
|
||
string ThumbnailString = null; | ||
|
||
using (Stream Thumbnail = SBMI.DataSource.GetThumbnailStream()) | ||
{ | ||
if (Thumbnail != null) | ||
{ | ||
ThumbnailString = SBET.Create(Thumbnail); | ||
Thumbnail.Flush(); | ||
} | ||
} | ||
|
||
SBMI.AudioData.State = true; | ||
SBMI.AudioData.Title = MediaDetails.Title; | ||
SBMI.AudioData.Artist = MediaDetails.Artist; | ||
SBMI.AudioData.Subtitle = MediaDetails.Subtitle; | ||
SBMI.AudioData.AlbumTitle = MediaDetails.AlbumTitle; | ||
SBMI.AudioData.AlbumArtist = MediaDetails.AlbumArtist; | ||
SBMI.AudioData.TrackNumber = MediaDetails.TrackNumber; | ||
SBMI.AudioData.AlbumTrackCount = MediaDetails.AlbumTrackCount; | ||
SBMI.AudioData.MediaType = MediaPlaybackDataSource.MediaSchemaToMediaPlaybackMode(MediaDetails.MediaClassPrimaryID); | ||
|
||
SBMI.AudioData.PID = SBMI.PlayingSession?.PID; | ||
SBMI.AudioData.Hwnd = SBMI.PlayingSession?.Hwnd; | ||
SBMI.AudioData.SourceAppId = SBMI.PlayingSession?.SourceAppId; | ||
SBMI.AudioData.SourceDeviceId = SBMI.PlayingSession?.SourceDeviceId; | ||
SBMI.AudioData.RenderDeviceId = SBMI.PlayingSession?.RenderDeviceId; | ||
|
||
SBMI.AudioData.RepeatMode = MediaPlaybackInfo.RepeatMode; | ||
SBMI.AudioData.PropsValid = MediaPlaybackInfo.PropsValid; | ||
SBMI.AudioData.PlaybackRate = MediaPlaybackInfo.PlaybackRate; | ||
SBMI.AudioData.PlaybackMode = MediaPlaybackInfo.PlaybackMode; | ||
SBMI.AudioData.PlaybackCaps = MediaPlaybackInfo.PlaybackCaps; | ||
SBMI.AudioData.PlaybackState = MediaPlaybackInfo.PlaybackState; | ||
SBMI.AudioData.ShuffleEnabled = MediaPlaybackInfo.ShuffleEnabled; | ||
SBMI.AudioData.LastPlayingFileTime = MediaPlaybackInfo.LastPlayingFileTime; | ||
|
||
SBMI.AudioData.EndTime = MediaTimeline.EndTime; | ||
SBMI.AudioData.Position = MediaTimeline.Position; | ||
SBMI.AudioData.StartTime = MediaTimeline.StartTime; | ||
SBMI.AudioData.MinSeekTime = MediaTimeline.MinSeekTime; | ||
SBMI.AudioData.MaxSeekTime = MediaTimeline.MaxSeekTime; | ||
SBMI.AudioData.PositionSetFileTime = MediaTimeline.PositionSetFileTime; | ||
|
||
SBMI.AudioData.ThumbnailString = ThumbnailString; | ||
|
||
if (string.IsNullOrEmpty(ThumbnailString)) | ||
{ | ||
SBMI.AudioData.ThumbnailAddress = ThumbnailString; | ||
} | ||
else | ||
{ | ||
SBMI.AudioData.ThumbnailAddress = "data:image/png;base64," + ThumbnailString; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
lock (SBMI.LockObject) | ||
{ | ||
SBMI.AudioData.State = false; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Drawing.Imaging; | ||
using System.IO; | ||
using SBMM = Sucrose.Backgroundog.Manage.Manager; | ||
|
||
namespace Sucrose.Backgroundog.Extension | ||
{ | ||
internal class Thumbnail | ||
{ | ||
public static string Create(Stream stream) | ||
{ | ||
using MemoryStream Stream = new(); | ||
|
||
Stream.Seek(0, SeekOrigin.Begin); | ||
stream.CopyTo(Stream); | ||
|
||
if (!SBMM.Windows11_OrGreater) | ||
{ | ||
using Bitmap Image = new(Stream); | ||
|
||
if (PixelAlpha(Image, 0, 0)) | ||
{ | ||
return CropImage(Image, 34, 1, 233, 233); | ||
} | ||
} | ||
|
||
byte[] Array = Stream.ToArray(); | ||
|
||
return Convert.ToBase64String(Array); | ||
} | ||
|
||
private static bool PixelAlpha(Bitmap Image, int X, int Y) | ||
{ | ||
return Image.GetPixel(X, Y).A == 0; | ||
} | ||
|
||
private static string CropImage(Bitmap Image, int X, int Y, int Width, int Height) | ||
{ | ||
Rectangle Rect = new(X, Y, Width, Height); | ||
|
||
using Bitmap CroppedBitmap = new(Rect.Width, Rect.Height, Image.PixelFormat); | ||
|
||
Graphics Graphic = Graphics.FromImage(CroppedBitmap); | ||
Graphic.DrawImage(Image, 0, 0, Rect, GraphicsUnit.Pixel); | ||
|
||
using MemoryStream Stream = new(); | ||
|
||
CroppedBitmap.Save(Stream, ImageFormat.Png); | ||
|
||
byte[] ByteImage = Stream.ToArray(); | ||
|
||
return Convert.ToBase64String(ByteImage); | ||
} | ||
} | ||
} |
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.