1 7 8 package javax.sound.sampled; 9 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 107 public class AudioFormat { 108 109 111 112 115 protected Encoding encoding; 116 117 120 protected float sampleRate; 121 122 125 protected int sampleSizeInBits; 126 127 130 protected int channels; 131 132 135 protected int frameSize; 136 137 140 protected float frameRate; 141 142 145 protected boolean bigEndian; 146 147 148 149 private HashMap <String , Object > properties; 150 151 152 167 public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits, 168 int channels, int frameSize, float frameRate, boolean bigEndian) { 169 170 this.encoding = encoding; 171 this.sampleRate = sampleRate; 172 this.sampleSizeInBits = sampleSizeInBits; 173 this.channels = channels; 174 this.frameSize = frameSize; 175 this.frameRate = frameRate; 176 this.bigEndian = bigEndian; 177 this.properties = null; 178 } 179 180 181 201 public AudioFormat(Encoding encoding, float sampleRate, 202 int sampleSizeInBits, int channels, 203 int frameSize, float frameRate, 204 boolean bigEndian, Map <String , Object > properties) { 205 this(encoding, sampleRate, sampleSizeInBits, channels, 206 frameSize, frameRate, bigEndian); 207 this.properties = new HashMap <String , Object >(properties); 208 } 209 210 211 225 public AudioFormat(float sampleRate, int sampleSizeInBits, 226 int channels, boolean signed, boolean bigEndian) { 227 228 this((signed == true ? Encoding.PCM_SIGNED : Encoding.PCM_UNSIGNED), 229 sampleRate, 230 sampleSizeInBits, 231 channels, 232 (channels == AudioSystem.NOT_SPECIFIED || sampleSizeInBits == AudioSystem.NOT_SPECIFIED)? 233 AudioSystem.NOT_SPECIFIED: 234 ((sampleSizeInBits + 7) / 8) * channels, 235 sampleRate, 236 bigEndian); 237 } 238 239 248 public Encoding getEncoding() { 249 250 return encoding; 251 } 252 253 270 public float getSampleRate() { 271 272 return sampleRate; 273 } 274 275 292 public int getSampleSizeInBits() { 293 294 return sampleSizeInBits; 295 } 296 297 310 public int getChannels() { 311 312 return channels; 313 } 314 315 330 public int getFrameSize() { 331 332 return frameSize; 333 } 334 335 350 public float getFrameRate() { 351 352 return frameRate; 353 } 354 355 356 363 public boolean isBigEndian() { 364 365 return bigEndian; 366 } 367 368 369 381 public Map <String ,Object > properties() { 382 Map <String ,Object > ret; 383 if (properties == null) { 384 ret = new HashMap <String ,Object >(0); 385 } else { 386 ret = (Map <String ,Object >) (properties.clone()); 387 } 388 return (Map <String ,Object >) Collections.unmodifiableMap(ret); 389 } 390 391 392 408 public Object getProperty(String key) { 409 if (properties == null) { 410 return null; 411 } 412 return properties.get(key); 413 } 414 415 416 431 434 public boolean matches(AudioFormat format) { 435 436 if (format.getEncoding().equals(getEncoding()) && 437 ( (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getSampleRate() == getSampleRate()) ) && 438 (format.getSampleSizeInBits() == getSampleSizeInBits()) && 439 (format.getChannels() == getChannels() && 440 (format.getFrameSize() == getFrameSize()) && 441 ( (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getFrameRate() == getFrameRate()) ) && 442 ( (format.getSampleSizeInBits() <= 8) || (format.isBigEndian() == isBigEndian()) ) ) ) 443 return true; 444 445 return false; 446 } 447 448 449 456 public String toString() { 457 String sEncoding = ""; 458 if (getEncoding() != null) { 459 sEncoding = getEncoding().toString() + " "; 460 } 461 462 String sSampleRate; 463 if (getSampleRate() == (float) AudioSystem.NOT_SPECIFIED) { 464 sSampleRate = "unknown sample rate, "; 465 } else { 466 sSampleRate = "" + getSampleRate() + " Hz, "; 467 } 468 469 String sSampleSizeInBits; 470 if (getSampleSizeInBits() == (float) AudioSystem.NOT_SPECIFIED) { 471 sSampleSizeInBits = "unknown bits per sample, "; 472 } else { 473 sSampleSizeInBits = "" + getSampleSizeInBits() + " bit, "; 474 } 475 476 String sChannels; 477 if (getChannels() == 1) { 478 sChannels = "mono, "; 479 } else 480 if (getChannels() == 2) { 481 sChannels = "stereo, "; 482 } else { 483 if (getChannels() == AudioSystem.NOT_SPECIFIED) { 484 sChannels = " unknown number of channels, "; 485 } else { 486 sChannels = ""+getChannels()+" channels, "; 487 } 488 } 489 490 String sFrameSize; 491 if (getFrameSize() == (float) AudioSystem.NOT_SPECIFIED) { 492 sFrameSize = "unknown frame size, "; 493 } else { 494 sFrameSize = "" + getFrameSize()+ " bytes/frame, "; 495 } 496 497 String sFrameRate = ""; 498 if (Math.abs(getSampleRate() - getFrameRate()) > 0.00001) { 499 if (getFrameRate() == (float) AudioSystem.NOT_SPECIFIED) { 500 sFrameRate = "unknown frame rate, "; 501 } else { 502 sFrameRate = getFrameRate() + " frames/second, "; 503 } 504 } 505 506 String sEndian = ""; 507 if ((getEncoding().equals(Encoding.PCM_SIGNED) 508 || getEncoding().equals(Encoding.PCM_UNSIGNED)) 509 && ((getSampleSizeInBits() > 8) 510 || (getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED))) { 511 if (isBigEndian()) { 512 sEndian = "big-endian"; 513 } else { 514 sEndian = "little-endian"; 515 } 516 } 517 518 return sEncoding 519 + sSampleRate 520 + sSampleSizeInBits 521 + sChannels 522 + sFrameSize 523 + sFrameRate 524 + sEndian; 525 526 } 527 528 561 public static class Encoding { 562 563 564 566 569 public static final Encoding PCM_SIGNED = new Encoding("PCM_SIGNED"); 570 571 574 public static final Encoding PCM_UNSIGNED = new Encoding("PCM_UNSIGNED"); 575 576 579 public static final Encoding ULAW = new Encoding("ULAW"); 580 581 584 public static final Encoding ALAW = new Encoding("ALAW"); 585 586 587 589 592 private String name; 593 594 595 597 601 public Encoding(String name) { 602 this.name = name; 603 } 604 605 606 608 611 public final boolean equals(Object obj) { 612 if (toString() == null) { 613 return (obj != null) && (obj.toString() == null); 614 } 615 if (obj instanceof Encoding) { 616 return toString().equals(obj.toString()); 617 } 618 return false; 619 } 620 621 624 public final int hashCode() { 625 if (toString() == null) { 626 return 0; 627 } 628 return toString().hashCode(); 629 } 630 631 639 public final String toString() { 640 return name; 641 } 642 643 } } 645 646 | Popular Tags |