Skip to content

Instantly share code, notes, and snippets.

@kevinlekiller
Last active July 18, 2025 16:35
Show Gist options
  • Save kevinlekiller/9fd21936411d8dc5998793470c6e3d16 to your computer and use it in GitHub Desktop.
Save kevinlekiller/9fd21936411d8dc5998793470c6e3d16 to your computer and use it in GitHub Desktop.
Lua script for mpv to enable sofalizer, with sofa file inside of mpv's ~~/
--[[
Copyright (C) 2023 kevincs
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
https://www.gnu.org/licenses/gpl-2.0.html
--]]
-- Lua script for mpv to enable sofalizer, with sofa file inside of mpv's ~~/
-- For more info :
-- https://old.reddit.com/r/mpv/comments/11cr5u9/is_it_possible_to_use_the_headphone_filter/
-- https://github.com/flathub/io.mpv.Mpv/issues/125
-- Audio must have at least this many channels to enable sofalizer.
local sofa_min_channels = 3
-- Audio must have at most this many channels to enable sofalizer.
local sofa_max_channels = 9
-- Amount of gain to add to sofalizer
local sofa_gain = 12
-- Extra options to add to sofalizer
-- Example: "interpolate=1:framesize=8192"
local sofa_opts = "interpolate=1"
-- Sofa file name (optional subdirectory)
local sofa_file = "sofa/ClubFritz6.sofa"
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
function main(name, channels)
local af_add = "sofalizer=sofa=\"" .. mp.command_native({"expand-path", "~~/"}) .. "/" .. sofa_file .. "\":gain=" .. sofa_gain
if (sofa_opts ~= "") then
af_add = af_add .. ":" .. sofa_opts
end
if (channels == nil or channels < sofa_min_channels or channels > sofa_max_channels) then
mp.command("no-osd af remove '" .. af_add .. "'")
return
end
local af = mp.get_property("af")
if (string.find(af, "sofalizer")) then
return
end
print("Current --af=" .. af)
if (af == "") then
print("New --af=" .. af_add)
else
print("New --af=" .. af .. ";" .. af_add)
end
mp.command("no-osd af add '" .. af_add .. "'")
end
-- This is here so both changing files and changing audio id (if channel count changes) should retrigger main.
function file_ended()
mp.unregister_event(file_ended)
mp.unobserve_property(main)
mp.register_event("file-loaded", file_loaded)
end
function file_loaded()
mp.unregister_event(file_loaded)
mp.register_event("end-file", file_ended)
mp.observe_property("audio-params/channel-count", "number", main)
end
mp.register_event("file-loaded", file_loaded)
@zenwav
Copy link

zenwav commented Mar 4, 2024

I receive an audio device underrun warning in the console every time I start watching a new video. However, this issue does not occur when resuming playback. The warning only appears once at the beginning of each video when using the Lua script. Interestingly, if I hardcode the audio filter in my mpv.conf, this problem does not manifest.

@basso
Copy link

basso commented Aug 4, 2024

Thank you for making this plugin, I have been using it every day now!

@insec404
Copy link

whoa this is amazing good job on this!

@xZabuzax
Copy link

I use MPV.net, so how does this work exactly? I only need this Lua script and the .sofa file and that's it? or do I need to install other additional softwares?

@Sensex00
Copy link

Sensex00 commented Jul 18, 2025

@kevinlekiller There's an audio underrun issue when sofalizer is applied like via script. If it's set with af-add in mpv.conf, the problem doesn't happen. Would be great if something could be done to avoid underruns without manually setting af add in mpv.conf.
I only use these for all media in mpv.conf
af=sofalizer="C:\Program Files\mpv\portable_config\sofa\ClubFritz6.sofa":gain=12

@Sensex00
Copy link

@kevinlekiller You can also check this link mpv-player/mpv#14889 and also test it by yourself by making logs when using it via script and using it by af add in mpv conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment