trill 0.7.3

Lean terminal Spotify client: Spotify's own player, small and quiet when idle
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
//! Audio settings: equalizer and crossfade.
//!
//! - **EQ**: a 10-band PipeWire filter-chain (`pipewire -c <conf>`) exposing a sink named
//!   `trill_eq`. Only trill's player is routed into it; it forwards to your normal output
//!   (following default-device changes). Gains change live via `pw-cli`, no audio gap.
//!   Cost: ~3 MB, 2 threads.
//! - **Crossfade**: Spotify's own crossfade inside Soloist, set through Soloist's prefs file
//!   before it starts (changing it restarts Soloist; trill resumes where you were).

use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
    process::{Command, Stdio},
    sync::mpsc,
    time::{Duration, Instant},
};

use librespot_playback::config::Bitrate;

pub const EQ_NODE: &str = "trill_eq";
pub const BANDS: [u32; 10] = [32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000];
pub const MAX_DB: f32 = 12.0;
pub const CROSSFADE_STEPS: [u32; 5] = [0, 3, 5, 8, 12];

pub const PRESETS: &[(&str, [f32; 10])] = &[
    ("Flat", [0.0; 10]),
    ("Bass Boost", [6.0, 5.0, 4.0, 2.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0]),
    ("Bass Reducer", [-6.0, -5.0, -4.0, -2.0, -0.5, 0.0, 0.0, 0.0, 0.0, 0.0]),
    ("Treble Boost", [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 4.0, 5.0, 6.0]),
    ("Vocal", [-2.0, -2.0, -1.0, 1.0, 3.0, 4.0, 3.0, 1.0, 0.0, -1.0]),
    ("Rock", [4.0, 3.0, 2.0, 0.0, -1.0, -1.0, 1.0, 2.0, 3.0, 4.0]),
    ("Pop", [-1.0, 0.0, 2.0, 3.0, 4.0, 3.0, 1.0, 0.0, -1.0, -1.0]),
    ("Hip-Hop", [5.0, 4.0, 1.0, 3.0, -1.0, -1.0, 1.0, -1.0, 2.0, 3.0]),
    ("Electronic", [5.0, 4.0, 1.0, 0.0, -2.0, 1.0, 0.0, 1.0, 4.0, 5.0]),
    ("Acoustic", [3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 2.0, 3.0, 2.0, 1.0]),
    ("Loudness", [6.0, 4.0, 0.0, 0.0, -2.0, 0.0, -1.0, -3.0, 4.0, 2.0]),
];
/// Index meaning "user-tuned bands".
pub const CUSTOM: usize = PRESETS.len();

/// Sort modes as named in the settings file (`app::Sort` keeps this order).
pub const SORTS: [&str; 6] = ["added", "playlist", "title", "artist", "album", "duration"];

/// Everything the user can change from the keyboard, kept across restarts
/// (`~/.local/share/trill/audio_settings`). Volume lives in librespot's cache.
pub struct AudioSettings {
    /// Selected preset index, or [`CUSTOM`].
    pub preset: usize,
    pub custom: [f32; 10],
    pub crossfade_s: u32,
    pub shuffle: bool,
    /// Smart shuffle: recommendations mixed in (only with `shuffle`).
    pub smart: bool,
    /// 0 off, 1 all, 2 one
    pub repeat: u8,
    /// Index into [`SORTS`].
    pub sort: u8,
    pub art: bool,
    /// Index into [`crate::theme::THEMES`], or `theme::COVER`.
    pub theme: usize,
    /// Last cover colour, so the `cover` theme starts in it instead of the default green.
    pub tint: Option<[u8; 3]>,
    /// When the private session trill turned on ends (unix seconds; 0 = none).
    pub private_until: u64,
    /// Lists sorted some other way than [`Self::sort`]: uri -> index into [`SORTS`].
    pub sorts: HashMap<String, u8>,
    /// Past searches, newest first.
    pub searches: Vec<String>,
    path: PathBuf,
}

/// How many past searches are kept.
pub const SEARCHES: usize = 30;

