Search in sources :

Example 11 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class AuFileWriter method getAudioFileTypes.

public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
    AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
    System.arraycopy(types, 0, filetypes, 0, types.length);
    // make sure we can write this stream
    AudioFormat format = stream.getFormat();
    AudioFormat.Encoding encoding = format.getEncoding();
    if ((AudioFormat.Encoding.ALAW.equals(encoding)) || (AudioFormat.Encoding.ULAW.equals(encoding)) || (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) || (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding))) {
        return filetypes;
    }
    return new AudioFileFormat.Type[0];
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat)

Example 12 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class AuFileWriter method getFileStream.

private InputStream getFileStream(AuFileFormat auFileFormat, InputStream audioStream) throws IOException {
    // private method ... assumes auFileFormat is a supported file type
    AudioFormat format = auFileFormat.getFormat();
    int magic = AuFileFormat.AU_SUN_MAGIC;
    int headerSize = AuFileFormat.AU_HEADERSIZE;
    long dataSize = auFileFormat.getFrameLength();
    //$$fb fix for Bug 4351296
    //int dataSizeInBytes = dataSize * format.getFrameSize();
    long dataSizeInBytes = (dataSize == AudioSystem.NOT_SPECIFIED) ? UNKNOWN_SIZE : dataSize * format.getFrameSize();
    if (dataSizeInBytes > 0x7FFFFFFFl) {
        dataSizeInBytes = UNKNOWN_SIZE;
    }
    int encoding_local = auFileFormat.getAuType();
    int sampleRate = (int) format.getSampleRate();
    int channels = format.getChannels();
    //$$fb below is the fix for 4297100.
    //boolean bigendian      = format.isBigEndian();
    // force bigendian
    boolean bigendian = true;
    byte[] header = null;
    ByteArrayInputStream headerStream = null;
    ByteArrayOutputStream baos = null;
    DataOutputStream dos = null;
    SequenceInputStream auStream = null;
    AudioFormat audioStreamFormat = null;
    AudioFormat.Encoding encoding = null;
    InputStream codedAudioStream = audioStream;
    // if we need to do any format conversion, do it here.
    codedAudioStream = audioStream;
    if (audioStream instanceof AudioInputStream) {
        audioStreamFormat = ((AudioInputStream) audioStream).getFormat();
        encoding = audioStreamFormat.getEncoding();
        //$$ fb 2001-07-13: Bug 4391108
        if ((AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) || (AudioFormat.Encoding.PCM_SIGNED.equals(encoding) && bigendian != audioStreamFormat.isBigEndian())) {
            // plug in the transcoder to convert to PCM_SIGNED, bigendian
            // NOTE: little endian AU is not common, so we're always converting
            //       to big endian unless the passed in audioFileFormat is little.
            // $$fb this NOTE is superseded. We always write big endian au files, this is by far the standard.
            codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioStreamFormat.getSampleRate(), audioStreamFormat.getSampleSizeInBits(), audioStreamFormat.getChannels(), audioStreamFormat.getFrameSize(), audioStreamFormat.getFrameRate(), bigendian), (AudioInputStream) audioStream);
        }
    }
    baos = new ByteArrayOutputStream();
    dos = new DataOutputStream(baos);
    if (bigendian) {
        dos.writeInt(AuFileFormat.AU_SUN_MAGIC);
        dos.writeInt(headerSize);
        dos.writeInt((int) dataSizeInBytes);
        dos.writeInt(encoding_local);
        dos.writeInt(sampleRate);
        dos.writeInt(channels);
    } else {
        dos.writeInt(AuFileFormat.AU_SUN_INV_MAGIC);
        dos.writeInt(big2little(headerSize));
        dos.writeInt(big2little((int) dataSizeInBytes));
        dos.writeInt(big2little(encoding_local));
        dos.writeInt(big2little(sampleRate));
        dos.writeInt(big2little(channels));
    }
    // Now create a new InputStream from headerStream and the InputStream
    // in audioStream
    dos.close();
    header = baos.toByteArray();
    headerStream = new ByteArrayInputStream(header);
    auStream = new SequenceInputStream(headerStream, new NoCloseInputStream(codedAudioStream));
    return auStream;
}
Also used : DataOutputStream(java.io.DataOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Example 13 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class AuFileWriter method getAudioFileFormat.

// -------------------------------------------------------------
/**
     * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
     * Throws IllegalArgumentException if not supported.
     */
private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    AudioFormat format = null;
    AuFileFormat fileFormat = null;
    AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
    AudioFormat streamFormat = stream.getFormat();
    AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
    float sampleRate;
    int sampleSizeInBits;
    int channels;
    int frameSize;
    float frameRate;
    int fileSize;
    if (!types[0].equals(type)) {
        throw new IllegalArgumentException("File type " + type + " not supported.");
    }
    if ((AudioFormat.Encoding.ALAW.equals(streamEncoding)) || (AudioFormat.Encoding.ULAW.equals(streamEncoding))) {
        encoding = streamEncoding;
        sampleSizeInBits = streamFormat.getSampleSizeInBits();
    } else if (streamFormat.getSampleSizeInBits() == 8) {
        encoding = AudioFormat.Encoding.PCM_SIGNED;
        sampleSizeInBits = 8;
    } else {
        encoding = AudioFormat.Encoding.PCM_SIGNED;
        sampleSizeInBits = streamFormat.getSampleSizeInBits();
    }
    format = new AudioFormat(encoding, streamFormat.getSampleRate(), sampleSizeInBits, streamFormat.getChannels(), streamFormat.getFrameSize(), streamFormat.getFrameRate(), // AU is always big endian
    true);
    if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
        fileSize = (int) stream.getFrameLength() * streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
    } else {
        fileSize = AudioSystem.NOT_SPECIFIED;
    }
    fileFormat = new AuFileFormat(AudioFileFormat.Type.AU, fileSize, format, (int) stream.getFrameLength());
    return fileFormat;
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat)

Example 14 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class AiffFileWriter method getAudioFileTypes.

// METHODS TO IMPLEMENT AudioFileWriter
public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
    AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
    System.arraycopy(types, 0, filetypes, 0, types.length);
    // make sure we can write this stream
    AudioFormat format = stream.getFormat();
    AudioFormat.Encoding encoding = format.getEncoding();
    if ((AudioFormat.Encoding.ALAW.equals(encoding)) || (AudioFormat.Encoding.ULAW.equals(encoding)) || (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) || (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding))) {
        return filetypes;
    }
    return new AudioFileFormat.Type[0];
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat)

Example 15 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class AiffFileWriter method getFileStream.

private InputStream getFileStream(AiffFileFormat aiffFileFormat, InputStream audioStream) throws IOException {
    // private method ... assumes aiffFileFormat is a supported file format
    AudioFormat format = aiffFileFormat.getFormat();
    AudioFormat streamFormat = null;
    AudioFormat.Encoding encoding = null;
    //$$fb a little bit nicer handling of constants
    //int headerSize          = 54;
    int headerSize = aiffFileFormat.getHeaderSize();
    //int fverChunkSize       = 0;
    int fverChunkSize = aiffFileFormat.getFverChunkSize();
    //int commChunkSize       = 26;
    int commChunkSize = aiffFileFormat.getCommChunkSize();
    int aiffLength = -1;
    int ssndChunkSize = -1;
    //int ssndOffset                        = headerSize - 16;
    int ssndOffset = aiffFileFormat.getSsndChunkOffset();
    short channels = (short) format.getChannels();
    short sampleSize = (short) format.getSampleSizeInBits();
    int ssndBlockSize = (channels * sampleSize);
    int numFrames = aiffFileFormat.getFrameLength();
    long dataSize = -1;
    if (numFrames != AudioSystem.NOT_SPECIFIED) {
        dataSize = (long) numFrames * ssndBlockSize / 8;
        ssndChunkSize = (int) dataSize + 16;
        aiffLength = (int) dataSize + headerSize;
    }
    float sampleFramesPerSecond = format.getSampleRate();
    int compCode = AiffFileFormat.AIFC_PCM;
    byte[] header = null;
    ByteArrayInputStream headerStream = null;
    ByteArrayOutputStream baos = null;
    DataOutputStream dos = null;
    SequenceInputStream aiffStream = null;
    InputStream codedAudioStream = audioStream;
    if (audioStream instanceof AudioInputStream) {
        streamFormat = ((AudioInputStream) audioStream).getFormat();
        encoding = streamFormat.getEncoding();
        // $$jb: Note that AIFF samples are ALWAYS signed
        if ((AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) || ((AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian())) {
            // plug in the transcoder to convert to PCM_SIGNED. big endian
            codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, streamFormat.getSampleRate(), streamFormat.getSampleSizeInBits(), streamFormat.getChannels(), streamFormat.getFrameSize(), streamFormat.getFrameRate(), true), (AudioInputStream) audioStream);
        } else if ((AudioFormat.Encoding.ULAW.equals(encoding)) || (AudioFormat.Encoding.ALAW.equals(encoding))) {
            if (streamFormat.getSampleSizeInBits() != 8) {
                throw new IllegalArgumentException("unsupported encoding");
            }
            //$$fb 2001-07-13: this is probably not what we want:
            //     writing PCM when ULAW/ALAW is requested. AIFC is able to write ULAW !
            // plug in the transcoder to convert to PCM_SIGNED_BIG_ENDIAN
            codedAudioStream = AudioSystem.getAudioInputStream(new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, streamFormat.getSampleRate(), streamFormat.getSampleSizeInBits() * 2, streamFormat.getChannels(), streamFormat.getFrameSize() * 2, streamFormat.getFrameRate(), true), (AudioInputStream) audioStream);
        }
    }
    // Now create an AIFF stream header...
    baos = new ByteArrayOutputStream();
    dos = new DataOutputStream(baos);
    // Write the outer FORM chunk
    dos.writeInt(AiffFileFormat.AIFF_MAGIC);
    dos.writeInt((aiffLength - 8));
    dos.writeInt(AiffFileFormat.AIFF_MAGIC2);
    // Write a FVER chunk - only for AIFC
    //dos.writeInt(FVER_MAGIC);
    //dos.writeInt( (fverChunkSize-8) );
    //dos.writeInt(FVER_TIMESTAMP);
    // Write a COMM chunk
    dos.writeInt(AiffFileFormat.COMM_MAGIC);
    dos.writeInt((commChunkSize - 8));
    dos.writeShort(channels);
    dos.writeInt(numFrames);
    dos.writeShort(sampleSize);
    // 10 bytes
    write_ieee_extended(dos, sampleFramesPerSecond);
    //Only for AIFC
    //dos.writeInt(compCode);
    //dos.writeInt(compCode);
    //dos.writeShort(0);
    // Write the SSND chunk header
    dos.writeInt(AiffFileFormat.SSND_MAGIC);
    dos.writeInt((ssndChunkSize - 8));
    // ssndOffset and ssndBlockSize set to 0 upon
    // recommendation in "Sound Manager" chapter in
    // "Inside Macintosh Sound", pp 2-87  (from Babu)
    // ssndOffset
    dos.writeInt(0);
    // ssndBlockSize
    dos.writeInt(0);
    // Concat this with the audioStream and return it
    dos.close();
    header = baos.toByteArray();
    headerStream = new ByteArrayInputStream(header);
    aiffStream = new SequenceInputStream(headerStream, new NoCloseInputStream(codedAudioStream));
    return aiffStream;
}
Also used : DataOutputStream(java.io.DataOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)74 AudioInputStream (javax.sound.sampled.AudioInputStream)32 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)11 AudioFileFormat (javax.sound.sampled.AudioFileFormat)10 SourceDataLine (javax.sound.sampled.SourceDataLine)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataInputStream (java.io.DataInputStream)6 File (java.io.File)6 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 Vector (java.util.Vector)5 DataLine (javax.sound.sampled.DataLine)5 LineUnavailableException (javax.sound.sampled.LineUnavailableException)5 SequenceInputStream (java.io.SequenceInputStream)4 AudioMetaData (ddf.minim.AudioMetaData)3 DataOutputStream (java.io.DataOutputStream)3