From 4ca96bb57896c02d4a6e81c62e08229671b24d34 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Mon, 18 May 2020 03:59:04 +0200 Subject: [PATCH] mpv: Add pitchcontrol script --- home/.config/mpv/input.conf | 2 + home/.config/mpv/scripts/pitchcontrol.lua | 95 +++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 home/.config/mpv/scripts/pitchcontrol.lua diff --git a/home/.config/mpv/input.conf b/home/.config/mpv/input.conf index be5d1e8..ea5c1ae 100644 --- a/home/.config/mpv/input.conf +++ b/home/.config/mpv/input.conf @@ -14,3 +14,5 @@ k cycle-values audio-pitch-correction "no" "yes" c cycle-values audio-channels stereo mono auto K cycle-values glsl-shaders ~~/shaders/ravu-lite-r4.hook ~~/shaders/ravu-lite-r3.hook ~~/shaders/ravu-lite-r2.hook /dev/null + +Ctrl+p script-message-to pitchcontrol toggle diff --git a/home/.config/mpv/scripts/pitchcontrol.lua b/home/.config/mpv/scripts/pitchcontrol.lua new file mode 100644 index 0000000..a92adc6 --- /dev/null +++ b/home/.config/mpv/scripts/pitchcontrol.lua @@ -0,0 +1,95 @@ +-- pitchcontrol.lua +-- +-- Version: 0.2.0 +-- URL: https://github.com/FichteFoll/mpv-scripts +-- License: ISC + +-- Commands for key bindings: +-- +-- * script-message-to pitchcontrol increase +-- Increase pitch by one half-tone. Bound to Alt+R. +-- * script-message-to pitchcontrol decrease +-- Increase pitch by one half-tone. Bound to Alt+r. +-- * script-message-to pitchcontrol toggle +-- Toggle pitching. +-- * script-message-to pitchcontrol set_halftone_pitch +-- Sets pitch to half-tones - positive, negative or 0. + + +-- Copyright 2016-2017 FichteFoll +-- +-- Permission to use, copy, modify, and/or distribute this software for any +-- purpose with or without fee is hereby granted, provided that the above +-- copyright notice and this permission notice appear in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +-- SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +-- IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + +local utils = require 'mp.utils' +local msg = require('mp.msg') +local options = require('mp.options') +local script_name = mp.get_script_name() -- "pitchcontrol" + +local HALFTONE_SCALE = math.pow(2, 1.0/12) -- 1.059463094352953 +local RUBBERBAND_LABEL = string.format("%s-rubberband", script_name) + +local current_pitch = 0 +local active = false + + +function set_halftone_pitch(pitch, activate) + if activate == nil then + activate = true + end + pitch = tonumber(pitch) + local pitch_scale = math.pow(HALFTONE_SCALE, pitch) + + if active then + mp.commandv('af-command', RUBBERBAND_LABEL, 'set-pitch', pitch_scale) + elseif activate then + mp.command(("af add @%s:rubberband=pitch-scale=%s"):format(RUBBERBAND_LABEL, pitch_scale)) + active = true + else + mp.osd_message(("'%s' is inactive"):format(script_name)) + return + end + + current_pitch = pitch + + -- output new pitch + msg.debug(("new pitch-scale: %f"):format(pitch_scale)) + mp.osd_message(("Pitch: %+d halftones"):format(pitch)) +end + + +function increase_handler() + set_halftone_pitch(current_pitch + 1, false) +end + + +function decrease_handler() + set_halftone_pitch(current_pitch - 1, false) +end + + +function toggle_handler() + if active then + mp.command(("af del @%s"):format(RUBBERBAND_LABEL)) + mp.osd_message(("'%s' deactivated"):format(script_name)) + active = false + else + set_halftone_pitch(current_pitch, true) + end +end + + +mp.add_key_binding("Alt+p", 'increase', decrease_handler) +mp.add_key_binding("Alt+P", 'decrease', increase_handler) +mp.register_script_message('toggle', toggle_handler) +mp.register_script_message('set_halftone_pitch', set_halftone_pitch)