Compare commits

..

36 commits

Author SHA1 Message Date
MrDulfin
9fac9ef777 updated Playlist functions 2024-05-20 00:39:51 -04:00
MrDulfin
0fa97c27d0 Cleaned up many warnings 2024-05-20 00:23:45 -04:00
MrDulfin
f79bf8d477 cleaned some warnings and updated player trait 2024-05-20 00:00:06 -04:00
MrDulfin
c8c00a765b removed a broken test function 2024-05-19 22:42:47 -04:00
MrDulfin
a2cb8bd0b5 Merge remote-tracking branch 'origin/controller' 2024-05-19 22:39:46 -04:00
MrDulfin
9457c5c996 fixed a function and removed Album Art function 2024-05-19 22:29:30 -04:00
MrDulfin
b04a166c1b Merge branch 'Library-Rework' into controller 2024-05-19 22:25:41 -04:00
MrDulfin
56040bfd28 Updated Controller and added Player Trait 2024-05-19 18:48:29 -04:00
MrDulfin
94e6c25219 made a few minor changes 2024-05-07 18:28:03 -04:00
MrDulfin
ab529f4c9b added out_queue function, rudamentary listenbrainz scrobbling and other small changes 2024-04-05 20:26:13 -04:00
MrDulfin
a75081d4fc added m3u8 to playlist function and fixed playlist to m3u8 function 2024-04-05 02:09:15 -04:00
MrDulfin
f02f5bca41 first draft of library Rework 2024-04-03 01:00:52 -04:00
MrDulfin
7da0b1a1db updated dependencies and made small changes to the library 2024-03-30 00:55:56 -04:00
MrDulfin
c0f71e0741 Merge branch 'controller' of https://github.com/Dangoware/dmp-core into controller 2024-03-24 21:41:48 -04:00
MrDulfin
50f42e3a26 removed image crate 2024-03-24 21:41:43 -04:00
MrDulfin
d992e277a8 Merge branch 'controller' of https://github.com/Dangoware/dmp-core into controller 2024-03-24 21:40:43 -04:00
MrDulfin
1734a39db5 removed image crate 2024-03-24 21:40:19 -04:00
MrDulfin
259dbec3a0 Updated album art viewer function 2024-03-24 21:39:27 -04:00
MrDulfin
162982ef86 Updated album art viewer function to 2024-03-24 21:36:22 -04:00
9da9b00bef Removed unused imports, moved some tests to mod tests 2024-03-23 23:34:47 -05:00
MrDulfin
c4c842e637 added function to show album art anf other small changes 2024-03-23 23:40:10 -04:00
MrDulfin
7dcca94174 committing possibly broken code as a backup 2024-03-15 21:23:23 -04:00
MrDulfin
96cfbe9a50 test commit 2024-03-11 20:29:28 -04:00
MrDulfin
588a9cbd94 Added more queue functions 2024-03-10 19:22:31 -04:00
MrDulfin
1c14bc5ebb added queue clearing functions 2024-02-27 23:28:52 -05:00
MrDulfin
e175fe7337 added remove_item() function to Queue 2024-02-27 01:23:09 -05:00
MrDulfin
79ec5aa1ef Added function to add items to the queue.
currently does not work with songs that have the `Played` State
2024-02-26 20:17:37 -05:00
MrDulfin
0e6a676e09 moved queue to its own file 2024-02-24 20:25:56 -05:00
MrDulfin
74e2933de1 Added more functions for testing 2024-02-23 20:51:59 -05:00
d2822bbb37 Moved location of uri.exists() to in Player 2024-02-17 21:30:53 -06:00
MrDulfin
c6b561c7af made prepatory changes to the library 2024-02-16 22:24:02 -05:00
MrDulfin
3c93110037 Added #![allow(unused)] 2024-02-16 00:56:38 -05:00
MrDulfin
c842bf0b9c edited the Play command and the test function 2024-02-16 00:26:07 -05:00
MrDulfin
e25a7bfcc8 playing 2 queues at once works 2024-02-12 14:12:51 -05:00
MrDulfin
599ddc584c Finished Controller Prototype 2024-02-12 13:43:30 -05:00
MrDulfin
3ad8b78e9d started work on controller protoype 2024-02-11 15:54:11 -05:00
16 changed files with 1156 additions and 259 deletions

2
.gitignore vendored
View file

@ -13,3 +13,5 @@ music_database*
*.m3u
*.m3u8
*.json
*.zip
*.xml

View file

@ -12,7 +12,15 @@ keywords = []
categories = []
[dependencies]
file-format = { version = "0.23.0", features = ["reader-asf", "reader-ebml", "reader-mp4", "reader-rm", "reader-txt", "reader-xml", "serde"] }
file-format = { version = "0.23.0", features = [
"reader-asf",
"reader-ebml",
"reader-mp4",
"reader-rm",
"reader-txt",
"reader-xml",
"serde",
] }
lofty = "0.18.2"
serde = { version = "1.0.195", features = ["derive"] }
walkdir = "2.4.0"
@ -32,7 +40,11 @@ leb128 = "0.2.5"
urlencoding = "2.1.3"
m3u8-rs = "5.0.5"
thiserror = "1.0.56"
font = "0.27.0"
uuid = { version = "1.6.1", features = ["v4", "serde"]}
uuid = { version = "1.6.1", features = ["v4", "serde"] }
serde_json = "1.0.111"
deunicode = "1.4.2"
opener = { version = "0.7.0", features = ["reveal"] }
tempfile = "3.10.1"
listenbrainz = "0.7.0"
discord-rpc-client = "0.4.0"
nestify = "0.3.3"

View file

