Click here to Skip to main content
15,887,214 members
Home / Discussions / Java
   

Java

 
AnswerRe: Good books on JDBC and Java Swing? Pin
TorstenH.12-Dec-13 3:08
TorstenH.12-Dec-13 3:08 
AnswerRe: Good books on JDBC and Java Swing? Pin
M Riaz Bashir15-Dec-13 1:38
M Riaz Bashir15-Dec-13 1:38 
QuestionJava Code to Print JFrame or JPannel Form Contents Pin
Cyber129-Dec-13 21:41
Cyber129-Dec-13 21:41 
AnswerRe: Java Code to Print JFrame or JPannel Form Contents Pin
Richard MacCutchan9-Dec-13 22:30
mveRichard MacCutchan9-Dec-13 22:30 
GeneralRe: Java Code to Print JFrame or JPannel Form Contents Pin
Cyber1213-Dec-13 23:17
Cyber1213-Dec-13 23:17 
GeneralRe: Java Code to Print JFrame or JPannel Form Contents Pin
Richard MacCutchan13-Dec-13 23:30
mveRichard MacCutchan13-Dec-13 23:30 
GeneralRe: Java Code to Print JFrame or JPannel Form Contents Pin
Cyber1214-Dec-13 20:24
Cyber1214-Dec-13 20:24 
QuestionAudio Lever Meter Pin
xchris009-Dec-13 0:27
xchris009-Dec-13 0:27 
Hello

I'm new to programming and I'm trying to make a java application that will "hear" (not record necessarily) the sound and display how loud is.I'm thinking of converting the sound recordings to numbers,so I can see the difference on the sound levels.I got this code and I added the "getLevel()" method,which returns the amplitude of the current recording,but it's returning -1 everytime.I guess I'm not using it properly. Any ideas how I must call this method?I have to deliver my project in a week,so any help will be much appreciated!

Java
public class Capture extends JFrame {

	  protected boolean running;
	  ByteArrayOutputStream out;

	  public Capture() {
	    super("Capture Sound Demo");
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
	    Container content = getContentPane();

	    final JButton capture = new JButton("Capture");
	    final JButton stop = new JButton("Stop");
	    final JButton play = new JButton("Play");

	    capture.setEnabled(true);
	    stop.setEnabled(false);
	    play.setEnabled(false);

	    ActionListener captureListener = 
	        new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        capture.setEnabled(false);
	        stop.setEnabled(true);
	        play.setEnabled(false);
	        captureAudio();
	      }
	    };
	    capture.addActionListener(captureListener);
	    content.add(capture, BorderLayout.NORTH);

	    ActionListener stopListener = 
	        new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        capture.setEnabled(true);
	        stop.setEnabled(false);
	        play.setEnabled(true);
	        running = false;
	      }
	    };
	    stop.addActionListener(stopListener);
	    content.add(stop, BorderLayout.CENTER);

	    ActionListener playListener = 
	        new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        playAudio();
	      }
	    };
	    play.addActionListener(playListener);
	    content.add(play, BorderLayout.SOUTH);
	  }

	  private void captureAudio() {
	    try {
	      final AudioFormat format = getFormat();
	      DataLine.Info info = new DataLine.Info(
	        TargetDataLine.class, format);
	      final TargetDataLine line = (TargetDataLine)
	        AudioSystem.getLine(info);
	      line.open(format);
	      line.start();
	      
	      Runnable runner = new Runnable() {
	        int bufferSize = (int)format.getSampleRate() 
	          * format.getFrameSize();
	        byte buffer[] = new byte[bufferSize];
	 
	        public void run() {
	          out = new ByteArrayOutputStream();
	          running = true;
	          try {
	            while (running) {
	              int count = 
	                line.read(buffer, 0, buffer.length);
	              if (count > 0) {
	                out.write(buffer, 0, count);
	               
	                System.out.println(line.getLevel());  // |-this is what i added-|
	              }
	            }
	            out.close();
	          } catch (IOException e) {
	            System.err.println("I/O problems: " + e);
	            System.exit(-1);
	          }
	        }
	      };
	      Thread captureThread = new Thread(runner);
	      captureThread.start();
	    } catch (LineUnavailableException e) {
	      System.err.println("Line unavailable: " + e);
	      System.exit(-2);
	    }
	  }

	  private void playAudio() {
	    try {
	      byte audio[] = out.toByteArray();
	      InputStream input = 
	        new ByteArrayInputStream(audio);
	      final AudioFormat format = getFormat();
	      final AudioInputStream ais = 
	        new AudioInputStream(input, format, 
	        audio.length / format.getFrameSize());
	      DataLine.Info info = new DataLine.Info(
	        SourceDataLine.class, format);
	      final SourceDataLine line = (SourceDataLine)
	        AudioSystem.getLine(info);
	      line.open(format);
	      line.start();
	      
	      Runnable runner = new Runnable() {
	        int bufferSize = (int) format.getSampleRate() 
	          * format.getFrameSize();
	        byte buffer[] = new byte[bufferSize];
	 
	        public void run() {
	          try {
	            int count;
	            while ((count = ais.read(
	                buffer, 0, buffer.length)) != -1) {
	              if (count > 0) {
	                line.write(buffer, 0, count);
	              }
	            }
	            
	            line.drain();
	            line.close();
	            
	          } catch (IOException e) {
	            System.err.println("I/O problems: " + e);
	            System.exit(-3);
	          }
	        }
	      };

      Thread playThread = new Thread(runner);
	      playThread.start();
	    } catch (LineUnavailableException e) {
	      System.err.println("Line unavailable: " + e);
	      System.exit(-4);
	    } 
	  }

	  private AudioFormat getFormat() {
	    float sampleRate = 8000;
	    int sampleSizeInBits = 8;
	    int channels = 1;
	    boolean signed = true;
	    boolean bigEndian = true;
	    return new AudioFormat(sampleRate, 
	      sampleSizeInBits, channels, signed, bigEndian);
	  }
	  
	  @SuppressWarnings("deprecation")
	public static void main(String args[]) {
	    JFrame frame = new Capture();
	    frame.pack();
	    frame.show();
	  }	  
}


