expose MPV ReplayGain options
This commit is contained in:
@@ -104,6 +104,14 @@ func (m libmpv) SetProperty(name string, format MPVFormat, value any) error {
|
||||
return toMPVError(C.mpv_set_property(m.handle, cname, C.mpv_format(format), p))
|
||||
}
|
||||
|
||||
func (m libmpv) SetPropertyString(name, value string) error {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
cvalue := C.CString(value)
|
||||
defer C.free(unsafe.Pointer(cvalue))
|
||||
return toMPVError(C.mpv_set_property_string(m.handle, cname, cvalue))
|
||||
}
|
||||
|
||||
func (m libmpv) GetProperty(name string, format MPVFormat) (any, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
|
||||
@@ -38,12 +38,30 @@ const (
|
||||
SeekRelativePercent
|
||||
)
|
||||
|
||||
// One of "no", "track", or "album"
|
||||
type ReplayGainMode string
|
||||
|
||||
const (
|
||||
ReplayGainNone ReplayGainMode = "no"
|
||||
ReplayGainTrack ReplayGainMode = "track"
|
||||
ReplayGainAlbum ReplayGainMode = "album"
|
||||
)
|
||||
|
||||
// Replay Gain options (argument to SetReplayGainOptions).
|
||||
type ReplayGainOptions struct {
|
||||
Mode ReplayGainMode
|
||||
FallbackGain float64
|
||||
PreampGain float64
|
||||
}
|
||||
|
||||
// Player encapsulates the mpv instance and provides functions
|
||||
// to control it and to check its status.
|
||||
type Player struct {
|
||||
mpv libmpv
|
||||
initialized bool
|
||||
vol int
|
||||
replayGainOpts ReplayGainOptions
|
||||
haveRGainOpts bool
|
||||
status Status
|
||||
seeking bool
|
||||
curPlaylistPos int64
|
||||
@@ -99,6 +117,9 @@ func (p *Player) Init(audioExclusive bool) error {
|
||||
if audioExclusive {
|
||||
m.SetOptionString("audio-exclusive", "yes")
|
||||
}
|
||||
if p.haveRGainOpts {
|
||||
p.SetReplayGainOptions(p.replayGainOpts)
|
||||
}
|
||||
|
||||
if p.clientName != "" {
|
||||
m.SetOptionString("audio-client-name", p.clientName)
|
||||
@@ -232,6 +253,26 @@ func (p *Player) SetVolume(vol int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sets the ReplayGain options of the player.
|
||||
// Unlike most Player functions, SetReplayGainOptions can be called
|
||||
// before Init, to set the initial volume of the player on startup.
|
||||
func (p *Player) SetReplayGainOptions(options ReplayGainOptions) error {
|
||||
p.replayGainOpts = options
|
||||
p.haveRGainOpts = true
|
||||
if p.initialized {
|
||||
if err := p.mpv.SetPropertyString("replaygain", string(options.Mode)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.mpv.SetProperty("replaygain-preamp", MPVFormatDouble, options.PreampGain); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.mpv.SetProperty("replaygain-fallback", MPVFormatDouble, options.FallbackGain); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Gets the current volume of the player.
|
||||
func (p *Player) GetVolume() int {
|
||||
return p.vol
|
||||
|
||||
Reference in New Issue
Block a user