impl AudioSettings {
    pub fn load(dir: &Path) -> Self {
        let mut s = AudioSettings {
            preset: 0,
            custom: [0.0; 10],
            crossfade_s: 0,
            shuffle: false,
            smart: false,
            repeat: 0,
            sort: 0,
            art: true,
            theme: 0,
            tint: None,
            private_until: 0,
            sorts: HashMap::new(),
            searches: Vec::new(),
            path: dir.join("audio_settings"),
        };
        let Ok(text) = fs::read_to_string(&s.path) else { return s };
        for line in text.lines() {
            let Some((k, v)) = line.split_once('=') else { continue };
            match k.trim() {
                "preset" => {
                    let v = v.trim();
                    s.preset = if v == "Custom" {
                        CUSTOM
                    } else {
                        PRESETS.iter().position(|(n, _)| *n == v).unwrap_or(0)
                    }
                }
                "custom" => {
                    for (i, g) in v.split(',').take(10).enumerate() {
                        s.custom[i] = g.trim().parse::<f32>().unwrap_or(0.0).clamp(-MAX_DB, MAX_DB);
                    }
                }
                "crossfade" => s.crossfade_s = v.trim().parse::<u32>().unwrap_or(0).min(12),
                "shuffle" => (s.shuffle, s.smart) = (matches!(v.trim(), "on" | "smart"), v.trim() == "smart"),
                "repeat" => s.repeat = ["off", "all", "one"].iter().position(|r| *r == v.trim()).unwrap_or(0) as u8,
                "sort" => s.sort = SORTS.iter().position(|n| *n == v.trim()).unwrap_or(0) as u8,
                "art" => s.art = v.trim() != "off",
                "theme" => s.theme = crate::theme::by_name(v.trim()),
                "private" => s.private_until = v.trim().parse().unwrap_or(0),
                "search" if !v.trim().is_empty() && s.searches.len() < SEARCHES => s.searches.push(v.trim().to_string()),
                k if k.starts_with("sort ") => {
                    if let Some(i) = SORTS.iter().position(|n| *n == v.trim()) {
                        s.sorts.insert(k["sort ".len()..].to_string(), i as u8);
                    }
                }
                "tint" => {
                    let c: Vec<u8> = v.split(',').filter_map(|c| c.trim().parse().ok()).collect();
                    s.tint = <[u8; 3]>::try_from(c).ok();
                }
                _ => {}
            }
        }
        s
    }

    pub fn save(&self) {
        let custom = self.custom.iter().map(|g| format!("{g:.1}")).collect::<Vec<_>>().join(",");
        let on = |b: bool| if b { "on" } else { "off" };
        let text = format!(
            "preset={}\ncustom={custom}\ncrossfade={}\nshuffle={}\nrepeat={}\nsort={}\nart={}\ntheme={}\n",
            self.preset_name(),
            self.crossfade_s,
            if self.shuffle && self.smart { "smart" } else { on(self.shuffle) },
            ["off", "all", "one"][self.repeat.min(2) as usize],
            SORTS[(self.sort as usize).min(SORTS.len() - 1)],
            on(self.art),
            crate::theme::name(self.theme),
        );
        let mut text = match self.tint {
            Some([r, g, b]) => format!("{text}tint={r},{g},{b}\n"),
            None => text,
        };
        if self.private_until > crate::app::unix_now() {
            text.push_str(&format!("private={}\n", self.private_until));
        }
        for (uri, &i) in &self.sorts {
            text.push_str(&format!("sort {uri}={}\n", SORTS[(i as usize).min(SORTS.len() - 1)]));
        }
        for q in &self.searches {
            text.push_str(&format!("search={}\n", q.replace('\n', " ")));
        }
        if let Some(d) = self.path.parent() {
            let _ = fs::create_dir_all(d);
        }
        let _ = fs::write(&self.path, text);
    }

    pub fn preset_name(&self) -> &'static str {
        PRESETS.get(self.preset).map(|p| p.0).unwrap_or("Custom")
    }

    pub fn is_flat(&self) -> bool {
        self.gains().iter().all(|g| *g == 0.0)
    }

    pub fn gains(&self) -> [f32; 10] {
        PRESETS.get(self.preset).map(|p| p.1).unwrap_or(self.custom)
    }

    pub fn cycle_preset(&mut self, back: bool) {
        let n = PRESETS.len() + 1; // + Custom
        self.preset = if back { (self.preset + n - 1) % n } else { (self.preset + 1) % n };
    }

    /// Adjust one band; switches to Custom starting from whatever is currently applied.
    pub fn nudge(&mut self, band: usize, delta: f32) {
        if self.preset != CUSTOM {
            self.custom = self.gains();
            self.preset = CUSTOM;
        }
        self.custom[band] = (self.custom[band] + delta).clamp(-MAX_DB, MAX_DB);
    }

    pub fn set_band(&mut self, band: usize, value: f32) {
        self.nudge(band, value - self.gains()[band]);
    }

    pub fn cycle_crossfade(&mut self) {
        let i = CROSSFADE_STEPS.iter().position(|&s| s == self.crossfade_s).unwrap_or(0);
        self.crossfade_s = CROSSFADE_STEPS[(i + 1) % CROSSFADE_STEPS.len()];
    }
}

/// Linear gain that keeps the loudest boost from clipping.
fn preamp(gains: &[f32; 10]) -> f32 {
    let max = gains.iter().cloned().fold(0.0f32, f32::max);
    10f32.powf(-max / 20.0)
}

fn props(gains: &[f32; 10]) -> String {
    let mut p = format!("\"pre:Mult\" {:.4}", preamp(gains));
    for (i, g) in gains.iter().enumerate() {
        p.push_str(&format!(" \"b{}:Gain\" {:.1}", i + 1, g));
    }
    format!("{{ params = [ {p} ] }}")
}

fn conf(gains: &[f32; 10]) -> String {
    let mut nodes = format!(
        "          {{ type = builtin name = pre label = linear control = {{ \"Mult\" = {:.4} \"Add\" = 0.0 }} }}\n",
        preamp(gains)
    );
    let mut links = String::from("          { output = \"pre:Out\" input = \"b1:In\" }\n");
    for (i, (f, g)) in BANDS.iter().zip(gains).enumerate() {
        let (label, q) = match i {
            0 => ("bq_lowshelf", 0.7),
            9 => ("bq_highshelf", 0.7),
            _ => ("bq_peaking", 1.4),
        };
        nodes.push_str(&format!(
            "          {{ type = builtin name = b{n} label = {label} control = {{ \"Freq\" = {f} \"Q\" = {q} \"Gain\" = {g:.1} }} }}\n",
            n = i + 1
        ));
        if i > 0 {
            links.push_str(&format!("          {{ output = \"b{}:Out\" input = \"b{}:In\" }}\n", i, i + 1));
        }
    }
    format!(
        r#"# Generated by trill — 10-band EQ for trill's player only.
context.properties = {{ log.level = 0 }}
context.spa-libs = {{ audio.convert.* = audioconvert/libspa-audioconvert support.* = support/libspa-support }}
context.modules = [
  {{ name = libpipewire-module-rt args = {{ nice.level = -11 }} flags = [ ifexists nofail ] }}
  {{ name = libpipewire-module-protocol-native }}
  {{ name = libpipewire-module-client-node }}
  {{ name = libpipewire-module-adapter }}
  {{ name = libpipewire-module-filter-chain
    args = {{
      node.description = "trill EQ"
      media.name = "trill EQ"
      filter.graph = {{
        nodes = [
{nodes}        ]
        links = [
{links}        ]
        inputs = [ "pre:In" ]
        outputs = [ "b10:Out" ]
      }}
      audio.channels = 2
      audio.position = [ FL FR ]
      # Music doesn't need low latency: bigger chunks = fewer wakeups.
      capture.props = {{ node.name = "{EQ_NODE}" media.class = Audio/Sink node.passive = true node.latency = 2048/48000 }}
      playback.props = {{ node.name = "{EQ_NODE}.output" node.passive = true media.role = Music node.latency = 2048/48000 }}
    }}
  }}
]
"#
    )
}

