forked from bjarneo/cliamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
322 lines (279 loc) · 9.67 KB
/
Copy pathmain.go
File metadata and controls
322 lines (279 loc) · 9.67 KB
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
// Package main is the entry point for the CLIAMP terminal music player.
package main
import (
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"cliamp/config"
"cliamp/external/local"
"cliamp/external/navidrome"
"cliamp/external/plex"
"cliamp/external/radio"
"cliamp/external/spotify"
"cliamp/external/ytmusic"
"cliamp/internal/resume"
"cliamp/mpris"
"cliamp/player"
"cliamp/playlist"
"cliamp/resolve"
"cliamp/theme"
"cliamp/ui"
"cliamp/upgrade"
)
// version is set at build time via -ldflags "-X main.version=vX.Y.Z".
var version string
func run(overrides config.Overrides, positional []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("config: %w", err)
}
overrides.Apply(&cfg)
// Build provider list: Radio is always available, Navidrome and Spotify if configured.
radioProv := radio.New()
var providers []ui.ProviderEntry
providers = append(providers, ui.ProviderEntry{Key: "radio", Name: "Radio", Provider: radioProv})
var navClient *navidrome.NavidromeClient
if c := navidrome.NewFromConfig(cfg.Navidrome); c != nil {
navClient = c
} else if c := navidrome.NewFromEnv(); c != nil {
navClient = c
}
if navClient != nil {
providers = append(providers, ui.ProviderEntry{Key: "navidrome", Name: "Navidrome", Provider: navClient})
}
if plexProv := plex.NewFromConfig(cfg.Plex); plexProv != nil {
providers = append(providers, ui.ProviderEntry{Key: "plex", Name: "Plex", Provider: plexProv})
}
var spotifyProv *spotify.SpotifyProvider
if cfg.Spotify.IsSet() {
spotifyProv = spotify.New(nil, cfg.Spotify.ClientID)
providers = append(providers, ui.ProviderEntry{Key: "spotify", Name: "Spotify", Provider: spotifyProv})
}
var ytProviders ytmusic.Providers
// Enable YouTube providers if any [yt]/[youtube]/[ytmusic] config exists,
// or if the --provider flag selects a YouTube provider,
// or if fallback credentials are available.
ytWanted := cfg.YouTubeMusic.IsSetOrFallback(ytmusic.FallbackCredentials)
if !ytWanted {
// Also enable if --provider flag selects a YouTube provider.
switch cfg.Provider {
case "yt", "youtube", "ytmusic":
ytWanted = true
}
}
if ytWanted {
ytClientID, ytClientSecret := cfg.YouTubeMusic.ResolveCredentials(ytmusic.FallbackCredentials)
// Configure yt-dlp cookie source for YouTube Music uploads/private tracks.
if cfg.YouTubeMusic.CookiesFrom != "" {
player.SetYTDLCookiesFrom(cfg.YouTubeMusic.CookiesFrom)
}
if ytClientID == "" || ytClientSecret == "" {
fmt.Fprintf(os.Stderr, "YouTube: no credentials available (configure client_id/client_secret in config.toml)\n")
} else {
// YouTube playback requires yt-dlp. Check early and offer to install.
if !player.YTDLPAvailable() {
fmt.Fprintf(os.Stderr, "\nYouTube requires yt-dlp for audio playback.\n")
fmt.Fprintf(os.Stderr, "Install command: %s\n\n", player.YtdlpInstallHint())
fmt.Fprintf(os.Stderr, "Press Enter to install automatically, or Ctrl+C to skip... ")
fmt.Scanln()
fmt.Fprintf(os.Stderr, "Installing yt-dlp...\n")
if err := player.InstallYTDLP(); err != nil {
fmt.Fprintf(os.Stderr, "Installation failed: %v\n", err)
fmt.Fprintf(os.Stderr, "YouTube providers disabled. Install manually and restart.\n\n")
} else {
fmt.Fprintf(os.Stderr, "yt-dlp installed successfully!\n\n")
}
}
if player.YTDLPAvailable() {
ytProviders = ytmusic.New(nil, ytClientID, ytClientSecret, cfg.YouTubeMusic.CookiesFrom != "")
providers = append(providers,
ui.ProviderEntry{Key: "yt", Name: "YouTube (All)", Provider: ytProviders.All},
ui.ProviderEntry{Key: "youtube", Name: "YouTube", Provider: ytProviders.Video},
ui.ProviderEntry{Key: "ytmusic", Name: "YouTube Music", Provider: ytProviders.Music},
)
}
}
}
localProv := local.New()
if spotifyProv != nil {
defer spotifyProv.Close()
}
if ytProviders.Music != nil {
defer ytProviders.Music.Close()
}
if len(positional) > 0 && (positional[0] == "search" || positional[0] == "search-sc") {
if len(positional) == 1 {
return fmt.Errorf("search requires a query string (e.g. cliamp search \"never gonna give you up\")")
}
prefix := "ytsearch1:"
if positional[0] == "search-sc" {
prefix = "scsearch1:"
}
query := strings.Join(positional[1:], " ")
positional = []string{prefix + query}
}
resolved, err := resolve.Args(positional)
if err != nil {
return err
}
// Determine default provider key.
defaultProvider := cfg.Provider
if defaultProvider == "" {
defaultProvider = "radio"
}
// No args + radio provider: stream the built-in radio directly.
if len(positional) == 0 && defaultProvider == "radio" {
resolved.Pending = append(resolved.Pending, "https://radio.cliamp.stream/streams.m3u")
}
pl := playlist.New()
pl.Add(resolved.Tracks...)
// Resolve sample rate: 0 means auto-detect from the system's default
// output audio device (e.g. 48 kHz for USB-C headphones). Falls back
// to 44100 Hz if detection is unavailable or returns an unusable value.
sampleRate := cfg.SampleRate
if sampleRate == 0 {
if detected := player.DeviceSampleRate(); detected > 0 {
sampleRate = detected
} else {
sampleRate = 44100
}
}
p, err := player.New(player.Quality{
SampleRate: sampleRate,
BufferMs: cfg.BufferMs,
ResampleQuality: cfg.ResampleQuality,
BitDepth: cfg.BitDepth,
})
if err != nil {
return fmt.Errorf("player: %w", err)
}
defer p.Close()
// Register Spotify streamer factory so spotify: URIs are decoded
// through go-librespot instead of the normal file/HTTP pipeline.
if spotifyProv != nil {
p.SetStreamerFactory(spotifyProv.NewStreamer)
}
cfg.ApplyPlayer(p)
cfg.ApplyPlaylist(pl)
ui.SetPadding(cfg.PaddingH, cfg.PaddingV)
themes := theme.LoadAll()
m := ui.NewModel(p, pl, providers, defaultProvider, localProv, themes, cfg.Navidrome, navClient)
m.SetSeekStepLarge(cfg.SeekStepLargeDuration())
m.SetPendingURLs(resolved.Pending)
if len(resolved.Tracks) == 0 && len(resolved.Pending) == 0 {
m.StartInProvider()
}
if cfg.EQPreset != "" && cfg.EQPreset != "Custom" {
m.SetEQPreset(cfg.EQPreset)
}
if cfg.Theme != "" {
m.SetTheme(cfg.Theme)
}
if cfg.Visualizer != "" {
m.SetVisualizer(cfg.Visualizer)
}
if cfg.AutoPlay {
m.SetAutoPlay(true)
}
if cfg.Compact {
m.SetCompact(true)
}
// PositionSec == 0 is indistinguishable from "never played"; skip resume.
if rs := resume.Load(); rs.Path != "" && rs.PositionSec > 0 {
m.SetResume(rs.Path, rs.PositionSec)
}
prog := tea.NewProgram(m, tea.WithAltScreen())
if svc, err := mpris.New(func(msg interface{}) { prog.Send(msg) }); err == nil && svc != nil {
defer svc.Close()
go prog.Send(mpris.InitMsg{Svc: svc})
}
finalModel, err := prog.Run()
if err != nil {
return err
}
// Persist theme selection and resume state across restarts.
if fm, ok := finalModel.(ui.Model); ok {
themeName := fm.ThemeName()
if themeName == theme.DefaultName {
themeName = ""
}
_ = config.Save("theme", fmt.Sprintf("%q", themeName)) // best-effort — non-critical persistence
if path, secs := fm.ResumeState(); path != "" && secs > 0 {
resume.Save(path, secs)
}
}
return nil
}
const helpText = `cliamp — retro terminal music player
Usage: cliamp [flags] <file|folder|url> [...]
Playback:
--volume <dB> Volume in dB, range [-30, +6] (e.g. --volume -5)
--shuffle
--repeat <off|all|one>
--mono / --no-mono
--auto-play Start playback immediately
Audio engine:
--sample-rate <Hz> Output sample rate (0=auto, 22050, 44100, 48000, 96000, 192000)
--buffer-ms <ms> Speaker buffer in milliseconds (50–500)
--resample-quality <n> Resample quality factor (1–4)
--bit-depth <n> PCM bit depth: 16 (default) or 32 (lossless)
Provider:
--provider <name> Default provider: radio, navidrome, plex, spotify, yt, youtube, ytmusic (default: radio)
Appearance:
--compact Compact mode (cap width at 80 columns)
--theme <name> UI theme name
--visualizer <mode> Visualizer mode (Bars, Bricks, Columns, Wave, Scatter, Flame, Retro, Pulse, Matrix, Binary, None)
--eq-preset <name> EQ preset name (e.g. "Bass Boost")
General:
-h, --help Show this help message
-v, --version Show the current version
--upgrade Upgrade cliamp to the latest release
Examples:
cliamp track.mp3 song.flac ~/Music
cliamp --shuffle --volume -5 track.mp3
cliamp track.mp3 --repeat all --mono
cliamp --auto-play --shuffle ~/Music
cliamp --eq-preset "Bass Boost" ~/Music
cliamp https://example.com/song.mp3
cliamp http://radio.example.com/stream.m3u
cliamp search "rick astley" # search YouTube
cliamp search-sc "lofi beats" # search SoundCloud
cliamp https://soundcloud.com/user/sets/playlist
cliamp https://www.youtube.com/watch?v=...
Environment:
NAVIDROME_URL, NAVIDROME_USER, NAVIDROME_PASS Navidrome server (env fallback)
Config: ~/.config/cliamp/config.toml (see config.toml.example)
Radios: ~/.config/cliamp/radios.toml
Playlists: ~/.config/cliamp/playlists/*.toml
Formats: mp3, wav, flac, ogg, m4a, aac, opus, wma (aac/opus/wma need ffmpeg)
SoundCloud/YouTube/Bandcamp require yt-dlp`
func main() {
action, overrides, positional, err := config.ParseFlags(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
switch action {
case "help":
fmt.Println(helpText)
return
case "version":
if version == "" {
fmt.Println("cliamp (dev build)")
} else {
fmt.Printf("cliamp %s\n", version)
}
return
case "upgrade":
if err := upgrade.Run(version); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
if err := run(overrides, positional); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}