From 1351c7da3933d8fc2c148730ddd63e4433c80596 Mon Sep 17 00:00:00 2001 From: rwjs Date: Sat, 29 Jun 2013 16:26:01 +1000 Subject: [PATCH 1/4] Changed file extension, separated logic and data, added try/finally for cleanup, switched to env(8), changed explicit variable boolean to flow control (break) --- README.md | 1 + piano.sh | 58 ------------------------------------------------------- 2 files changed, 1 insertion(+), 58 deletions(-) delete mode 100755 piano.sh diff --git a/README.md b/README.md index efb3741..aa0f20b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ LINUX BEEP PIANO! + An extremely lightweight piano that you can run in your terminal. Thanks for reading this README file. diff --git a/piano.sh b/piano.sh deleted file mode 100755 index b2bab4c..0000000 --- a/piano.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/python -#or /your/python/path - -done = True - -#Importing the libraries -import subprocess -import curses - -#Set up Curses -lin = curses.initscr() #Initialize window -curses.cbreak() #Turn off buffering of chars, allows to be read without pressing enter - -#Be friendly -lin.addstr("Welcome to the linux beep piano! (Coded in Python!)\n") -lin.addstr("Press keys a-k for whole steps, keys w-u for half steps, and q to exit.\n") - -#Infinite loop! (kinda) -while done == True: - #Get that character - x = lin.getch() - #Check that character - if(x == ord('a')): - #Using subprocess to call beep from command line - subprocess.call("beep -f 261.63", shell=True) #C4 - elif(x == ord('w')): - subprocess.call("beep -f 277.18", shell=True) #Db4 - elif(x == ord('s')): - subprocess.call("beep -f 293.66", shell=True) #D4 - elif(x == ord('e')): - subprocess.call("beep -f 311.13", shell=True) #Eb4 - elif(x == ord('d')): - subprocess.call("beep -f 329.63", shell=True) #E4 - elif(x == ord('f')): - subprocess.call("beep -f 349.23", shell=True) #F4 - elif(x == ord('t')): - subprocess.call("beep -f 369.99", shell=True) #Gb4 - elif(x == ord('g')): - subprocess.call("beep -f 392.00", shell=True) #G4 - elif(x == ord('y')): - subprocess.call("beep -f 415.30", shell=True) #Ab4 - elif(x == ord('h')): - subprocess.call("beep -f 440.00", shell=True) #A4 - elif(x == ord('u')): - subprocess.call("beep -f 466.16", shell=True) #Bb4 - elif(x == ord('j')): - subprocess.call("beep -f 493.88", shell=True) #B4 - elif(x == ord('k')): - subprocess.call("beep -f 523.25", shell=True) #C5 - elif(x == ord('q')): - done = False - -#Call commands to properly exit curses and return terminal to original state -curses.nocbreak() -lin.keypad(0) -curses.echo() -curses.endwin() - From ee66e002c17d2e976e065aa1d56dd4e9826a5b94 Mon Sep 17 00:00:00 2001 From: rwjs Date: Sat, 29 Jun 2013 16:28:07 +1000 Subject: [PATCH 2/4] Changed file extension, separated logic and data, added try/finally for cleanup, switched to env(8), changed explicit variable boolean to flow control (break) --- piano.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 piano.py diff --git a/piano.py b/piano.py new file mode 100755 index 0000000..4d7e337 --- /dev/null +++ b/piano.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +# Importing the libraries +import subprocess +import curses + +notes = { + ord('a') : 261.63, #C4 + ord('w') : 277.18, #Db4 + ord('s') : 293.66, #D4 + ord('e') : 311.13, #Eb4 + ord('d') : 329.63, #E4 + ord('f') : 349.23, #F4 + ord('t') : 369.99, #Gb4 + ord('g') : 392.00, #G4 + ord('y') : 415.30, #Ab4 + ord('h') : 440.00, #A4 + ord('u') : 466.16, #Bb4 + ord('j') : 493.88, #B4 + ord('k') : 523.25, #C5 + } + +try: + + #Set up Curses + lin = curses.initscr() #Initialize window + curses.cbreak() #Turn off buffering of chars, allows to be read without pressing enter + + #Be friendly + lin.addstr(" Welcome to the linux beep piano! (Coded in Python!)\n") + lin.addstr(" Press keys a-k for whole steps, keys w-u for half steps, and q to exit.\n") + + while True: + + #Get that character + x = lin.getch() + if x == ord('q'): + break + + elif x in notes.keys(): + subprocess.call("beep -f {0}".format(notes.get(x, 0)), shell=True) + +finally: + #Call commands to properly exit curses and return terminal to original state + curses.nocbreak() + lin.keypad(0) + curses.echo() + curses.endwin() From e0af0707072095d6649bc2df3f28a1abccc68d1e Mon Sep 17 00:00:00 2001 From: rwjs Date: Sun, 30 Jun 2013 11:42:56 +1000 Subject: [PATCH 3/4] Changed tabs to spaces (PEP8) --- piano.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/piano.py b/piano.py index 4d7e337..c7e58c7 100755 --- a/piano.py +++ b/piano.py @@ -32,13 +32,13 @@ while True: - #Get that character - x = lin.getch() - if x == ord('q'): - break + #Get that character + x = lin.getch() - elif x in notes.keys(): - subprocess.call("beep -f {0}".format(notes.get(x, 0)), shell=True) + if x == ord('q'): + break + elif x in notes.keys(): + subprocess.call("beep -f {0}".format(notes.get(x, 0)), shell=True) finally: #Call commands to properly exit curses and return terminal to original state From babb9a9cf1924c362a57b9c4c8dbf4a1a14e1ddc Mon Sep 17 00:00:00 2001 From: rwjs Date: Sun, 30 Jun 2013 11:46:12 +1000 Subject: [PATCH 4/4] Fixed dumb misalignment error (regression fix from commit e0af070) --- piano.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/piano.py b/piano.py index c7e58c7..885a2e7 100755 --- a/piano.py +++ b/piano.py @@ -32,13 +32,13 @@ while True: - #Get that character - x = lin.getch() + #Get that character + x = lin.getch() - if x == ord('q'): - break - elif x in notes.keys(): - subprocess.call("beep -f {0}".format(notes.get(x, 0)), shell=True) + if x == ord('q'): + break + elif x in notes.keys(): + subprocess.call("beep -f {0}".format(notes.get(x, 0)), shell=True) finally: #Call commands to properly exit curses and return terminal to original state