/** 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(); } }
/** * Construct with a reference to a channel to represent. * * @param chan The channel whose waveform we are manipulating. */ public OscillatorPane(Channel chan) { this.chan = chan; setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(new JLabel("Volume:")); add(vol = new JSpinSlide(0, 127, chan.vol)); add(new JLabel("Frequency:")); add(freq = new JSpinSlide(0, sps / 4, chan.freq)); add(new JLabel("Duration (ms):")); add(dur = new JSpinSlide(0, 60000, chan.dur)); add(new JLabel("Wave Equasion:")); add(eq = new JTextField(chan.eq)); add(play = new JButton("Play")); setMaximumSize(new Dimension(200, Integer.MAX_VALUE)); vol.setMajorTicks(true, 8); vol.addChangeListener(this); freq.setMajorTicks(true, 100); freq.addChangeListener(this); dur.setMajorTicks(true, 2000); dur.addChangeListener(this); play.addActionListener(this); recalculateBytes(); }