jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
author serb
Wed, 16 Mar 2016 15:55:27 +0300
changeset 36888 0905e6c2d1af
parent 35656 e5582272e65b
child 38399 bd91ce346b5b
permissions -rw-r--r--
8146239: Support of PCM_FLOAT for the AU file format Reviewed-by: amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    28
import java.io.DataInputStream;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    29
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sound.sampled.AudioFileFormat;
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    33
import javax.sound.sampled.AudioFileFormat.Type;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    34
import javax.sound.sampled.AudioFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * AU file reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    45
public final class AuFileReader extends SunFileReader {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    47
    @Override
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    48
    public AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    49
            throws UnsupportedAudioFileException, IOException {
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    50
        final DataInputStream dis = new DataInputStream(stream);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    51
        final int magic = dis.readInt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    53
        if (magic != AuFileFormat.AU_SUN_MAGIC) {
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    54
            // not AU, throw exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new UnsupportedAudioFileException("not an AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    58
        final int headerSize = dis.readInt();
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    59
        final int dataSize = dis.readInt();
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    60
        final int auType = dis.readInt();
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    61
        final int sampleRate = dis.readInt();
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    62
        final int channels = dis.readInt();
21245
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    63
        if (channels <= 0) {
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    64
            throw new UnsupportedAudioFileException("Invalid number of channels");
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    65
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    67
        final int sampleSizeInBits;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    68
        final AudioFormat.Encoding encoding;
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    69
        switch (auType) {
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    70
            case AuFileFormat.AU_ULAW_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    71
                encoding = AudioFormat.Encoding.ULAW;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    72
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    73
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    74
            case AuFileFormat.AU_ALAW_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    75
                encoding = AudioFormat.Encoding.ALAW;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    76
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    77
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    78
            case AuFileFormat.AU_LINEAR_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    79
                // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    80
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    81
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    82
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    83
            case AuFileFormat.AU_LINEAR_16:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    84
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    85
                sampleSizeInBits = 16;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    86
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    87
            case AuFileFormat.AU_LINEAR_24:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    88
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    89
                sampleSizeInBits = 24;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    90
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    91
            case AuFileFormat.AU_LINEAR_32:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    92
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    93
                sampleSizeInBits = 32;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    94
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    95
            case AuFileFormat.AU_FLOAT:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    96
                encoding = AudioFormat.Encoding.PCM_FLOAT;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    97
                sampleSizeInBits = 32;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    98
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    99
            // we don't support these ...
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   100
            /*          case AuFileFormat.AU_DOUBLE:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        encoding = new AudioFormat.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        case AuFileFormat.AU_ADPCM_G721:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        encoding = new AudioFormat.G721_ADPCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        case AuFileFormat.AU_ADPCM_G723_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        encoding = new AudioFormat.G723_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        sampleSize = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        case AuFileFormat.AU_ADPCM_G723_5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        encoding = new AudioFormat.G723_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        sampleSize = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            */
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   119
            default:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   120
                // unsupported filetype, throw exception
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   121
                throw new UnsupportedAudioFileException("not a valid AU file");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   124
        final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   126
        final int length;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   127
        if (dataSize < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            length = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            length = dataSize / frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // now seek past the header
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   134
        dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   135
        final AudioFormat format = new AudioFormat(encoding, sampleRate,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   136
                                                   sampleSizeInBits, channels,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   137
                                                   frameSize, sampleRate, true);
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   138
        return new AuFileFormat(Type.AU, dataSize + headerSize, format, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}