/
main.rs
470 lines (438 loc) · 15.6 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//#![windows_subsystem = "windows"]
use dirs::data_local_dir;
use iced::{executor, window, Align, Application, Element, Settings, Subscription};
use log::{log_enabled, LevelFilter};
use player::{PlayerCommand, PlayerStatus};
pub mod prelude {
pub use log::{debug, error, info, trace, warn};
pub use stable_eyre::eyre::{eyre, Report, WrapErr};
pub type Result<T> = std::result::Result<T, Report>;
}
mod player;
mod playlist;
use prelude::*;
use iced_native::{
button, slider, Button, Column, Command, HorizontalAlignment, Length, Row, Slider, Text,
};
use rand::prelude::*;
use serde::Deserialize;
use serde::Serialize;
use std::thread;
use std::{
borrow::Cow,
collections::HashMap,
fs::File,
io::Write,
path::PathBuf,
sync::mpsc::{Receiver, Sender},
time::Duration,
};
use std::{collections::HashSet, thread::JoinHandle};
const SAVE_INTERVAL: Duration = Duration::from_secs(60 * 30);
#[derive(Serialize, Deserialize, Default)]
struct ConfigData<'a> {
playlists: Cow<'a, HashMap<PathBuf, Vec<String>>>,
favorites: Cow<'a, HashSet<String>>,
volume: u8,
path: PathBuf,
current_playlist: Cow<'a, String>,
}
struct PlaybackControl {
path: PathBuf,
play_next: button::State,
is_paused: bool,
pause: button::State,
favorite: button::State,
export_favorites: button::State,
trash_current: button::State,
data_favorites: HashSet<String>,
is_favorite: bool,
volume_input: slider::State,
volume: u8,
length: Option<Duration>,
playtime: Option<Duration>,
total_playtime: Option<Duration>,
tx: Sender<PlayerCommand>,
rx: Receiver<PlayerStatus>,
current_playlist: String,
/// Displayed current file,
/// also used by play_next to remove the current file from the playlist, if this is not empty
current_file: String,
playlists: HashMap<PathBuf, Vec<String>>,
child: JoinHandle<()>,
}
impl PlaybackControl {
fn play_next(&mut self) {
let mut remove = false;
if let Some(v) = self.playlists.get_mut(&self.path) {
if !v.is_empty() {
if !self.current_file.is_empty() {
let removed = v.remove(0);
trace!("Removing {}", removed);
}
}
if !v.is_empty() {
self.tx
.send(PlayerCommand::Play(v[0].clone(), self.volume))
.expect("Can't send playback command!");
self.current_playlist = self.path.to_string_lossy().into_owned();
} else {
remove = true;
}
}
if remove {
debug!("Removing playlist");
self.playlists.remove(&self.path);
}
}
fn store_state(&self) {
let data = ConfigData {
playlists: Cow::Borrowed(&self.playlists),
volume: self.volume,
current_playlist: Cow::Borrowed(&self.current_playlist),
path: self.path.clone(),
favorites: Cow::Borrowed(&self.data_favorites),
};
match serde_json::to_string(&data) {
Err(e) => warn!("Can't serialize data! {}", e),
Ok(v) => {
thread::spawn(move || {
let file = config_path(true);
match File::create(&file) {
Err(e) => warn!("Can't create config file {:?}: {}", file, e),
Ok(mut file) => match file.write_all(v.as_bytes()) {
Err(e) => warn!("Error writing config {}", e),
Ok(_) => match std::fs::rename(config_path(true), config_path(false)) {
Ok(_) => info!("Config saved"),
Err(e) => error!("Can't move file over backup: {}", e),
},
},
}
});
}
}
}
/// Handle time tick for updating UI from player state updates
fn handle_tick(&mut self) {
if let Ok(msg) = self.rx.try_recv() {
if log_enabled!(log::Level::Trace) {
match msg {
PlayerStatus::Playtime(_) => (),
_ => trace!("Player state: {:?}", msg),
}
}
match msg {
PlayerStatus::Playing(f, length) => {
self.current_file = f;
self.is_paused = false;
self.is_favorite = self.data_favorites.contains(&self.current_file);
debug!("Length {:?}", length);
self.length = length;
}
PlayerStatus::Ended => {
debug!("Playback ended");
self.play_next();
self.current_file = String::new();
}
PlayerStatus::Paused => {
self.is_paused = true;
}
PlayerStatus::Playtime(time) => {
self.playtime = time;
}
PlayerStatus::InvalidFile(f) => {
dbg!(&f);
// set as file, so play_next removes it
self.current_file = f;
self.play_next();
self.current_file = String::new();
}
}
}
}
fn file_dropped(&mut self, file: PathBuf) {
match std::fs::read_to_string(&file) {
Ok(data) => match playlist_decoder::decode(&data) {
Ok(mut playlist) => {
if let Some(v) = self.playlists.get_mut(&file) {
if v.is_empty() {
v.append(&mut playlist);
}
} else {
playlist.shuffle(&mut thread_rng());
self.playlists.insert(file.clone(), playlist);
}
self.path = file;
// reset current_file to not remove this file from playback
self.current_file = String::new();
self.play_next();
}
Err(e) => error!("{}", e),
},
Err(e) => warn!("Can't open dropped file {}", e),
}
}
fn trash_file(&mut self) {
if !self.current_file.is_empty() {
match trash::delete(&self.current_file) {
Ok(_) => info!("Trashed {}", self.current_file),
Err(e) => error!("Can't trash file {}: {}", self.current_file, e),
}
}
}
}
#[derive(Debug, Clone)]
pub enum Message {
PlayNext,
Pause,
SliderChanged(u8),
Window(iced_native::Event),
Tick,
ToggleFavorite,
ExportFavorites,
SaveConfig,
TrashFile,
}
/// Config file, temp specifies if a .bak version should be used
fn config_path(temp: bool) -> PathBuf {
let mut file = data_local_dir().unwrap();
match temp {
true => file.push("audio_wrench.json.bak"),
false => file.push("audio_wrench.json"),
}
file
}
impl Default for PlaybackControl {
fn default() -> Self {
let file = config_path(false);
let data: ConfigData = if file.is_file() {
match std::fs::read_to_string(&file)
.map_err(Report::from)
.and_then(|v| serde_json::from_str(&v).map_err(Report::from))
{
Ok(v) => v,
Err(e) => {
error!("Unable to read config at {:?}: {}", file, e);
Default::default()
}
}
} else {
Default::default()
};
let (tx, rx, child) = player::Player::new().expect("Can't start audio controller");
// TODO: don't use into_owned, avoid copy
Self {
path: data.path,
play_next: Default::default(),
pause: Default::default(),
volume_input: Default::default(),
favorite: Default::default(),
trash_current: Default::default(),
export_favorites: Default::default(),
volume: data.volume,
tx,
rx,
playlists: data.playlists.into_owned(),
current_playlist: data.current_playlist.into_owned(),
current_file: Default::default(),
is_favorite: false,
is_paused: false,
data_favorites: data.favorites.into_owned(),
length: None,
total_playtime: None,
playtime: None,
child,
}
}
}
impl Drop for PlaybackControl {
fn drop(&mut self) {
self.store_state();
}
}
impl Application for PlaybackControl {
type Executor = executor::Default;
type Message = Message;
type Flags = ();
fn view(&mut self) -> Element<Self::Message> {
let fav_text = match self.is_favorite {
true => "Unfavorite",
false => "Favorite",
};
let play_text = match self.current_file.is_empty() {
true => "Play",
false => "Next",
};
let pause_text = match self.is_paused {
true => "Resume",
false => "Pause",
};
let length_text = match self.length {
None => String::from("--:--"),
Some(v) => {
let secs_total = v.as_secs();
let minutes = secs_total / 60;
format!("{:02}:{:02}", minutes, secs_total - (minutes * 60))
}
};
let playtime_text = match self.playtime {
None => String::from("--:--"),
Some(v) => {
let secs_total = v.as_secs();
let minutes = secs_total / 60;
format!("{:02}:{:02}", minutes, secs_total - (minutes * 60))
}
};
let timer_text = format!("{}/{}", playtime_text, length_text);
let mut row_controls = Row::new()
.align_items(Align::Center)
.spacing(20)
.push(
Button::new(&mut self.play_next, Text::new(play_text)).on_press(Message::PlayNext),
)
.push(Button::new(&mut self.pause, Text::new(pause_text)).on_press(Message::Pause));
if !self.current_file.is_empty() {
row_controls = row_controls
.push(
Button::new(&mut self.favorite, Text::new(fav_text))
.on_press(Message::ToggleFavorite),
)
.push(
Button::new(&mut self.trash_current, Text::new("Trash File"))
.on_press(Message::TrashFile),
);
}
Column::new()
.max_width(800)
.spacing(20)
.align_items(Align::Center)
.push(
Text::new(&self.current_playlist.to_string())
.size(20)
.width(Length::Fill)
.horizontal_alignment(HorizontalAlignment::Center),
)
.push(
Text::new(&self.current_file.to_string())
.size(20)
.width(Length::Fill)
.horizontal_alignment(HorizontalAlignment::Center),
)
.push(row_controls)
.push(
Text::new(timer_text)
.size(20)
.width(Length::Fill)
.horizontal_alignment(HorizontalAlignment::Center),
)
.push(
Text::new(format!("{}% Volume", self.volume))
.size(20)
.width(Length::Fill)
.horizontal_alignment(HorizontalAlignment::Center),
)
// TODO: use https://crates.io/crates/iced_audio control elements
.push(Slider::new(
&mut self.volume_input,
0..=100,
self.volume,
Message::SliderChanged,
))
.padding(20)
.push(
Text::new("Drop a playlist file to start (.m3u/.pls/.xspf/.asx)")
.size(20)
.width(Length::Fill)
.horizontal_alignment(HorizontalAlignment::Center),
)
.padding(20)
.push(
Button::new(&mut self.export_favorites, Text::new("Export Favorites"))
.on_press(Message::ExportFavorites),
)
.into()
}
fn update(&mut self, message: Message) -> Command<Self::Message> {
match message {
Message::PlayNext => {
self.play_next();
}
Message::Pause => {
self.tx
.send(PlayerCommand::Pause)
.expect("Can't send playback command!");
}
Message::SliderChanged(v) => {
self.volume = v;
self.tx
.send(PlayerCommand::Volume(v))
.expect("Can't send playback command!");
}
Message::Window(iced_native::Event::Window(
iced_native::window::Event::FileDropped(f),
)) => self.file_dropped(f),
Message::Tick => self.handle_tick(),
Message::Window(_) => (),
Message::ToggleFavorite => {
if !self.current_file.is_empty() {
if self.is_favorite {
self.data_favorites.remove(&self.current_file);
} else {
self.data_favorites.insert(self.current_file.clone());
}
self.is_favorite = !self.is_favorite;
}
}
Message::ExportFavorites => {
let path = "favorites.xspf";
match playlist::write_playlist(self.data_favorites.iter(), path) {
Ok(_) => info!("Favorites written to {}", path),
Err(e) => error!("Can't write favorites to {}: {}", path, e),
}
}
Message::SaveConfig => {
self.store_state();
}
Message::TrashFile => self.trash_file(),
}
Command::none()
}
fn new(_flags: ()) -> (PlaybackControl, Command<Message>) {
(PlaybackControl::default(), Command::none())
}
fn title(&self) -> String {
String::from("Audio Wrench")
}
fn subscription(&self) -> Subscription<Self::Message> {
let timer_ticks = iced::time::every(Duration::from_millis(100)).map(|_| Message::Tick);
let timer_save = iced::time::every(SAVE_INTERVAL).map(|_| Message::SaveConfig);
let window_ticks = iced_native::subscription::events().map(Message::Window);
Subscription::batch(vec![window_ticks, timer_ticks, timer_save])
}
fn background_color(&self) -> iced_native::Color {
iced_native::Color::WHITE
}
fn scale_factor(&self) -> f64 {
1.0
}
fn mode(&self) -> iced::window::Mode {
iced::window::Mode::Windowed
}
}
fn main() -> Result<()> {
stable_eyre::install().expect("Can't initialize backtrace handling!");
let mut builder = env_logger::Builder::new();
builder.filter_level(LevelFilter::Warn);
#[cfg(debug_assertions)]
builder.filter_module("audio_wrench", LevelFilter::Trace);
#[cfg(not(debug_assertions))]
builder.filter_module("audio_wrench", LevelFilter::Info);
builder.parse_env("RUST_LOG");
builder.init();
let mut settings: Settings<()> = Settings::default();
let mut window_settings = window::Settings::default();
window_settings.size = (500, 500);
settings.window = window_settings;
PlaybackControl::run(settings).expect("Failed to run GUI");
Ok(())
}