query MPV astats node directly in C for efficiency

This commit is contained in:
Drew Weymouth
2024-07-23 17:38:43 -07:00
parent 1814e2a6a3
commit 02e5c784b3
5 changed files with 73 additions and 13 deletions
+49
View File
@@ -0,0 +1,49 @@
#include <mpv/client.h>
#include <stdlib.h>
#include <string.h>
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;
}
+19
View File
@@ -0,0 +1,19 @@
package mpv
// #include <mpv/client.h>
// 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
}
+2 -10
View File
@@ -455,19 +455,11 @@ func (p *Player) GetPeaks() (float64, float64, float64, float64) {
if p.status.State != player.Playing { if p.status.State != player.Playing {
return nInf, nInf, nInf, nInf 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 { if err != nil {
return nInf, nInf, nInf, nInf return nInf, nInf, nInf, nInf
} }
m := prop.(*mpv.Node).Data.(map[string]*mpv.Node) return lPeak, rPeak, lRMS, rRMS
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
} }
// sets the state and invokes callbacks, if triggered // sets the state and invokes callbacks, if triggered
+1 -1
View File
@@ -11,7 +11,7 @@ require (
github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe github.com/dweymouth/fyne-advanced-list v0.0.0-20240623145729-9c6b8f99bcfe
github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64 github.com/dweymouth/fyne-lyrics v0.0.0-20240528234907-15eee7ce5e64
github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645 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/dweymouth/go-subsonic v0.0.0-20240721002411-f2df85be66f1
github.com/godbus/dbus/v5 v5.1.0 github.com/godbus/dbus/v5 v5.1.0
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
+2 -2
View File
@@ -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/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 h1:KzqSaQwG3HsTZQlEtkp0BeUy9vmYZ0rq0B15qIPSiBs=
github.com/dweymouth/go-jellyfin v0.0.0-20240517151952-5ceca61cb645/go.mod h1:fcUagHBaQnt06GmBAllNE0J4O/7064zXRWdqnTTtVjI= 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-20240724002347-c5e5b36f1bbf h1:QVjXWx7XGkSPOCEu5EL9QVY3fkdDSnG5KEkok5o1svM=
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/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 h1:Ibjafpy6ds3DtmX43hxsnqiBR7mBcihl4IRq+jwlyJA=
github.com/dweymouth/go-subsonic v0.0.0-20240721002411-f2df85be66f1/go.mod h1:OWtcumdQsan8uM6wmx6PqKhldaCthH10CQ+vb+94kzo= 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= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=