Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 21 additions & 0 deletions typing-sounds/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Loc Huynh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions typing-sounds/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Typing Sounds

Play mechanical keyboard sounds as you type on your keyboard.

## Plugin

| Field | Value |
| --- | --- |
| ID | `hthienloc/typing-sounds` |
| Entries | Service: `daemon` |

## Requirements

- `evtest` installed on `PATH`.
- User added to the `input` group (`sudo usermod -aG input $USER`).
- PipeWire installed with `pw-play` on `PATH`.

## Usage

Enable the plugin in Noctalia Settings (`Plugins -> Typing Sounds`).

All configuration, including toggling sounds on/off, volume level, and choosing switch sound profiles, is managed directly in the plugin settings.

## Settings

| Setting | Type | Default | Description |
| --- | --- | --- | --- |
| `enabled` | `bool` | `true` | Toggle typing sounds on or off. |
| `volume` | `int` | `100` | Sound volume percentage from 0% to 200% (min=0, max=200, step=5). |
| `sound_pack` | `select` | `nk-cream` | Select mechanical switch sound profile (18 profiles available). |
| `input_devices` | `string_list` | `["/dev/input/by-id/*kbd*", "/dev/input/by-id/*Keyboard*", "/dev/input/by-path/*kbd*"]` | List of input paths or globs to monitor. |
| `executable_path` | `file` | `evtest` | Path to the `evtest` binary. |

## IPC

Toggle or control typing sounds from external scripts and shortcuts:

```sh
noctalia msg plugin hthienloc/typing-sounds:daemon all toggle
noctalia msg plugin hthienloc/typing-sounds:daemon all enable
noctalia msg plugin hthienloc/typing-sounds:daemon all disable
```

## Notes

- Uses `pw-play` (PipeWire) to stream sounds with minimal latency.
- Binds to keyboard devices directly via `evtest` stream with automatic symlink deduplication and event debouncing.
77 changes: 77 additions & 0 deletions typing-sounds/plugin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
id = "hthienloc/typing-sounds"
name = "Typing Sounds"
version = "1.0.0"
plugin_api = 20
author = "Loc Huynh"
license = "MIT"
icon = "keyboard"
description = "Play mechanical keyboard sounds as you type"
tags = ["audio", "fun", "utility", "service"]
dependencies = ["evtest"]

[[service]]
id = "daemon"
entry = "service.luau"

[[setting]]
key = "enabled"
type = "bool"
label_key = "settings.enabled.label"
description_key = "settings.enabled.description"
default = true

[[setting]]
key = "volume"
type = "int"
label_key = "settings.volume.label"
description_key = "settings.volume.description"
default = 100
min = 0
max = 200
step = 5

[[setting]]
key = "sound_pack"
type = "select"
label_key = "settings.sound_pack.label"
description_key = "settings.sound_pack.description"
default = "nk-cream"
options = [
{ value = "cherrymx-black-abs", label_key = "pack.cherrymx-black-abs" },
{ value = "cherrymx-black-pbt", label_key = "pack.cherrymx-black-pbt" },
{ value = "cherrymx-blue-abs", label_key = "pack.cherrymx-blue-abs" },
{ value = "cherrymx-blue-pbt", label_key = "pack.cherrymx-blue-pbt" },
{ value = "cherrymx-brown-abs", label_key = "pack.cherrymx-brown-abs" },
{ value = "cherrymx-brown-pbt", label_key = "pack.cherrymx-brown-pbt" },
{ value = "cherrymx-red-abs", label_key = "pack.cherrymx-red-abs" },
{ value = "cherrymx-red-pbt", label_key = "pack.cherrymx-red-pbt" },
{ value = "cream-travel", label_key = "pack.cream-travel" },
{ value = "eg-crystal-purple", label_key = "pack.eg-crystal-purple" },
{ value = "eg-oreo", label_key = "pack.eg-oreo" },
{ value = "holy-pandas", label_key = "pack.holy-pandas" },
{ value = "mxblack-travel", label_key = "pack.mxblack-travel" },
{ value = "mxblue-travel", label_key = "pack.mxblue-travel" },
{ value = "mxbrown-travel", label_key = "pack.mxbrown-travel" },
{ value = "nk-cream", label_key = "pack.nk-cream" },
{ value = "topre-purple-hybrid-pbt", label_key = "pack.topre-purple-hybrid-pbt" },
{ value = "turquoise", label_key = "pack.turquoise" }
]

[[setting]]
key = "input_devices"
type = "string_list"
label_key = "settings.input_devices.label"
description_key = "settings.input_devices.description"
default = [
"/dev/input/by-id/*kbd*",
"/dev/input/by-id/*Keyboard*",
"/dev/input/by-path/*kbd*"
]

[[setting]]
key = "executable_path"
type = "file"
label_key = "settings.executable_path.label"
default = "evtest"


195 changes: 195 additions & 0 deletions typing-sounds/service.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
--!nonstrict
-- service.luau
-- Background headless service for Typing Sounds

local soundPaths = {} -- [keycode_str] -> absolute path to .wav
local lastKeyTime = {}
local DEBOUNCE_SEC = 0.04 -- 40ms debounce to prevent duplicate events across virtual devices

-- Read configuration
local enabled = noctalia.getConfig("enabled") ~= false
local volumePct = tonumber(noctalia.getConfig("volume")) or 100
local soundPack = noctalia.getConfig("sound_pack") or "nk-cream"

-- Calculate volume factor for pw-play (100% -> 1.0, 200% -> 2.0)
local volumeFactor = string.format("%.2f", math.clamp(volumePct, 0, 200) / 100)

local function loadSoundPack(packName)
soundPaths = {}

local configPath = "soundpacks/" .. packName .. "/config.json"
local rawConfig = noctalia.readFile(configPath)
if not rawConfig then
noctalia.notifyError("Typing Sounds", "Could not load soundpack config: " .. packName)
return
end

local cfg, err = noctalia.json.decode(rawConfig)
if not cfg or not cfg.defines then
noctalia.notifyError("Typing Sounds", "Invalid soundpack config: " .. tostring(err))
return
end

local pluginDir = noctalia.pluginDir() or "."
for keycode, filename in pairs(cfg.defines) do
if filename and filename ~= "" then
soundPaths[tostring(keycode)] = pluginDir .. "/soundpacks/" .. packName .. "/" .. filename
end
end
end

local function triggerKeySound(keycode)
if not enabled then
return
end

local keyStr = tostring(keycode)
local now = os.clock()
if lastKeyTime[keyStr] and (now - lastKeyTime[keyStr]) < DEBOUNCE_SEC then
return
end
lastKeyTime[keyStr] = now

local filePath = soundPaths[keyStr] or soundPaths["57"] or soundPaths["28"]
if filePath then
local quoted = "'" .. filePath:gsub("'", "'\\''") .. "'"
noctalia.runAsync("pw-play --volume " .. volumeFactor .. " " .. quoted)
end
end

local function has(line, needle)
return line:find(needle, 1, true) ~= nil
end

-- Process evtest events
local function onEvtestLine(line)
if not has(line, "EV_KEY") or has(line, "BTN_TOOL_") then
return
end

if has(line, "value 1") then
local codeStr = line:match("code%s+(%d+)")
if codeStr then
local code = tonumber(codeStr)
if code then
triggerKeySound(code)
end
end
end
end

local function isAllowedInputPatternByte(byte)
return (byte >= 48 and byte <= 57) -- 0-9
or (byte >= 65 and byte <= 90) -- A-Z
or (byte >= 97 and byte <= 122) -- a-z
or byte == 47 -- /
or byte == 46 -- .
or byte == 95 -- _
or byte == 45 -- -
or byte == 58 -- :
or byte == 43 -- +
or byte == 42 -- *
or byte == 63 -- ?
or byte == 91 -- [
or byte == 93 -- ]
end

local function isSupportedInputPattern(pattern)
return pattern:match("^/dev/input/event") ~= nil
or pattern:match("^/dev/input/by%-id/") ~= nil
or pattern:match("^/dev/input/by%-path/") ~= nil
end

local function validateInputPattern(pattern)
if type(pattern) ~= "string" or pattern == "" then
return false
end
if pattern:find("..", 1, true) ~= nil or not isSupportedInputPattern(pattern) then
return false
end
for i = 1, #pattern do
if not isAllowedInputPatternByte(pattern:byte(i)) then
return false
end
end
return true
end

-- Start evtest stream (bongocat style)
local function startInputReader()
local devices = noctalia.getConfig("input_devices")
if devices == nil or #devices == 0 then
devices = {
"/dev/input/by-id/*kbd*",
"/dev/input/by-id/*Keyboard*",
"/dev/input/by-path/*kbd*"
}
end

if type(devices) ~= "table" then
noctalia.notifyError("Typing Sounds", "Invalid input_devices (expected a list of /dev/input paths or globs)")
return
end

local patterns = {}
local seen = {}
for _, pattern in ipairs(devices) do
if not validateInputPattern(pattern) then
noctalia.notifyError("Typing Sounds", "Invalid input_devices entry: " .. tostring(pattern))
return
end
if seen[pattern] == nil then
seen[pattern] = true
table.insert(patterns, pattern)
end
end

if #patterns == 0 then
return
end

local executable = noctalia.getConfig("executable_path")
if executable == nil or executable == "" then
executable = "evtest"
end

if not noctalia.commandExists(executable) then
noctalia.notifyError("Typing Sounds", "The evtest binary is missing (" .. executable .. "). Please install evtest.")
return
end

local quotedExecutable = "'" .. executable:gsub("'", "'\\''") .. "'"
local command = "seen=''; for device in " .. table.concat(patterns, " ")
.. '; do [ -e "$device" ] || continue; '
.. 'case "$device" in *-event-*|*-event-kbd) ;; *) continue ;; esac; '
.. 'target=$(realpath "$device" 2>/dev/null); '
.. 'case "$target" in /dev/input/event*) '
.. 'case " $seen " in *" $target "*) ;; *) seen="$seen $target"; '
.. quotedExecutable .. ' "$target" 2>/dev/null & ;; esac;; esac; done; wait'

local started = noctalia.runStream(command, onEvtestLine)
if not started then
noctalia.notifyError("Typing Sounds", "Unable to start evtest input reader stream")
end
end

-- IPC Handler: noctalia msg plugin <id> <event>
function onIpc(event, _payload)
if event == "toggle" then
enabled = not enabled
elseif event == "enable" then
enabled = true
elseif event == "disable" then
enabled = false
end
end

-- Cleanup when entry exits
function onExit(_signal, _reason)
soundPaths = {}
lastKeyTime = {}
end

-- Initialize
loadSoundPack(soundPack)
startInputReader()
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/1.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/2.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/3.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/4.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/5.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/6.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/8.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added typing-sounds/soundpacks/cherrymx-black-abs/9.wav
Binary file not shown.
Loading
Loading