Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wish your family and friends a merryxmas.sh (Merry Christmas) via their keyboards and mice

merryxmas.sh

Wish your family and friends a Merry Christmas via their keyboards and mice!

Read the blog post for more, watch the video to see how it sings.

Requirements

  • Bash
  • xinput
  • play from Sox
  • stdbuf for resolving buffer issue

Usage

By default, it uses keyboard and mouse devices:

AT Translated Set 2 keyboard
SynPS/2 Synaptics TouchPad

You should run with:

% stdbuf -oL merryxmas.sh &

To get the right device names and change them, use:

% xinput list
% INPUT_K='KB_DEV_NAME' INPUT_M='M_DEV_NAME' stdbuf -oL merryxmas.sh &

You should kill it with if necessary:

% pkill merryxmas.sh
% pkill xinput

Note

The script creates two files:

  1. /tmp/.tmp_k
  2. /tmp/.tmp_m

Copyright

This project has been placed in public domain, or via UNLICENSE if not applicable.

#!/bin/bash
# Wish your family and friends a Merry Christmas via their keyboards and mice!
# Written by Yu-Jie Lin in 2015
#
# This script has been placed in public domain, or via Unlicense if not
# applicable, see UNLICENSE.
#
# Gist: https://gist.github.com/livibetter/6fdafcf4279cb1c90460
# Usage: stdbuf -oL merryxmas.sh &
INPUT_K=${INPUT_K:-AT Translated Set 2 keyboard}
INPUT_M=${INPUT_M:-SynPS/2 Synaptics TouchPad}
#################################
# We Wish You a Merry Christmas #
#################################
C4=261.626
D4=293.665
E4=329.628
SF4=369.994
G4=391.995
A4=440.000
B4=493.883
C5=523.251
D5=587.330
L2=0.5
L4=0.25
L8=0.125
# musical notes source: http://www.music-scores.com/graphics/xXes5.gif
NOTES=(
D4 L4
G4 L4
G4 L8
A4 L8
G4 L8
SF4 L8
E4 L4
C4 L4
E4 L4
A4 L4
A4 L8
B4 L8
A4 L8
G4 L8
SF4 L4
D4 L4
SF4 L4
B4 L4
B4 L8
C5 L8
B4 L8
A4 L8
G4 L4
E4 L4
D4 L4
D4 L4
E4 L4
A4 L4
SF4 L4
G4 L2
D4 L4
G4 L4
G4 L4
G4 L4
SF4 L2
SF4 L4
G4 L4
SF4 L4
E4 L4
D4 L2
A4 L4
B4 L4
A4 L8
A4 L8
G4 L8
G4 L8
D5 L4
D4 L4
D4 L8
D4 L8
E4 L4
A4 L4
SF4 L4
G4 L2
)
########
# main #
########
TMP_K=/tmp/.tmp_k
TMP_M=/tmp/.tmp_m
rm -f $TMP_K $TMP_M
touch -f $TMP_K $TMP_M
xinput test "$INPUT_K" | grep -o press >> $TMP_K &
xinput test "$INPUT_M" | grep -o 'press\|motion' >> $TMP_M &
m=0
i=0
tail -f $TMP_K $TMP_M | grep -o 'press\|motion' | while read; do
if [[ $REPLY == motion ]]; then
((++m < 16)) && continue
m=0
fi
play -q -n synth ${!NOTES[i + 1]} sin ${!NOTES[i]}
((i += 2))
((i >= ${#NOTES[@]})) && i=0
done
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment