35 * @author Kara Kytle 36 * @author Florian Bomers 37 */ 38 public final class Toolkit { 39 40 /** 41 * Suppresses default constructor, ensuring non-instantiability. 42 */ 43 private Toolkit() { 44 } 45 46 /** 47 * Converts bytes from signed to unsigned. 48 */ 49 static void getUnsigned8(byte[] b, int off, int len) { 50 for (int i = off; i < (off+len); i++) { 51 b[i] += 128; 52 } 53 } 54 55 56 /** 57 * Swaps bytes. 58 * @throws ArrayOutOfBoundsException if len is not a multiple of 2. 59 */ 60 static void getByteSwapped(byte[] b, int off, int len) { 61 62 byte tempByte; 63 for (int i = off; i < (off+len); i+=2) { 64 65 tempByte = b[i]; 66 b[i] = b[i+1]; 67 b[i+1] = tempByte; 68 } 69 } 70 71 72 /** 73 * Linear to DB scale conversion. 74 */ 75 static float linearToDB(float linear) { 76 77 float dB = (float) (Math.log(((linear==0.0)?0.0001:linear))/Math.log(10.0) * 20.0); 78 return dB; 79 } 80 81 82 /** 83 * DB to linear scale conversion. 84 */ 85 static float dBToLinear(float dB) { 86 87 float linear = (float) Math.pow(10.0, dB/20.0); 88 return linear; 89 } 90 91 /* 92 * returns bytes aligned to a multiple of blocksize 93 * the return value will be in the range of (bytes-blocksize+1) ... bytes 94 */ 95 static long align(long bytes, int blockSize) { 96 // prevent null pointers 97 if (blockSize <= 1) { 98 return bytes; 99 } 100 return bytes - (bytes % blockSize); 101 } 102 103 static int align(int bytes, int blockSize) { 104 // prevent null pointers 105 if (blockSize <= 1) { 106 return bytes; 107 } 108 return bytes - (bytes % blockSize); 109 } 110 111 112 /* 113 * gets the number of bytes needed to play the specified number of milliseconds 114 */ 115 static long millis2bytes(AudioFormat format, long millis) { 116 long result = (long) (millis * format.getFrameRate() / 1000.0f * format.getFrameSize()); 117 return align(result, format.getFrameSize()); 118 } 119 120 /* 121 * gets the time in milliseconds for the given number of bytes 122 */ 123 static long bytes2millis(AudioFormat format, long bytes) { 124 return (long) (bytes / format.getFrameRate() * 1000.0f / format.getFrameSize()); 125 } 126 127 /* 128 * gets the number of bytes needed to play the specified number of microseconds 129 */ 130 static long micros2bytes(AudioFormat format, long micros) { 131 long result = (long) (micros * format.getFrameRate() / 1000000.0f * format.getFrameSize()); 171 +((format.getSampleRate()==-1)? 172 "NOT_SPECIFIED":String.valueOf(format.getSampleRate()))); 173 } 174 if (format.getSampleSizeInBits() <= 0) { 175 throw new IllegalArgumentException("invalid sample size in bits: " 176 +((format.getSampleSizeInBits()==-1)? 177 "NOT_SPECIFIED":String.valueOf(format.getSampleSizeInBits()))); 178 } 179 if (format.getFrameSize() <= 0) { 180 throw new IllegalArgumentException("invalid frame size: " 181 +((format.getFrameSize()==-1)? 182 "NOT_SPECIFIED":String.valueOf(format.getFrameSize()))); 183 } 184 if (format.getChannels() <= 0) { 185 throw new IllegalArgumentException("invalid number of channels: " 186 +((format.getChannels()==-1)? 187 "NOT_SPECIFIED":String.valueOf(format.getChannels()))); 188 } 189 } 190 191 192 static boolean isFullySpecifiedPCMFormat(AudioFormat format) { 193 if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) 194 && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) { 195 return false; 196 } 197 if ((format.getFrameRate() <= 0) 198 || (format.getSampleRate() <= 0) 199 || (format.getSampleSizeInBits() <= 0) 200 || (format.getFrameSize() <= 0) 201 || (format.getChannels() <= 0)) { 202 return false; 203 } 204 return true; 205 } 206 207 208 public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) { 209 // we can't open the device for non-PCM playback, so we have 210 // convert any other encodings to PCM here (at least we try!) 211 AudioFormat af = ais.getFormat(); 212 213 if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) && 214 (!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) { 215 216 try { 217 AudioFormat newFormat = 218 new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, 219 af.getSampleRate(), 220 16, 221 af.getChannels(), 222 af.getChannels() * 2, 223 af.getSampleRate(), 224 Platform.isBigEndian()); 225 ais = AudioSystem.getAudioInputStream(newFormat, ais); 226 } catch (Exception e) { 227 if (Printer.err) e.printStackTrace(); 228 ais = null; 229 } 230 } 231 232 return ais; 233 } 234 235 } | 35 * @author Kara Kytle 36 * @author Florian Bomers 37 */ 38 public final class Toolkit { 39 40 /** 41 * Suppresses default constructor, ensuring non-instantiability. 42 */ 43 private Toolkit() { 44 } 45 46 /** 47 * Converts bytes from signed to unsigned. 48 */ 49 static void getUnsigned8(byte[] b, int off, int len) { 50 for (int i = off; i < (off+len); i++) { 51 b[i] += 128; 52 } 53 } 54 55 /** 56 * Swaps bytes. 57 * @throws ArrayOutOfBoundsException if len is not a multiple of 2. 58 */ 59 static void getByteSwapped(byte[] b, int off, int len) { 60 61 byte tempByte; 62 for (int i = off; i < (off+len); i+=2) { 63 64 tempByte = b[i]; 65 b[i] = b[i+1]; 66 b[i+1] = tempByte; 67 } 68 } 69 70 /** 71 * Linear to DB scale conversion. 72 */ 73 static float linearToDB(float linear) { 74 75 float dB = (float) (Math.log(((linear==0.0)?0.0001:linear))/Math.log(10.0) * 20.0); 76 return dB; 77 } 78 79 /** 80 * DB to linear scale conversion. 81 */ 82 static float dBToLinear(float dB) { 83 84 float linear = (float) Math.pow(10.0, dB/20.0); 85 return linear; 86 } 87 88 /* 89 * returns bytes aligned to a multiple of blocksize 90 * the return value will be in the range of (bytes-blocksize+1) ... bytes 91 */ 92 static long align(long bytes, int blockSize) { 93 // prevent null pointers 94 if (blockSize <= 1) { 95 return bytes; 96 } 97 return bytes - (bytes % blockSize); 98 } 99 100 static int align(int bytes, int blockSize) { 101 // prevent null pointers 102 if (blockSize <= 1) { 103 return bytes; 104 } 105 return bytes - (bytes % blockSize); 106 } 107 108 /* 109 * gets the number of bytes needed to play the specified number of milliseconds 110 */ 111 static long millis2bytes(AudioFormat format, long millis) { 112 long result = (long) (millis * format.getFrameRate() / 1000.0f * format.getFrameSize()); 113 return align(result, format.getFrameSize()); 114 } 115 116 /* 117 * gets the time in milliseconds for the given number of bytes 118 */ 119 static long bytes2millis(AudioFormat format, long bytes) { 120 return (long) (bytes / format.getFrameRate() * 1000.0f / format.getFrameSize()); 121 } 122 123 /* 124 * gets the number of bytes needed to play the specified number of microseconds 125 */ 126 static long micros2bytes(AudioFormat format, long micros) { 127 long result = (long) (micros * format.getFrameRate() / 1000000.0f * format.getFrameSize()); 167 +((format.getSampleRate()==-1)? 168 "NOT_SPECIFIED":String.valueOf(format.getSampleRate()))); 169 } 170 if (format.getSampleSizeInBits() <= 0) { 171 throw new IllegalArgumentException("invalid sample size in bits: " 172 +((format.getSampleSizeInBits()==-1)? 173 "NOT_SPECIFIED":String.valueOf(format.getSampleSizeInBits()))); 174 } 175 if (format.getFrameSize() <= 0) { 176 throw new IllegalArgumentException("invalid frame size: " 177 +((format.getFrameSize()==-1)? 178 "NOT_SPECIFIED":String.valueOf(format.getFrameSize()))); 179 } 180 if (format.getChannels() <= 0) { 181 throw new IllegalArgumentException("invalid number of channels: " 182 +((format.getChannels()==-1)? 183 "NOT_SPECIFIED":String.valueOf(format.getChannels()))); 184 } 185 } 186 187 static boolean isFullySpecifiedPCMFormat(AudioFormat format) { 188 if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) 189 && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) { 190 return false; 191 } 192 if ((format.getFrameRate() <= 0) 193 || (format.getSampleRate() <= 0) 194 || (format.getSampleSizeInBits() <= 0) 195 || (format.getFrameSize() <= 0) 196 || (format.getChannels() <= 0)) { 197 return false; 198 } 199 return true; 200 } 201 202 public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) { 203 // we can't open the device for non-PCM playback, so we have 204 // convert any other encodings to PCM here (at least we try!) 205 AudioFormat af = ais.getFormat(); 206 207 if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) && 208 (!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) { 209 210 try { 211 AudioFormat newFormat = 212 new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, 213 af.getSampleRate(), 214 16, 215 af.getChannels(), 216 af.getChannels() * 2, 217 af.getSampleRate(), 218 Platform.isBigEndian()); 219 ais = AudioSystem.getAudioInputStream(newFormat, ais); 220 } catch (Exception e) { 221 if (Printer.err) e.printStackTrace(); 222 ais = null; 223 } 224 } 225 226 return ais; 227 } 228 } |