javax.sound.sampled.AudioFileFormat Java Examples

The following examples show how to use javax.sound.sampled.AudioFileFormat. 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: AiffFileWriter.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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];
    }
 
Example #2
Source File: MusicController.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the duration of the current track, in milliseconds.
 * Currently only works for MP3s.
 * @return the duration, or -1 if no track exists, else the {@code endTime}
 *         field of the beatmap loaded
 * @author Tom Brito (http://stackoverflow.com/a/3056161)
 */
public static int getDuration() {
	if (!trackExists() || lastBeatmap == null)
		return -1;

	if (duration == 0) {
		// TAudioFileFormat method only works for MP3s
		if (lastBeatmap.audioFilename.getName().endsWith(".mp3")) {
			try {
				AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(lastBeatmap.audioFilename);
				if (fileFormat instanceof TAudioFileFormat) {
					Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
					Long microseconds = (Long) properties.get("duration");
					duration = (int) (microseconds / 1000);
					return duration;
				}
			} catch (UnsupportedAudioFileException | IOException e) {}
		}

		// fallback: use beatmap end time (often not the track duration)
		duration = lastBeatmap.endTime;
	}
	return duration;
}
 
Example #3
Source File: AiffFileReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an audio stream from the File provided.  The File must
 * point to valid audio file data.
 * @param file the File for which the <code>AudioInputStream</code> should be
 * constructed
 * @return an <code>AudioInputStream</code> object based on the audio file data pointed
 * to by the File
 * @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 AudioInputStream getAudioInputStream(File file)
    throws UnsupportedAudioFileException, IOException {

    FileInputStream fis = new FileInputStream(file); // throws IOException
    AudioFileFormat fileFormat = null;
    // part of fix for 4325421
    try {
        fileFormat = getCOMM(fis, false);
    } finally {
        if (fileFormat == null) {
            fis.close();
        }
    }
    return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength());
}
 
Example #4
Source File: WaveExtensibleFileReader.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public AudioInputStream getAudioInputStream(InputStream stream)
        throws UnsupportedAudioFileException, IOException {

    AudioFileFormat format = getAudioFileFormat(stream);
    RIFFReader riffiterator = new RIFFReader(stream);
    if (!riffiterator.getFormat().equals("RIFF"))
        throw new UnsupportedAudioFileException();
    if (!riffiterator.getType().equals("WAVE"))
        throw new UnsupportedAudioFileException();
    while (riffiterator.hasNextChunk()) {
        RIFFReader chunk = riffiterator.nextChunk();
        if (chunk.getFormat().equals("data")) {
            return new AudioInputStream(chunk, format.getFormat(), chunk
                    .getSize());
        }
    }
    throw new UnsupportedAudioFileException();
}
 
Example #5
Source File: WaveFileWriter.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {

        //$$fb the following check must come first ! Otherwise
        // the next frame length check may throw an IOException and
        // interrupt iterating File Writers. (see bug 4351296)

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);

        //$$fb when we got this far, we are committed to write this file

        // we must know the total data length to calculate the file length
        if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
            throw new IOException("stream length not specified");
        }

        int bytesWritten = writeWaveFile(stream, waveFileFormat, out);
        return bytesWritten;
    }
 
Example #6
Source File: WaveFloatFileReader.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public AudioInputStream getAudioInputStream(InputStream stream)
        throws UnsupportedAudioFileException, IOException {

    AudioFileFormat format = getAudioFileFormat(stream);
    RIFFReader riffiterator = new RIFFReader(stream);
    if (!riffiterator.getFormat().equals("RIFF"))
        throw new UnsupportedAudioFileException();
    if (!riffiterator.getType().equals("WAVE"))
        throw new UnsupportedAudioFileException();
    while (riffiterator.hasNextChunk()) {
        RIFFReader chunk = riffiterator.nextChunk();
        if (chunk.getFormat().equals("data")) {
            return new AudioInputStream(chunk, format.getFormat(),
                    chunk.getSize());
        }
    }
    throw new UnsupportedAudioFileException();
}
 
Example #7
Source File: AiffFileWriter.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    //$$fb the following check must come first ! Otherwise
    // the next frame length check may throw an IOException and
    // interrupt iterating File Writers. (see bug 4351296)

    // throws IllegalArgumentException if not supported
    AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);

    // we must know the total data length to calculate the file length
    if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
        throw new IOException("stream length not specified");
    }

    return writeAiffFile(stream, aiffFileFormat, out);
}
 
Example #8
Source File: WaveFileFormat.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
WaveFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) {

        super(type,lengthInBytes,format,lengthInFrames);

        AudioFormat.Encoding encoding = format.getEncoding();

        if( encoding.equals(AudioFormat.Encoding.ALAW) ) {
            waveType = WAVE_FORMAT_ALAW;
        } else if( encoding.equals(AudioFormat.Encoding.ULAW) ) {
            waveType = WAVE_FORMAT_MULAW;
        } else if( encoding.equals(AudioFormat.Encoding.PCM_SIGNED) ||
                   encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) ) {
            waveType = WAVE_FORMAT_PCM;
        } else {
            waveType = WAVE_FORMAT_UNKNOWN;
        }
    }
 
