$ show taigrr/spank
spank is a macOS CLI tool that detects physical hits/slaps on Apple Silicon MacBooks via the accelerometer and plays audio responses. It requires sudo for IOKit HID access to the accelerometer and is built as a single-file Go application. The tool reads raw accelerometer data, runs vibration detection algorithms (STA/LTA, CUSUM, kurtosis, peak/MAD), and triggers audio playback when impacts exceed a configurable amplitude threshold. It offers multiple modes: pain (random 'ow!' sounds), sexy (escalating responses based on slap frequency), halo (Halo death sounds), lizard (similar to sexy but with lizard sounds), and custom (user-specified MP3 directory). Audio playback uses the beep package with resampling to a fixed sample rate, optional volume scaling based on impact force, and adjustable playback speed. The application uses Cobra for CLI parsing and Fang for command execution, with audio assets embedded via go:embed. Key reusable components include the audio resampling function that handles sample rate conversion and speed adjustment, and the amplitude-to-volume mapping function that applies a logarithmic curve for natural volume scaling.
The LLM identified 2 files worth highlighting for reuse:
The resampleForPlayback function handles audio resampling to a fixed speaker rate while incorporating playback speed changes. It ensures consistent audio output regardless of the original file's sample rate and desired speed factor, demonstrating non-obvious skill in audio engineering. This isolated function could be extracted and reused in any Go project using the beep package for audio playback requiring variable speed control.
The amplitudeToVolume function maps detected acceleration amplitude to a playback volume level using a logarithmic curve to match human perception. It converts sensor input (amplitude in g-force) to a decibel scale for the beep Volume effect, providing natural volume scaling where light taps are quieter and hard slaps are louder. This self-contained, dependency-free function is easily reusable in any application needing to map sensor readings to audio volume.