/** Re-crunch the numbers to get new wave data. */ public void recalculateBytes() { chan.setValues(vol.getValue(), freq.getValue(), dur.getValue(), eq.getText()); int duration = (int) (dur.getValue() / 1000.0 * sps); myEnviron.put("sps", sps); myEnviron.put("bps", sps); myEnviron.put("dur", duration); myEnviron.put("period", sps / (double) freq.getValue()); myEnviron.put("freq", freq.getValue()); myEnviron.put("vol", vol.getValue()); byte[] ret = new byte[duration]; myEnviron.put("z", ret); String script = "function calc(x) { return " + eq.getText() + "; }"; script += " for (i = 0; i < " + duration + "; i++) z[i] = calc(i); z;"; try { ret = (byte[]) myEnviron.engine.eval(script); } catch (ScriptException e) { System.err.println(e.getMessage()); } // byte[] ret = new byte[duration]; // for (int i = 0; i < ret.length; i++) // { // ret[i] = (byte) (vol.getValue() * Math.sin(i / (sps / (double) freq.getValue()) * 2 * // Math.PI)); // // ret[i] = (byte) eval(eq.getText(),"x=" + i + ", freq=" + freq.getValue() + ", vol=" // // + vol.getValue()); // } for (int i = 0; i < 100 && i < ret.length; i++) System.out.print(ret[i] + ", "); System.out.println(); waveData = ret; if (renderPane != null) { renderPane.renew(); renderPane.updateUI(); } }
/** * Wrap to myEnviron.eval(), for convenience. Evaluates the given expression via our math engine. * * @param expression The expression to evaluate. * @return The value gleaned from the given expression. */ static double eval(String expression) { // FIXME: Recasting from 'Double' to 'double' to 'Double' to 'double'? return (Double) myEnviron.eval(expression); }