D
Doug Miller
I'm trying to use the Java Sound API to get samples from the microphone.
When I start the app or reset it, I'd expect it to have no samples ready and
get new samples at the sample rate. Instead, it seems to have the number of
samples in the ByteArrayInputStream and never get new samples. What do I
need to do different?
Typical output:
av = 8000
av = 8000
av = 8000
av = 8000
av = 8000
av = 8000
av = 6400
av = 4800
av = 3200
av = 1600
frames = 10
av = 0
Source code:
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFileFormat;
public class AudioSource
extends Thread
{
private AudioInputStream m_audioInputStream;
public AudioSource(InputStream stream, AudioFormat format, long length)
{
m_audioInputStream = new AudioInputStream(stream, format, length);
}
public void read(byte[] buf) {
try {
m_audioInputStream.read(buf);
}
catch (Exception e) {
System.out.println(e);
}
}
public int available() throws IOException {
int retval = m_audioInputStream.available();
return retval;
}
public static void main(String[] args)
{
byte[] buf;
buf = new byte[8000];
byte[] outbuf;
outbuf = new byte[1600]; // 1/10th of a second buffer
long frames = 0;
/* PCM, 8 kSample/sec, mono, frameSize = 2 Bytes, LITTLE_ENDIAN */
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
8000.0F, 16, 1, 2, 8000.0F, false);
ByteArrayInputStream MicrophoneStream = new ByteArrayInputStream(buf);
AudioSource microphone = new AudioSource( MicrophoneStream, audioFormat,
8000);
MicrophoneStream.reset();
try {
for(;frames < 200
{
// Wait until 1/10 of a second of data is ready
System.out.println("av = " + microphone.available());
while (microphone.available() < 800) {
Thread.sleep(1);
}
microphone.read(outbuf);
MicrophoneStream.reset();
frames += 1;
if (frames%10 == 0) {
System.out.println("frames = " + frames);
}
MicrophoneStream.close();
} // end for
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Done");
}
}
When I start the app or reset it, I'd expect it to have no samples ready and
get new samples at the sample rate. Instead, it seems to have the number of
samples in the ByteArrayInputStream and never get new samples. What do I
need to do different?
Typical output:
av = 8000
av = 8000
av = 8000
av = 8000
av = 8000
av = 8000
av = 6400
av = 4800
av = 3200
av = 1600
frames = 10
av = 0
Source code:
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFileFormat;
public class AudioSource
extends Thread
{
private AudioInputStream m_audioInputStream;
public AudioSource(InputStream stream, AudioFormat format, long length)
{
m_audioInputStream = new AudioInputStream(stream, format, length);
}
public void read(byte[] buf) {
try {
m_audioInputStream.read(buf);
}
catch (Exception e) {
System.out.println(e);
}
}
public int available() throws IOException {
int retval = m_audioInputStream.available();
return retval;
}
public static void main(String[] args)
{
byte[] buf;
buf = new byte[8000];
byte[] outbuf;
outbuf = new byte[1600]; // 1/10th of a second buffer
long frames = 0;
/* PCM, 8 kSample/sec, mono, frameSize = 2 Bytes, LITTLE_ENDIAN */
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
8000.0F, 16, 1, 2, 8000.0F, false);
ByteArrayInputStream MicrophoneStream = new ByteArrayInputStream(buf);
AudioSource microphone = new AudioSource( MicrophoneStream, audioFormat,
8000);
MicrophoneStream.reset();
try {
for(;frames < 200
// Wait until 1/10 of a second of data is ready
System.out.println("av = " + microphone.available());
while (microphone.available() < 800) {
Thread.sleep(1);
}
microphone.read(outbuf);
MicrophoneStream.reset();
frames += 1;
if (frames%10 == 0) {
System.out.println("frames = " + frames);
}
MicrophoneStream.close();
} // end for
}
catch (Exception e) {
System.out.println(e);
}
System.out.println("Done");
}
}