< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/WaveFileReader.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2015, 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


  28 import java.io.DataInputStream;
  29 import java.io.EOFException;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 
  33 import javax.sound.sampled.AudioFileFormat;
  34 import javax.sound.sampled.AudioFormat;
  35 import javax.sound.sampled.AudioSystem;
  36 import javax.sound.sampled.UnsupportedAudioFileException;
  37 
  38 /**
  39  * WAVE file reader.
  40  *
  41  * @author Kara Kytle
  42  * @author Jan Borgersen
  43  * @author Florian Bomers
  44  */
  45 public final class WaveFileReader extends SunFileReader {
  46 
  47     @Override
  48     AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
  49             throws UnsupportedAudioFileException, IOException {
  50 
  51         // assumes sream is rewound
  52 
  53         int nread = 0;
  54         int fmt;
  55         int length = 0;
  56         int wav_type = 0;
  57         short channels;
  58         long sampleRate;
  59         long avgBytesPerSec;
  60         short blockAlign;
  61         int sampleSizeInBits;
  62         AudioFormat.Encoding encoding = null;
  63 
  64         DataInputStream dis = new DataInputStream( stream );
  65 
  66         int magic = dis.readInt();
  67         int fileLength = rllong(dis);
  68         int waveMagic = dis.readInt();
  69         int totallength;
  70         if (fileLength <= 0) {
  71             fileLength = AudioSystem.NOT_SPECIFIED;
  72             totallength = AudioSystem.NOT_SPECIFIED;
  73         } else {
  74             totallength = fileLength + 8;
  75         }
  76 
  77         if ((magic != WaveFileFormat.RIFF_MAGIC) || (waveMagic != WaveFileFormat.WAVE_MAGIC)) {
  78             // not WAVE, throw UnsupportedAudioFileException
  79             throw new UnsupportedAudioFileException("not a WAVE file");
  80         }
  81 
  82         // find and read the "fmt" chunk
  83         // we break out of this loop either by hitting EOF or finding "fmt "
  84         while(true) {
  85 
  86             try {
  87                 fmt = dis.readInt();
  88                 nread += 4;
  89                 if( fmt==WaveFileFormat.FMT_MAGIC ) {


 169         nread = 0;
 170         while(true) {
 171             try{
 172                 int datahdr = dis.readInt();
 173                 nread+=4;
 174                 if (datahdr == WaveFileFormat.DATA_MAGIC) {
 175                     // we've found the 'data' chunk
 176                     break;
 177                 } else {
 178                     // else not 'data', skip this chunk
 179                     int thisLength = rllong(dis); nread += 4;
 180                     if (thisLength % 2 > 0) thisLength++;
 181                     nread += dis.skipBytes(thisLength);
 182                 }
 183             } catch (EOFException eof) {
 184                 // we've reached the end of the file without finding the 'data' chunk
 185                 throw new UnsupportedAudioFileException("Not a valid WAV file");
 186             }
 187         }
 188         // this is the length of the data chunk
 189         int dataLength = rllong(dis); nread += 4;
 190 
 191         // now build the new AudioFileFormat and return
 192 
 193         AudioFormat format = new AudioFormat(encoding,
 194                                              (float)sampleRate,
 195                                              sampleSizeInBits, channels,
 196                                              calculatePCMFrameSize(sampleSizeInBits, channels),
 197                                              (float)sampleRate, false);
 198 
 199         return new WaveFileFormat(AudioFileFormat.Type.WAVE,
 200                                   totallength,
 201                                   format,
 202                                   dataLength / format.getFrameSize());
 203     }
 204 }
   1 /*
   2  * Copyright (c) 1999, 2016, 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


  28 import java.io.DataInputStream;
  29 import java.io.EOFException;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 
  33 import javax.sound.sampled.AudioFileFormat;
  34 import javax.sound.sampled.AudioFormat;
  35 import javax.sound.sampled.AudioSystem;
  36 import javax.sound.sampled.UnsupportedAudioFileException;
  37 
  38 /**
  39  * WAVE file reader.
  40  *
  41  * @author Kara Kytle
  42  * @author Jan Borgersen
  43  * @author Florian Bomers
  44  */
  45 public final class WaveFileReader extends SunFileReader {
  46 
  47     @Override
  48     StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
  49             throws UnsupportedAudioFileException, IOException {
  50 
  51         // assumes sream is rewound
  52 
  53         int nread = 0;
  54         int fmt;
  55         int length = 0;
  56         int wav_type = 0;
  57         short channels;
  58         long sampleRate;
  59         long avgBytesPerSec;
  60         short blockAlign;
  61         int sampleSizeInBits;
  62         AudioFormat.Encoding encoding = null;
  63 
  64         DataInputStream dis = new DataInputStream( stream );
  65 
  66         int magic = dis.readInt();
  67         long /* unsigned int */ fileLength = rllong(dis) & 0xffffffffL;
  68         int waveMagic = dis.readInt();
  69         long totallength;
  70         if (fileLength <= 0) {
  71             fileLength = AudioSystem.NOT_SPECIFIED;
  72             totallength = AudioSystem.NOT_SPECIFIED;
  73         } else {
  74             totallength = fileLength + 8;
  75         }
  76 
  77         if ((magic != WaveFileFormat.RIFF_MAGIC) || (waveMagic != WaveFileFormat.WAVE_MAGIC)) {
  78             // not WAVE, throw UnsupportedAudioFileException
  79             throw new UnsupportedAudioFileException("not a WAVE file");
  80         }
  81 
  82         // find and read the "fmt" chunk
  83         // we break out of this loop either by hitting EOF or finding "fmt "
  84         while(true) {
  85 
  86             try {
  87                 fmt = dis.readInt();
  88                 nread += 4;
  89                 if( fmt==WaveFileFormat.FMT_MAGIC ) {


 169         nread = 0;
 170         while(true) {
 171             try{
 172                 int datahdr = dis.readInt();
 173                 nread+=4;
 174                 if (datahdr == WaveFileFormat.DATA_MAGIC) {
 175                     // we've found the 'data' chunk
 176                     break;
 177                 } else {
 178                     // else not 'data', skip this chunk
 179                     int thisLength = rllong(dis); nread += 4;
 180                     if (thisLength % 2 > 0) thisLength++;
 181                     nread += dis.skipBytes(thisLength);
 182                 }
 183             } catch (EOFException eof) {
 184                 // we've reached the end of the file without finding the 'data' chunk
 185                 throw new UnsupportedAudioFileException("Not a valid WAV file");
 186             }
 187         }
 188         // this is the length of the data chunk
 189         long /* unsigned int */ dataLength = rllong(dis) & 0xffffffffL; nread += 4;
 190 
 191         // now build the new AudioFileFormat and return
 192         final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
 193         AudioFormat format = new AudioFormat(encoding,
 194                                              (float)sampleRate,
 195                                              sampleSizeInBits, channels,
 196                                              frameSize,
 197                                              (float)sampleRate, false);
 198 
 199         long frameLength = dataLength / format.getFrameSize();
 200         return new WaveFileFormat(AudioFileFormat.Type.WAVE, totallength,
 201                                   format, frameLength);

 202     }
 203 }
< prev index next >