modified 9-Dec-13 6:47am.

AnswerRe: Audio Lever Meter Pin
Richard MacCutchan9-Dec-13 2:10
mveRichard MacCutchan9-Dec-13 2:10 
GeneralRe: Audio Lever Meter Pin
xchris009-Dec-13 2:15
xchris009-Dec-13 2:15 
GeneralRe: Audio Lever Meter Pin
Richard MacCutchan9-Dec-13 2:20
mveRichard MacCutchan9-Dec-13 2:20 
GeneralRe: Audio Lever Meter Pin
xchris009-Dec-13 2:26
xchris009-Dec-13 2:26 
GeneralRe: Audio Lever Meter Pin
Richard MacCutchan9-Dec-13 2:47
mveRichard MacCutchan9-Dec-13 2:47 
GeneralRe: Audio Lever Meter Pin
xchris009-Dec-13 2:58
xchris009-Dec-13 2:58 
AnswerRe: Audio Lever Meter Pin
xchris0017-Dec-13 2:30
xchris0017-Dec-13 2:30 
Questionretrieving path from Mysql database to draw bezier curve on JFrame Pin
f268-Dec-13 21:10
f268-Dec-13 21:10 
AnswerRe: retrieving path from Mysql database to draw bezier curve on JFrame Pin
Richard MacCutchan8-Dec-13 22:32
mveRichard MacCutchan8-Dec-13 22:32 
Questionjava string Pin
Trupti0017-Dec-13 1:32
Trupti0017-Dec-13 1:32 
AnswerRe: java string Pin
jschell7-Dec-13 10:49
jschell7-Dec-13 10:49 
AnswerRe: java string Pin
Peter_in_27807-Dec-13 22:44
professionalPeter_in_27807-Dec-13 22:44 
Questioncode to develop main window in auto mobile spare parts Pin
Member 104492436-Dec-13 0:49
Member 104492436-Dec-13 0:49 
AnswerRe: code to develop main window in auto mobile spare parts Pin
Richard MacCutchan6-Dec-13 2:12
mveRichard MacCutchan6-Dec-13 2:12 
AnswerRe: code to develop main window in auto mobile spare parts Pin
jschell6-Dec-13 12:14
jschell6-Dec-13 12:14 
Generalcareer after bcs Pin
Member 104492436-Dec-13 0:48
Member 104492436-Dec-13 0:48 
GeneralRe: career after bcs Pin
Richard MacCutchan6-Dec-13 2:11
mveRichard MacCutchan6-Dec-13 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.