Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: AudioInputStream problems
|
Posted: Jun 17, 2002 7:51 PM
|
|
/* Visualize.java * @author: Charles Bell * @version June 17, 2002 */
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.sound.sampled.*; import javax.swing.*;
public class Visualize extends JFrame implements ActionListener, Runnable{ private Thread thread; private AudioInputStream ais; private float samplerate = 11025.0f; private float framerate = 11025.0f; private int buffersize = 16384; private JLabel imageLabel; private boolean debug = false; public Visualize(){ super("Visualize"); init(); } public static void main(String[] args){ new Visualize(); } public void init(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton startButton = new JButton("Start"); JButton stopButton = new JButton("Stop"); JButton exitButton = new JButton("Exit"); startButton.addActionListener(this); stopButton.addActionListener(this); exitButton.addActionListener(this); JPanel controlPanel = new JPanel(); controlPanel.add(startButton); controlPanel.add(stopButton); controlPanel.add(exitButton); getContentPane().add(controlPanel,BorderLayout.NORTH); JPanel imagePanel = new JPanel(); imageLabel = new JLabel(); imageLabel.setPreferredSize(new Dimension(344,344));
imagePanel.add(imageLabel); getContentPane().add(imagePanel, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); show(); }
public void actionPerformed(ActionEvent actionevent){ String command = actionevent.getActionCommand(); if (command.compareTo("Start")==0){ start(); }else if (command.compareTo("Stop")==0){ stop(); }else if (command.compareTo("Exit")==0){ if (thread != null) stop(); System.exit(0); } } public void start() { thread = new Thread(this); thread.start(); }
public void stop() { thread = null; }
public void run(){ record(); } public void record() { TargetDataLine line = null; // define the required attributes for our line, // and make sure a compatible line is supported. AudioFormat audioformat = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED, samplerate, 8, 1, 1, framerate,false); DataLine.Info info = new DataLine.Info(TargetDataLine.class,audioformat); if (!AudioSystem.isLineSupported(info)) { System.err.println("Line matching " + info + " not supported."); System.exit(0); } // get and open the target data line for capture. try{ line = (TargetDataLine) AudioSystem.getLine(info); line.open(audioformat, line.getBufferSize()); }catch (LineUnavailableException lune) { System.err.println("Unable to open the line: " + lune.getMessage()); System.exit(0); }catch (SecurityException se) { System.err.println("SecurityException: " + se.toString()); System.exit(0); }catch(Exception ex) { System.err.println("Exception: " + ex.getMessage()); System.exit(0); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); int frameSizeInBytes = audioformat.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data = new byte[bufferLengthInBytes]; int bytenumber = 0; line.start(); if (debug) System.out.println("Started Recording"); int row = 0; int width = bufferLengthInBytes/2; imageLabel.setPreferredSize(new Dimension(width,width)); pack(); setLocationRelativeTo(null);
int pix[] = new int[width * width]; while (thread != null) { if ((bytenumber = line.read(data, 0, bufferLengthInBytes)) == -1) { break; } baos.write(data, 0, bytenumber); if (debug) System.out.println("Recorded " + data.length + " bytes." + " Row = " + row);
for (int i = 0; i < data.length; i++) { int index = i + row*width; if (index >= pix.length) index = pix.length - 1; pix[index] = (int) data[i]; } row++; if (row >= width) row = 0; Image img = createImage(new MemoryImageSource(width, width, pix, 0, width)); imageLabel.setIcon(new ImageIcon(img)); } if (debug) System.out.println("Stopped Recording"); //stop and close the line line.stop(); line.close(); line = null; // stop and close the output stream try{ baos.flush(); baos.close(); }catch (IOException ioe) { System.err.println(ioe.getMessage()); } } } [/pre}
|
|