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.
46 lines
1.6 KiB
Go
46 lines
1.6 KiB
Go
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")
|
|
}
|
|
}
|