Permalink
Browse files

ported to support numpy as well as Numeric

  • Loading branch information...
1 parent 53320e3 commit 6ad9041504b325886cb3bdff7836fb5186544415 kragen committed May 20, 2012
Showing with 38 additions and 26 deletions.
  1. +10 −8 README.md
  2. +9 −3 pytebeat.py
  3. +19 −15 shuntparse.py
View
18 README.md
@@ -13,19 +13,21 @@ In case this is not legally possible, I grant any entity the right to
use this work for any purpose, without any conditions, unless such
conditions are required by law.
-Prerequisites (or why it probably won’t work on your computer)
---------------------------------------------------------------
+Prerequisites
+-------------
+
+If one of these commands works for you, you can probably run it with
+no trouble:
-If this command works for you, you can probably run it with no
-trouble:
+ sudo apt-get install alsa-oss python-numpy python-pygame
+
+or
sudo apt-get install alsa-oss python-numeric python-pygame
My netbook is running a pretty obsolete version of Linux, so this uses
-the following obsolete interfaces:
-
-- Numeric (the old version of numpy)
-- `/dev/dsp`, the OSS device interface that can be implemented by the
+the obsolete interface
+`/dev/dsp`, the OSS device interface that can be implemented by the
`aoss` command from the alsa-oss package.
It also uses PyGame, the Python interface to SDL.
View
12 pytebeat.py
@@ -5,7 +5,13 @@
# - integer object has no attribute astype
# - improve parse errors
-import sys, wave, os, time, subprocess, pygame, shuntparse, Numeric, sdltextfield
+import sys, wave, os, time, subprocess, pygame, shuntparse, sdltextfield
+
+try:
+ from Numeric import array, arange, UInt8
+except ImportError:
+ from numpy import array, arange, uint8
+ UInt8 = uint8
rate = 8000
@@ -27,7 +33,7 @@ def eval_formula(error, formula):
try:
new_formula = shuntparse.parse(shuntparse.tokenize(formula.text))
- new_formula.eval({'t': Numeric.array(0)})
+ new_formula.eval({'t': array(0)})
current_formula = new_formula
except:
_, exc, _ = sys.exc_info()
@@ -36,7 +42,7 @@ def eval_formula(error, formula):
error.text=''
try:
- rv = current_formula.eval({'t': Numeric.arange(t, t+needed)}).astype(Numeric.UInt8).tostring()
+ rv = current_formula.eval({'t': arange(t, t+needed)}).astype(UInt8).tostring()
t += needed
return rv
except:
View
34 shuntparse.py
@@ -20,7 +20,11 @@
import subprocess
import sys
-import Numeric
+try:
+ from Numeric import where, arange, UInt8
+except ImportError:
+ from numpy import where, arange, uint8
+ UInt8 = uint8
class ParseError(Exception): pass
class MissingOperator(ParseError): pass
@@ -246,23 +250,23 @@ def eval(self, env):
# Integer overflow in multiply.
'*': lambda a, b: (a & (2**15-1)) * (b & (2**15-1)),
# These two have to worry about SIGFPE from division by zero.
- '/': lambda a, b: a / Numeric.where(b == 0, 1, b),
- '%': lambda a, b: a % Numeric.where(b == 0, 1, b),
-
- '&&': lambda a, b: Numeric.where(a, Numeric.where(b, 1, 0), 0),
- '||': lambda a, b: Numeric.where(a, 1, Numeric.where(b, 1, 0)),
- '==': lambda a, b: Numeric.where(a == b, 1, 0),
- '!=': lambda a, b: Numeric.where(a != b, 1, 0),
- '<': lambda a, b: Numeric.where(a < b, 1, 0),
- '>': lambda a, b: Numeric.where(a > b, 1, 0),
- '<=': lambda a, b: Numeric.where(a <= b, 1, 0),
- '>=': lambda a, b: Numeric.where(a >= b, 1, 0),
+ '/': lambda a, b: a / where(b == 0, 1, b),
+ '%': lambda a, b: a % where(b == 0, 1, b),
+
+ '&&': lambda a, b: where(a, where(b, 1, 0), 0),
+ '||': lambda a, b: where(a, 1, where(b, 1, 0)),
+ '==': lambda a, b: where(a == b, 1, 0),
+ '!=': lambda a, b: where(a != b, 1, 0),
+ '<': lambda a, b: where(a < b, 1, 0),
+ '>': lambda a, b: where(a > b, 1, 0),
+ '<=': lambda a, b: where(a <= b, 1, 0),
+ '>=': lambda a, b: where(a >= b, 1, 0),
}
unary_denotations = {
'~': operator.inv,
'-': operator.neg,
- '!': lambda x: Numeric.where(x, 0, 1),
+ '!': lambda x: where(x, 0, 1),
}
def ps(astr):
@@ -286,8 +290,8 @@ def play_bytebeat(astr, out):
print formula
while True:
- x = formula.eval({'t': Numeric.arange(t, t+n_samples)})
- out.write(x.astype(Numeric.UInt8).tostring())
+ x = formula.eval({'t': arange(t, t+n_samples)})
+ out.write(x.astype(UInt8).tostring())
t += n_samples
if __name__ == '__main__':

0 comments on commit 6ad9041

Please sign in to comment.