I am a beginner in java Sound API and read many things in oracle and richard brawlin and try some projects with his programms. The first i want to capture is the sound and downsample it and give in a right audioformat back. Then i will try the same but without recording.
First I'll show you important parts of the code then i will tell you my problem:
here i record the sound with my micophone:
stopCapture = false;
try{
int i = 0;
System.out.println("Start");
//Schleife um daten aufzunehmen
while(!stopCapture){
//daten vom targetDataLine lesen
int cnt = targetDataLine.read(tempBuffer,0,tempBuffer.length);
if(cnt > 0){//Jeder 5. Wert wird übernommen
//Die daten in der bytearrayoutputstream speichern
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
Then I Convert the byteArrayOutputStream in a Bytearray audioData and try to "downsample" my recorded audio with the samplerate 48000/5=9800. In other way it's every 5th value of my bytearray audioData. Then i want to sinc interpolate it back to the original samplerate 48000. See code:
public void run(){
try{
int cnt;
int n = 0;
int k = 0;
int m = 0;
double summe = 0;
ByteArrayOutputStream aufnahme_2 = new ByteArrayOutputStream();
System.out.println("Replay");
//Schleife soll solange laufen bis die letzte Datei abgespielt wurde.
//Am ende gibt die Datei -1 raus
Downsampling 5==> Jeder 5. Wert wird übernommen
for(; m<= audioData.length;m++) {
if ( m%5 < 0.000001 & m != 0) {
k++;
n=k;
}
for(;n<=5+k;n++) {
if(n*5 < audioData.length) {
if(Math.abs((double) m/5-n) <0.00001) {
summe = summe+ audioData[n*5];
}
else {
summe = summe + audioData[n*5]*Math.sin(Math.PI*((double) m/5-n))/( Math.PI*((double) m/5-n)); //Der double cast muss sein, damit die zahl als double und nicht als int gerechnet wird
}
}
}
//byteBuffer.putShort((short) summe);
aufnahme_2.write((short) summe); //Short weil der Datentyp short 2Byte große ist
summe = 0;
n=k;
}
ergebnis = aufnahme_2.toByteArray();
InputStream byteArrayInputStream_down = new ByteArrayInputStream(ergebnis);
audioInputStream = new AudioInputStream(byteArrayInputStream_down, audioFormat,ergebnis.length);
while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length))!=-1) {
if(cnt > 0){
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
My Audioformat:
private AudioFormat getAudioFormat(){
float sampleRate = 48000;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
With this audioformat i recieve a very bad quality of a sound. When i change the sampleSizeInBits in 8 the sound is clearer why? I want to get a resolution of 16 bits. I tried to safe my sinc values in integer, byte .... format and nothing helps. So i hope that someone can help and tell me the reason why is doesn't work.
PS: I send a picture that i made in Matlab to show u my aim: The blue line is my audioinput and the black one is downsampled reconstructed audio output