Total members 11889 |It is currently Fri Mar 29, 2024 6:45 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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!

Code:
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);
             }
           }
         };





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Sound Level Meter
 Sound Player which save Mic sound     -  
 add sound to your animations in flash     -  
 level of conformance in multicast     -  
 high-level thread states     -  
 play a sound when the user enters the area- audio feedback     -  
 file exists in upper level folder link     -  
 Logging based on severity Level : INFO,Warning,ERROR,Debu     -  
 [Java][C#][Skype][WinAPI] simple plugin for Skype, sound red     -  
 different between cfa image and gray level image     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com