The seek bar of the player is the shape of the track, with the part already
heard in colour, as in the approved mockups.
- GET /tracks/{id}/peaks gives 120 levels from 0 to 255 (the loudest part is
255 whatever the volume). The server finds them by decoding the file, MP3 with
go-mp3 and FLAC with eaburns/flac, both in pure Go. It 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 (migration 0025), worked out again
if the size or the date of the file change, one track at a time so that a NAS
is not kept busy, and a file that cannot be decoded is remembered as such. A
track deleted while its file is being decoded is not an error.
- The seek bar of the player bar and of the full-screen player draws the shape
and asks again every few seconds until it is there. Until then, and for a
friend's track, which has none, the bar is flat. The heard part is teal, or
the friend colour. It is still a native range input, 44 pixels high in the
bar.
- OpenAPI path and schema, contract test, tests with real MP3 and FLAC files,
and a real server checked: a 4-minute MP3 is ready in about a second and a
half, a 12 MB FLAC in three, and asking again takes a millisecond.
60 lines
2.1 KiB
Go
60 lines
2.1 KiB
Go
package storage
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"codeberg.org/squidd/sonda/internal/waveform"
|
|
)
|
|
|
|
func TestTheShapeOfATrackIsKeptAndReplaced(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewWaveformRepo(db)
|
|
var id int64
|
|
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('song') RETURNING id").Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, found, err := repo.Get(ctx, id); err != nil || found {
|
|
t.Fatalf("before any save: found %v, %v", found, err)
|
|
}
|
|
peaks := bytes.Repeat([]byte{0, 128, 255}, 40)
|
|
if err := repo.Save(ctx, id, waveform.Record{SizeBytes: 123456, MTimeUnix: 1_700_000_000, Peaks: peaks}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, found, err := repo.Get(ctx, id)
|
|
if err != nil || !found || got.SizeBytes != 123456 || got.MTimeUnix != 1_700_000_000 || !bytes.Equal(got.Peaks, peaks) {
|
|
t.Fatalf("Get = %+v, %v, %v", got, found, err)
|
|
}
|
|
// Saving again replaces it (the file changed).
|
|
if err := repo.Save(ctx, id, waveform.Record{SizeBytes: 7, MTimeUnix: 8, Peaks: nil}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _, _ = repo.Get(ctx, id)
|
|
if got.SizeBytes != 7 || got.MTimeUnix != 8 || len(got.Peaks) != 0 {
|
|
t.Errorf("after the second save: %+v: no peaks means the file cannot be decoded, and that is kept too", got)
|
|
}
|
|
// A track that goes takes its shape with it.
|
|
if _, err := db.Exec("DELETE FROM tracks WHERE id = ?", id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, found, _ := repo.Get(ctx, id); found {
|
|
t.Error("the shape of a deleted track is still kept")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTheShapeOfATrackThatIsGoneIsNotAnError(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
repo := NewWaveformRepo(db)
|
|
if err := repo.Save(context.Background(), 999, waveform.Record{SizeBytes: 1, MTimeUnix: 1, Peaks: []byte{1}}); err != nil {
|
|
t.Errorf("Save for a track that is not there: %v", err)
|
|
}
|
|
if _, found, _ := repo.Get(context.Background(), 999); found {
|
|
t.Error("a shape was kept for a track that is not there")
|
|
}
|
|
})
|
|
}
|