diff --git a/backend/player/mpv/peaks.c b/backend/player/mpv/peaks.c new file mode 100644 index 0000000..0e609f8 --- /dev/null +++ b/backend/player/mpv/peaks.c @@ -0,0 +1,49 @@ +#include +#include +#include + +int mpv_get_peaks(mpv_handle* handle, double* lPeak, double* rPeak, double* lRMS, double* rRMS) { + mpv_node result; + int ret = mpv_get_property(handle, "af-metadata/astats", MPV_FORMAT_NODE, &result); + if (ret != MPV_ERROR_SUCCESS) { + return ret; + } + if (result.format != MPV_FORMAT_NODE_MAP) { + return MPV_ERROR_PROPERTY_FORMAT; + } + + int found = 0; + for (int i = 0; found < 4 && i < result.u.list->num; i++) { + if (strcmp("lavfi.astats.1.Peak_level", result.u.list->keys[i]) == 0) { + if (result.u.list->values[i].format != MPV_FORMAT_STRING) { + return MPV_ERROR_PROPERTY_FORMAT; + } + *lPeak = atof(result.u.list->values[i].u.string); + found++; + } + if (strcmp("lavfi.astats.2.Peak_level", result.u.list->keys[i]) == 0) { + if (result.u.list->values[i].format != MPV_FORMAT_STRING) { + return MPV_ERROR_PROPERTY_FORMAT; + } + *rPeak = atof(result.u.list->values[i].u.string); + found++; + } + if (strcmp("lavfi.astats.1.RMS_level", result.u.list->keys[i]) == 0) { + if (result.u.list->values[i].format != MPV_FORMAT_STRING) { + return MPV_ERROR_PROPERTY_FORMAT; + } + *lRMS = atof(result.u.list->values[i].u.string); + found++; + } + if (strcmp("lavfi.astats.2.RMS_level", result.u.list->keys[i]) == 0) { + if (result.u.list->values[i].format != MPV_FORMAT_STRING) { + return MPV_ERROR_PROPERTY_FORMAT; + } + *rRMS = atof(result.u.list->values[i].u.string); + found++; + } + } + mpv_free_node_contents(&result); + + return MPV_ERROR_SUCCESS; +} diff --git a/backend/player/mpv/peaks.go b/backend/player/mpv/peaks.go new file mode 100644 index 0000000..e2bbbd3 --- /dev/null +++ b/backend/player/mpv/peaks.go @@ -0,0 +1,19 @@ +package mpv + +// #include +// int mpv_get_peaks(mpv_handle* handle, double* lPeak, double* rPeak, double* lRMS, double* rRMS); +import "C" +import ( + "github.com/dweymouth/go-mpv" +) + +// rather than querying peaks through m.mpv.GetProperty which necessitates +// converting the MPV node to a Go map, we can do so in C with no Go allocations +func (m *Player) getPeaks() (float64, float64, float64, float64, error) { + var lPeak, rPeak, lRMS, rRMS C.double + ret := int(C.mpv_get_peaks((*C.mpv_handle)(m.mpv.MPVHandle()), &lPeak, &rPeak, &lRMS, &rRMS)) + if err := mpv.NewError(ret); err != nil { + return 0, 0, 0, 0, err + } + return float64(lPeak), float64(rPeak), float64(lRMS), float64(rRMS), nil +} diff --git a/backend/player/mpv/player.go b/backend/player/mpv/player.go index 5dfd52b..30feef4 100644 --- a/backend/player/mpv/player.go +++ b/backend/player/mpv/player.go @@ -455,19 +455,11 @@ func (p *Player) GetPeaks() (float64, float64, float64, float64) { if p.status.State != player.Playing { return nInf, nInf, nInf, nInf } - prop, err := p.mpv.GetProperty("af-metadata/astats", mpv.FORMAT_NODE) + lPeak, rPeak, lRMS, rRMS, err := p.getPeaks() if err != nil { return nInf, nInf, nInf, nInf } - m := prop.(*mpv.Node).Data.(map[string]*mpv.Node) - if lPeakNode, ok := m["lavfi.astats.1.Peak_level"]; ok { - lPeak, _ := strconv.ParseFloat(lPeakNode.Data.(string), 64) - rPeak, _ := strconv.ParseFloat(m["lavfi.astats.2.Peak_level"].Data.(string), 64) - lRMS, _ := strconv.ParseFloat(m["lavfi.astats.1.RMS_level"].Data.(string), 64) - rRMS, _ := strconv.ParseFloat(m["lavfi.astats.2.RMS_level"].Data.(string), 64) - return lPeak, rPeak, lRMS, rRMS - } - return nInf, nInf, nInf, nInf + return lPeak, rPeak, lRMS, rRMS } // sets the state and invokes callbacks, if triggered diff --git a/go.mod b/go.mod index fe3267d..3406326 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64 github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645 - github.com/dweymouth/go-mpv v0.0.0-20240722012629-450f1f6a6031 + github.com/dweymouth/go-mpv v0.0.0-20240724002347-c5e5b36f1bbf github.com/dweymouth/go-subsonic v0.0.0-20240721002411-f2df85be66f1 github.com/godbus/dbus/v5 v5.1.0 github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum index 1af59bc..840888e 100644 --- a/go.sum +++ b/go.sum @@ -83,8 +83,8 @@ github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240721235224-2c49fb2028fa h1:c+1vz4r github.com/dweymouth/fyne/v2 v2.3.0-rc1.0.20240721235224-2c49fb2028fa/go.mod h1:9D4oT3NWeG+MLi/lP7ItZZyujHC/qqMJpoGTAYX5Uqc= github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645 h1:KzqSaQwG3HsTZQlEtkp0BeUy9vmYZ0rq0B15qIPSiBs= github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645/go.mod h1:fcUagHBaQnt06GmBAllNE0J4O/7064zXRWdqnTTtVjI= -github.com/dweymouth/go-mpv v0.0.0-20240722012629-450f1f6a6031 h1:5baPYIzQ8K0AS8zrjVY3Xm4T2DD/fZ4CXJKnO5g3HHI= -github.com/dweymouth/go-mpv v0.0.0-20240722012629-450f1f6a6031/go.mod h1:Ov0ieN90M7i+0k3OxhA/g1dozGs+UcPHDsMKqPgRDk0= +github.com/dweymouth/go-mpv v0.0.0-20240724002347-c5e5b36f1bbf h1:QVjXWx7XGkSPOCEu5EL9QVY3fkdDSnG5KEkok5o1svM= +github.com/dweymouth/go-mpv v0.0.0-20240724002347-c5e5b36f1bbf/go.mod h1:Ov0ieN90M7i+0k3OxhA/g1dozGs+UcPHDsMKqPgRDk0= github.com/dweymouth/go-subsonic v0.0.0-20240721002411-f2df85be66f1 h1:Ibjafpy6ds3DtmX43hxsnqiBR7mBcihl4IRq+jwlyJA= github.com/dweymouth/go-subsonic v0.0.0-20240721002411-f2df85be66f1/go.mod h1:OWtcumdQsan8uM6wmx6PqKhldaCthH10CQ+vb+94kzo= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=