The Artima Developer Community
Sponsored Link

Java Answers Forum
AudioInputStream problems

1 reply on 1 page. Most recent reply: Jun 17, 2002 7:51 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sandu Cosmin

Posts: 1
Nickname: cossandu
Registered: Jun, 2002

AudioInputStream problems Posted: Jun 17, 2002 12:00 PM
Reply to this message Reply
Advertisement
I'm trying to read data from an AudioInputStream. The stream is connected to a microphone.
I want to draw a graphics but I don't know how to get the information from the stream.

I tried this way :
 DataInputStream dis = new DataInputStream(ais)

where ais is my AudioInputStream. Then :
 dis.readShort();

but it doesn't work. How can I convert the byte array I get from the AudioInputStream to a short or a int.
Or maybe in the stream are floats?

Thanks.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: AudioInputStream problems Posted: Jun 17, 2002 7:51 PM
Reply to this message Reply


/* 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}

Flat View: This topic has 1 reply on 1 page
Topic: Program not Working(For Loop) Previous Topic   Next Topic Topic: about security manager in applet/tomcat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use