Results 1 to 5 of 5
  1. #1
    ssbattousai is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Detecting sound from microphone using Java. Issue with 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"

    Code compiles and runs fine in both versions.

    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.


    Java Code:
    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
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

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


  3. #3
    ssbattousai is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

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

    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
    ssbattousai is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Re: Detecting sound from microphone using Java. Issue with 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.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,655
    Rep Power
    5

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

    Quote Originally Posted by ssbattousai View Post
    One guy mentioned it's a bug?
    You can't selectively leave out words to change what people say. One guy said that MAYBE it is a bug and that is an incredibly pointless thing to say without some form of proof. Of course maybe it is a bug, and maybe not.

Similar Threads

  1. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:02 PM
  2. Java Sound recording audio file's extension issue
    By Shareb in forum Advanced Java
    Replies: 0
    Last Post: 12-04-2010, 11:42 AM
  3. Replies: 2
    Last Post: 12-01-2010, 02:16 PM
  4. different versions of java
    By rajinder5 in forum Advanced Java
    Replies: 3
    Last Post: 10-31-2010, 03:50 PM
  5. Using different versions of Java
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •