some progress in generating waveform images ahead of time

This commit is contained in:
Drew Weymouth
2025-07-25 18:38:59 -07:00
parent 7dae735d10
commit 88b7f62e50
5 changed files with 242 additions and 62 deletions
+14 -2
View File
@@ -6,10 +6,12 @@ import (
"image"
"image/color"
"io"
"log"
"math"
"os"
"path/filepath"
"github.com/dweymouth/supersonic/backend/util"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
"github.com/supersonic-app/go-mpv"
@@ -71,10 +73,20 @@ func GenerateWaveformImage(data *WaveformData, imgbuf *WaveformImage, c color.Co
}
}
func GetWaveformDataForFile(ctx context.Context, fpath string) (*WaveformData, error) {
func GetWaveformDataForFile(ctx context.Context, fpath string, fileIsDone func() bool) (*WaveformData, error) {
dir := filepath.Dir(fpath)
transcodeFile := filepath.Join(dir, filepath.Base(fpath)+"_waveform.wav")
if !fileIsDone() {
srv, err := util.NewFileStreamerServer(fpath, fileIsDone)
if err != nil {
return nil, err
}
fpath = srv.Addr()
log.Println("streaming file to MPV at ", fpath)
go srv.Serve()
}
err := convertToWav(ctx, fpath, transcodeFile)
if err != nil {
return nil, err
@@ -82,6 +94,7 @@ func GetWaveformDataForFile(ctx context.Context, fpath string) (*WaveformData, e
f, err := os.Open(transcodeFile)
if err != nil {
log.Println("error opening transcoded file")
return nil, err
}
defer f.Close()
@@ -199,7 +212,6 @@ func convertToWav(ctx context.Context, inPath, outPath string) error {
m.Command([]string{"loadfile", inPath, "replace"})
//log.Println("generating wav file from %s using MPV", inPath)
return mpvWaitForIdle(ctx, m)
}