Creating stereo sound and panning the sound
Im reading a book on developing games in Java (Developing Games in Java, by David Brackeen),but he does not explain how to make stereo sound and how to make panning between left and right speaker.
I have this SimpleSoundPlayer from the book:
publicclass SimpleSoundPlayer{
private AudioFormat format;
privatebyte[] samples;
publicstaticvoid main(String[] args){
SimpleSoundPlayer sp =new SimpleSoundPlayer("sounds/ch04/voice.wav");
InputStream is =new ByteArrayInputStream(sp.getSamples());
sp.play(is);
System.exit(0);
}
public SimpleSoundPlayer(String fileName){
try{
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileName));
format = ais.getFormat();
//format = new AudioFormat(format.getSampleRate(), format.getSampleSizeInBits(), 2, true, format.isBigEndian());
samples = getSamples(ais);
}catch(UnsupportedAudioFileException uafe){
uafe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
publicbyte[] getSamples(){
return samples;
}
publicbyte[] getSamples(AudioInputStream ais){
int length = (int)(ais.getFrameLength()*format.getFrameSize());
byte[] samples =newbyte[length];
DataInputStream dis =new DataInputStream(ais);
try{
dis.readFully(samples);
}catch(IOException ioe){
ioe.printStackTrace();
}
return samples;
}
publicvoid play(InputStream is){
//Use short 100ms (1/10th sec) buffer for real-time change to sound stream
int bufferSize = format.getFrameSize()*Math.round(format.getSampleRate()/10);
byte[] buffer =newbyte[bufferSize];
SourceDataLine line;
try{
DataLine.Info info =new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}catch(LineUnavailableException luae){
luae.printStackTrace();
return;
}
line.start();
//Copy data to line
try{
int numBytesRead = 0;
while(numBytesRead != -1){
numBytesRead = is.read(buffer, 0, buffer.length);
if(numBytesRead != -1){
line.write(buffer, 0, numBytesRead);
}
}
}catch(IOException ioe){
ioe.printStackTrace();
}
line.drain();
line.close();
}
}
As you can see I have commented out a line where I have experimented with creating stereo sound. However I do not know how I make data in the wav file become stereo, and how do I change the amplitude for the left/right speaker?
I have searched the net, but have failed to find anything that I could understand... Could you guys maybe help me out here, and explain this to me? I mean, this class reads the wav as a AudioInputStream from a wav file, how would I then "split" this file up to be stereo, and then adjust the different channels induvidually?
Best regards
S鴕en

