Updated audio image extraction logic. Added caching classes.
This commit is contained in:
36
Harmonia.Core/Caching/AudioImageMemoryCache.cs
Normal file
36
Harmonia.Core/Caching/AudioImageMemoryCache.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public class AudioImageMemoryCache(IAudioImageExtractor audioImageExtractor) : MemoryCache<Song, SongPictureInfo>, IAudioImageCache
|
||||
{
|
||||
protected override object? GetKey(Song key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key.ImageHash) == false)
|
||||
{
|
||||
return key.ImageHash;
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(key.ImageName) == false)
|
||||
{
|
||||
return key.ImageName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override SongPictureInfo? Fetch(Song key)
|
||||
{
|
||||
return audioImageExtractor.ExtractImage(key.FileName);
|
||||
}
|
||||
|
||||
protected override void AddToCache(object key, SongPictureInfo entry)
|
||||
{
|
||||
base.AddToCache(key, entry);
|
||||
}
|
||||
|
||||
protected override long GetEntrySize(SongPictureInfo entry)
|
||||
{
|
||||
return entry.Size;
|
||||
}
|
||||
}
|
||||
40
Harmonia.Core/Caching/Cache.cs
Normal file
40
Harmonia.Core/Caching/Cache.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public abstract class Cache<TKey, TValue> : ICache<TKey, TValue> where TKey : notnull
|
||||
{
|
||||
protected abstract TValue? TryGetValue(object key);
|
||||
protected abstract TValue? Fetch(TKey key);
|
||||
protected abstract void AddToCache(object key, TValue value);
|
||||
|
||||
public TValue? Get(TKey key)
|
||||
{
|
||||
object? actualKey = GetKey(key);
|
||||
|
||||
if (actualKey == null)
|
||||
return default;
|
||||
|
||||
return TryGetValue(actualKey) ?? Refresh(key);
|
||||
}
|
||||
|
||||
public TValue? Refresh(TKey key)
|
||||
{
|
||||
object? actualKey = GetKey(key);
|
||||
|
||||
if (actualKey == null)
|
||||
return default;
|
||||
|
||||
TValue? value = Fetch(key);
|
||||
|
||||
if (value == null)
|
||||
return default;
|
||||
|
||||
AddToCache(actualKey, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
protected virtual object? GetKey(TKey key)
|
||||
{
|
||||
return key;
|
||||
}
|
||||
}
|
||||
9
Harmonia.Core/Caching/IAudioImageCache.cs
Normal file
9
Harmonia.Core/Caching/IAudioImageCache.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Models;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public interface IAudioImageCache : ICache<Song, SongPictureInfo>
|
||||
{
|
||||
|
||||
}
|
||||
7
Harmonia.Core/Caching/ICache.cs
Normal file
7
Harmonia.Core/Caching/ICache.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public interface ICache<TKey, TValue>
|
||||
{
|
||||
TValue? Get(TKey key);
|
||||
TValue? Refresh(TKey key);
|
||||
}
|
||||
43
Harmonia.Core/Caching/MemoryCache.cs
Normal file
43
Harmonia.Core/Caching/MemoryCache.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace Harmonia.Core.Caching;
|
||||
|
||||
public abstract class MemoryCache<TKey, TValue> : Cache<TKey, TValue> where TKey : notnull
|
||||
{
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
|
||||
protected virtual long? SizeLimit => 2000000000;
|
||||
protected virtual double CompactionPercentage => 0.2;
|
||||
protected virtual TimeSpan SlidingExpiration => TimeSpan.FromSeconds(600);
|
||||
|
||||
public MemoryCache()
|
||||
{
|
||||
MemoryCacheOptions memoryCacheOptions = new()
|
||||
{
|
||||
SizeLimit = SizeLimit,
|
||||
CompactionPercentage = CompactionPercentage
|
||||
};
|
||||
|
||||
_memoryCache = new MemoryCache(memoryCacheOptions);
|
||||
}
|
||||
|
||||
protected override TValue? TryGetValue(object key)
|
||||
{
|
||||
_memoryCache.TryGetValue(key, out TValue? value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
protected override void AddToCache(object key, TValue entry)
|
||||
{
|
||||
long entrySize = GetEntrySize(entry);
|
||||
|
||||
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSize(entrySize)
|
||||
.SetSlidingExpiration(SlidingExpiration);
|
||||
|
||||
_memoryCache.Set(key, entry, cacheEntryOptions);
|
||||
}
|
||||
|
||||
protected abstract long GetEntrySize(TValue entry);
|
||||
}
|
||||
25
Harmonia.Core/Extensions/ServiceCollectionExtensions.cs
Normal file
25
Harmonia.Core/Extensions/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Harmonia.Core.Engine;
|
||||
using Harmonia.Core.Imaging;
|
||||
using Harmonia.Core.Player;
|
||||
using Harmonia.Core.Playlists;
|
||||
using Harmonia.Core.Scanner;
|
||||
using Harmonia.Core.Tags;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Harmonia.Core.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddHarmonia(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IAudioEngine, BassAudioEngine>();
|
||||
services.AddSingleton<IAudioPlayer, AudioPlayer>();
|
||||
services.AddSingleton<ITagResolver, TagLibTagResolver>();
|
||||
services.AddSingleton<IAudioImageExtractor, AudioImageExtractor>();
|
||||
services.AddSingleton<IAudioFileScanner, AudioFileScanner>();
|
||||
services.AddSingleton<IPlaylistRepository, PlaylistRepository>();
|
||||
services.AddSingleton<IPlaylistManager, PlaylistManager>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ManagedBass" Version="3.1.1" />
|
||||
<PackageReference Include="ManagedBass.Flac" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.2" />
|
||||
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
|
||||
<PackageReference Include="TagLibSharp" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -2,16 +2,58 @@
|
||||
|
||||
namespace Harmonia.Core.Imaging;
|
||||
|
||||
public class AudioImageExtractor(ITagResolver tagResolver) : IAudioImageExtractor
|
||||
public class AudioImageExtractor : IAudioImageExtractor
|
||||
{
|
||||
public SongPictureInfo ExtractImage(string fileName)
|
||||
{
|
||||
SongTagInfo songTagInfo = tagResolver.GetSongTagInfo(fileName);
|
||||
private readonly ITagResolver _tagResolver;
|
||||
private readonly string[] _searchPatterns;
|
||||
private readonly string[] _imageExtensions;
|
||||
|
||||
return ExtractImage(fileName, songTagInfo);
|
||||
public AudioImageExtractor(ITagResolver tagResolver)
|
||||
{
|
||||
_tagResolver = tagResolver;
|
||||
_searchPatterns = GetSearchPatterns();
|
||||
_imageExtensions = GetImageExtensions();
|
||||
}
|
||||
|
||||
public SongPictureInfo ExtractImage(string fileName, SongTagInfo songTagInfo)
|
||||
protected virtual string[] GetSearchPatterns()
|
||||
{
|
||||
return
|
||||
[
|
||||
@"*%filename%*",
|
||||
@"*folder*",
|
||||
@"*front*",
|
||||
@"*cover*",
|
||||
@"Cover\*folder*",
|
||||
@"Cover\*front*",
|
||||
@"Cover\*cover*",
|
||||
@"Cover\*",
|
||||
@"Scans\*folder*",
|
||||
@"Scans\*front*",
|
||||
@"Scans\*cover*",
|
||||
@"Scans\*",
|
||||
@"*"
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected virtual string[] GetImageExtensions()
|
||||
{
|
||||
return
|
||||
[
|
||||
"jpg",
|
||||
"jpeg",
|
||||
"png"
|
||||
];
|
||||
}
|
||||
|
||||
public SongPictureInfo? ExtractImage(string path)
|
||||
{
|
||||
SongTagInfo songTagInfo = _tagResolver.GetSongTagInfo(path);
|
||||
|
||||
return ExtractImage(path, songTagInfo);
|
||||
}
|
||||
|
||||
public SongPictureInfo? ExtractImage(string path, SongTagInfo songTagInfo)
|
||||
{
|
||||
if (songTagInfo.FrontCover != null)
|
||||
{
|
||||
@@ -21,7 +63,47 @@ public class AudioImageExtractor(ITagResolver tagResolver) : IAudioImageExtracto
|
||||
}
|
||||
else
|
||||
{
|
||||
return SongPictureInfo.FromFile(fileName);
|
||||
string? imagePath = GetImagePath(path);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(imagePath))
|
||||
return null;
|
||||
|
||||
return SongPictureInfo.FromFile(path);
|
||||
}
|
||||
}
|
||||
|
||||
private string? GetImagePath(string path)
|
||||
{
|
||||
string[] fileNames;
|
||||
string extension;
|
||||
|
||||
foreach (string searchPattern in _searchPatterns)
|
||||
{
|
||||
fileNames = GetFilesFromDirectory(path, searchPattern);
|
||||
|
||||
foreach (string fileName in fileNames)
|
||||
{
|
||||
extension = Path.GetExtension(fileName)?.Replace(".", "") ?? string.Empty;
|
||||
|
||||
if (_imageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase) == false)
|
||||
continue;
|
||||
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string[] GetFilesFromDirectory(string path, string searchPattern)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.GetFiles(path, searchPattern);
|
||||
}
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,6 @@ namespace Harmonia.Core.Imaging;
|
||||
|
||||
public interface IAudioImageExtractor
|
||||
{
|
||||
SongPictureInfo ExtractImage(string fileName);
|
||||
SongPictureInfo ExtractImage(string fileName, SongTagInfo songTagInfo);
|
||||
SongPictureInfo? ExtractImage(string path);
|
||||
SongPictureInfo? ExtractImage(string path, SongTagInfo songTagInfo);
|
||||
}
|
||||
@@ -7,7 +7,7 @@ public class SongPictureInfo : IDisposable
|
||||
public required Stream Stream { get; init; }
|
||||
public string? ImageHash { get; init; }
|
||||
public required string ImageName { get; init; }
|
||||
public ulong Size { get; init; }
|
||||
public long Size { get; init; }
|
||||
|
||||
private SongPictureInfo()
|
||||
{
|
||||
@@ -45,7 +45,7 @@ public class SongPictureInfo : IDisposable
|
||||
Stream = stream,
|
||||
ImageHash = imageHash,
|
||||
ImageName = imageName,
|
||||
Size = (ulong)stream.Length
|
||||
Size = stream.Length
|
||||
};
|
||||
|
||||
return songPictureInfo;
|
||||
|
||||
@@ -30,7 +30,7 @@ public class AudioFileScanner(ITagResolver tagResolver, IAudioImageExtractor aud
|
||||
{
|
||||
FileInfo fileInfo = new(fileName);
|
||||
SongTagInfo songTagInfo = tagResolver.GetSongTagInfo(fileName);
|
||||
using SongPictureInfo songPictureInfo = audioImageExtractor.ExtractImage(fileName, songTagInfo);
|
||||
using SongPictureInfo? songPictureInfo = audioImageExtractor.ExtractImage(fileName, songTagInfo);
|
||||
|
||||
Song song = new()
|
||||
{
|
||||
@@ -48,8 +48,8 @@ public class AudioFileScanner(ITagResolver tagResolver, IAudioImageExtractor aud
|
||||
Genre = songTagInfo.Genre,
|
||||
BitRate = songTagInfo.BitRate,
|
||||
SampleRate = songTagInfo.SampleRate,
|
||||
ImageName = songPictureInfo.ImageName,
|
||||
ImageHash = songPictureInfo.ImageHash
|
||||
ImageName = songPictureInfo?.ImageName,
|
||||
ImageHash = songPictureInfo?.ImageHash
|
||||
};
|
||||
|
||||
return song;
|
||||
|
||||
Reference in New Issue
Block a user