diff --git a/README.md b/README.md index 44f8540..084da88 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Scanner.** Reads MP3 and FLAC tags and audio properties with several workers in parallel. Rescans are incremental, renamed files keep their identity, vanished files are flagged (never deleted), and a library whose folder looks empty or unreadable (an unmounted NAS) is left alone instead of being wiped. Audio in other formats (m4a, ogg, opus, wav, wma and similar) is not read yet, but it is counted as `unsupported` in the scan result instead of disappearing silently. NAS housekeeping folders (`@Recycle`, `#recycle`, `@eaDir`, `.@__thumb`, `$RECYCLE.BIN`) are skipped. - **Catalog.** Browse and search artists, albums and tracks, with paging, by library, artist or album. Only tracks you can actually play are listed. - **Duplicates.** `GET /library/duplicates` (admin) lists the songs that are in more than one file: the same set of main artists, the same title and durations within 3 seconds, possibly on different albums. It is worked out from the index, touches no file, and returns no file path. Each group proposes the copy to keep (lossless first, then the higher bitrate, then the bigger file, then the oldest). A background job (`POST /library/duplicates/check`, like the other bulk jobs) reads the files of the groups and keeps their SHA-256, so a group can be called `identical` (byte for byte). `POST /library/duplicates/clean` **deletes the extra copies of one song from the disk**, and it is the one thing Sonda does that writes in your music folders, so it is off until you set `library.allow_delete` (or `SONDA_ALLOW_DELETE=true`) and mount the folders writable. It deletes only when an admin asks, never by itself, and the server checks again at that moment that no scan is running, that the tracks are copies of one song, and that every file is still the one the last scan saw (same size and date, a regular file, not a link, inside its library): so the last copy of a song is never deleted, a file that changed is not deleted, and every file is checked before the first is deleted. The listens and the playlist items of a deleted copy move to the one that stays, so your taste and your playlists are not lost. Folders are never removed, and nothing is sent anywhere. -- **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. +- **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. `GET /tracks/{id}/peaks` gives the shape of a track for the seek bar: 120 levels from 0 to 255, how loud it is along its length. The server finds it by decoding the file (MP3 and FLAC, in pure Go: nothing else to install), which takes a few seconds, so the first request starts the work in the background and answers `202` with `Retry-After`; it is done once for each file, worked out again if the size or the date of the file change, and one track at a time, so a NAS is not kept busy. A file that cannot be decoded answers `waveform_unavailable`, and that is remembered too. A friend's track has no shape yet. - **API documentation.** The full API is described in OpenAPI and browsable with Swagger UI, served by Sonda itself. The UI is embedded in the binary, so opening it contacts no other server. - **Listening history.** Clients report explicit `play`, `complete` and `skip` events. An event counts as a listen when it is `complete`, or when it was played for at least 20 seconds (`listening.min_listen_seconds`); the rule is applied when the history is read, so changing it re-reads the past. You can read your own history, with a `counted` flag on every event. It stays in your database and is never sent anywhere. - **Your taste.** `GET /me/taste` shows what Sonda has learned from your own listening: the artists you like and the kinds of music (tags) they have in common. A complete listen counts for more than a short one, a skip counts against the artist, and older listens weigh less (they halve every 90 days, `taste.half_life_days`; only the last ten half-lives are read). It is worked out on every request from data already in your database; nothing is sent to any provider. @@ -409,4 +409,4 @@ Migrations are plain SQL files in `migrations/sqlite` and `migrations/postgres`. ## Third-party code -Audio tags are read with `go.senan.xyz/taglib`, which embeds [TagLib](https://github.com/taglib/taglib) compiled to WebAssembly and is licensed under the LGPL 2.1. The Swagger UI assets come from `github.com/swaggest/swgui` (Apache-2.0), which embeds [Swagger UI](https://github.com/swagger-api/swagger-ui) (Apache-2.0). The other dependencies are listed in `go.mod`. +Audio tags are read with `go.senan.xyz/taglib`, which embeds [TagLib](https://github.com/taglib/taglib) compiled to WebAssembly and is licensed under the LGPL 2.1. The Swagger UI assets come from `github.com/swaggest/swgui` (Apache-2.0), which embeds [Swagger UI](https://github.com/swagger-api/swagger-ui) (Apache-2.0). The other dependencies are listed in `go.mod`. The shape of a track is found with `github.com/hajimehoshi/go-mp3` (Apache-2.0) and `github.com/eaburns/flac` (MIT), two decoders written in Go. diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index f4199ea..27df376 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -45,6 +45,7 @@ import ( "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/usersettings" + "codeberg.org/squidd/sonda/internal/waveform" "codeberg.org/squidd/sonda/internal/webui" "codeberg.org/squidd/sonda/migrations" ) @@ -320,6 +321,7 @@ func run() error { PeerCatalog: catalog.NewService(storage.NewSharedCatalogRepo(db)), PeerStreams: streaming.NewService(libraryRepo.Shared()), PeerCovers: images.StoredOnly{Service: pictures}, + Waveform: waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libraryRepo)), Web: webui.FS(), }) diff --git a/go.mod b/go.mod index 7a71802..bd9710e 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,11 @@ go 1.27.1 require ( github.com/anthropics/anthropic-sdk-go/v2 v2.9.0 + github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1 github.com/getkin/kin-openapi v0.153.0 github.com/gin-gonic/gin v1.12.0 github.com/golang-jwt/jwt/v5 v5.3.1 + github.com/hajimehoshi/go-mp3 v0.3.4 github.com/jackc/pgx/v5 v5.12.0 github.com/pelletier/go-toml/v2 v2.4.3 github.com/swaggest/swgui v1.8.9 @@ -29,6 +31,7 @@ require ( github.com/bytedance/sonic/loader v0.5.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect github.com/dustin/go-humanize v1.1.0 // indirect + github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect github.com/go-logr/logr v1.4.2 // indirect diff --git a/go.sum b/go.sum index f2df066..92a05cd 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,10 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI= github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d h1:HB5J9+f1xpkYLgWQ/RqEcbp3SEufyOIMYLoyKNKiG7E= +github.com/eaburns/bit v0.0.0-20131029213740-7bd5cd37375d/go.mod h1:CHkHWWZ4kbGY6jEy1+qlitDaCtRgNvCOQdakj/1Yl/Q= +github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1 h1:wl/ggSfTHqAy46hyzw1IlrUYwjqmXYvbJyPdH3rT7YE= +github.com/eaburns/flac v0.0.0-20171003200620-9a6fb92396d1/go.mod h1:frG94byMNy+1CgGrQ25dZ+17tf98EN+OYBQL4Zh612M= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= @@ -113,6 +117,9 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfF github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/hajimehoshi/go-mp3 v0.3.4 h1:NUP7pBYH8OguP4diaTZ9wJbUbk3tC0KlfzsEpWmYj68= +github.com/hajimehoshi/go-mp3 v0.3.4/go.mod h1:fRtZraRFcWb0pu7ok0LqyFhCUrPeMsGRSVop0eemFmo= +github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/invopop/jsonschema v0.14.0 h1:MHQqLhvpNUZfw+hM3AZDYK7jxO8FZoQeQM77g8iyZjg= @@ -256,6 +263,7 @@ golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 8f459af..c0e8e60 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -37,6 +37,7 @@ import ( "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/usersettings" + "codeberg.org/squidd/sonda/internal/waveform" "codeberg.org/squidd/sonda/migrations" ) @@ -112,6 +113,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun libRepo := storage.NewLibraryRepo(db) deps.PeerCatalog = catalog.NewService(storage.NewSharedCatalogRepo(db)) deps.PeerStreams = streaming.NewService(libRepo.Shared()) + deps.Waveform = waveform.NewService(storage.NewWaveformRepo(db), streaming.NewService(libRepo)) feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db)) deps.Feedback = feedbackSvc deps.Playlists = playlists.NewService(storage.NewPlaylistRepo(db)) diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index a693c5e..837108b 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -832,6 +832,7 @@ func TestResponsesMatchTheSpec(t *testing.T) { c.mixes(t, token, libID) c.smartPlaylists(t, token) c.names(t, token) + c.peaks(t, token) c.federation(t, token, libID, albumID) // ---- every operation in the spec was exercised ---- @@ -1355,3 +1356,14 @@ func (c *contract) friends(t *testing.T, token string) { c.expect(c.call("GET", base+"/tracks", token, nil), 409, "a friend that removed this server", "friend_gone") c.expect(c.call("DELETE", "/federation/friends/"+fid, token, nil), 204, "forget the friend") } + +// peaks asks for the shape of tracks. The files of this test are not audio, so none can be decoded. +func (c *contract) peaks(t *testing.T, token string) { + t.Helper() + c.expect(c.call("GET", "/tracks/1/peaks", "", nil), 401, "the shape of a track without a login", "unauthorized") + c.expect(c.call("GET", "/tracks/abc/peaks", token, nil), 400, "the shape of a track that is no number", "invalid_input") + c.expect(c.call("GET", "/tracks/999999/peaks", token, nil), 404, "the shape of a track that is not there", "track_not_found") + tracks := c.expect(c.call("GET", "/tracks?limit=1", token, nil), 200, "a track")["items"].([]any) + id := idOf(tracks[0].(map[string]any)) + c.expect(c.call("GET", "/tracks/"+id+"/peaks", token, nil), 404, "the shape of a file that is not audio", "waveform_unavailable") +} diff --git a/internal/api/http/handlers_waveform.go b/internal/api/http/handlers_waveform.go new file mode 100644 index 0000000..6c9251a --- /dev/null +++ b/internal/api/http/handlers_waveform.go @@ -0,0 +1,46 @@ +package httpapi + +import ( + "context" + "errors" + "net/http" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/streaming" + "codeberg.org/squidd/sonda/internal/waveform" +) + +type waveformHandlers struct{ svc waveformService } + +// peaks gives the shape of a track: how loud it is along its length, in a few levels from 0 to 255, for the seek bar. +// The first time it is asked for, the server starts working it out and says so (202); the page asks again. +func (h *waveformHandlers) peaks(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + peaks, err := h.svc.Peaks(c.Request.Context(), id) + switch { + case err == nil: + levels := make([]int, len(peaks)) + for i, v := range peaks { + levels[i] = int(v) + } + c.JSON(http.StatusOK, gin.H{"count": len(levels), "peaks": levels}) + case errors.Is(err, waveform.ErrNotReady): + c.Header("Retry-After", "3") + c.JSON(http.StatusAccepted, gin.H{"status": "working"}) + case errors.Is(err, streaming.ErrNotFound): + writeError(c, http.StatusNotFound, "track_not_found", "no playable file for this track") + case errors.Is(err, streaming.ErrFileMissing), errors.Is(err, streaming.ErrUnsafePath): + writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") + case errors.Is(err, waveform.ErrUnsupported): + writeError(c, http.StatusNotFound, "waveform_unavailable", "the waveform of this file cannot be worked out") + case errors.Is(err, context.Canceled): + c.AbortWithStatus(statusClientClosed) // the page was left; nobody is waiting + default: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + } +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go index b0c1ee9..9ede41b 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -179,6 +179,10 @@ type catalogService interface { Track(ctx context.Context, id int64) (catalog.Track, error) } +type waveformService interface { + Peaks(ctx context.Context, trackID int64) ([]byte, error) +} + type libraryService interface { List(ctx context.Context) ([]library.Library, error) Create(ctx context.Context, name, path string) (library.Library, error) @@ -224,6 +228,8 @@ type Deps struct { PeerCatalog catalogService PeerStreams streamService PeerCovers coverService + // The shape of a track, for the seek bar. + Waveform waveformService // Web is the built web app, served on / and for its own links. It is optional: with none (a build // without the app) / shows a page that says how to build it, and the API works the same. Web fs.FS @@ -360,6 +366,7 @@ func NewRouter(d Deps) *gin.Engine { protected.GET("/albums/:id", cat.getAlbum) protected.GET("/tracks", cat.listTracks) protected.GET("/tracks/:id", cat.getTrack) + protected.GET("/tracks/:id/peaks", (&waveformHandlers{svc: d.Waveform}).peaks) // Audio loaded by an