Created
February 25, 2015 22:57
-
-
Save sumpygump/b2ffe1cb79402f3eab9c to your computer and use it in GitHub Desktop.
Splay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This is a quick CLI tool to preview a folder of audio samples. | |
# Run it in a directory with some .wav files. | |
# Uses mplayer to trigger playing the sounds. | |
# Currently it loads up as many samples as will fit on the keyboard (lowercase letters) | |
# In the future it will allow you to change directories, page through samples on the keyboard and maybe some other fun stuff. | |
import sys | |
import os | |
import subprocess | |
import fnmatch | |
import cStringIO | |
class _Getch: | |
"""Gets a single character from standard input. Does not echo to the | |
screen.""" | |
def __init__(self): | |
try: | |
self.impl = _GetchWindows() | |
except ImportError: | |
self.impl = _GetchUnix() | |
def __call__(self): return self.impl() | |
class _GetchUnix: | |
def __init__(self): | |
import tty, sys | |
def __call__(self): | |
import sys, tty, termios | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
class _GetchWindows: | |
def __init__(self): | |
import msvcrt | |
def __call__(self): | |
import msvcrt | |
return msvcrt.getch() | |
getch = _Getch() | |
class Sample: | |
filename = "" | |
process = None | |
def __init__(self, filename): | |
self.filename = filename | |
def play(self, keyPressed): | |
if self.process: | |
if self.process.poll() is None: | |
self.stop(keyPressed) | |
else: | |
self.triggerPlay(keyPressed) | |
else: | |
self.triggerPlay(keyPressed) | |
def triggerPlay(self, keyPressed): | |
print keyPressed, self.filename, 'ON' | |
self.process = subprocess.Popen(['mplayer', '-nolirc', '-really-quiet', self.filename]) | |
def stop(self, keyPressed = ''): | |
if self.process and self.process.poll() is None: | |
if keyPressed: | |
print keyPressed, self.filename, 'OFF' | |
self.process.kill() | |
self.process = None | |
# get list of files | |
files = [] | |
def getFiles(path): | |
for f in os.listdir(path): | |
f = os.path.join(path, f) | |
if os.path.isdir(f): getFiles(f) # recurse | |
if len(files) > 1000: break # break if we have enough | |
if fnmatch.fnmatch(f, '*.wav'): files.append(f) | |
getFiles(os.path.expanduser(".")) | |
print 'Press Q (upper-case) to exit' | |
buttons = ['q','w','e','r','t','y','u','i','o','p', | |
'a','s','d','f','g','h','j','k','l',';', | |
'z','x','c','v','b','n','m',',','.'] | |
# Set up the samples, assigning to keys | |
samples = {} | |
offset = 0 | |
cursor = 0 | |
for button in buttons: | |
try: | |
samples[button] = Sample(files[offset + cursor]) | |
except IndexError: | |
break | |
cursor = cursor + 1 | |
for key in buttons: | |
if key in samples: | |
print '[', key, ']', samples[key].filename | |
# Main loop | |
pressed = "" | |
while pressed != "Q": | |
pressed = getch() | |
if (pressed in samples): | |
samples[pressed].play(pressed) | |
for i in samples: | |
samples[i].stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment