Add options to abcplay.py to pass through to abc2midi and timidity.
authorW. Trevor King <wking@drexel.edu>
Wed, 17 Nov 2010 13:56:43 +0000 (08:56 -0500)
committerW. Trevor King <wking@drexel.edu>
Wed, 17 Nov 2010 13:56:43 +0000 (08:56 -0500)
posts/abcplay/abcplay.py

index c8a40e96737ccf4f55983a75f078cccc9bab209a..846416409c2fbf08d1a77500eee8470c8a729f00 100755 (executable)
@@ -22,14 +22,22 @@ For example::
 
     abcplay.py my_collection.abc:3:7:8 another_collection.abc
 
+An example of using timidity options would be slowing down a tune
+while you are learning::
+
+    abcplay.py --timidity '--adjust-tempo 50' my_collection.abc:3
+
 SIGINT (usually ^C) will kill the current tune and proceed the next
 one.  Two SIGINTs in less than one second will kill the abcplay.py
-process.
+process.  This should be familiar to users of mpg123_ or ogg123_.
 
 .. _abc2midi: http://abc.sourceforge.net/abcMIDI/
 .. _timidity: http://timidity.sourceforge.net/
+.. _mpg123: http://www.mpg123.org/
+.. _ogg123: http://www.vorbis.com
 """
 
+import shlex
 from subprocess import Popen
 from tempfile import mkstemp
 from time import time
@@ -39,8 +47,14 @@ from os import remove
 REFNUM_SEP = ':'
 
 class ABCPlayer (object):
-    def __init__(self):
+    def __init__(self, abc2midi_options=None, timidity_options=None):
         f,self._tempfile = mkstemp(prefix='abcplay-', suffix='.midi')
+        self._abc2midi = ['abc2midi']
+        if abc2midi_options:
+            self._abc2midi.extend(abc2midi_options)
+        self._timidity = ['timidity']
+        if timidity_options:
+            self._timidity.extend(timidity_options)
         self._last_interrupt = 0
         self._p = None
 
@@ -82,12 +96,12 @@ class ABCPlayer (object):
 
     def _convert_to_midi(self, filename, refnum):
         self._p = Popen(
-            ['abc2midi', filename, str(refnum), '-o', self._tempfile])
+            self._abc2midi + [filename, str(refnum), '-o', self._tempfile])
         self._p.wait()
         self._p = None
 
     def _play_midi(self):
-        self._p = Popen(['timidity', self._tempfile])
+        self._p = Popen(self._timidity + [self._tempfile])
         self._p.wait()
         self._p = None
 
@@ -108,14 +122,25 @@ if __name__ == '__main__':
     import sys
     import optparse
 
-    usage = '%prog file[:refnum[:refnum:...]] ...'
+    usage = '%prog [options] file[:refnum[:refnum:...]] ...'
     epilog = __doc__
     p = optparse.OptionParser(usage=usage, epilog=epilog)
     p.format_epilog = lambda formatter: epilog+'\n'
+    p.add_option('-a', '--abc2midi', dest='abc2midi',
+                 help='Extra options to pass to abc2midi.')
+    p.add_option('-t', '--timidity', dest='timidity',
+                 help='Extra options to pass to timidity.')
     options,args = p.parse_args()
     del p
 
-    p = ABCPlayer()
+    abc2midi = options.abc2midi
+    if abc2midi:
+        abc2midi = shlex.split(abc2midi)
+    timidity = options.timidity
+    if timidity:
+        timidity = shlex.split(timidity)
+
+    p = ABCPlayer(abc2midi, timidity)
     try:
         p.play_files(args)
     finally: