Search in sources :

Example 66 with AudioFormat

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

the class AuFileReader method getAudioFileFormat.

// METHODS TO IMPLEMENT AudioFileReader
/**
     * Obtains the audio file format of the input stream provided.  The stream must
     * point to valid audio file data.  In general, audio file providers may
     * need to read some data from the stream before determining whether they
     * support it.  These parsers must
     * be able to mark the stream, read enough data to determine whether they
     * support the stream, and, if not, reset the stream's read pointer to its original
     * position.  If the input stream does not support this, this method may fail
     * with an IOException.
     * @param stream the input stream from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     * @see InputStream#markSupported
     * @see InputStream#mark
     */
public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
    AudioFormat format = null;
    AuFileFormat fileFormat = null;
    int maxReadLength = 28;
    boolean bigendian = false;
    int magic = -1;
    int headerSize = -1;
    int dataSize = -1;
    int encoding_local = -1;
    int sampleRate = -1;
    int frameRate = -1;
    int frameSize = -1;
    int channels = -1;
    final int sampleSizeInBits;
    int length = 0;
    int nread = 0;
    AudioFormat.Encoding encoding = null;
    DataInputStream dis = new DataInputStream(stream);
    dis.mark(maxReadLength);
    magic = dis.readInt();
    if (!(magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) || (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC)) {
        // not AU, reset the stream, place into exception, throw exception
        dis.reset();
        throw new UnsupportedAudioFileException("not an AU file");
    }
    if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
        // otherwise little-endian
        bigendian = true;
    }
    headerSize = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    dataSize = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    encoding_local = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    sampleRate = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    channels = (bigendian == true ? dis.readInt() : rllong(dis));
    nread += 4;
    if (channels <= 0) {
        dis.reset();
        throw new UnsupportedAudioFileException("Invalid number of channels");
    }
    frameRate = sampleRate;
    switch(encoding_local) {
        case AuFileFormat.AU_ULAW_8:
            encoding = AudioFormat.Encoding.ULAW;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_ALAW_8:
            encoding = AudioFormat.Encoding.ALAW;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_LINEAR_8:
            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 8;
            break;
        case AuFileFormat.AU_LINEAR_16:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 16;
            break;
        case AuFileFormat.AU_LINEAR_24:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 24;
            break;
        case AuFileFormat.AU_LINEAR_32:
            encoding = AudioFormat.Encoding.PCM_SIGNED;
            sampleSizeInBits = 32;
            break;
        /*          case AuFileFormat.AU_FLOAT:
                        encoding = new AudioFormat.FLOAT;
                        sampleSizeInBits = 32;
                        break;
                        case AuFileFormat.AU_DOUBLE:
                        encoding = new AudioFormat.DOUBLE;
                        sampleSizeInBits = 8;
                        break;
                        case AuFileFormat.AU_ADPCM_G721:
                        encoding = new AudioFormat.G721_ADPCM;
                        sampleSizeInBits = 16;
                        break;
                        case AuFileFormat.AU_ADPCM_G723_3:
                        encoding = new AudioFormat.G723_3;
                        sampleSize = 24;
                        SamplePerUnit = 8;
                        break;
                        case AuFileFormat.AU_ADPCM_G723_5:
                        encoding = new AudioFormat.G723_5;
                        sampleSize = 40;
                        SamplePerUnit = 8;
                        break;
            */
        default:
            // unsupported filetype, throw exception
            dis.reset();
            throw new UnsupportedAudioFileException("not a valid AU file");
    }
    frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
    //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
    if (dataSize < 0) {
        length = AudioSystem.NOT_SPECIFIED;
    } else {
        //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
        length = dataSize / frameSize;
    }
    format = new AudioFormat(encoding, (float) sampleRate, sampleSizeInBits, channels, frameSize, (float) frameRate, bigendian);
    fileFormat = new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize, format, length);
    // Throws IOException
    dis.reset();
    return fileFormat;
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) AudioFormat(javax.sound.sampled.AudioFormat) DataInputStream(java.io.DataInputStream)

Example 67 with AudioFormat

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

the class AuFileReader method getAudioFileFormat.

/**
     * Obtains the audio file format of the File provided.  The File must
     * point to valid audio file data.
     * @param file the File from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    AudioFileFormat fileFormat = null;
    AudioFormat format = null;
    // throws IOException
    fis = new FileInputStream(file);
    // part of fix for 4325421
    try {
        bis = new BufferedInputStream(fis, bisBufferSize);
        // throws UnsupportedAudioFileException
        fileFormat = getAudioFileFormat(bis);
    } finally {
        fis.close();
    }
    return fileFormat;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) AudioFormat(javax.sound.sampled.AudioFormat) FileInputStream(java.io.FileInputStream) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 68 with AudioFormat

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

the class AudioFloatFormatConverter method getAudioInputStream.

public AudioInputStream getAudioInputStream(Encoding targetEncoding, AudioInputStream sourceStream) {
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(Encoding.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits, channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
Also used : Encoding(javax.sound.sampled.AudioFormat.Encoding) AudioFormat(javax.sound.sampled.AudioFormat)

Example 69 with AudioFormat

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

the class DLSSample method getData.

public Object getData() {
    AudioFormat format = getFormat();
    InputStream is = data.getInputStream();
    if (is == null)
        return null;
    return new AudioInputStream(is, format, data.capacity());
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Example 70 with AudioFormat

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

the class DLSSoundbank method writeSample.

private void writeSample(RIFFWriter writer, DLSSample sample) throws IOException {
    AudioFormat audioformat = sample.getFormat();
    Encoding encoding = audioformat.getEncoding();
    float sampleRate = audioformat.getSampleRate();
    int sampleSizeInBits = audioformat.getSampleSizeInBits();
    int channels = audioformat.getChannels();
    int frameSize = audioformat.getFrameSize();
    float frameRate = audioformat.getFrameRate();
    boolean bigEndian = audioformat.isBigEndian();
    boolean convert_needed = false;
    if (audioformat.getSampleSizeInBits() == 8) {
        if (!encoding.equals(Encoding.PCM_UNSIGNED)) {
            encoding = Encoding.PCM_UNSIGNED;
            convert_needed = true;
        }
    } else {
        if (!encoding.equals(Encoding.PCM_SIGNED)) {
            encoding = Encoding.PCM_SIGNED;
            convert_needed = true;
        }
        if (bigEndian) {
            bigEndian = false;
            convert_needed = true;
        }
    }
    if (convert_needed) {
        audioformat = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, frameRate, bigEndian);
    }
    // fmt
    RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
    int sampleformat = 0;
    if (audioformat.getEncoding().equals(Encoding.PCM_UNSIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_SIGNED))
        sampleformat = 1;
    else if (audioformat.getEncoding().equals(Encoding.PCM_FLOAT))
        sampleformat = 3;
    fmt_chunk.writeUnsignedShort(sampleformat);
    fmt_chunk.writeUnsignedShort(audioformat.getChannels());
    fmt_chunk.writeUnsignedInt((long) audioformat.getSampleRate());
    long srate = ((long) audioformat.getFrameRate()) * audioformat.getFrameSize();
    fmt_chunk.writeUnsignedInt(srate);
    fmt_chunk.writeUnsignedShort(audioformat.getFrameSize());
    fmt_chunk.writeUnsignedShort(audioformat.getSampleSizeInBits());
    fmt_chunk.write(0);
    fmt_chunk.write(0);
    writeSampleOptions(writer.writeChunk("wsmp"), sample.sampleoptions);
    if (convert_needed) {
        RIFFWriter data_chunk = writer.writeChunk("data");
        AudioInputStream stream = AudioSystem.getAudioInputStream(audioformat, (AudioInputStream) sample.getData());
        byte[] buff = new byte[1024];
        int ret;
        while ((ret = stream.read(buff)) != -1) {
            data_chunk.write(buff, 0, ret);
        }
    } else {
        RIFFWriter data_chunk = writer.writeChunk("data");
        ModelByteBuffer databuff = sample.getDataBuffer();
        databuff.writeTo(data_chunk);
    /*
            data_chunk.write(databuff.array(),
            databuff.arrayOffset(),
            databuff.capacity());
             */
    }
    writeInfo(writer.writeList("INFO"), sample.info);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Encoding(javax.sound.sampled.AudioFormat.Encoding) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)112 AudioInputStream (javax.sound.sampled.AudioInputStream)43 IOException (java.io.IOException)24 DataLine (javax.sound.sampled.DataLine)21 SourceDataLine (javax.sound.sampled.SourceDataLine)21 AudioFileFormat (javax.sound.sampled.AudioFileFormat)18 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)18 LineUnavailableException (javax.sound.sampled.LineUnavailableException)17 File (java.io.File)15 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 TargetDataLine (javax.sound.sampled.TargetDataLine)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 Vector (java.util.Vector)5 SequenceInputStream (java.io.SequenceInputStream)4 Clip (javax.sound.sampled.Clip)4