@ -1,16 +1,14 @@
use std::{
fs::{self, File, OpenOptions},
io::{Error, Read, Write},
path::PathBuf,
fs::{File, OpenOptions, self},
io::{Error, Write, Read}, sync::{Arc, RwLock},
};
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use serde_json::to_string_pretty;
use thiserror::Error;
use uuid::Uuid;
use crate::music_storage::library::{MusicLibrary, self};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigLibrary {
pub name: String,
@ -43,7 +41,7 @@ impl ConfigLibrary {
pub fn open(&self) -> Result<File, Error> {
match File::open(self.path.as_path()) {
Ok(ok) => Ok(ok),
Err(e) => Err(e)
Err(e) => Err(e),
}
}
}
@ -64,7 +62,7 @@ impl ConfigLibraries {
pub fn get_default(&self) -> Result<&ConfigLibrary, ConfigError> {
for library in &self.libraries {
if library.uuid == self.default_library {
return Ok(library)
return Ok(library);
}
}
Err(ConfigError::NoDefaultLibrary)
@ -72,8 +70,9 @@ impl ConfigLibraries {
pub fn get_library(&self, uuid: &Uuid) -> Result<ConfigLibrary, ConfigError> {
for library in &self.libraries {
// dbg!(&library.uuid, &uuid);
if &library.uuid == uuid {
return Ok(library.to_owned())
return Ok(library.to_owned());
}
}
Err(ConfigError::NoConfigLibrary(*uuid))
@ -82,7 +81,7 @@ impl ConfigLibraries {
pub fn uuid_exists(&self, uuid: &Uuid) -> bool {
for library in &self.libraries {
if &library.uuid == uuid {
return true
return true;
}
}
false
@ -90,11 +89,18 @@ impl ConfigLibraries {
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ConfigConnections {
pub listenbrainz_token: Option<String>,
}
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct Config {
pub path: PathBuf,
pub backup_folder: Option<PathBuf>,
pub libraries: ConfigLibraries,
pub volume: f32,
pub connections: ConfigConnections,
}
impl Config {
@ -115,7 +121,12 @@ impl Config {
pub fn write_file(&self) -> Result<(), Error> {
let mut writer = self.path.clone();
writer.set_extension("tmp");
let mut file = OpenOptions::new().create(true).truncate(true).read(true).write(true).open(&writer)?;
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.read(true)
.write(true)
.open(&writer)?;
let config = to_string_pretty(self)?;
// dbg!(&config);
@ -124,12 +135,41 @@ impl Config {
Ok(())
}
pub fn save_backup(&self) -> Result<(), Box<dyn std::error::Error>> {
match &self.backup_folder {
Some(path) => {
let mut writer = path.clone();
writer.set_extension("tmp");
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.read(true)
.write(true)
.open(&writer)?;
let config = to_string_pretty(self)?;
// dbg!(&config);
file.write_all(config.as_bytes())?;
fs::rename(writer, self.path.as_path())?;
Ok(())
}
None => Err(ConfigError::NoBackupLibrary.into()),
}
}
pub fn read_file(path: PathBuf) -> Result<Self, Error> {
let mut file: File = File::open(path)?;
let mut bun: String = String::new();
_ = file.read_to_string(&mut bun);
let ny: Config = serde_json::from_str::<Config>(&bun)?;
Ok(ny)
let config: Config = serde_json::from_str::<Config>(&bun)?;
Ok(config)
}
pub fn push_library(&mut self, lib: ConfigLibrary) {
if self.libraries.libraries.is_empty() {
self.libraries.default_library = lib.uuid;
}
self.libraries.libraries.push(lib);
}
}
@ -142,50 +182,68 @@ pub enum ConfigError {
//TODO: do something about playlists
#[error("Please provide a better m3u8 Playlist")]
BadPlaylist,
#[error("No backup Config folder present")]
NoBackupLibrary,
}
#[test]
fn config_test() {
let lib_a = ConfigLibrary::new(PathBuf::from("test-config/library1"), String::from("library1"), None);
let lib_b = ConfigLibrary::new(PathBuf::from("test-config/library2"), String::from("library2"), None);
let lib_c = ConfigLibrary::new(PathBuf::from("test-config/library3"), String::from("library3"), None);
let config = Config {
#[cfg(test)]
pub mod tests {
use super::{Config, ConfigLibrary};
use crate::music_storage::library::MusicLibrary;
use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
pub fn new_config_lib() -> (Config, MusicLibrary) {
let lib = ConfigLibrary::new(
PathBuf::from("test-config/library"),
String::from("library"),
None,
);
let mut config = Config {
path: PathBuf::from("test-config/config_test.json"),
libraries: ConfigLibraries {
libraries: vec![
lib_a.clone(),
lib_b.clone(),
lib_c.clone(),
],
..Default::default()
},
..Default::default()
};
config.write_file();
let arc = Arc::new(RwLock::from(config));
MusicLibrary::init(arc.clone(), lib_a.uuid.clone()).unwrap();
MusicLibrary::init(arc.clone(), lib_b.uuid.clone()).unwrap();
MusicLibrary::init(arc.clone(), lib_c.uuid.clone()).unwrap();
}
config.push_library(lib);
config.write_file().unwrap();
#[test]
fn test2() {
let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap();
let uuid = config.libraries.get_default().unwrap().uuid.clone();
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap();
let mut lib = MusicLibrary::init(
Arc::new(RwLock::from(config.clone())),
dbg!(config.libraries.default_library),
)
.unwrap();
lib.scan_folder("test-config/music/").unwrap();
lib.save(config.clone()).unwrap();
dbg!(&lib);
dbg!(&config);
}
#[test]
fn test3() {
(config, lib)
}
pub fn read_config_lib() -> (Config, MusicLibrary) {
let config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap();
let uuid = config.libraries.get_default().unwrap().uuid;
let mut lib = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), uuid).unwrap();
dbg!(lib);
// dbg!(&config);
let mut lib = MusicLibrary::init(
Arc::new(RwLock::from(config.clone())),
config.libraries.get_default().unwrap().uuid,
)
.unwrap();
lib.scan_folder("test-config/music/").unwrap();
lib.save(config.clone()).unwrap();
(config, lib)
}
#[test]
fn test3() {
let (config, _) = read_config_lib();
_ = config.write_file();
dbg!(config);
}
}

View file

@ -1,7 +1,3 @@
use std::{marker::PhantomData, fs::File, path::PathBuf};
use font::Font;
pub enum Setting {
String {
name: String,

View file

@ -8,12 +8,16 @@ pub mod music_storage {
pub mod db_reader;
}
pub mod music_controller{
pub mod music_controller {
pub mod controller;
pub mod connections;
pub mod queue;
}
pub mod music_player;
pub mod music_player {
pub mod gstreamer;
pub mod player;
}
#[allow(clippy::module_inception)]
pub mod config {
pub mod config;

View file

@ -1,8 +1,103 @@
use std::{env, thread, time};
// use std::{
// sync::{Arc, RwLock},
// error::Error,
// };
use super::controller::Controller;
// use discord_rpc_client::Client;
// use listenbrainz::ListenBrainz;
// use uuid::Uuid;
// use crate::{
// config::config::Config, music_controller::controller::{Controller, QueueCmd, QueueResponse}, music_storage::library::{MusicLibrary, Song, Tag}
// };
// use super::controller::DatabaseResponse;
impl Controller {
//more stuff goes here
}
// impl Controller {
// pub fn listenbrainz_authenticate(&mut self) -> Result<ListenBrainz, Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// let mut client = ListenBrainz::new();
// let lbz_token = match &config.connections.listenbrainz_token {
// Some(token) => token,
// None => todo!("No ListenBrainz token in config")
// };
// if !client.is_authenticated() {
// client.authenticate(lbz_token)?;
// }
// Ok(client)
// }
// pub fn lbz_scrobble(&self, client: ListenBrainz, uuid: Uuid) -> Result<(), Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// &self.db_mail.send(super::controller::DatabaseCmd::QueryUuid(uuid));
// let res = &self.db_mail.recv()?;
// let song = match res {
// DatabaseResponse::Song(song) => song,
// _ => todo!()
// };
// let unknown = &"unknown".to_string();
// let artist = song.get_tag(&Tag::Artist).unwrap_or(unknown);
// let track = song.get_tag(&Tag::Title).unwrap_or(unknown);
// let release = song.get_tag(&Tag::Album).map(|rel| rel.as_str());
// client.listen(artist, track, release)?;
// Ok(())
// }
// pub fn lbz_now_playing(&self, client: ListenBrainz, uuid: Uuid) -> Result<(), Box<dyn Error>> {
// let config = &self.config.read().unwrap();
// &self.db_mail.send(super::controller::DatabaseCmd::QueryUuid(uuid));
// let res = &self.db_mail.recv()?;
// let song = match res {
// DatabaseResponse::Song(song) => song,
// _ => todo!()
// };
// let unknown = &"unknown".to_string();
// let artist = song.get_tag(&Tag::Artist).unwrap_or(unknown);
// let track = song.get_tag(&Tag::Title).unwrap_or(unknown);
// let release = song.get_tag(&Tag::Album).map(|rel| rel.as_str());
// client.listen(artist, track, release)?;
// Ok(())
// }
// pub fn discord_song_change(client: &mut Client,song: Song) {
// client.set_activity(|a| {
// a.state(format!("Listening to {}", song.get_tag(&Tag::Title).unwrap()))
// .into()
// });
// }
// }
// #[cfg(test)]
// mod test_super {
// use std::{thread::sleep, time::Duration};
// use super::*;
// use crate::config::config::tests::read_config_lib;
// #[test]
// fn listenbrainz() {
// let mut c = Controller::start(".\\test-config\\config_test.json").unwrap();
// let client = c.listenbrainz_authenticate().unwrap();
// c.q_new().unwrap();
// c.queue_mail[0].send(QueueCmd::SetVolume(0.04)).unwrap();
// let songs = c.lib_get_songs();
// c.q_enqueue(0, songs[1].location.to_owned()).unwrap();
// c.q_play(0).unwrap();
// sleep(Duration::from_secs(100));
// c.lbz_scrobble(client, songs[1].uuid).unwrap();
// }
// }

View file

@ -2,25 +2,86 @@
//! player. It manages queues, playback, library access, and
//! other functions
use crossbeam_channel;
use crossbeam_channel::{Receiver, Sender};
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use crossbeam_channel::unbounded;
use std::error::Error;
use uuid::Uuid;
use crate::music_player::player::Player;
use crate::{
music_player::Player,
music_storage::library::Song,
config::config::Config
config::config::Config, music_controller::queue::Queue, music_storage::library::MusicLibrary,
};
struct Queue {
player: Player,
name: String,
songs: Vec<Song>,
pub struct Controller<P: Player> {
pub queue: Queue,
pub config: Arc<RwLock<Config>>,
pub library: MusicLibrary,
pub player: Box<P>,
}
pub struct Controller {
queues: Vec<Queue>,
config: Arc<RwLock<Config>>,
#[derive(Debug)]
pub(super) struct MailMan<T: Send, U: Send> {
pub tx: Sender<T>,
rx: Receiver<U>,
}
impl Controller {
// more stuff to come
impl<T: Send> MailMan<T, T> {
pub fn new() -> Self {
let (tx, rx) = unbounded::<T>();
MailMan { tx, rx }
}
}
impl<T: Send, U: Send> MailMan<T, U> {
pub fn double() -> (MailMan<T, U>, MailMan<U, T>) {
let (tx, rx) = unbounded::<T>();
let (tx1, rx1) = unbounded::<U>();
(MailMan { tx, rx: rx1 }, MailMan { tx: tx1, rx })
}
pub fn send(&self, mail: T) -> Result<(), Box<dyn Error>> {
self.tx.send(mail).unwrap();
Ok(())
}
pub fn recv(&self) -> Result<U, Box<dyn Error>> {
let u = self.rx.recv()?;
Ok(u)
}
}
#[allow(unused_variables)]
impl<P: Player> Controller<P> {
pub fn start<T>(config_path: T) -> Result<Self, Box<dyn Error>>
where
std::path::PathBuf: std::convert::From<T>,
P: Player,
{
let config_path = PathBuf::from(config_path);
let config = Config::read_file(config_path)?;
let uuid = config.libraries.get_default()?.uuid;
let config_ = Arc::new(RwLock::from(config));
let library = MusicLibrary::init(config_.clone(), uuid)?;
Ok(Controller {
queue: Queue::default(),
config: config_.clone(),
library,
player: Box::new(P::new()),
})
}
pub fn q_add(&mut self, item: &Uuid, source: super::queue::PlayerLocation, by_human: bool) {
let item = self.library.query_uuid(item).unwrap().0.to_owned();
self.queue.add_item(item, source, by_human)
}
}
#[cfg(test)]
mod test_super {}

View file

@ -0,0 +1,265 @@
use crate::music_storage::library::Song;
use std::error::Error;
use uuid::Uuid;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum QueueError {
#[error("Index out of bounds! Index {0} is over len {1}")]
OutOfBounds(usize, usize),
#[error("The Queue is empty!")]
EmptyQueue,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum QueueState {
Played,
First,
AddHere,
NoState,
}
// TODO: move this to a different location to be used elsewhere
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum PlayerLocation {
Test,
Library,
Playlist(Uuid),
File,
Custom,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct QueueItem {
pub(super) item: Song,
pub(super) state: QueueState,
pub(super) source: PlayerLocation,
pub(super) by_human: bool,
}
impl From<Song> for QueueItem {
fn from(song: Song) -> Self {
QueueItem {
item: song,
state: QueueState::NoState,
source: PlayerLocation::Library,
by_human: false,
}
}
}
#[derive(Debug, Default)]
pub struct Queue {
pub items: Vec<QueueItem>,
pub played: Vec<QueueItem>,
pub loop_: bool,
pub shuffle: bool,
}
impl Queue {
fn has_addhere(&self) -> bool {
for item in &self.items {
if item.state == QueueState::AddHere {
return true;
}
}
false
}
fn dbg_items(&self) {
dbg!(
self.items
.iter()
.map(|item| item.item.clone())
.collect::<Vec<Song>>(),
self.items.len()
);
}
pub fn set_items(&mut self, tracks: Vec<QueueItem>) {
let mut tracks = tracks;
self.items.clear();
self.items.append(&mut tracks);
}
pub fn add_item(&mut self, item: Song, source: PlayerLocation, by_human: bool) {
let mut i: usize = 0;
self.items = self
.items
.iter()
.enumerate()
.map(|(j, item_)| {
let mut item_ = item_.to_owned();
// get the index of the current AddHere item and give it to i
if item_.state == QueueState::AddHere {
i = j;
item_.state = QueueState::NoState;
}
item_
})
.collect::<Vec<QueueItem>>();
self.items.insert(
i + if self.items.is_empty() { 0 } else { 1 },
QueueItem {
item,
state: QueueState::AddHere,
source,
by_human,
},
);
}
pub fn add_item_next(&mut self, item: Song, source: PlayerLocation) {
use QueueState::*;
let empty = self.items.is_empty();
self.items.insert(
if empty { 0 } else { 1 },
QueueItem {
item,
state: if (self.items.get(1).is_none()
|| !self.has_addhere() && self.items.get(1).is_some())
|| empty
{
AddHere
} else {
NoState
},
source,
by_human: true,
},
)
}
pub fn add_multi(&mut self, items: Vec<Song>, source: PlayerLocation, by_human: bool) {}
pub fn remove_item(&mut self, remove_index: usize) -> Result<(), QueueError> {
// dbg!(/*&remove_index, self.current_index(), &index,*/ &self.items[remove_index]);
if remove_index < self.items.len() {
// update the state of the next item to replace the item being removed
if self.items.get(remove_index + 1).is_some() {
self.items[remove_index + 1].state = self.items[remove_index].state;
}
self.items[remove_index].state = QueueState::NoState;
self.items.remove(remove_index);
Ok(())
} else {
Err(QueueError::EmptyQueue)
}
}
pub fn clear(&mut self) {
self.items.clear();
}
pub fn clear_except(&mut self, index: usize) -> Result<(), Box<dyn Error>> {
use QueueState::*;
let empty = self.items.is_empty();
if !empty && index < self.items.len() {
let i = self.items[index].clone();
self.items.retain(|item| *item == i);
self.items[0].state = AddHere;
} else if empty {
return Err("Queue is empty!".into());
} else {
return Err("index out of bounds!".into());
}
Ok(())
}
pub fn clear_played(&mut self) {
self.played.clear();
}
pub fn clear_all(&mut self) {
self.items.clear();
self.played.clear();
}
// TODO: uh, fix this?
fn move_to(&mut self, index: usize) -> Result<(), QueueError> {
use QueueState::*;
let empty = self.items.is_empty();
let index = if !empty {
index
} else {
return Err(QueueError::EmptyQueue);
};
if !empty && index < self.items.len() {
let to_item = self.items[index].clone();
loop {
let empty = !self.items.is_empty();
let item = self.items[0].item.to_owned();
if item != to_item.item && !empty {
if self.items[0].state == AddHere && self.items.get(1).is_some() {
self.items[1].state = AddHere;
}
if let Err(e) = self.remove_item(0) {
dbg!(&e);
self.dbg_items();
return Err(e);
}
// dbg!(&to_item.item, &self.items[ind].item);
} else if empty {
return Err(QueueError::EmptyQueue);
} else {
break;
}
}
} else {
return Err(QueueError::EmptyQueue);
}
Ok(())
}
pub fn swap(&mut self, a: usize, b: usize) {
self.items.swap(a, b)
}
pub fn move_item(&mut self, a: usize, b: usize) {
let item = self.items[a].to_owned();
if a != b {
self.items.remove(a);
}
self.items.insert(b, item);
}
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Result<&QueueItem, Box<dyn Error>> {
if self.items.is_empty() {
if self.loop_ {
return Err(QueueError::EmptyQueue.into()); // TODO: add function to loop the queue
} else {
return Err(QueueError::EmptyQueue.into());
}
}
// TODO: add an algorithm to detect if the song should be skipped
let item = self.items[0].clone();
if self.items[0].state == QueueState::AddHere || !self.has_addhere() {
self.items[1].state = QueueState::AddHere;
}
self.played.push(item);
self.items.remove(0);
Ok(&self.items[1])
}
pub fn prev() {}
pub fn check_played(&mut self) {
while self.played.len() > 50 {
self.played.remove(0);
}
}
}

View file

@ -15,8 +15,10 @@ use gstreamer::prelude::*;
use chrono::Duration;
use thiserror::Error;
use super::player::{Player, PlayerError};
#[derive(Debug)]
pub enum PlayerCmd {
pub enum GstCmd {
Play,
Pause,
Eos,
@ -24,7 +26,7 @@ pub enum PlayerCmd {
}
#[derive(Debug, PartialEq, Eq)]
pub enum PlayerState {
pub enum GstState {
Playing,
Paused,
Ready,
@ -33,7 +35,7 @@ pub enum PlayerState {
VoidPending,
}
impl From<gst::State> for PlayerState {
impl From<gst::State> for GstState {
fn from(value: gst::State) -> Self {
match value {
gst::State::VoidPending => Self::VoidPending,
@ -45,7 +47,7 @@ impl From<gst::State> for PlayerState {
}
}
impl TryInto<gst::State> for PlayerState {
impl TryInto<gst::State> for GstState {
fn try_into(self) -> Result<gst::State, Box<dyn Error>> {
match self {
Self::VoidPending => Ok(gst::State::VoidPending),
@ -60,22 +62,6 @@ impl TryInto<gst::State> for PlayerState {
type Error = Box<dyn Error>;
}
#[derive(Error, Debug)]
pub enum PlayerError {
#[error("player initialization failed")]
Init(#[from] glib::Error),
#[error("element factory failed to create playbin3")]
Factory(#[from] glib::BoolError),
#[error("could not change playback state")]
StateChange(#[from] gst::StateChangeError),
#[error("failed to build gstreamer item")]
Build,
#[error("poison error")]
Poison,
#[error("general player error")]
General,
}
#[derive(Debug, PartialEq, Eq)]
enum PlaybackStats {
Idle,
@ -88,10 +74,11 @@ enum PlaybackStats {
}
/// An instance of a music player with a GStreamer backend
pub struct Player {
#[derive(Debug)]
pub struct GStreamer {
source: Option<URI>,
//pub message_tx: Sender<PlayerCmd>,
pub message_rx: crossbeam::channel::Receiver<PlayerCmd>,
pub message_rx: crossbeam::channel::Receiver<GstCmd>,
playback_tx: crossbeam::channel::Sender<PlaybackStats>,
playbin: Arc<RwLock<Element>>,
@ -102,7 +89,7 @@ pub struct Player {
position: Arc<RwLock<Option<Duration>>>,
}
impl Player {
impl GStreamer {
pub fn new() -> Result<Self, PlayerError> {
// Initialize GStreamer, maybe figure out how to nicely fail here
gst::init()?;
@ -159,14 +146,14 @@ impl Player {
// Check if the current playback position is close to the end
let finish_point = end - Duration::milliseconds(250);
if pos_temp.unwrap() >= end {
let _ = playback_tx.try_send(PlayerCmd::Eos);
let _ = playback_tx.try_send(GstCmd::Eos);
playbin_arc
.write()
.unwrap()
.set_state(gst::State::Ready)
.expect("Unable to set the pipeline state");
} else if pos_temp.unwrap() >= finish_point {
let _ = playback_tx.try_send(PlayerCmd::AboutToFinish);
let _ = playback_tx.try_send(GstCmd::AboutToFinish);
}
// This has to be done AFTER the current time in the file
@ -177,7 +164,7 @@ impl Player {
*position_update.write().unwrap() = None;
break
},
PlaybackStats::Idle | PlaybackStats::Switching => println!("waiting!"),
PlaybackStats::Idle | PlaybackStats::Switching => {},
_ => ()
}
@ -257,12 +244,17 @@ impl Player {
&self.source
}
pub fn enqueue_next(&mut self, next_track: &URI) {
self.set_source(next_track);
pub fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError> {
self.set_source(next_track)
}
/// Set the playback URI
fn set_source(&mut self, source: &URI) {
fn set_source(&mut self, source: &URI) -> Result<(), PlayerError> {
if !source.exists().is_ok_and(|x| x) {
// If the source doesn't exist, gstreamer will crash!
return Err(PlayerError::NotFound)
}
// Make sure the playback tracker knows the stuff is stopped
self.playback_tx.send(PlaybackStats::Switching).unwrap();
@ -290,7 +282,7 @@ impl Player {
let now = std::time::Instant::now();
while now.elapsed() < std::time::Duration::from_millis(20) {
if self.seek_to(Duration::from_std(*start).unwrap()).is_ok() {
return;
return Ok(());
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
@ -321,6 +313,8 @@ impl Player {
}).unwrap();
}
}
Ok(())
}
/// Gets a mutable reference to the playbin element
@ -458,7 +452,7 @@ impl Player {
}
/// Get the current state of the playback
pub fn state(&mut self) -> PlayerState {
pub fn state(&mut self) -> GstState {
self.playbin().unwrap().current_state().into()
/*
match *self.buffer.read().unwrap() {
@ -488,7 +482,9 @@ impl Player {
}
}
impl Drop for Player {
// impl Player for GStreamer {}
impl Drop for GStreamer {
/// Cleans up the `GStreamer` pipeline and the monitoring
/// thread when [Player] is dropped.
fn drop(&mut self) {

0
src/music_player/kira.rs Normal file
View file

View file

@ -0,0 +1,56 @@
use chrono::Duration;
use gstreamer as gst;
use thiserror::Error;
use crate::music_storage::library::URI;
#[derive(Error, Debug)]
pub enum PlayerError {
#[error("player initialization failed")]
Init(#[from] glib::Error),
#[error("element factory failed to create playbin3")]
Factory(#[from] glib::BoolError),
#[error("could not change playback state")]
StateChange(#[from] gst::StateChangeError),
#[error("the file or source is not found")]
NotFound,
#[error("failed to build gstreamer item")]
Build,
#[error("poison error")]
Poison,
#[error("general player error")]
General,
}
pub trait Player {
fn new() -> Self;
fn source(&self) -> &Option<URI>;
fn enqueue_next(&mut self, next_track: &URI) -> Result<(), PlayerError>;
fn set_volume(&mut self, volume: f64);
fn volume(&mut self) -> f64;
fn ready(&mut self) -> Result<(), PlayerError>;
fn play(&mut self) -> Result<(), PlayerError>;
fn resume(&mut self) -> Result<(), PlayerError>;
fn pause(&mut self) -> Result<(), PlayerError>;
fn stop(&mut self) -> Result<(), PlayerError>;
fn is_paused(&mut self) -> bool;
fn position(&mut self) -> Option<Duration>;
fn duration(&mut self) -> Option<Duration>;
fn raw_duration(&self) -> Option<Duration>;
fn seek_by(&mut self, seek_amount: Duration) -> Result<(), PlayerError>;
fn seek_to(&mut self, target_pos: Duration) -> Result<(), PlayerError>;
}

View file

@ -1,6 +1,8 @@
use std::collections::BTreeMap;
use std::{fs::File, io::Read, path::Path, time::Duration};
use uuid::Uuid;
use super::utils::meta_offset;
use crate::music_storage::db_reader::common::{get_bytes, get_bytes_vec};
use crate::music_storage::db_reader::extern_library::ExternalLibrary;
@ -174,12 +176,15 @@ pub struct FoobarPlaylistTrack {
impl FoobarPlaylistTrack {
fn find_song(&self) -> Song {
let location = URI::Local(self.file_name.clone().into());
let internal_tags = Vec::new();
Song {
location,
location: vec![location],
uuid: Uuid::new_v4(),
plays: 0,
skips: 0,
favorited: false,
banned: None,
rating: None,
format: None,
duration: self.duration,
@ -189,6 +194,7 @@ impl FoobarPlaylistTrack {
date_modified: None,
album_art: Vec::new(),
tags: BTreeMap::new(),
internal_tags,
}
}
}

View file

@ -2,6 +2,7 @@ use file_format::FileFormat;
use lofty::{AudioFile, LoftyError, ParseOptions, Probe, TagType, TaggedFileExt};
use quick_xml::events::Event;
use quick_xml::reader::Reader;
use uuid::Uuid;
use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::path::{Path, PathBuf};
@ -12,7 +13,7 @@ use std::vec::Vec;
use chrono::prelude::*;
use crate::music_storage::db_reader::extern_library::ExternalLibrary;
use crate::music_storage::library::{AlbumArt, Service, Song, Tag, URI};
use crate::music_storage::library::{AlbumArt, BannedType, Service, Song, Tag, URI};
use crate::music_storage::utils;
use urlencoding::decode;
@ -146,7 +147,7 @@ impl ExternalLibrary for ITunesLibrary {
continue;
}
let sug: URI = if track.location.contains("file://localhost/") {
let location: URI = if track.location.contains("file://localhost/") {
URI::Local(PathBuf::from(
decode(track.location.strip_prefix("file://localhost/").unwrap())
.unwrap()
@ -165,11 +166,19 @@ impl ExternalLibrary for ITunesLibrary {
};
let play_time_ = StdDur::from_secs(track.plays as u64 * dur.as_secs());
let internal_tags = Vec::new(); // TODO: handle internal tags generation
let ny: Song = Song {
location: sug,
location: vec![location],
uuid: Uuid::new_v4(),
plays: track.plays,
skips: 0,
favorited: track.favorited,
banned: if track.banned {
Some(BannedType::All)
}else {
None
},
rating: track.rating,
format: match FileFormat::from_file(PathBuf::from(&loc)) {
Ok(e) => Some(e),
@ -185,6 +194,7 @@ impl ExternalLibrary for ITunesLibrary {
Err(_) => Vec::new(),
},
tags: tags_,
internal_tags,
};
// dbg!(&ny.tags);
bun.push(ny);
@ -321,3 +331,28 @@ impl ITunesSong {
Ok(song)
}
}
#[cfg(test)]
mod tests {
use std::{path::{Path, PathBuf}, sync::{Arc, RwLock}};
use crate::{config::config::{Config, ConfigLibrary}, music_storage::{db_reader::extern_library::ExternalLibrary, library::MusicLibrary}};
use super::ITunesLibrary;
#[test]
fn itunes_lib_test() {
let mut config = Config::read_file(PathBuf::from("test-config/config_test.json")).unwrap();
let config_lib = ConfigLibrary::new(PathBuf::from("test-config/library2"), String::from("library2"), None);
config.libraries.libraries.push(config_lib.clone());
let songs = ITunesLibrary::from_file(Path::new("test-config\\iTunesLib.xml")).to_songs();
let mut library = MusicLibrary::init(Arc::new(RwLock::from(config.clone())), config_lib.uuid).unwrap();
songs.iter().for_each(|song| library.add_song(song.to_owned()).unwrap());
config.write_file().unwrap();
library.save(config).unwrap();
}
}

View file

@ -1,3 +1,4 @@
use super::playlist::PlaylistFolder;
// Crate things
use super::utils::{find_images, normalize, read_file, write_file};
use crate::config::config::Config;
@ -6,16 +7,15 @@ use crate::config::config::Config;
use std::collections::BTreeMap;
use std::error::Error;
use std::ops::ControlFlow::{Break, Continue};
use std::ops::Deref;
// Files
use file_format::{FileFormat, Kind};
use glib::filename_to_uri;
use lofty::{AudioFile, ItemKey, ItemValue, ParseOptions, Probe, TagType, TaggedFileExt};
use rcue::parser::parse_from_file;
use uuid::Uuid;
use std::fs;
use std::path::{Path, PathBuf};
use uuid::Uuid;
use walkdir::WalkDir;
// Time
@ -115,13 +115,53 @@ impl ToString for Field {
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum InternalTag {
DoNotTrack(DoNotTrack),
SongType(SongType),
SongLink(Uuid, SongType),
// Volume Adjustment from -100% to 100%
VolumeAdjustment(i8),
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[non_exhaustive]
pub enum BannedType {
Shuffle,
All,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum DoNotTrack {
// TODO: add services to not track
LastFM,
LibreFM,
MusicBrainz,
Discord,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum SongType {
// TODO: add MORE?! song types
#[default]
Main,
Instrumental,
Remix,
Custom(String),
}
/// Stores information about a single song
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Song {
pub location: URI,
pub location: Vec<URI>,
pub uuid: Uuid,
pub plays: i32,
pub skips: i32,
pub favorited: bool,
pub banned: Option<BannedType>,
pub rating: Option<u8>,
pub format: Option<FileFormat>,
pub duration: Duration,
@ -134,6 +174,7 @@ pub struct Song {
pub date_modified: Option<DateTime<Utc>>,
pub album_art: Vec<AlbumArt>,
pub tags: BTreeMap<Tag, String>,
pub internal_tags: Vec<InternalTag>,
}
impl Song {
@ -155,7 +196,7 @@ impl Song {
pub fn get_field(&self, target_field: &str) -> Option<Field> {
let lower_target = target_field.to_lowercase();
match lower_target.as_str() {
"location" => Some(Field::Location(self.location.clone())),
"location" => Some(Field::Location(self.primary_uri().unwrap().0.clone())), //TODO: make this not unwrap()
"plays" => Some(Field::Plays(self.plays)),
"skips" => Some(Field::Skips(self.skips)),
"favorited" => Some(Field::Favorited(self.favorited)),
@ -254,11 +295,15 @@ impl Song {
// TODO: Fix error handling
let binding = fs::canonicalize(target_file).unwrap();
// TODO: Handle creation of internal tag: Song Type and Song Links
let internal_tags = { Vec::new() };
let new_song = Song {
location: URI::Local(binding),
location: vec![URI::Local(binding)],
uuid: Uuid::new_v4(),
plays: 0,
skips: 0,
favorited: false,
banned: None,
rating: None,
format,
duration,
@ -268,6 +313,7 @@ impl Song {
date_modified: Some(chrono::offset::Utc::now()),
tags,
album_art,
internal_tags,
};
Ok(new_song)
}
@ -287,7 +333,6 @@ impl Song {
for file in cue_data.files.iter() {
let audio_location = &parent_dir.join(file.file.clone());
if !audio_location.exists() {
continue;
}
@ -372,15 +417,17 @@ impl Song {
let album_art = find_images(&audio_location.to_path_buf()).unwrap();
let new_song = Song {
location: URI::Cue {
location: vec![URI::Cue {
location: audio_location.clone(),
index: i,
start,
end,
},
}],
uuid: Uuid::new_v4(),
plays: 0,
skips: 0,
favorited: false,
banned: None,
rating: None,
format,
duration,
@ -390,11 +437,39 @@ impl Song {
date_modified: Some(chrono::offset::Utc::now()),
tags,
album_art,
internal_tags: Vec::new(),
};
tracks.push((new_song, audio_location.clone()));
}
}
Ok((tracks))
Ok(tracks)
}
/// Returns a reference to the first valid URI in the song, and any invalid URIs that come before it, or errors if there are no valid URIs
#[allow(clippy::type_complexity)]
pub fn primary_uri(&self) -> Result<(&URI, Option<Vec<&URI>>), Box<dyn Error>> {
let mut invalid_uris = Vec::new();
let mut valid_uri = None;
for uri in &self.location {
if uri.exists()? {
valid_uri = Some(uri);
break;
} else {
invalid_uris.push(uri);
}
}
match valid_uri {
Some(uri) => Ok((
uri,
if !invalid_uris.is_empty() {
Some(invalid_uris)
} else {
None
},
)),
None => Err("No valid URIs for this song".into()),
}
}
}
@ -461,10 +536,18 @@ impl URI {
path_str.to_string()
}
pub fn as_path(&self) -> Result<&PathBuf, Box<dyn Error>> {
if let Self::Local(path) = self {
Ok(path)
} else {
Err("This URI is not local!".into())
}
}
pub fn exists(&self) -> Result<bool, std::io::Error> {
match self {
URI::Local(loc) => loc.try_exists(),
URI::Cue {location, ..} => location.try_exists(),
URI::Cue { location, .. } => location.try_exists(),
URI::Remote(_, _loc) => todo!(),
}
}
@ -489,7 +572,7 @@ pub enum Service {
None,
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct Album<'a> {
title: &'a String,
artist: Option<&'a String>,
@ -500,7 +583,7 @@ pub struct Album<'a> {
#[allow(clippy::len_without_is_empty)]
impl Album<'_> {
//returns the Album title
fn title(&self) -> &String {
pub fn title(&self) -> &String {
self.title
}
@ -548,14 +631,8 @@ pub struct MusicLibrary {
pub name: String,
pub uuid: Uuid,
pub library: Vec<Song>,
}
#[test]
fn library_init() {
let config = Config::read_file(PathBuf::from("config_test.json")).unwrap();
let target_uuid = config.libraries.libraries[0].uuid;
let a = MusicLibrary::init(Arc::new(RwLock::from(config)), target_uuid).unwrap();
dbg!(a);
pub playlists: PlaylistFolder,
pub backup_songs: Vec<Song>, // maybe move this to the config instead?
}
impl MusicLibrary {
@ -565,6 +642,8 @@ impl MusicLibrary {
name,
uuid,
library: Vec::new(),
playlists: PlaylistFolder::default(),
backup_songs: Vec::new(),
}
}
@ -575,13 +654,15 @@ impl MusicLibrary {
/// the [MusicLibrary] Vec
pub fn init(config: Arc<RwLock<Config>>, uuid: Uuid) -> Result<Self, Box<dyn Error>> {
let global_config = &*config.read().unwrap();
let path = global_config.libraries.get_library(&uuid)?.path;
let library: MusicLibrary = match global_config.libraries.get_library(&uuid)?.path.exists() {
true => read_file(global_config.libraries.get_library(&uuid)?.path)?,
let library: MusicLibrary = match path.exists() {
true => read_file(path)?,
false => {
// If the library does not exist, re-create it
let lib = MusicLibrary::new(String::new(), uuid);
write_file(&lib, global_config.libraries.get_library(&uuid)?.path)?;
write_file(&lib, path)?;
lib
}
};
@ -631,7 +712,30 @@ impl MusicLibrary {
.par_iter()
.enumerate()
.try_for_each(|(i, track)| {
if path == &track.location {
for location in &track.location {
//TODO: check that this works
if path == location {
return std::ops::ControlFlow::Break((track, i));
}
}
Continue(())
});
match result {
Break(song) => Some(song),
Continue(_) => None,
}
}
/// Queries for a [Song] by its [Uuid], returning a single `Song`
/// with the `Uuid` that matches along with its position in the library
pub fn query_uuid(&self, uuid: &Uuid) -> Option<(&Song, usize)> {
let result = self
.library
.par_iter()
.enumerate()
.try_for_each(|(i, track)| {
if uuid == &track.uuid {
return std::ops::ControlFlow::Break((track, i));
}
Continue(())
@ -648,7 +752,8 @@ impl MusicLibrary {
fn query_path(&self, path: PathBuf) -> Option<Vec<&Song>> {
let result: Arc<Mutex<Vec<&Song>>> = Arc::new(Mutex::new(Vec::new()));
self.library.par_iter().for_each(|track| {
if path == track.location.path() {
if path == track.primary_uri().unwrap().0.path() {
//TODO: make this also not unwrap
result.clone().lock().unwrap().push(track);
}
});
@ -660,10 +765,7 @@ impl MusicLibrary {
}
/// Finds all the audio files within a specified folder
pub fn scan_folder(
&mut self,
target_path: &str,
) -> Result<i32, Box<dyn std::error::Error>> {
pub fn scan_folder(&mut self, target_path: &str) -> Result<i32, Box<dyn std::error::Error>> {
let mut total = 0;
let mut errors = 0;
for target_file in WalkDir::new(target_path)
@ -726,7 +828,6 @@ impl MusicLibrary {
}
pub fn add_file(&mut self, target_file: &Path) -> Result<(), Box<dyn Error>> {
let new_song = Song::from_file(target_file)?;
match self.add_song(new_song) {
Ok(_) => (),
@ -742,14 +843,13 @@ impl MusicLibrary {
let tracks = Song::from_cue(cuesheet)?;
let mut tracks_added = tracks.len() as i32;
for (new_song, location) in tracks {
// Try to remove the original audio file from the db if it exists
if self.remove_uri(&URI::Local(location.clone())).is_ok() {
tracks_added -= 1
}
match self.add_song(new_song) {
Ok(_) => {},
Ok(_) => {}
Err(_error) => {
//println!("{}", _error);
continue;
@ -760,13 +860,14 @@ impl MusicLibrary {
}
pub fn add_song(&mut self, new_song: Song) -> Result<(), Box<dyn Error>> {
if self.query_uri(&new_song.location).is_some() {
return Err(format!("URI already in database: {:?}", new_song.location).into());
let location = new_song.primary_uri()?.0;
if self.query_uri(location).is_some() {
return Err(format!("URI already in database: {:?}", location).into());
}
match new_song.location {
URI::Local(_) if self.query_path(new_song.location.path()).is_some() => {
return Err(format!("Location exists for {:?}", new_song.location).into())
match location {
URI::Local(_) if self.query_path(location.path()).is_some() => {
return Err(format!("Location exists for {:?}", location).into())
}
_ => (),
}
@ -789,17 +890,18 @@ impl MusicLibrary {
}
/// Scan the song by a location and update its tags
// TODO: change this to work with multiple uris
pub fn update_uri(
&mut self,
target_uri: &URI,
new_tags: Vec<Tag>,
) -> Result<(), Box<dyn std::error::Error>> {
let target_song = match self.query_uri(target_uri) {
let (target_song, _) = match self.query_uri(target_uri) {
Some(song) => song,
None => return Err("URI not in database!".to_string().into()),
};
println!("{:?}", target_song.0.location);
println!("{:?}", target_song.location);
for tag in new_tags {
println!("{:?}", tag);
@ -1014,3 +1116,21 @@ impl MusicLibrary {
Ok(albums)
}
}
#[cfg(test)]
mod test {
use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
use crate::{config::config::Config, music_storage::library::MusicLibrary};
#[test]
fn library_init() {
let config = Config::read_file(PathBuf::from("test_config/config_test.json")).unwrap();
let target_uuid = config.libraries.libraries[0].uuid;
let a = MusicLibrary::init(Arc::new(RwLock::from(config)), target_uuid).unwrap();
dbg!(a);
}
}

View file

@ -1,91 +1,146 @@
use std::{fs::File, path::{Path, PathBuf}, io::{Read, Error}};
use bincode::config;
use chrono::Duration;
use uuid::Uuid;
// use walkdir::Error;
use crate::music_controller::controller::Controller;
use super::{
library::{AlbumArt, Song, Tag},
music_collection::MusicCollection, db_reader::{
itunes::reader::ITunesLibrary,
extern_library::ExternalLibrary
},
use std::error::Error;
use std::{
fs::File,
io::Read,
path::PathBuf,
sync::{Arc, RwLock},
};
use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment, Playlist as List2, MasterPlaylist};
use std::time::Duration;
#[derive(Debug, Clone)]
// use chrono::Duration;
use super::library::{AlbumArt, MusicLibrary, Song, Tag, URI};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment, Playlist as List2};
use nestify::nest;
use rayon::prelude::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SortOrder {
Manual,
Tag(Tag)
Tag(Vec<Tag>),
}
#[derive(Debug, Clone)]
pub struct Playlist<'a> {
nest! {
#[derive(Debug, Clone, Deserialize, Serialize)]*
#[derive(Default)]
pub struct PlaylistFolder {
name: String,
items: Vec<
pub enum PlaylistFolderItem {
Folder(PlaylistFolder),
List(Playlist)
}
>
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Playlist {
uuid: Uuid,
title: String,
cover: Option<&'a AlbumArt>,
cover: Option<AlbumArt>,
tracks: Vec<Uuid>,
sort_order: SortOrder,
play_count: i32,
play_time: Duration,
}
impl<'a> Playlist<'a> {
impl Playlist {
pub fn new() -> Self {
Default::default()
}
pub fn play_count(&self) -> i32 {
self.play_count
}
pub fn play_time(&self) -> chrono::Duration {
pub fn play_time(&self) -> Duration {
self.play_time
}
pub fn title(&self) -> &String {
&self.title
}
pub fn cover(&self) -> Option<&AlbumArt> {
match &self.cover {
Some(e) => Some(e),
None => None,
}
}
pub fn tracks(&self) -> Vec<Uuid> {
self.tracks.to_owned()
}
pub fn set_tracks(&mut self, tracks: Vec<Uuid>) {
self.tracks = tracks;
}
pub fn add_track(&mut self, track: Uuid) -> Result<(), Error> {
pub fn add_track(&mut self, track: Uuid) {
self.tracks.push(track);
Ok(())
}
pub fn remove_track(&mut self, index: i32) -> Result<(), Error> {
pub fn remove_track(&mut self, index: i32) {
let index = index as usize;
if (self.tracks.len() - 1) >= index {
self.tracks.remove(index);
}
}
pub fn get_index(&self, uuid: Uuid) -> Option<usize> {
let mut i = 0;
if self.contains(uuid) {
for track in &self.tracks {
i += 1;
if &uuid == track {
dbg!("Index gotted! ", i);
return Some(i);
}
}
}
None
}
pub fn contains(&self, uuid: Uuid) -> bool {
self.get_index(uuid).is_some()
}
pub fn to_file(&self, path: &str) -> Result<(), Box<dyn Error>> {
super::utils::write_file(self, PathBuf::from(path))?;
Ok(())
}
// pub fn get_index(&self, song_name: &str) -> Option<usize> {
// let mut index = 0;
// if self.contains_value(&Tag::Title, song_name) {
// for track in &self.tracks {
// index += 1;
// if song_name == track.tags.get_key_value(&Tag::Title).unwrap().1 {
// dbg!("Index gotted! ", index);
// return Some(index);
// }
// }
// }
// None
// }
pub fn contains_value(&self, tag: &Tag, value: &str) -> bool {
&self.tracks.iter().for_each(|track| {
});
false
pub fn from_file(path: &str) -> Result<Playlist, Box<dyn Error>> {
super::utils::read_file(PathBuf::from(path))
}
pub fn to_m3u8(&mut self, tracks: Vec<Song>) {
let seg = tracks
pub fn to_m3u8(
&mut self,
lib: Arc<RwLock<MusicLibrary>>,
location: &str,
) -> Result<(), Box<dyn Error>> {
let lib = lib.read().unwrap();
let seg = self
.tracks
.iter()
.map({
|track| {
MediaSegment {
uri: track.location.to_string().into(),
.filter_map(|uuid| {
// TODO: The Unwraps need to be handled here
if let Some((track, _)) = lib.query_uuid(uuid) {
if let URI::Local(_) = track.primary_uri().unwrap().0 {
Some(MediaSegment {
uri: track.primary_uri().unwrap().0.to_string(),
duration: track.duration.as_millis() as f32,
title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.into()),
title: track
.tags
.get_key_value(&Tag::Title)
.map(|tag| tag.1.into()),
..Default::default()
})
} else {
None
}
} else {
None
}
})
.collect::<Vec<MediaSegment>>();
@ -100,19 +155,24 @@ impl<'a> Playlist<'a> {
segments: seg.clone(),
..Default::default()
};
//TODO: change this to put in a real file path
let mut file = std::fs::OpenOptions::new()
.read(true)
.create(true)
.truncate(true)
.write(true)
.open("F:\\Dango Music Player\\playlist.m3u8")
.unwrap();
m3u8.write_to(&mut file).unwrap();
.open(location)?;
m3u8.write_to(&mut file)?;
Ok(())
}
pub fn from_m3u8(path: &str) -> Result<Playlist<'a>, Error> {
pub fn from_m3u8(
path: &str,
lib: Arc<RwLock<MusicLibrary>>,
) -> Result<Playlist, Box<dyn Error>> {
let mut file = match File::open(path) {
Ok(file) => file,
Err(e) => return Err(e),
Err(e) => return Err(e.into()),
};
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).unwrap();
@ -120,56 +180,174 @@ impl<'a> Playlist<'a> {
let parsed = m3u8_rs::parse_playlist(&bytes);
let playlist = match parsed {
Result::Ok((i, playlist)) => playlist,
Result::Ok((_, playlist)) => playlist,
Result::Err(e) => panic!("Parsing error: \n{}", e),
};
match playlist {
List2::MasterPlaylist(_) => panic!(),
List2::MediaPlaylist(pl) => {
let values = pl.segments.iter().map(|seg| seg.uri.to_owned() ).collect::<Vec<String>>();
List2::MasterPlaylist(_) => {
Err("This is a Master Playlist!\nPlase input a Media Playlist".into())
}
List2::MediaPlaylist(playlist_) => {
let mut uuids = Vec::new();
for seg in playlist_.segments {
let path_ = PathBuf::from(seg.uri.to_owned());
let mut lib = lib.write().unwrap();
let uuid = if let Some((song, _)) = lib.query_uri(&URI::Local(path_.clone())) {
song.uuid
} else {
let song_ = Song::from_file(&path_)?;
let uuid = song_.uuid.to_owned();
lib.add_song(song_)?;
uuid
};
uuids.push(uuid);
}
let mut playlist = Playlist::new();
#[cfg(target_family = "windows")]
{
playlist.title = path
.split('\\')
.last()
.unwrap_or_default()
.strip_suffix(".m3u8")
.unwrap_or_default()
.to_string();
}
#[cfg(target_family = "unix")]
{
playlist.title = path
.split("/")
.last()
.unwrap_or_default()
.strip_suffix(".m3u8")
.unwrap_or_default()
.to_string();
}
playlist.set_tracks(uuids);
Ok(playlist)
}
}
}
todo!()
}
fn title(&self) -> &String {
&self.title
}
fn cover(&self) -> Option<&AlbumArt> {
match self.cover {
Some(e) => Some(e),
None => None,
pub fn out_tracks(&self, lib: Arc<RwLock<MusicLibrary>>) -> (Vec<Song>, Vec<&Uuid>) {
let lib = lib.read().unwrap();
let mut songs = vec![];
let mut invalid_uuids = vec![];
for uuid in &self.tracks {
if let Some((track, _)) = lib.query_uuid(uuid) {
songs.push(track.to_owned());
} else {
invalid_uuids.push(uuid);
}
}
fn tracks(&self) -> Vec<Uuid> {
self.tracks.to_owned()
if let SortOrder::Tag(sort_by) = &self.sort_order {
println!("sorting by: {:?}", sort_by);
songs.par_sort_by(|a, b| {
for (i, sort_option) in sort_by.iter().enumerate() {
dbg!(&i);
let tag_a = match sort_option {
Tag::Field(field_selection) => {
match a.get_field(field_selection.as_str()) {
Some(field_value) => field_value.to_string(),
None => continue,
}
}
_ => match a.get_tag(sort_option) {
Some(tag_value) => tag_value.to_owned(),
None => continue,
},
};
let tag_b = match sort_option {
Tag::Field(field_selection) => match b.get_field(field_selection) {
Some(field_value) => field_value.to_string(),
None => continue,
},
_ => match b.get_tag(sort_option) {
Some(tag_value) => tag_value.to_owned(),
None => continue,
},
};
dbg!(&i);
if let (Ok(num_a), Ok(num_b)) = (tag_a.parse::<i32>(), tag_b.parse::<i32>()) {
// If parsing succeeds, compare as numbers
return dbg!(num_a.cmp(&num_b));
} else {
// If parsing fails, compare as strings
return dbg!(tag_a.cmp(&tag_b));
}
}
// If all tags are equal, sort by Track number
let path_a = PathBuf::from(a.get_field("location").unwrap().to_string());
let path_b = PathBuf::from(b.get_field("location").unwrap().to_string());
path_a.file_name().cmp(&path_b.file_name())
})
}
(songs, invalid_uuids)
}
}
impl Default for Playlist<'_> {
impl Default for Playlist {
fn default() -> Self {
Playlist {
uuid: Uuid::new_v4(),
title: String::default(),
cover: None,
tracks: Vec::default(),
sort_order: SortOrder::Manual,
play_count: 0,
play_time: Duration::zero(),
play_time: Duration::from_secs(0),
}
}
}
// #[test]
// fn list_to_m3u8() {
// let lib = ITunesLibrary::from_file(Path::new(
// "F:\\Music\\Mp3\\Music Main\\iTunes Music Library.xml",
// ));
// let mut a = Playlist::new();
// let c = lib.to_songs();
// let mut b = c.iter().map(|song| song.to_owned()).collect::<Vec<Song>>();
// a.tracks.append(&mut b);
// a.to_m3u8()
// }
#[cfg(test)]
mod test_super {
use super::*;
use crate::config::config::tests::read_config_lib;
#[test]
fn list_to_m3u8() {
let (_, lib) = read_config_lib();
let mut playlist = Playlist::new();
let tracks = lib.library.iter().map(|track| track.uuid).collect();
playlist.set_tracks(tracks);
_ = playlist.to_m3u8(
Arc::new(RwLock::from(lib)),
".\\test-config\\playlists\\playlist.m3u8",
);
}
fn m3u8_to_list() -> Playlist {
let (_, lib) = read_config_lib();
let arc = Arc::new(RwLock::from(lib));
let playlist =
Playlist::from_m3u8(".\\test-config\\playlists\\playlist.m3u8", arc).unwrap();
_ = playlist.to_file(".\\test-config\\playlists\\playlist");
dbg!(playlist)
}
#[test]
fn out_queue_sort() {
let (_, lib) = read_config_lib();
let mut list = m3u8_to_list();
list.sort_order = SortOrder::Tag(vec![Tag::Album]);
let songs = &list.out_tracks(Arc::new(RwLock::from(lib)));
dbg!(songs);
}
}

View file

@ -1,15 +1,17 @@
use std::fs::{File, self};
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
use std::error::Error;
use walkdir::WalkDir;
use deunicode::deunicode_with_tofu;
use file_format::{FileFormat, Kind};
use snap;
use deunicode::deunicode_with_tofu;
use std::error::Error;
use std::fs::{self, File};
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use super::library::{AlbumArt, URI};
#[cfg(target_family = "windows")]
use std::os::windows::fs::MetadataExt;
pub(super) fn normalize(input_string: &str) -> String {
// Normalize the string to latin characters... this needs a lot of work
let mut normalized = deunicode_with_tofu(input_string, " ");
@ -23,12 +25,15 @@ pub(super) fn normalize(input_string: &str) -> String {
/// Write any data structure which implements [serde::Serialize]
/// out to a [bincode] encoded file compressed using [snap]
pub(super) fn write_file<T: serde::Serialize>(
pub(super) fn write_file<
T: serde::Serialize,
U: std::convert::AsRef<Path> + std::convert::AsRef<std::ffi::OsStr> + Clone,
>(
library: T,
path: PathBuf,
path: U,
) -> Result<(), Box<dyn Error>> {
// Create a temporary name for writing out
let mut writer_name = path.clone();
let mut writer_name = PathBuf::from(&path);
writer_name.set_extension("tmp");
// Create a new BufWriter on the file and a snap frame encoder
@ -50,7 +55,9 @@ pub(super) fn write_file<T: serde::Serialize>(
/// Read a file serialized out with [write_file] and turn it into
/// the desired structure
pub(super) fn read_file<T: for<'de> serde::Deserialize<'de>>(path: PathBuf) -> Result<T, Box<dyn Error>> {
pub(super) fn read_file<T: for<'de> serde::Deserialize<'de>>(
path: PathBuf,
) -> Result<T, Box<dyn Error>> {
// Create a new snap reader over the file
let file_reader = BufReader::new(File::open(path)?);
let mut d = snap::read::FrameDecoder::new(file_reader);
@ -74,11 +81,12 @@ pub fn find_images(song_path: &Path) -> Result<Vec<AlbumArt>, Box<dyn Error>> {
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.depth() < 3) // Don't recurse very deep
.filter(|e| e.depth() < 3)
// Don't recurse very deep
{
println!("{:?}", target_file);
let path = target_file.path();
if !path.is_file() {
if !path.is_file() || !path.exists() {
continue;
}
@ -87,6 +95,11 @@ pub fn find_images(song_path: &Path) -> Result<Vec<AlbumArt>, Box<dyn Error>> {
continue;
}
#[cfg(target_family = "windows")]
if (4 & path.metadata().unwrap().file_attributes()) == 4 {
continue;
}
let image_uri = URI::Local(path.to_path_buf().canonicalize()?);
images.push(AlbumArt::External(image_uri));