-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rs
351 lines (317 loc) · 14.6 KB
/
app.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
use eframe::egui::{self, DroppedFile, ScrollArea, Vec2};
use std::collections::{HashMap, HashSet};
use structopt::StructOpt;
use crate::theme::Theme;
use crate::ui_components::*;
use kira::instance::{InstanceSettings, InstanceState, StopInstanceSettings};
use kira::manager::AudioManagerSettings;
use kira::{
instance::{PauseInstanceSettings, ResumeInstanceSettings},
manager::AudioManager,
};
use log::{debug, info};
use super::sound::*;
use eframe::epi;
#[cfg(feature = "persistence")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "persistence", derive(Deserialize, Serialize))]
pub struct ApplicationState {
#[serde(skip)]
pub audiomanager: Option<AudioManager>,
pub active_sound: Option<MetaSound>,
volume: f64,
queue: SoundQueue,
play_count: HashMap<MetaSound, usize>,
favourites: HashSet<MetaSound>,
bookmarks: HashSet<MetaSound>,
theme: Theme,
powersave: bool,
}
impl Default for ApplicationState {
fn default() -> Self {
Self {
audiomanager: None,
active_sound: None,
volume: 1.0,
queue: vec![],
play_count: HashMap::default(),
favourites: HashSet::default(),
bookmarks: HashSet::default(),
theme: Theme::default(),
powersave: true,
}
}
}
impl epi::App for ApplicationState {
fn name(&self) -> &str {
env!("CARGO_PKG_NAME")
}
#[cfg(feature = "persistence")]
fn setup(
&mut self,
ctx: &egui::CtxRef,
_frame: &mut epi::Frame<'_>,
storage: Option<&dyn epi::Storage>,
) {
if let Some(storage) = storage {
let storage: ApplicationState =
epi::get_value(storage, epi::APP_KEY).unwrap_or_default();
*self = storage;
}
self.theme.apply(ctx);
// Parse arguments to auto-play sound
let args = super::Opt::from_args();
// Create an AudioManager
self.audiomanager = AudioManager::new(AudioManagerSettings::default()).ok();
// If the application was called with files as an argument, play the first
if let Some(first_arg) = args.files.first() {
if let Some(manager) = &mut self.audiomanager {
// restore previous volume
let _ = manager.main_track().set_volume(self.volume);
// load the sound from disk
let sound = MetaSound::default()
.with_path(first_arg)
.try_meta()
.load_soundhandle(manager);
self.active_sound = Some(sound.clone());
self.active_sound.as_mut().map(|s| s.play());
// check if playlist has this sound
if !self.queue.contains(&sound) {
self.queue.push(sound);
}
}
}
}
#[cfg(feature = "persistence")]
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
fn update(&mut self, ctx: &egui::CtxRef, _frame: &mut epi::Frame<'_>) {
let ApplicationState {
audiomanager: manager,
active_sound,
volume,
queue,
bookmarks,
favourites,
play_count,
theme,
powersave,
} = self;
if egui::CentralPanel::default()
.show(ctx, |ui| {
// ctx.style_ui(ui);
ctx.request_repaint();
// info!("{:?}", ctx.input().raw);
if !ctx.input().raw.dropped_files.is_empty() {
info!("{:?}", ctx.input().raw.dropped_files);
handle_dropped(&ctx.input().raw.dropped_files, queue);
}
if let Some(manager) = manager {
if let Some(current_metasound) = active_sound {
ui.label(¤t_metasound.name);
if ui
.add(
egui::Slider::new(volume, 0.0..=3.0)
.text("🔈")
.show_value(false),
)
.changed()
{
let _ = manager.main_track().set_volume(*volume as f64);
}
}
if let Some(current_metasound) = active_sound {
if let Some(instancehandle) = current_metasound.instancehandle.as_mut() {
if let Some(soundhandle) = current_metasound.soundhandle.as_mut() {
let cur_pos = instancehandle.position();
let len = soundhandle.duration();
let progress = (cur_pos / len) as f32;
// current_metasound.soundhandle.unwrap().
let response = scrubber(ui, progress);
if ui.input().pointer.any_pressed() {
if let Some(pos) = response.interact_pointer_pos() {
let w = ui.available_size().x;
let p = pos.x;
let fac = (p / w) as f64;
let _ = instancehandle.seek_to(fac * len);
}
}
}
}
} else {
ui.label("No sound active");
}
ui.horizontal(|ui| {
if let Some(current_metasound) = active_sound {
if ui.add(egui::Button::new("⏮")).clicked() {
if let Some(i) = queue.to_index(¤t_metasound.clone()) {
let ri = (i as isize - 1).max(0) as usize;
play_as_active(active_sound, &queue[ri], manager, play_count);
}
}
}
// info about current song
if let Some(current_metasound) = active_sound {
if let Some(instancehandle) = current_metasound.instancehandle.as_mut()
{
if let Some(soundhandle) = current_metasound.soundhandle.as_mut() {
// done playing?
debug!(
"{}",
instancehandle.position() - soundhandle.duration()
);
if instancehandle.position() - soundhandle.duration() > -0.05 {
info!("Sound has finished playing, next one!");
if let Some(i) = queue.to_index(current_metasound) {
let ri = (i + 1).min(queue.len() - 1);
play_as_active(
active_sound,
&queue[ri],
manager,
play_count,
);
}
}
}
}
}
// info about current song
if let Some(current_metasound) = active_sound {
if let Some(instancehandle) = current_metasound.instancehandle.as_mut()
{
if let Some(soundhandle) = current_metasound.soundhandle.as_mut() {
match instancehandle.state() {
InstanceState::Playing => {
if ui.button("⏸").clicked() {
let _ =
soundhandle.pause(PauseInstanceSettings::new());
}
if ui.button("⏹").clicked() {
let _ =
soundhandle.stop(StopInstanceSettings::new());
}
}
InstanceState::Paused(_) => {
if ui.button("▶").clicked() {
let _ = soundhandle
.resume(ResumeInstanceSettings::new());
}
if ui.button("⏹").clicked() {
let _ =
soundhandle.stop(StopInstanceSettings::new());
}
}
InstanceState::Stopped => {
if ui.button("▶").clicked() {
let _ = soundhandle.play(InstanceSettings::new());
}
}
_ => {}
}
} else {
ui.label("No active sound handle");
}
} else {
// There is no active instance handle, offer to play
// ui.label("No active sound instance");
if ui.button("▶").clicked() {
*current_metasound =
current_metasound.load_soundhandle(manager);
info!("{:?}", current_metasound.play());
*play_count.entry(current_metasound.clone()).or_insert(0) += 1;
}
}
if ui
.add(
egui::Button::new("⏭"), // .enabled(*queue_index < queue.len())
)
.clicked()
{
if let Some(i) = queue.to_index(current_metasound) {
let ri = (i + 1).min(queue.len() - 1);
play_as_active(active_sound, &queue[ri], manager, play_count);
}
}
} else {
ui.label("No sound active");
}
if let Some(s) = active_sound {
if ui.button("♡").clicked() {
favourites.insert(s.clone());
}
}
if let Some(s) = active_sound {
if ui.button("🔖").clicked() {
if let Some(instancehandle) = &s.instancehandle {
s.bookmarks.push(instancehandle.position());
let mut prev_bookmarks = bookmarks
.get(s)
.map(|b| b.bookmarks.clone())
.unwrap_or_default();
prev_bookmarks.extend(s.bookmarks.clone());
prev_bookmarks.sort_by(|a, b| a.partial_cmp(b).unwrap());
prev_bookmarks.dedup();
debug!("{:?}", prev_bookmarks);
s.bookmarks = prev_bookmarks;
bookmarks.replace(s.clone());
}
}
}
// end horizontal layout
});
ScrollArea::auto_sized().show(ui, |ui| {
playlist_ui(queue, active_sound, play_count, manager, ui);
playcount_ui(active_sound, play_count, manager, ui);
favourite_ui(active_sound, favourites, play_count, manager, ui);
bookmark_ui(active_sound, bookmarks, manager, ui);
settings_ui(theme, powersave, ui);
});
} else {
ui.label("No Audio manager");
}
})
.response
.hovered()
|| !*powersave
{
// only repaint on hover
ctx.request_repaint();
}
}
fn warm_up_enabled(&self) -> bool {
false
}
fn on_exit(&mut self) {}
fn auto_save_interval(&self) -> std::time::Duration {
std::time::Duration::from_secs(30)
}
fn clear_color(&self) -> egui::Rgba {
// NOTE: a bright gray makes the shadows of the windows look weird.
// We use a bit of transparency so that if the user switches on the
// `transparent()` option they get immediate results.
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()
}
}
/// Recurse dropped folders
fn handle_dropped(dropped_files: &Vec<DroppedFile>, queue: &mut SoundQueue) {
for p in dropped_files.iter().filter_map(|d| d.path.as_ref()) {
if p.is_dir() {
for f in walkdir::WalkDir::new(p)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some())
{
let s = MetaSound::default().with_path(f.path()).try_meta();
if s.is_supported() {
queue.push(s);
}
}
} else {
let s = MetaSound::default().with_path(p).try_meta();
if s.is_supported() {
queue.push(s);
}
}
}
}