Skip to content

Instantly share code, notes, and snippets.

@silky
Created June 21, 2018 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silky/6a3166dc189c3f93b5b3b2167c57db63 to your computer and use it in GitHub Desktop.
Save silky/6a3166dc189c3f93b5b3b2167c57db63 to your computer and use it in GitHub Desktop.
Script to bump sound using `amixer` from `alsa-utils`
#!/usr/bin/env stack
-- stack script --resolver lts-8.12
-- --package turtle
-- --package string-conv
-- Compile like:
-- > stack exec --package turtle --package string-conv -- ghc --make BumpSound.hs
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import qualified Control.Foldl as Fold
import Data.String.Conv
import Control.Monad
stepSize = 4
maxSound = 70
minSound = 0
shell' c = shell c empty
parser :: Parser (Text)
parser = argText "direction" "Either 'up' or 'down'."
main :: IO ()
main = void $ do
cmd <- options "amixer sound bumper" parser
vol <- currentVolume
case cmd of
"down" -> step vol (-stepSize)
"up" -> step vol stepSize
step :: Maybe Int -> Int -> IO ()
step Nothing _ = return ()
step (Just v) s = do
sh $ shell' ("amixer set Master " <> (toS (show nextVol)) <> "%")
where
nextVol :: Int
nextVol = min (max minSound (v+s)) maxSound
currentVolume :: IO (Maybe Int)
currentVolume = do
v <- flip fold Fold.head $ inshell "amixer get Master | grep '\\[on\\]' | awk '{ print $4 }' | grep -Po '\\d+'" ""
return $ read . toS . lineToText <$> v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment