SourceDataLine returns wierd sound

J

jacksuyu

I read one wav file, and could play with Clip. But when I tried to use
SourceDataLine, I got wierd sound back, basically the sound is very
quick, 15 sec sound finishes in 1 sec or so.



Also, another question, how to get the pure sound data? the byte array
I read from wav file contains header and other meta information. Is it
possible to seperate wav files's meta data and real sound data?

Thank you.


Here is the code:

import javax.sound.sampled.*;
import javax.sound.sampled.Mixer.*;
import javax.sound.sampled.spi.*;
import java.io.*;
import java.util.*;

public class A {
public static void main(String args[]) throws Exception {
Info[] infos = AudioSystem.getMixerInfo();

Info info = null;
for (int i = 0; i < infos.length; i++) {
if (infos.getName().equals("Java Sound Audio Engine")) {
info = infos;
}
}
Mixer mixer = AudioSystem.getMixer(info);

Line.Info lInfos[] = mixer.getSourceLineInfo();
Line clipLine = null;
for (int i = 0; i < lInfos.length; i++) {
if
(lInfos.getLineClass().toString().indexOf("javax.sound.sampled.SourceDataLine")
clipLine = mixer.getLine(lInfos);
}
}

AudioFileReader a = getReader();
File f = new File("a.wav");
AudioFileFormat fmt = a.getAudioFileFormat(f);
AudioFormat afmt = fmt.getFormat();

FileInputStream fIn = new FileInputStream(f);
AudioInputStream fiFmt = new AudioInputStream(new FileInputStream(f),
afmt, f.length());
Date d = new Date();
SourceDataLine sLine = ((SourceDataLine)clipLine);
sLine.open(afmt);

byte buf[] = new byte[2048];
int n ;
sLine.start();

while (( n = fIn.read(buf)) > 0) {
sLine.write(buf, 0, n);
sLine.drain();
}
sLine.stop();
}


static AudioFileReader getReader() {
Iterator ps = sun.misc.Service.providers(AudioFileReader.class);
AudioFileReader mi = null;

while (ps.hasNext()) {
mi = (AudioFileReader)ps.next();
System.out.println(mi);
}

return mi;
}
}
 
O

Oliver Wong

I read one wav file, and could play with Clip. But when I tried to use
SourceDataLine, I got wierd sound back, basically the sound is very
quick, 15 sec sound finishes in 1 sec or so.

Could it be that you've got the sampling rate wrong? A 15 to 1 ratio is
surprising to me though. If your original sound was at 8Khz (telephone
quality), and you played it at 96Khz (DVD-Audio quality), you'd only have a
12 to 1 ratio.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top