00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.sound;
00023
00024 import java.io.IOException;
00025 import javax.sound.sampled.AudioFormat;
00026 import javax.sound.sampled.AudioInputStream;
00027 import javax.sound.sampled.AudioSystem;
00028 import javax.sound.sampled.LineUnavailableException;
00029 import javax.sound.sampled.SourceDataLine;
00030 import javax.sound.sampled.UnsupportedAudioFileException;
00031 import org.jetbrains.annotations.NotNull;
00032
00037 public class Processor implements Runnable {
00038
00042 private static final float MIN_VALUE = 1E-3F;
00043
00048 private static final float VOLUME_STEP_PER_SAMPLE = 1.00005F;
00049
00053 @NotNull
00054 private final String name;
00055
00059 @NotNull
00060 private final AudioFileLoader audioFileLoader;
00061
00066 private int state = 0;
00067
00072 private float volume = MIN_VALUE;
00073
00079 public Processor(@NotNull final String name, @NotNull final AudioFileLoader audioFileLoader) {
00080 this.name = name;
00081 this.audioFileLoader = audioFileLoader;
00082 }
00083
00089 public void terminate(final boolean fadeOut) {
00090 state = fadeOut ? 2 : 4;
00091 }
00092
00096 @Override
00097 public void run() {
00098 try {
00099 AudioInputStream audioInputStream = openAudioInputStream();
00100 try {
00101 final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioInputStream.getFormat());
00102 final AudioFormat audioFormat = sourceDataLine.getFormat();
00103
00104 if (audioFormat.getChannels() > 2) {
00105 System.err.println("music "+name+": cannot handle more than two channels");
00106 return;
00107 }
00108 if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
00109 System.err.println("music "+name+": encoding must be PCM_SIGNED");
00110 return;
00111 }
00112 if (audioFormat.getSampleSizeInBits() != 16) {
00113 System.err.println("music "+name+": sample size must be 16 bits");
00114 return;
00115 }
00116 if (audioFormat.isBigEndian()) {
00117 System.err.println("music "+name+": cannot handle little endian encoding");
00118 return;
00119 }
00120
00121 sourceDataLine.open(audioInputStream.getFormat());
00122 try {
00123 sourceDataLine.start();
00124 try {
00125 final byte[] buf = new byte[8192];
00126 while (state < 3 && !Thread.currentThread().isInterrupted()) {
00127 int len = audioInputStream.read(buf, 0, buf.length);
00128 if (len == -1) {
00129 final AudioInputStream newAudioInputStream = openAudioInputStream();
00130 if (!newAudioInputStream.getFormat().matches(audioInputStream.getFormat())) {
00131 newAudioInputStream.close();
00132 System.err.println("music "+name+": file format has changed");
00133 break;
00134 }
00135 final AudioInputStream oldAudioInputStream = audioInputStream;
00136 audioInputStream = newAudioInputStream;
00137 oldAudioInputStream.close();
00138 len = audioInputStream.read(buf, 0, buf.length);
00139 if (len == -1) {
00140 System.err.println("music "+name+": cannot re-read file");
00141 break;
00142 }
00143 }
00144
00145 switch (state) {
00146 case 0:
00147 for (int i = 0; i+3 < len; i += 4) {
00148 volume *= VOLUME_STEP_PER_SAMPLE;
00149 if (volume >= 1F) {
00150 state = 1;
00151 volume = 1F;
00152 break;
00153 }
00154
00155 convertSample(buf, i);
00156 convertSample(buf, i+2);
00157 }
00158 break;
00159
00160 case 1:
00161 break;
00162
00163 case 2:
00164 for (int i = 0; i+3 < len; i += 4) {
00165 volume /= VOLUME_STEP_PER_SAMPLE;
00166 if (volume <= MIN_VALUE) {
00167 state = 3;
00168 len = i;
00169 break;
00170 }
00171
00172 convertSample(buf, i);
00173 convertSample(buf, i+2);
00174 }
00175 break;
00176
00177 default:
00178 throw new AssertionError();
00179 }
00180
00181 sourceDataLine.write(buf, 0, len);
00182 }
00183 if (state != 4) {
00184 sourceDataLine.drain();
00185 }
00186 } finally {
00187 sourceDataLine.stop();
00188 }
00189 } finally {
00190 sourceDataLine.close();
00191 }
00192 } finally {
00193 audioInputStream.close();
00194 }
00195 } catch (final IOException ex) {
00196 System.err.println("music "+name+": "+ex.getMessage());
00197 } catch (final LineUnavailableException ex) {
00198 System.err.println("music "+name+": "+ex.getMessage());
00199 } catch (final UnsupportedAudioFileException ex) {
00200 System.err.println("music "+name+": "+ex.getMessage());
00201 }
00202 }
00203
00209 private void convertSample(@NotNull final byte[] buf, final int i) {
00210 final float value = (short)((buf[i]&0xFF)+(buf[i+1]&0xFF)*0x100)*volume;
00211 final short s = (short)value;
00212 if (s >= 0) {
00213 buf[i] = (byte)s;
00214 buf[i+1] = (byte)(s/0x100);
00215 } else {
00216 buf[i] = (byte)s;
00217 buf[i+1] = (byte)((s+0x10000)/0x100);
00218 }
00219 }
00220
00227 @NotNull
00228 private AudioInputStream openAudioInputStream() throws IOException, UnsupportedAudioFileException {
00229 return AudioSystem.getAudioInputStream(audioFileLoader.getInputStream(null, name));
00230 }
00231
00232 }