1 /*
2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
431 public boolean matches(AudioFormat format) {
432 if (format.getEncoding().equals(getEncoding())
433 && (format.getChannels() == AudioSystem.NOT_SPECIFIED
434 || format.getChannels() == getChannels())
435 && (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED
436 || format.getSampleRate() == getSampleRate())
437 && (format.getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED
438 || format.getSampleSizeInBits() == getSampleSizeInBits())
439 && (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED
440 || format.getFrameRate() == getFrameRate())
441 && (format.getFrameSize() == AudioSystem.NOT_SPECIFIED
442 || format.getFrameSize() == getFrameSize())
443 && (getSampleSizeInBits() <= 8
444 || format.isBigEndian() == isBigEndian())) {
445 return true;
446 }
447 return false;
448 }
449
450 /**
451 * Returns a string that describes the format, such as: "PCM SIGNED 22050 Hz
452 * 16 bit mono big-endian". The contents of the string may vary between
453 * implementations of Java Sound.
454 *
455 * @return a string that describes the format parameters
456 */
457 @Override
458 public String toString() {
459 String sEncoding = "";
460 if (getEncoding() != null) {
461 sEncoding = getEncoding().toString() + " ";
462 }
463
464 String sSampleRate;
465 if (getSampleRate() == (float) AudioSystem.NOT_SPECIFIED) {
466 sSampleRate = "unknown sample rate, ";
467 } else {
468 sSampleRate = "" + getSampleRate() + " Hz, ";
469 }
470
471 String sSampleSizeInBits;
472 if (getSampleSizeInBits() == (float) AudioSystem.NOT_SPECIFIED) {
473 sSampleSizeInBits = "unknown bits per sample, ";
474 } else {
475 sSampleSizeInBits = "" + getSampleSizeInBits() + " bit, ";
476 }
477
478 String sChannels;
479 if (getChannels() == 1) {
480 sChannels = "mono, ";
481 } else
482 if (getChannels() == 2) {
483 sChannels = "stereo, ";
484 } else {
485 if (getChannels() == AudioSystem.NOT_SPECIFIED) {
486 sChannels = " unknown number of channels, ";
487 } else {
488 sChannels = ""+getChannels()+" channels, ";
489 }
490 }
491
492 String sFrameSize;
493 if (getFrameSize() == (float) AudioSystem.NOT_SPECIFIED) {
494 sFrameSize = "unknown frame size, ";
495 } else {
496 sFrameSize = "" + getFrameSize()+ " bytes/frame, ";
497 }
498
499 String sFrameRate = "";
500 if (Math.abs(getSampleRate() - getFrameRate()) > 0.00001) {
501 if (getFrameRate() == (float) AudioSystem.NOT_SPECIFIED) {
502 sFrameRate = "unknown frame rate, ";
503 } else {
504 sFrameRate = getFrameRate() + " frames/second, ";
505 }
506 }
507
508 String sEndian = "";
509 if ((getEncoding().equals(Encoding.PCM_SIGNED)
510 || getEncoding().equals(Encoding.PCM_UNSIGNED))
511 && ((getSampleSizeInBits() > 8)
512 || (getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED))) {
513 if (isBigEndian()) {
514 sEndian = "big-endian";
515 } else {
516 sEndian = "little-endian";
517 }
518 }
519
520 return sEncoding
521 + sSampleRate
522 + sSampleSizeInBits
523 + sChannels
524 + sFrameSize
525 + sFrameRate
526 + sEndian;
527
528 }
529
530 /**
531 * The {@code Encoding} class names the specific type of data representation
532 * used for an audio stream. The encoding includes aspects of the sound
533 * format other than the number of channels, sample rate, sample size, frame
534 * rate, frame size, and byte order.
535 * <p>
536 * One ubiquitous type of audio encoding is pulse-code modulation (PCM),
537 * which is simply a linear (proportional) representation of the sound
538 * waveform. With PCM, the number stored in each sample is proportional to
539 * the instantaneous amplitude of the sound pressure at that point in time.
540 * The numbers may be signed or unsigned integers or floats. Besides PCM,
541 * other encodings include mu-law and a-law, which are nonlinear mappings of
542 * the sound amplitude that are often used for recording speech.
543 * <p>
544 * You can use a predefined encoding by referring to one of the static
545 * objects created by this class, such as {@code PCM_SIGNED} or
546 * {@code PCM_UNSIGNED}. Service providers can create new encodings, such as
547 * compressed audio formats, and make these available through the
613 if (this == obj) {
614 return true;
615 }
616 if (!(obj instanceof Encoding)) {
617 return false;
618 }
619 return Objects.equals(name, ((Encoding) obj).name);
620 }
621
622 /**
623 * Returns a hash code value for this encoding.
624 *
625 * @return a hash code value for this encoding
626 */
627 @Override
628 public final int hashCode() {
629 return name != null ? name.hashCode() : 0;
630 }
631
632 /**
633 * Provides the {@code String} representation of the encoding. This
634 * {@code String} is the same name that was passed to the constructor.
635 * For the predefined encodings, the name is similar to the encoding's
636 * variable (field) name. For example, {@code PCM_SIGNED.toString()}
637 * returns the name "PCM_SIGNED".
638 *
639 * @return the encoding name
640 */
641 @Override
642 public final String toString() {
643 return name;
644 }
645 }
646 }
|
1 /*
2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
431 public boolean matches(AudioFormat format) {
432 if (format.getEncoding().equals(getEncoding())
433 && (format.getChannels() == AudioSystem.NOT_SPECIFIED
434 || format.getChannels() == getChannels())
435 && (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED
436 || format.getSampleRate() == getSampleRate())
437 && (format.getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED
438 || format.getSampleSizeInBits() == getSampleSizeInBits())
439 && (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED
440 || format.getFrameRate() == getFrameRate())
441 && (format.getFrameSize() == AudioSystem.NOT_SPECIFIED
442 || format.getFrameSize() == getFrameSize())
443 && (getSampleSizeInBits() <= 8
444 || format.isBigEndian() == isBigEndian())) {
445 return true;
446 }
447 return false;
448 }
449
450 /**
451 * Returns a string that describes the audio format, such as: "PCM SIGNED
452 * 22050 Hz 16 bit mono big-endian". The contents of the string may vary
453 * between implementations of Java Sound.
454 *
455 * @return a string representation of the audio format
456 */
457 @Override
458 public String toString() {
459 String sampleRate = getSampleRate() == AudioSystem.NOT_SPECIFIED ?
460 "unknown sample rate" : getSampleRate() + " Hz";
461
462 String sampleSize = getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED ?
463 "unknown bits per sample" : getSampleSizeInBits() + " bit";
464
465 String channels = switch (getChannels()) {
466 case 1 -> "mono";
467 case 2 -> "stereo";
468 case AudioSystem.NOT_SPECIFIED -> "unknown number of channels";
469 default -> getChannels() + " channels";
470 };
471
472 String frameSize = getFrameSize() == AudioSystem.NOT_SPECIFIED ?
473 "unknown frame size" : getFrameSize() + " bytes/frame";
474
475 String frameRate = "";
476 if (Math.abs(getSampleRate() - getFrameRate()) > 0.00001) {
477 frameRate = getFrameRate() == AudioSystem.NOT_SPECIFIED ?
478 ", unknown frame rate":", " + getFrameRate() + " frames/second";
479 }
480
481 String bigEndian = "";
482 if ((getEncoding().equals(Encoding.PCM_SIGNED)
483 || getEncoding().equals(Encoding.PCM_UNSIGNED))
484 && ((getSampleSizeInBits() > 8)
485 || (getSampleSizeInBits() == AudioSystem.NOT_SPECIFIED))) {
486 bigEndian = isBigEndian() ? ", big-endian" : ", little-endian";
487 }
488
489 return String.format("%s %s, %s, %s, %s%s%s", getEncoding(),
490 sampleRate, sampleSize, channels, frameSize,
491 frameRate, bigEndian);
492 }
493
494 /**
495 * The {@code Encoding} class names the specific type of data representation
496 * used for an audio stream. The encoding includes aspects of the sound
497 * format other than the number of channels, sample rate, sample size, frame
498 * rate, frame size, and byte order.
499 * <p>
500 * One ubiquitous type of audio encoding is pulse-code modulation (PCM),
501 * which is simply a linear (proportional) representation of the sound
502 * waveform. With PCM, the number stored in each sample is proportional to
503 * the instantaneous amplitude of the sound pressure at that point in time.
504 * The numbers may be signed or unsigned integers or floats. Besides PCM,
505 * other encodings include mu-law and a-law, which are nonlinear mappings of
506 * the sound amplitude that are often used for recording speech.
507 * <p>
508 * You can use a predefined encoding by referring to one of the static
509 * objects created by this class, such as {@code PCM_SIGNED} or
510 * {@code PCM_UNSIGNED}. Service providers can create new encodings, such as
511 * compressed audio formats, and make these available through the
577 if (this == obj) {
578 return true;
579 }
580 if (!(obj instanceof Encoding)) {
581 return false;
582 }
583 return Objects.equals(name, ((Encoding) obj).name);
584 }
585
586 /**
587 * Returns a hash code value for this encoding.
588 *
589 * @return a hash code value for this encoding
590 */
591 @Override
592 public final int hashCode() {
593 return name != null ? name.hashCode() : 0;
594 }
595
596 /**
597 * Returns encoding's name as the string representation of the encoding.
598 * For the predefined encodings, the name is similar to the encoding's
599 * variable (field) name. For example, {@code PCM_SIGNED.toString()}
600 * returns the name "PCM_SIGNED".
601 *
602 * @return a string representation of the encoding
603 */
604 @Override
605 public final String toString() {
606 return name;
607 }
608 }
609 }
|