fn node_exists() -> bool {
    Command::new("pw-cli")
        .args(["info", EQ_NODE])
        .output()
        .map(|o| {
            let all = [o.stdout, o.stderr].concat();
            let s = String::from_utf8_lossy(&all);
            s.contains("id:") && !s.contains("unknown global")
        })
        .unwrap_or(false)
}

/// The running EQ process.
pub struct Eq {
    pid: u32,
    tx: mpsc::Sender<[f32; 10]>,
}

impl Eq {
    pub fn start(cache_dir: &Path, gains: &[f32; 10]) -> Result<Eq, String> {
        let path = cache_dir.join("trill-eq.conf");
        fs::write(&path, conf(gains)).map_err(|e| e.to_string())?;
        let log = fs::File::create(cache_dir.join("eq.log")).map_err(|e| e.to_string())?;
        let mut c = Command::new("pipewire");
        c.arg("-c").arg(&path).stdin(Stdio::null()).stdout(log.try_clone().map_err(|e| e.to_string())?).stderr(log);
        // SAFETY: prctl is async-signal-safe; the EQ goes away if trill dies.
        #[cfg(target_os = "linux")]
        unsafe {
            std::os::unix::process::CommandExt::pre_exec(&mut c, || {
                libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
                Ok(())
            });
        }
        let mut child = c.spawn().map_err(|e| format!("can't run pipewire: {e}"))?;
        let pid = child.id();
        let deadline = Instant::now() + Duration::from_secs(5);
        while !node_exists() {
            if let Ok(Some(st)) = child.try_wait() {
                return Err(format!("EQ exited ({st}); see ~/.cache/trill/eq.log"));
            }
            if Instant::now() > deadline {
                let _ = child.kill();
                return Err("EQ didn't come up in 5 s".into());
            }
            std::thread::sleep(Duration::from_millis(50));
        }
        // Reap the child when it exits so it never lingers as a zombie.
        std::thread::spawn(move || {
            let _ = child.wait();
        });
        // One worker applies gain changes in order, skipping to the newest if keys repeat.
        let (tx, rx) = mpsc::channel::<[f32; 10]>();
        let _ = std::thread::Builder::new().name("eq".into()).spawn(move || {
            while let Ok(mut g) = rx.recv() {
                while let Ok(newer) = rx.try_recv() {
                    g = newer;
                }
                let _ = Command::new("pw-cli")
                    .args(["set-param", EQ_NODE, "Props", &props(&g)])
                    .stdout(Stdio::null())
                    .stderr(Stdio::null())
                    .status();
            }
        });
        Ok(Eq { pid, tx })
    }

    pub fn apply(&self, gains: &[f32; 10]) {
        let _ = self.tx.send(*gains);
    }

    pub fn stop(&self) {
        crate::engine::stop_process(self.pid);
    }
}

