metadata handling changes

This commit is contained in:
Royce551 2021-11-21 12:55:16 -06:00
parent 336082b63c
commit df8bfa97f8
6 changed files with 61 additions and 26 deletions

View file

@ -3,18 +3,48 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FRESHMusicPlayer.Backends
{
public class FileMetadataProvider : IMetadataProvider
{
public string Title => ATLTrack.Title;
// for mp3s, ATL still uses /
// for mp3s, ATL still uses /
public string[] Artists => ATLTrack.Artist.Split(Settings.DisplayValueSeparator, '/');
public string Album => ATLTrack.Album;
public byte[] CoverArt => ATLTrack.EmbeddedPictures.Count != 0 ? ATLTrack.EmbeddedPictures[0].PictureData : null;
public byte[] CoverArt
{
get
{
if (ATLTrack.EmbeddedPictures.Count != 0) return ATLTrack.EmbeddedPictures[0].PictureData;
else // no embedded cover art? fall back to getting it from the directory the audio file is in
{
if (File.Exists(path))
{
var currentDirectory = Path.GetDirectoryName(path);
foreach (var file in Directory.EnumerateFiles(currentDirectory))
{
if (Path.GetFileNameWithoutExtension(file).ToUpper() == "COVER" ||
Path.GetFileNameWithoutExtension(file).ToUpper() == "ARTWORK" ||
Path.GetFileNameWithoutExtension(file).ToUpper() == "FRONT" ||
Path.GetFileNameWithoutExtension(file).ToUpper() == "BACK" ||
Path.GetFileNameWithoutExtension(file).ToUpper() == path)
{
if (Path.GetExtension(file) == ".png" || Path.GetExtension(file) == ".jpg" || Path.GetExtension(file) == ".jpeg")
{
return File.ReadAllBytes(file);
}
}
}
}
return null;
}
}
}
public string[] Genres => ATLTrack.Genre.Split(Settings.DisplayValueSeparator, '/');
@ -30,6 +60,11 @@ namespace FRESHMusicPlayer.Backends
public Track ATLTrack { get; set; }
public FileMetadataProvider(string path) => ATLTrack = new Track(path);
private string path;
public FileMetadataProvider(string path)
{
this.path = path;
ATLTrack = new Track(path);
}
}
}

View file

@ -9,14 +9,14 @@ namespace FRESHMusicPlayer.Backends
public interface IAudioBackend : IDisposable
{
Task<BackendLoadResult> LoadSongAsync(string file);
Task<IMetadataProvider> LoadMetadataAsync(string file);
void Play();
void Pause();
TimeSpan CurrentTime { get; set; }
TimeSpan TotalTime { get; }
IMetadataProvider Metadata { get; }
float Volume { get; set; }
event EventHandler<EventArgs> OnPlaybackStopped;

View file

@ -30,8 +30,6 @@ namespace FRESHMusicPlayer.Backends
}
public TimeSpan TotalTime => AudioFile.TotalTime;
public IMetadataProvider Metadata { get; private set; }
public NAudioBackend()
{
if (OutputDevice is null)
@ -53,8 +51,6 @@ namespace FRESHMusicPlayer.Backends
{
AudioFile = new AudioFileReader(file);
OutputDevice.Init(AudioFile);
Metadata = new FileMetadataProvider(file);
});
}
catch (ArgumentException)
@ -76,6 +72,8 @@ namespace FRESHMusicPlayer.Backends
return BackendLoadResult.OK;
}
public async Task<IMetadataProvider> LoadMetadataAsync(string file) => await Task.Run(() => new FileMetadataProvider(file));
public void Play()
{
OutputDevice.Play();

View file

@ -119,15 +119,6 @@ namespace FRESHMusicPlayer
AvoidNextQueue = false;
}
/// <summary>
/// Repositions the playback position of the player.
/// </summary>
/// <param name="seconds">The position in to the track to skip in, in seconds.</param>
public void RepositionMusic(int seconds)
{
CurrentBackend.CurrentTime = TimeSpan.FromSeconds(seconds);
}
/// <summary>
/// Plays a track. This is equivalent to calling Queue.Add() and then PlayMusic()./>
/// </summary>

View file

@ -21,8 +21,6 @@ namespace FmpBassBackend
public event EventHandler<EventArgs> OnPlaybackStopped;
public IMetadataProvider Metadata { get; private set; }
private readonly MediaPlayer player = new MediaPlayer();
public FmpBassBackend()
@ -43,12 +41,12 @@ namespace FmpBassBackend
{
var wasSuccessful = await player.LoadAsync(file);
Metadata = new FileMetadataProvider(file);
if (!wasSuccessful) return BackendLoadResult.Invalid;
else return BackendLoadResult.OK;
}
public async Task<IMetadataProvider> LoadMetadataAsync(string file) => await Task.Run(() => new FileMetadataProvider(file));
public void Pause() => player.Pause();
public void Play() => player.Play();

View file

@ -22,8 +22,6 @@ namespace FmpCdLibBackend
public float Volume { get => (float)player.Volume; set => player.Volume = value; }
public IMetadataProvider Metadata { get; private set; }
public event EventHandler<EventArgs> OnPlaybackStopped;
public FmpCdLibBackend()
@ -71,11 +69,26 @@ namespace FmpCdLibBackend
result = BackendLoadResult.OK;
}
}
if (trackToPlay != null) Metadata = new CDLibMetadataProvider(trackToPlay);
return result;
}
public async Task<IMetadataProvider> LoadMetadataAsync(string file)
{
if (Path.GetExtension(file).ToUpper() != ".CDA") return null;
IAudioCDTrack trackToPlay = null;
// super hacky; assumes that the path is something like D:\Track01.cda, might be a better way to do this
var driveLetter = char.Parse(file.Substring(0, 1));
var trackNumber = int.Parse(file.Substring(8, 2));
var drives = player.GetDrives();
foreach (var drive in drives)
{
if (drive.DriveLetter == driveLetter) trackToPlay = drive.InsertedMedia.Tracks[trackNumber - 1];
}
return new CDLibMetadataProvider(trackToPlay);
}
public void Pause()
{
player.Pause();