javax.sound.sampled.AudioFileFormat.Type Java Examples

The following examples show how to use javax.sound.sampled.AudioFileFormat.Type. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuFileWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, OutputStream out) throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    // we must know the total data length to calculate the file length
    //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
    //if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
    //      throw new IOException("stream length not specified");
    //}

    // throws IllegalArgumentException if not supported
    AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
    return writeAuFile(stream, auFileFormat, out);
}
 
Example #2
Source File: AuFileWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public Type[] getAudioFileTypes(AudioInputStream stream) {

    Type[] filetypes = new 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)
            || AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {
        return filetypes;
    }

    return new Type[0];
}
 
Example #3
Source File: AutoCloseTimeCheck.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    // Prepare the audio file
    File file = new File("audio.wav");
    try {
        AudioFormat format =
                new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);
        AudioSystem.write(getStream(format), Type.WAVE, file);
    } catch (final Exception ignored) {
        return; // the test is not applicable
    }
    try {
        testSmallDelay(file);
        testBigDelay(file);
    } finally {
        Files.delete(file.toPath());
    }
}
 
Example #4
Source File: WaveFloatFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, OutputStream out)
        throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #5
Source File: WaveFloatFileWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, OutputStream out)
        throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    try (final RIFFWriter writer = new RIFFWriter(
            new NoCloseOutputStream(out), "WAVE")) {
        write(stream, writer);
        return (int) writer.getFilePointer();
    }
}
 
Example #6
Source File: WaveFloatFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, File out)
        throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(out, "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #7
Source File: AuFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type[] getAudioFileTypes(AudioInputStream stream) {

    Type[] filetypes = new 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)
            || AudioFormat.Encoding.PCM_FLOAT.equals(encoding)) {
        return filetypes;
    }

    return new Type[0];
}
 
Example #8
Source File: AuFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, OutputStream out) throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    // we must know the total data length to calculate the file length
    //$$fb 2001-07-13: fix for bug 4351296: do not throw an exception
    //if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
    //      throw new IOException("stream length not specified");
    //}

    // throws IllegalArgumentException if not supported
    AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);
    return writeAuFile(stream, auFileFormat, out);
}
 
Example #9
Source File: AutoCloseTimeCheck.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    // Prepare the audio file
    File file = new File("audio.wav");
    try {
        AudioFormat format =
                new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);
        AudioSystem.write(getStream(format), Type.WAVE, file);
    } catch (final Exception ignored) {
        return; // the test is not applicable
    }
    try {
        testSmallDelay(file);
        testBigDelay(file);
    } finally {
        Files.delete(file.toPath());
    }
}
 
Example #10
Source File: AutoCloseTimeCheck.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    // Prepare the audio file
    File file = new File("audio.wav");
    try {
        AudioFormat format =
                new AudioFormat(PCM_SIGNED, 44100, 8, 1, 1, 44100, false);
        AudioSystem.write(getStream(format), Type.WAVE, file);
    } catch (final Exception ignored) {
        return; // the test is not applicable
    }
    try {
        testSmallDelay(file);
        testBigDelay(file);
    } finally {
        Files.delete(file.toPath());
    }
}
 