/// Write Soloist's per-user prefs (crossfade, bitrate) before it starts. Soloist only reads
/// these when the matching `*_migrated*` flag is false, so we reset the flags each time.
pub fn write_soloist_prefs(data_dir: &Path, user: &str, crossfade_s: u32, bitrate: Bitrate) {
    let dir = data_dir.join("settings").join("Users").join(format!("{user}-user"));
    let path = dir.join("prefs");
    let old = fs::read_to_string(&path).unwrap_or_default();
    let managed = ["audio.crossfade", "audio.play_bitrate", "audio.equalizer"];
    let mut lines: Vec<String> = old
        .lines()
        .filter(|l| !managed.iter().any(|m| l.starts_with(m)))
        .map(String::from)
        .collect();
    // Spotify's quality enumeration: 4 = very high (320 kbps, verified), 3 = high, 2 = normal.
    let q = match bitrate {
        Bitrate::Bitrate320 => 4,
        Bitrate::Bitrate160 => 3,
        Bitrate::Bitrate96 => 2,
    };
    lines.extend([
        "audio.crossfade_migrated_to_ps=false".into(),
        format!("audio.crossfade_v2={}", crossfade_s > 0),
        format!("audio.crossfade.time_v2={}", crossfade_s.max(1) * 1000),
        "audio.play_bitrate_non_metered_migrated=false".into(),
        format!("audio.play_bitrate_non_metered_enumeration={q}"),
        format!("audio.play_bitrate_enumeration={q}"),
    ]);
    let _ = fs::create_dir_all(&dir);
    let _ = fs::write(&path, lines.join("\n") + "\n");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn presets_and_custom_roundtrip() {
        let dir = std::env::temp_dir().join(format!("trill-test-eq-{}", std::process::id()));
        let mut s = AudioSettings::load(&dir);
        assert_eq!(s.preset_name(), "Flat");
        s.cycle_preset(false);
        assert_eq!(s.preset_name(), "Bass Boost");
        s.nudge(9, 3.0); // tweak treble -> becomes Custom based on Bass Boost
        assert_eq!(s.preset, CUSTOM);
        assert_eq!(s.gains()[0], 6.0);
        assert_eq!(s.gains()[9], 3.0);
        s.nudge(0, 100.0);
        assert_eq!(s.gains()[0], MAX_DB);
        s.cycle_crossfade();
        s.save();
        let t = AudioSettings::load(&dir);
        assert_eq!(t.preset, CUSTOM);
        assert_eq!(t.gains(), s.gains());
        assert_eq!(t.crossfade_s, 3);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn preamp_prevents_clipping() {
        assert!((preamp(&[0.0; 10]) - 1.0).abs() < 1e-6);
        let mut g = [0.0; 10];
        g[0] = 6.0;
        assert!((preamp(&g) - 0.5012).abs() < 1e-3); // -6 dB
        assert!(props(&g).contains("\"b1:Gain\" 6.0"));
        assert!(conf(&g).contains("bq_lowshelf") && conf(&g).contains("inputs = [ \"pre:In\" ]"));
    }

    #[test]
    fn settings_survive_restart() {
        let dir = std::env::temp_dir().join(format!("trill-test-prefs-{}", std::process::id()));
        let mut a = AudioSettings::load(&dir);
        assert!(!a.shuffle && a.repeat == 0 && a.sort == 0 && a.art);
        (a.shuffle, a.smart, a.repeat, a.sort, a.art, a.crossfade_s) = (true, true, 2, 4, false, 6);
        (a.theme, a.tint) = (crate::theme::COVER, Some([224, 135, 99]));
        a.save();
        let b = AudioSettings::load(&dir);
        let _ = fs::remove_dir_all(&dir);
        assert!(b.shuffle && b.smart && b.repeat == 2 && b.sort == 4 && !b.art && b.crossfade_s == 6);
        assert!(b.theme == crate::theme::COVER && b.tint == Some([224, 135, 99]));
    }

    #[test]
    fn per_list_sorts_and_searches_survive_restart() {
        let dir = std::env::temp_dir().join(format!("trill-test-lists-{}", std::process::id()));
        let mut a = AudioSettings::load(&dir);
        a.sorts.insert("spotify:playlist:x".into(), 3);
        a.searches = vec!["daft punk".into(), "a = b".into()];
        a.save();
        let b = AudioSettings::load(&dir);
        assert_eq!(b.sorts.get("spotify:playlist:x"), Some(&3));
        assert_eq!(b.searches, ["daft punk", "a = b"]);
        // a file written by an older trill (no per-list sorts, no searches) still loads
        fs::write(dir.join("audio_settings"), "preset=Rock\nsort=title\n").unwrap();
        let c = AudioSettings::load(&dir);
        let _ = fs::remove_dir_all(&dir);
        assert!(c.sorts.is_empty() && c.searches.is_empty() && c.sort == 2 && c.preset_name() == "Rock");
    }
}