Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Created February 7, 2019 12:01
Show Gist options
  • Save sayanriju/8008484f1b4db0dc63da721d620f07ff to your computer and use it in GitHub Desktop.
Save sayanriju/8008484f1b4db0dc63da721d620f07ff to your computer and use it in GitHub Desktop.
A tiny, nifty script for playing random musical notes on each keypress
#!/usr/bin/env python
## A tiny, nifty script for playing random musical notes on each keypress.
##
## Copyright Sayan "Riju" Chakrabarti <s26c.sayan@gmail.com> 2009-2019
## Released under WTFPL Version 2
## (DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE)
## Copy of license text can be found online at
## http://sam.zoy.org/wtfpl/COPYING
##
## External/System Dependencies:
## * Sox (http://sox.sourceforge.net/sox.html)
##
## Python Dependencies:
## * python-xlib (https://github.com/python-xlib/python-xlib)
from Xlib.display import Display
import os
import time
import random
ZERO, SHIFT, ALT, CTL = [], [], [], []
for i in range(0,32):
ZERO.append(0)
if i==6:
SHIFT.append(4)
else:
SHIFT.append(0)
if i==4:
CTL.append(32)
else:
CTL.append(0)
if i==8:
ALT.append(1)
else:
ALT.append(0)
ignorelist = [ZERO,ALT,SHIFT,CTL]
def randomNoteFreq():
return random.choice([ 220, 246.94, 261.63, 293.66, 329.63, 349.23, 392 ])
def main():
disp = Display() # connect to display
while 1: #event loop
keymap = disp.query_keymap()
if keymap not in ignorelist:
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (0.05, 2 * randomNoteFreq()))
time.sleep(0.1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment