Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Detecting sound from microphone using Java. Issue specific to Java Versions

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Detecting sound from microphone using Java. Issue specific to Java Versions

    Hello there!

    I'm running this simple code that listens to my microphone and prints out bytes.

    Problem:
    It listens to the microphone for any changes correctly on
    java version "1.6.0_43"

    but it doesn't listen to the microphone for any changes correctly on
    java version "1.7.0_25"

    Both version compile and run fine.

    Example Output using Java 1.6
    5172 1
    5196 2
    5103 3
    2859 4
    2549 5
    2742 6
    3112 7
    5028 8
    4515 9
    3856 10
    5189 11
    5080 12
    5165 13
    4925 14

    When it drops down to around 2500 that's when there was sound going into the microphone.

    Example Output using Java 1.7
    5652 1
    5655 2
    5699 3
    6081 4
    6213 5
    5972 6
    5907 7
    5828 8
    5931 9
    6187 10
    6015 11
    5949 12
    6196 13

    It no longer detects any noise from the microphone.


    import java.io.*;
    import javax.sound.sampled.*;
     
    public class SpeechDetectionTest {
     
        public static void main(String[] args) {
     
            ByteArrayOutputStream byteArrayOutputStream;
            TargetDataLine targetDataLine;
            int cnt;
            boolean stopCapture = false;
            byte tempBuffer[] = new byte[8000];
            int countzero, countdownTimer;   
            short convert[] = new short[tempBuffer.length];
     
     
     
            try {
                byteArrayOutputStream = new ByteArrayOutputStream();
                stopCapture = false;
                countdownTimer = 0;
                while (!stopCapture) {
                    AudioFormat audioFormat = new AudioFormat(8000.0F, 16, 1, true, false);
                    DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
                    targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
                    targetDataLine.open(audioFormat);
                    targetDataLine.start();
                    cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
                    byteArrayOutputStream.write(tempBuffer, 0, cnt);
                    try {
                        countzero = 0;
                        for (int i = 0; i < tempBuffer.length; i++) {                                    
                            convert[i] = tempBuffer[i];
                            if (convert[i] == 0) {
                                countzero++;
                            }
                        }
     
                        countdownTimer++;
                        System.out.println(countzero + " " + countdownTimer);
     
                    } catch (StringIndexOutOfBoundsException e) {
                        System.out.println(e.getMessage());
                    }
                    Thread.sleep(0);
                    targetDataLine.close();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

    Any ideas?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Detecting sound from microphone using Java. Issue specific to Java Versions


  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Detecting sound from microphone using Java. Issue specific to Java Versions

    Quote Originally Posted by copeg View Post
    I also posted on these forums:

    Detecting Sound From Microphone Using Java. Issue With Java Versions - Java | Dream.In.Code

    Detecting sound from microphone using Java (I/O and Streams forum at JavaRanch)

    One guy mentioned it's a bug?

    I just want it to work on 1.7, sorry I mega cross posted.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Detecting sound from microphone using Java. Issue specific to Java Versions

    Solved,

    The issue wasn't with Java versions, I'm not sure what went wrong but it works fine after I reinstalled my audio drivers.

    Thanks.

Similar Threads

  1. Filtering microphone sound
    By badoumba in forum Java Theory & Questions
    Replies: 0
    Last Post: April 28th, 2012, 08:02 AM
  2. Replies: 0
    Last Post: January 12th, 2012, 03:54 PM
  3. Cannot get Java to play a sound...
    By JumpingUpAndDown in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2011, 09:35 AM
  4. Java sound API
    By bondage in forum Java SE APIs
    Replies: 3
    Last Post: July 22nd, 2010, 01:01 PM
  5. playing sound using java
    By sdhilipan89 in forum Java Applets
    Replies: 6
    Last Post: March 25th, 2010, 08:59 AM