Example #9
Source File: AuFileWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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];
    }
 
Example #10
Source File: WaveExtensibleFileReader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #11
Source File: WaveFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)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 = writeWaveFile(stream, waveFileFormat, 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( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip RIFF magic
            raf.skipBytes(4);
            raf.writeInt(big2little( riffLength ));
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
            raf.writeInt(big2little( dataLength ));
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example #12
Source File: AuFileWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {

        // 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);

        int bytesWritten = writeAuFile(stream, auFileFormat, out);
        return bytesWritten;
    }
 
Example #13
Source File: WaveFloatFileWriter.java    From TencentKona-8 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 #14
Source File: WaveExtensibleFileReader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #15
Source File: WriterCloseInput.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    test(AudioFileFormat.Type.AIFF);
    test(AudioFileFormat.Type.AU);
    test(AudioFileFormat.Type.WAVE);

    if (testFailed == 0) {
        out("All tests passed.");
    } else {
        out("" + testFailed + " of " + testTotal + " tests FAILED.");
        System.out.flush();
        throw new RuntimeException("Test FAILED.");
    }
}
 
Example #16
Source File: WaveFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)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 = writeWaveFile(stream, waveFileFormat, 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( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip RIFF magic
            raf.skipBytes(4);
            raf.writeInt(big2little( riffLength ));
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
            raf.writeInt(big2little( dataLength ));
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example #17
Source File: SoftMidiAudioFileReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(Sequence seq)
        throws UnsupportedAudioFileException, IOException {

    long totallen = seq.getMicrosecondLength() / 1000000;
    long len = (long) (format.getFrameRate() * (totallen + 4));
    return new AudioFileFormat(MIDI, format, (int) len);
}
 
Example #18
Source File: WaveExtensibleFileReader.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #19
Source File: AiffFileReader.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 {
    AudioFileFormat fileFormat = null;
    FileInputStream fis = new FileInputStream(file);       // throws IOException
    // part of fix for 4325421
    try {
        fileFormat = getCOMM(fis, false);
    } finally {
        fis.close();
    }

    return fileFormat;
}
 
Example #20
Source File: WaveExtensibleFileReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #21
Source File: DesktopAudioRecordingService.java    From attach with GNU General Public License v3.0 5 votes vote down vote up
private void save(String fileName) throws IOException {
    byte[] audioData = recordBytes.toByteArray();
    final File wavFile = new File(getAudioFolder(), fileName + ".wav");
    ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
    try (AudioInputStream audioInputStream = new AudioInputStream(bais, format, audioData.length / format.getFrameSize())) {
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wavFile);
    }
    recordBytes.close();
    if (debug) {
        LOG.log(Level.INFO, String.format("File %s.wav added to %s", fileName, getAudioFolder()));
    }
    addChunk.apply(fileName + ".wav");
}
 
Example #22
Source File: WAVReader.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
    InputStream is = url.openStream();
    try{
        return getAudioFileFormat(is);
    }
    finally{
        is.close();
    }
}
 
Example #23
Source File: WaveFloatFileReader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(URL url)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = url.openStream();
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #24
Source File: WaveExtensibleFileReader.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #25
Source File: SoftMidiAudioFileReader.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(Sequence seq)
        throws UnsupportedAudioFileException, IOException {

    long totallen = seq.getMicrosecondLength() / 1000000;
    long len = (long) (format.getFrameRate() * (totallen + 4));
    return new AudioFileFormat(MIDI, format, (int) len);
}
 
Example #26
Source File: WaveFloatFileReader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(File file)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = new FileInputStream(file);
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #27
Source File: WaveFloatFileReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFileFormat getAudioFileFormat(URL url)
        throws UnsupportedAudioFileException, IOException {
    InputStream stream = url.openStream();
    AudioFileFormat format;
    try {
        format = getAudioFileFormat(new BufferedInputStream(stream));
    } finally {
        stream.close();
    }
    return format;
}
 
Example #28
Source File: AuFileWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // 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: WaveFileWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)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 = writeWaveFile(stream, waveFileFormat, 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( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip RIFF magic
            raf.skipBytes(4);
            raf.writeInt(big2little( riffLength ));
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
            raf.writeInt(big2little( dataLength ));
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example #30
Source File: AudioFileWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Indicates whether an audio file of the type specified can be written
 * from the audio input stream indicated.
 * @param fileType file type for which write capabilities are queried
 * @param stream for which file writing support is queried
 * @return <code>true</code> if the file type is supported for this audio input stream,
 * otherwise <code>false</code>
 */
public boolean isFileTypeSupported(AudioFileFormat.Type fileType, AudioInputStream stream) {

    AudioFileFormat.Type types[] = getAudioFileTypes( stream );

    for(int i=0; i<types.length; i++) {
        if( fileType.equals( types[i] ) ) {
            return true;
        }
    }
    return false;
}