1 7 8 package javax.sound.sampled; 9 10 import java.io.InputStream ; 11 import java.io.PushbackInputStream ; 12 import java.io.IOException ; 13 14 15 44 public class AudioInputStream extends InputStream { 45 46 50 private InputStream stream; 51 52 55 protected AudioFormat format; 56 57 60 protected long frameLength; 61 62 65 protected int frameSize; 66 67 70 protected long framePos; 71 72 75 private long markpos; 76 77 82 private byte[] pushBackBuffer = null; 83 84 87 private int pushBackLen = 0; 88 89 92 private byte[] markPushBackBuffer = null; 93 94 97 private int markPushBackLen = 0; 98 99 100 108 public AudioInputStream(InputStream stream, AudioFormat format, long length) { 109 110 super(); 111 112 this.format = format; 113 this.frameLength = length; 114 this.frameSize = format.getFrameSize(); 115 116 if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 119 this.frameSize = 1; 120 } 121 122 this.stream = stream; 123 framePos = 0; 124 markpos = 0; 125 } 126 127 128 135 public AudioInputStream(TargetDataLine line) { 136 137 TargetDataLineInputStream tstream = new TargetDataLineInputStream(line); 138 format = line.getFormat(); 139 frameLength = AudioSystem.NOT_SPECIFIED; 140 frameSize = format.getFrameSize(); 141 142 if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) { 143 frameSize = 1; 144 } 145 this.stream = tstream; 146 framePos = 0; 147 markpos = 0; 148 } 149 150 151 155 public AudioFormat getFormat() { 156 return format; 157 } 158 159 160 164 public long getFrameLength() { 165 return frameLength; 166 } 167 168 169 181 public int read() throws IOException { 182 if( frameSize != 1 ) { 183 throw new IOException ("cannot read a single byte if frame size > 1"); 184 } 185 186 byte[] data = new byte[1]; 187 int temp = read(data); 188 if (temp <= 0) { 189 return -1; 191 } 192 return temp & 0xFF; 193 } 194 195 196 214 public int read(byte[] b) throws IOException { 215 return read(b,0,b.length); 216 } 217 218 219 239 public int read(byte[] b, int off, int len) throws IOException { 240 241 if( (len%frameSize) != 0 ) { 243 len -= (len%frameSize); 244 if (len == 0) { 245 return 0; 246 } 247 } 248 249 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 250 if( framePos >= frameLength ) { 251 return -1; 252 } else { 253 254 if( (len/frameSize) > (frameLength-framePos) ) { 256 len = (int) (frameLength-framePos) * frameSize; 257 } 258 } 259 } 260 261 int bytesRead = 0; 262 int thisOff = off; 263 264 if (pushBackLen > 0 && len >= pushBackLen) { 267 System.arraycopy(pushBackBuffer, 0, 268 b, off, pushBackLen); 269 thisOff += pushBackLen; 270 len -= pushBackLen; 271 bytesRead += pushBackLen; 272 pushBackLen = 0; 273 } 274 275 int thisBytesRead = stream.read(b, thisOff, len); 276 if (thisBytesRead == -1) { 277 return -1; 278 } 279 if (thisBytesRead > 0) { 280 bytesRead += thisBytesRead; 281 } 282 if (bytesRead > 0) { 283 pushBackLen = bytesRead % frameSize; 284 if (pushBackLen > 0) { 285 if (pushBackBuffer == null) { 288 pushBackBuffer = new byte[frameSize]; 289 } 290 System.arraycopy(b, off + bytesRead - pushBackLen, 291 pushBackBuffer, 0, pushBackLen); 292 bytesRead -= pushBackLen; 293 } 294 framePos += bytesRead/frameSize; 296 } 297 return bytesRead; 298 } 299 300 301 310 public long skip(long n) throws IOException { 311 312 if( (n%frameSize) != 0 ) { 314 n -= (n%frameSize); 315 } 316 317 if( frameLength != AudioSystem.NOT_SPECIFIED ) { 318 if( (n/frameSize) > (frameLength-framePos) ) { 320 n = (frameLength-framePos) * frameSize; 321 } 322 } 323 long temp = stream.skip(n); 324 325 if( temp%frameSize != 0 ) { 327 328 throw new IOException ("Could not skip an integer number of frames."); 330 } 331 if( temp >= 0 ) { 332 framePos += temp/frameSize; 333 } 334 return temp; 335 336 } 337 338 339 353 public int available() throws IOException { 354 355 int temp = stream.available(); 356 357 if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) { 359 return (int) (frameLength-framePos) * frameSize; 360 } else { 361 return temp; 362 } 363 } 364 365 366 371 public void close() throws IOException { 372 stream.close(); 373 } 374 375 376 383 384 public void mark(int readlimit) { 385 386 stream.mark(readlimit); 387 if (markSupported()) { 388 markpos = framePos; 389 markPushBackLen = pushBackLen; 391 if (markPushBackLen > 0) { 392 if (markPushBackBuffer == null) { 393 markPushBackBuffer = new byte[frameSize]; 394 } 395 System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen); 396 } 397 } 398 } 399 400 401 408 public void reset() throws IOException { 409 410 stream.reset(); 411 framePos = markpos; 412 pushBackLen = markPushBackLen; 414 if (pushBackLen > 0) { 415 if (pushBackBuffer == null) { 416 pushBackBuffer = new byte[frameSize - 1]; 417 } 418 System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen); 419 } 420 } 421 422 423 431 public boolean markSupported() { 432 433 return stream.markSupported(); 434 } 435 436 437 440 private class TargetDataLineInputStream extends InputStream { 441 442 445 TargetDataLine line; 446 447 448 TargetDataLineInputStream(TargetDataLine line) { 449 super(); 450 this.line = line; 451 } 452 453 454 public int available() throws IOException { 455 return line.available(); 456 } 457 458 public void close() throws IOException { 461 if (line.isActive()) { 464 line.flush(); 465 line.stop(); 466 } 467 line.close(); 468 } 469 470 public int read() throws IOException { 471 472 byte[] b = new byte[1]; 473 474 int value = read(b, 0, 1); 475 476 if (value == -1) { 477 return -1; 478 } 479 480 value = (int)b[0]; 481 482 if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) { 483 value += 128; 484 } 485 486 return value; 487 } 488 489 490 public int read(byte[] b, int off, int len) throws IOException { 491 try { 492 return line.read(b, off, len); 493 } catch (IllegalArgumentException e) { 494 throw new IOException (e.getMessage()); 495 } 496 } 497 } 498 } 499 | Popular Tags |