#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    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 on 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.


    [code=java]
    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);
    }
    }
    }

    [/code]

    Any ideas?

    Also posted on these forums, but no luck =(

    http://www.coderanch.com/t/615528//java/Detecting-sound-microphone-Java-Java#2811088

    http://www.javaprogrammingforums.com/file-i-o-other-i-o-streams/30388-detecting-sound-microphone-using-java-issue-specific-java-versions.html#post117343

    http://www.java-forums.org/new-java/79985-detecting-sound-microphone-using-java-issue-java-versions.html#post346338

    http://www.dreamincode.net/forums/topic/324610-detecting-sound-from-microphone-using-java-issue-with-java-versions/
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Hey dude,

    I tested it with my Microphone and it seems to be working fine with 1.6 and 1.7. The output didn't change much between versions of Java.

    Output
    1942 1
    1974 2
    2004 3
    1896 4
    1173 5
    713 6
    1421 7
    1793 8
    1934 9
    1267 10
    1560 11
    1397 12
    1468 13

IMN logo majestic logo threadwatch logo seochat tools logo