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")
}
}
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;
}
}
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);= 0) {
}
}
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;
}
}