I am learning how to use JSpeex for a VoIP application that I will be writing later on for educational purposes. As an attempt to understand how to use JSpeex, I decided to write a simple echo application. Basically, it reads the input from the audio-in line, encodes the data (using Speex), then decodes the data, and writes it to the audio-out line. However, when I run the application, all that I hear is static and no voice whatsoever. I have tried to tinker with the audio format, how I initialize the decoder, and encoder, all of which I have had no luck with. Can anybody perhaps look through the code and try to point out what I am doing wrong? Thanks.
Code:
(Is there a better way to paste code? Because when I paste it, highlight it all, and press the Code button, the indentation gets messed up.)
import java.util.Arrays;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import org.xiph.speex.SpeexDecoder;
import org.xiph.speex.SpeexEncoder;
public class SpeexTest {
public static void main(String[] args) throws Exception {
int sample_rate = 8000;
int sample_size = 16;
int channels = 1;
AudioFormat format = new AudioFormat(sample_rate, sample_size,
channels, true, true);
TargetDataLine line_in;
DataLine.Info info_in = new DataLine.Info(TargetDataLine.class, format);
try {
line_in = (TargetDataLine) AudioSystem.getLine(info_in);
line_in.open(format);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
DataLine.Info info_out = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line_out;
try {
line_out = (SourceDataLine) AudioSystem.getLine(info_out);
line_out.open(format);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
SpeexEncoder encoder = new SpeexEncoder();
SpeexDecoder decoder = new SpeexDecoder();
encoder.init(1, 1, sample_rate, channels);
decoder.init(1, sample_rate, channels, false);
int raw_block_size = encoder.getFrameSize() * channels
* (sample_size / 8);
byte[] buffer = new byte[raw_block_size * 2];
line_in.start();
line_out.start();
while (true) {
int read = line_in.read(buffer, 0, raw_block_size);
if (!encoder.processData(buffer, 0, raw_block_size)) {
System.err.println("Could not encode data!");
break;
}
int encoded = encoder.getProcessedData(buffer, 0);
System.out.println(encoded
+ " bytes resulted as a result of encoding " + read
+ " raw bytes.");
byte[] encoded_data = new byte[encoded];
System.arraycopy(buffer, 0, encoded_data, 0, encoded);
decoder.processData(encoded_data, 0, encoded);
byte[] decoded_data = new byte[decoder.getProcessedDataByteSize()];
int decoded = decoder.getProcessedData(decoded_data, 0);
System.out.println(decoded
+ " bytes resulted as a result of decoding " + encoded
+ " encoded bytes.");
line_out.write(decoded_data, 0, decoded);
}
}
}
And here is some output (the same always repeats):
15 bytes resulted as a result of encoding 640 raw bytes.
640 bytes resulted as a result of decoding 15 encoded bytes.
The JSpeex input/output is little-endian. Either swap the bytes before encoding/after decoding, or open the
DataLine
s with