2 Replies - 829 Views - Last Post: 11 July 2013 - 01:07 AM Rate Topic: -----

#1 ssbattousai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-July 13

Detecting sound from microphone using Java. Issue with Java Versions

Posted 10 July 2013 - 02:07 PM

Hello there!

I'm running this simple code that simply 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?

Is This A Good Question/Topic? 0
  • +

Replies To: Detecting sound from microphone using Java. Issue with Java Versions

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

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

Posted 10 July 2013 - 03:19 PM

Yeah, use and package your app with the version that works. Maybe it's a bug in newer versions. File bug report.
Was This Post Helpful? 0
  • +
  • -

#3 ssbattousai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-July 13

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

Posted 11 July 2013 - 01:07 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1