Example #11
Source File: WaveFloatFileWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, File out)
        throws IOException {
    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(out, "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #12
Source File: WaveFloatFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #13
Source File: WaveFloatFileWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(
            AudioFloatConverter.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #14
Source File: WaveFloatFileWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Type[] getAudioFileTypes(AudioInputStream stream) {

        if (!stream.getFormat().getEncoding().equals(
                AudioFloatConverter.PCM_FLOAT))
            return new Type[0];
        return new Type[] { Type.WAVE };
    }
 
Example #15
Source File: PdOffline.java    From gdx-pd with Apache License 2.0 5 votes vote down vote up
/**
 * Bake a path to a wav file
 * @param patch the patch to bake
 * @param wav the wav file to write
 * @param channels how many channels (1 for mono, 2 for stereo, can be more than 2 channels)
 * @param sampleRate sample rate used by Pd
 * @param time baking duration in seconds
 * @throws IOException
 */
public static void bake(File patch, File wav, int channels, int sampleRate, float time) throws IOException {
	
	// disable Pd : does nothing if Pd alreay initialized.
	PdConfiguration.disabled = true;

	// Pause audio.
	// Does nothing in headless mode but required to 
	// have Pd static code executed (load library)
	Pd.audio.pause();
	
	int handle = PdBase.openPatch(patch);
	PdBase.openAudio(0, channels, sampleRate);
	PdBase.computeAudio(true);
	
	int frames = (int)(time * sampleRate);
	int samples = frames * channels;
	short [] data = new short[samples];
	int ticks = frames / PdBase.blockSize();
	PdBase.process(ticks, new short[]{}, data);
	
	PdBase.closePatch(handle);
	
	// save
	byte [] buf = new byte[data.length * 2];
	for(int i=0 ; i<data.length ; i++){
		buf[i*2+0] = (byte)(data[i] & 0xFF);
		buf[i*2+1] = (byte)((data[i] >> 8) & 0xFF);
	}
	
	ByteArrayInputStream stream = new ByteArrayInputStream(buf);
	AudioFormat format = new AudioFormat(sampleRate, 16, channels, true, false);
	AudioInputStream audioStream = new AudioInputStream(stream, format, data.length);
	AudioSystem.write(audioStream, Type.WAVE, wav);
	
	// resume audio
	Pd.audio.resume();
}
 
Example #16
Source File: WaveFloatFileWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, OutputStream out)
        throws IOException {

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #17
Source File: WaveFloatFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, File out)
        throws IOException {
    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(out, "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #18
Source File: WaveFloatFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, OutputStream out)
        throws IOException {

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #19
Source File: WaveFloatFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #20
Source File: WaveFloatFileWriter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #21
Source File: WaveFloatFileWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #22
Source File: WaveFloatFileWriter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void checkFormat(AudioFileFormat.Type type, AudioInputStream stream) {
    if (!Type.WAVE.equals(type))
        throw new IllegalArgumentException("File type " + type
                + " not supported.");
    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        throw new IllegalArgumentException("File format "
                + stream.getFormat() + " not supported.");
}
 
Example #23
Source File: WaveFloatFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, File out)
        throws IOException {
    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(out, "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #24
Source File: WaveFloatFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, OutputStream out)
        throws IOException {

    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(new NoCloseOutputStream(out), "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #25
Source File: WaveFloatFileWriter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, Type fileType, File out)
        throws IOException {
    checkFormat(fileType, stream);
    if (stream.getFormat().isBigEndian())
        stream = toLittleEndian(stream);
    RIFFWriter writer = new RIFFWriter(out, "WAVE");
    write(stream, writer);
    int fpointer = (int) writer.getFilePointer();
    writer.close();
    return fpointer;
}
 
Example #26
Source File: WriteUnsupportedAudioFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    for (final AudioFileFormat.Type type : types) {
        for (final AudioFormat format : formats) {
            testAS(type, format);
            for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
                testAFW(afw, type, format);
            }
        }
    }
    Files.delete(Paths.get(FILE.getAbsolutePath()));
}
 
Example #27
Source File: AuFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
 * Throws IllegalArgumentException if not supported.
 */
private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {
    if (!isFileTypeSupported(type, stream)) {
        throw new IllegalArgumentException("File type " + type + " not supported.");
    }

    AudioFormat streamFormat = stream.getFormat();
    AudioFormat.Encoding encoding = streamFormat.getEncoding();

    if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
        encoding = AudioFormat.Encoding.PCM_SIGNED;
    }

    // We always write big endian au files, this is by far the standard
    AudioFormat format = new AudioFormat(encoding,
                                         streamFormat.getSampleRate(),
                                         streamFormat.getSampleSizeInBits(),
                                         streamFormat.getChannels(),
                                         streamFormat.getFrameSize(),
                                         streamFormat.getFrameRate(), true);

    int fileSize;
    if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
        fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
    } else {
        fileSize = AudioSystem.NOT_SPECIFIED;
    }

    return new AuFileFormat(Type.AU, fileSize, format,
                            (int) stream.getFrameLength());
}
 
Example #28
Source File: AuFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int write(AudioInputStream stream, Type fileType, File out) throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    // throws IllegalArgumentException if not supported
    AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);

    // first write the file without worrying about length fields
    FileOutputStream fos = new FileOutputStream( out );     // throws IOException
    BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
    int bytesWritten = writeAuFile(stream, auFileFormat, bos );
    bos.close();

    // now, if length fields were not specified, calculate them,
    // open as a random access file, write the appropriate fields,
    // close again....
    if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

        // $$kk: 10.22.99: jan: please either implement this or throw an exception!
        // $$fb: 2001-07-13: done. Fixes Bug 4479981
        RandomAccessFile raf=new RandomAccessFile(out, "rw");
        if (raf.length()<=0x7FFFFFFFl) {
            // skip AU magic and data offset field
            raf.skipBytes(8);
            raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
            // that's all
        }
        raf.close();
    }

    return bytesWritten;
}
 
Example #29
Source File: WaveFloatFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type[] getAudioFileTypes(AudioInputStream stream) {

    if (!stream.getFormat().getEncoding().equals(Encoding.PCM_FLOAT))
        return new Type[0];
    return new Type[] { Type.WAVE };
}
 
Example #30
Source File: AuFileWriter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
 * Throws IllegalArgumentException if not supported.
 */
private AudioFileFormat getAudioFileFormat(Type type, AudioInputStream stream) {
    if (!isFileTypeSupported(type, stream)) {
        throw new IllegalArgumentException("File type " + type + " not supported.");
    }

    AudioFormat streamFormat = stream.getFormat();
    AudioFormat.Encoding encoding = streamFormat.getEncoding();

    if (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) {
        encoding = AudioFormat.Encoding.PCM_SIGNED;
    }

    // We always write big endian au files, this is by far the standard
    AudioFormat format = new AudioFormat(encoding,
                                         streamFormat.getSampleRate(),
                                         streamFormat.getSampleSizeInBits(),
                                         streamFormat.getChannels(),
                                         streamFormat.getFrameSize(),
                                         streamFormat.getFrameRate(), true);

    int fileSize;
    if (stream.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
        fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AuFileFormat.AU_HEADERSIZE;
    } else {
        fileSize = AudioSystem.NOT_SPECIFIED;
    }

    return new AuFileFormat(Type.AU, fileSize, format,
                            (int) stream.getFrameLength());
}