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
// SPDX-License-Identifier: GPL-3.0-or-later
//! The player as a modern skin sees it, and a player that plays nothing.
use std::collections::BTreeMap;
use crate::host::Clock;
/// One playlist entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Track {
/// The title shown.
pub title: String,
/// The length in milliseconds.
pub length_ms: i32,
}
/// What the transport is doing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Status {
/// Nothing plays.
Stopped,
/// The song plays.
Playing,
/// The song is paused.
Paused,
}
/// The player as a modern skin sees it. Positions and lengths are milliseconds; volume is 0..255.
pub trait Player {
/// What the transport is doing.
fn status(&self) -> Status;
/// Where the song is, in milliseconds.
fn position_ms(&self) -> i32;
/// The song's length, in milliseconds.
fn length_ms(&self) -> i32;
/// The song's title.
fn title(&self) -> String;
/// Makes `title`, `length_ms` long, the current song, where the player allows a host to
/// stage one (a check page's fake player); nothing elsewhere.
fn set_track(&mut self, title: &str, length_ms: i32) {
let _ = (title, length_ms);
}
/// Makes the player hand out silence (an empty-ish spectrum and a flat wave) where it allows
/// a host to stage one (a check page's fake player); nothing elsewhere.
fn set_silent(&mut self, silent: bool) {
let _ = silent;
}
/// Stages the song's format where the player allows it (a check page's fake player):
/// `kbps`, `khz` and `channels`; nothing elsewhere.
fn set_format(&mut self, kbps: i32, khz: i32, channels: i32) {
let _ = (kbps, khz, channels);
}
/// The bitrate.
fn bitrate_kbps(&self) -> i32;
/// The sample rate.
fn sample_rate_khz(&self) -> i32;
/// The channel count.
fn channels(&self) -> i32;
/// The current file's tags by lowercase field name (artist, album, ...); a file without tags has none.
fn tags(&self) -> BTreeMap<String, String> {
BTreeMap::new()
}
/// The input plug-in's name for the current file, as Winamp's `getDecoderName` reports it; empty when unknown.
fn decoder_name(&self) -> String {
String::new()
}
/// The volume, 0..255.
fn volume(&self) -> i32;
/// Sets the volume.
fn set_volume(&mut self, v: i32);
/// Shuffle on.
fn shuffle(&self) -> bool;
/// Sets shuffle.
fn set_shuffle(&mut self, on: bool);
/// Repeat on.
fn repeat(&self) -> bool;
/// Sets repeat.
fn set_repeat(&mut self, on: bool);
/// Crossfade on.
fn crossfade(&self) -> bool;
/// Sets crossfade.
fn set_crossfade(&mut self, on: bool);
/// The equalizer on.
fn eq_enabled(&self) -> bool;
/// Sets the equalizer on or off.
fn set_eq_enabled(&mut self, on: bool);
/// The ten band values, 0..255.
fn eq_bands(&self) -> Vec<i32>;
/// The preamp, 0..255.
fn eq_preamp(&self) -> i32;
/// Sets the preamp.
fn set_eq_preamp(&mut self, v: i32);
/// The balance: -127 hard left, 0 the middle, 127 hard right.
fn pan(&self) -> i32;
/// Sets the balance.
fn set_pan(&mut self, v: i32);
/// Starts playing.
fn play(&mut self);
/// Pauses, or resumes when paused.
fn pause(&mut self);
/// Stops.
fn stop(&mut self);
/// The next song.
fn next(&mut self);
/// The previous song.
fn previous(&mut self);
/// Ejects: opens a file.
fn eject(&mut self);
/// Seeks to `ms`.
fn seek_to(&mut self, ms: i32);
/// Sets band `band` to `value`.
fn set_eq_band(&mut self, band: i32, value: i32);
/// The playlist; a player without one has just the current song.
fn tracks(&self) -> Vec<Track> {
vec![Track {
title: self.title(),
length_ms: self.length_ms(),
}]
}
/// Which entry is current.
fn track_index(&self) -> i32 {
0
}
/// Makes entry `index` current and plays it.
fn play_track(&mut self, _index: i32) {
self.play();
}
/// Spectrum levels 0..1, 75 bands like Winamp's vis, or empty when nothing plays.
fn spectrum(&self) -> Vec<f32>;
/// The waveform being heard: samples -1..1, oldest first, 576 of them as Winamp hands a
/// visualizer, or empty when nothing plays.
fn waveform(&self) -> Vec<f32> {
Vec::new()
}
/// Left and right VU 0..255.
fn vu(&self) -> (i32, i32);
}
/// A player that plays nothing, for tests and for a first look at a skin. The clock positions,
/// the demo spectrum and the demo wave follow; a test gives it the runtime's.
pub struct FakePlayer {
clock: Clock,
status: Status,
started_at: i64,
paused_at: i32,
/// Set to show several entries; empty means the playlist is the current song alone.
pub playlist: Vec<Track>,
index: i32,
/// The song's length.
pub length_ms: i32,
/// The song's title.
pub title: String,
/// What `getDecoderName` answers.
pub decoder_name: String,
/// The bitrate.
pub bitrate_kbps: i32,
/// The sample rate.
pub sample_rate_khz: i32,
/// The channel count.
pub channels: i32,
/// Whether it hands out silence.
pub silent: bool,
volume: i32,
shuffle: bool,
repeat: bool,
crossfade: bool,
eq_enabled: bool,
eq_bands: Vec<i32>,
eq_preamp: i32,
pan: i32,
}
impl FakePlayer {
/// A stopped player reading `clock`.
pub fn new(clock: Clock) -> Self {
FakePlayer {
clock,
status: Status::Stopped,
started_at: 0,
paused_at: 0,
playlist: Vec::new(),
index: 0,
length_ms: 215_000,
title: "Andamp - Modern skin demo".to_string(),
decoder_name: String::new(),
bitrate_kbps: 192,
sample_rate_khz: 44,
channels: 2,
silent: false,
volume: 200,
shuffle: false,
repeat: false,
crossfade: false,
eq_enabled: false,
eq_bands: vec![128; 10],
eq_preamp: 128,
pan: 0,
}
}
fn now(&self) -> i64 {
(self.clock)()
}
}
impl Player for FakePlayer {
fn set_silent(&mut self, silent: bool) {
self.silent = silent;
}
fn set_format(&mut self, kbps: i32, khz: i32, channels: i32) {
self.bitrate_kbps = kbps;
self.sample_rate_khz = khz;
self.channels = channels;
}
fn set_track(&mut self, title: &str, length_ms: i32) {
self.title = title.to_string();
self.length_ms = length_ms;
if let Some(t) = self.playlist.get_mut(self.index.max(0) as usize) {
t.title = title.to_string();
t.length_ms = length_ms;
}
}
fn status(&self) -> Status {
self.status
}
fn position_ms(&self) -> i32 {
match self.status {
Status::Playing => (((self.now() - self.started_at) as i32)
.wrapping_add(self.paused_at))
.min(self.length_ms),
Status::Paused => self.paused_at,
Status::Stopped => 0,
}
}
fn length_ms(&self) -> i32 {
self.length_ms
}
fn title(&self) -> String {
self.title.clone()
}
fn bitrate_kbps(&self) -> i32 {
self.bitrate_kbps
}
fn sample_rate_khz(&self) -> i32 {
self.sample_rate_khz
}
fn channels(&self) -> i32 {
self.channels
}
fn decoder_name(&self) -> String {
self.decoder_name.clone()
}
fn volume(&self) -> i32 {
self.volume
}
fn set_volume(&mut self, v: i32) {
self.volume = v;
}
fn shuffle(&self) -> bool {
self.shuffle
}
fn set_shuffle(&mut self, on: bool) {
self.shuffle = on;
}
fn repeat(&self) -> bool {
self.repeat
}
fn set_repeat(&mut self, on: bool) {
self.repeat = on;
}
fn crossfade(&self) -> bool {
self.crossfade
}
fn set_crossfade(&mut self, on: bool) {
self.crossfade = on;
}
fn eq_enabled(&self) -> bool {
self.eq_enabled
}
fn set_eq_enabled(&mut self, on: bool) {
self.eq_enabled = on;
}
fn eq_bands(&self) -> Vec<i32> {
self.eq_bands.clone()
}
fn eq_preamp(&self) -> i32 {
self.eq_preamp
}
fn set_eq_preamp(&mut self, v: i32) {
self.eq_preamp = v;
}
fn pan(&self) -> i32 {
self.pan
}
fn set_pan(&mut self, v: i32) {
self.pan = v;
}
fn play(&mut self) {
if self.status == Status::Playing {
return;
}
if self.status == Status::Stopped {
self.paused_at = 0;
}
self.started_at = self.now();
self.status = Status::Playing;
}
fn pause(&mut self) {
match self.status {
Status::Playing => {
self.paused_at = self.position_ms();
self.status = Status::Paused;
}
Status::Paused => self.play(),
Status::Stopped => {}
}
}
fn stop(&mut self) {
self.status = Status::Stopped;
self.paused_at = 0;
}
fn next(&mut self) {
self.seek_to(0);
}
fn previous(&mut self) {
self.seek_to(0);
}
fn eject(&mut self) {}
fn seek_to(&mut self, ms: i32) {
self.paused_at = ms.clamp(0, self.length_ms.max(0));
self.started_at = self.now();
}
fn set_eq_band(&mut self, band: i32, value: i32) {
if let Some(b) = usize::try_from(band)
.ok()
.and_then(|i| self.eq_bands.get_mut(i))
{
*b = value;
}
}
fn tracks(&self) -> Vec<Track> {
if self.playlist.is_empty() {
vec![Track {
title: self.title.clone(),
length_ms: self.length_ms,
}]
} else {
self.playlist.clone()
}
}
fn track_index(&self) -> i32 {
self.index.clamp(0, self.tracks().len() as i32 - 1)
}
fn play_track(&mut self, index: i32) {
let tracks = self.tracks();
let Some(t) = usize::try_from(index).ok().and_then(|i| tracks.get(i)) else {
return;
};
self.index = index;
self.title.clone_from(&t.title);
self.length_ms = t.length_ms;
self.status = Status::Stopped;
self.play();
}
fn spectrum(&self) -> Vec<f32> {
if self.status != Status::Playing {
return Vec::new();
}
if self.silent {
return vec![0.0; 75];
}
let t = self.now() as f64 / 60.0;
(0..75)
.map(|i| ((0.5 + 0.5 * (t + i as f64 * 0.3).sin()) as f32) * (1.0 - i as f32 / 90.0))
.collect()
}
/// Two tones drifting with the clock, a slow one and one three times as fast, while playing.
fn waveform(&self) -> Vec<f32> {
if self.status != Status::Playing {
return Vec::new();
}
let t = self.now() as f64 / 60.0;
(0..576)
.map(|i| {
(0.35 * (t + i as f64 * 0.05).sin() + 0.15 * (3.0 * t + i as f64 * 0.27).sin())
as f32
})
.collect()
}
fn vu(&self) -> (i32, i32) {
if self.status == Status::Playing {
(160, 150)
} else {
(0, 0)
}
}
}