/* * WaveApplet.java * * Created on 2008/06/12, 12:38 */ package info.noip.arton.wave; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.util.Arrays; import javax.sound.sampled.*; import javax.swing.JOptionPane; /** * * @author arton */ public class WaveApplet extends javax.swing.JApplet { static final int RES = 80; static final int RATE = 16000; static final int QUANT_BITS = Short.SIZE; static final int QUANT = 2; boolean mouseListening; short[] wave = new short[RES]; AudioFormat format; /** Initializes the applet WaveApplet */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } Arrays.fill(wave, 1, RES / QUANT - 1, Short.MAX_VALUE); Arrays.fill(wave, RES / QUANT + 1, wave.length - 1, Short.MIN_VALUE); format = new AudioFormat(RATE, QUANT_BITS, 1, true, true); } /** This method is called from within the init() method to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { playButton = new javax.swing.JToggleButton(); waveCanvas = new WaveCanvas(); playButton.setText("Play"); playButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { playButtonActionPerformed(evt); } }); waveCanvas.setBackground(new java.awt.Color(0, 0, 0)); waveCanvas.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { waveCanvasMousePressed(evt); } public void mouseReleased(java.awt.event.MouseEvent evt) { waveCanvasMouseReleased(evt); } }); waveCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { waveCanvasMouseDragged(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(waveCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE) .addComponent(playButton)) .addContainerGap(22, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(playButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(waveCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(130, Short.MAX_VALUE)) ); }// //GEN-END:initComponents byte[] reformat(double dbl) { int cnt = (int)(wave.length * dbl); byte[] buff = new byte[cnt * 2]; for (int i = 0; i < cnt; i++) { int index = (int)(i / dbl); if (index >= wave.length) { index = wave.length - 1; } buff[i * 2] = (byte)(wave[index] >> Byte.SIZE); buff[i * 2 + 1] = (byte)wave[index]; } return buff; } static final double[] PICS = { 1, 1.2, 1.5, 2 }; void play() throws LineUnavailableException { byte[][] buff = new byte[PICS.length][]; for (int i = 0; i < PICS.length; i++) { buff[i] = reformat(PICS[i]); } DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info); line.open(format); try { line.start(); for (int i = 0; i < buff.length; i++) { for (int j = 0; j < (int)(100 / PICS[i]); j++) { line.write(buff[i], 0, buff[i].length); } } line.drain(); line.stop(); } finally { line.close(); } } class WaveCanvas extends Canvas { public void paint(Graphics g) { g.setColor(Color.WHITE); int center_y = getHeight() / 2; g.drawLine(0, center_y, getWidth(), center_y); g.setColor(Color.GREEN); double xoff = getWidth() / (double)wave.length; double yoff = getHeight() / (double)0xffff; int oldy = center_y; for (int i = 1; i < wave.length; i++) { int x = (int)(i * xoff); int y = center_y - (int) (wave[i] * yoff); g.drawLine((int)((i - 1) * xoff), oldy, (int)(i * xoff), y); oldy = y; } } void repoint(int mousex, int mousey) { double xoff = wave.length / (double)getWidth(); double yoff = 0xffff / (double) getHeight(); int mx = (int) (xoff * mousex); if (mx >= 0 && mx < wave.length) { wave[mx] = (short) (Short.MAX_VALUE - mousey * yoff); repaint(); } } } private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playButtonActionPerformed try { play(); } catch (Exception e) { JOptionPane.showMessageDialog(this, e, "error", JOptionPane.OK_OPTION); } playButton.setSelected(false); }//GEN-LAST:event_playButtonActionPerformed private void waveCanvasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_waveCanvasMousePressed mouseListening = true; }//GEN-LAST:event_waveCanvasMousePressed private void waveCanvasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_waveCanvasMouseReleased mouseListening = false; }//GEN-LAST:event_waveCanvasMouseReleased private void waveCanvasMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_waveCanvasMouseDragged if (mouseListening) { ((WaveCanvas)waveCanvas).repoint(evt.getX(), evt.getY()); } }//GEN-LAST:event_waveCanvasMouseDragged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JToggleButton playButton; private java.awt.Canvas waveCanvas; // End of variables declaration//GEN-END:variables }