Java Code Examples for javax.sound.sampled.AudioSystem#getAudioFileFormat()

The following examples show how to use javax.sound.sampled.AudioSystem#getAudioFileFormat() . 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: AudioClip.java    From GpsPrune with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return length of this audio clip in seconds
 */
public int getLengthInSeconds()
{
	if (_lengthInSeconds == LENGTH_UNKNOWN)
	{
		try {
			AudioFileFormat format = null;
			if (getFile() != null)
				format = AudioSystem.getAudioFileFormat(getFile());
			else
				format = AudioSystem.getAudioFileFormat(new ByteArrayInputStream(_data));
			_lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate());
		}
		catch (Exception e) {
			_lengthInSeconds = LENGTH_NOT_AVAILABLE;
		}
	}
	return _lengthInSeconds;
}
 
Example 2
Source File: BlorbSounds.java    From petscii-bbs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected boolean putToDatabase(final Chunk chunk, final int resnum) {

  final InputStream aiffStream =
    new  MemoryAccessInputStream(chunk.getMemoryAccess(), 0,
        chunk.getSize() + Chunk.CHUNK_HEADER_LENGTH);
  try {

    final AudioFileFormat aiffFormat =
      AudioSystem.getAudioFileFormat(aiffStream);
    final AudioInputStream stream = new AudioInputStream(aiffStream,
      aiffFormat.getFormat(), (long) chunk.getSize());
    final Clip clip = AudioSystem.getClip();
    clip.open(stream);      
    sounds.put(resnum, new DefaultSoundEffect(clip));
    return true;

  } catch (Exception ex) {

    ex.printStackTrace();
  }
  return false;
}
 
Example 3
Source File: MusicController.java    From opsu 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 4
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 5
Source File: OpenWaveFile.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void check(Object source) throws Exception {
     AudioFileFormat aff2 = null;
     if (source instanceof File) {
        aff2 = AudioSystem.getAudioFileFormat((File) source);
     }
     else if (source instanceof InputStream) {
        aff2 = AudioSystem.getAudioFileFormat((InputStream) source);
     }
     else if (source instanceof URL) {
        aff2 = AudioSystem.getAudioFileFormat((URL) source);
     } else throw new Exception("wrong source. Test FAILED");
     System.out.println("Got: "+aff2);
     if (aff2.getFormat().getSampleSizeInBits()==-1) {
        throw new Exception("wrong audio format. Test FAILED");
     }
}
 
Example 6
Source File: ReadersExceptions.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
Example 7
Source File: ReadersExceptions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
Example 8
Source File: AuZeroLength.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void test(byte[] file) throws Exception {
    InputStream inputStream = new ByteArrayInputStream(file);
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);

    if (aff.getFrameLength() != 0) {
        throw new Exception("File length is "+aff.getFrameLength()+" instead of 0. test FAILED");
    }
    System.out.println(aff.getType()+" file length is 0.");
}
 
Example 9
Source File: Aiff12bit.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void test(byte[] file) throws Exception {
    InputStream inputStream = new ByteArrayInputStream(file);
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);

    if (aff.getFormat().getSampleSizeInBits() != 12) {
        throw new Exception("Wrong sample size. test FAILED");
    }
    if (aff.getFormat().getFrameSize() != 2) {
        throw new Exception("Wrong frame size. test FAILED");
    }
    if (aff.getFrameLength() != 100) {
        throw new Exception("Wrong file length. test FAILED");
    }
}
 
Example 10
Source File: RecognizeHugeAiffFiles.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(final byte bits, final int rate,
                            final int channel, final long frameLength)
        throws Exception {
    final byte[] header = createHeader(bits, rate, channel, frameLength);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);

    if (aff.getType() != AudioFileFormat.Type.AIFF) {
        throw new RuntimeException("Error");
    }

    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(bits, rate, channel, aff.getFormat());
}
 
Example 11
Source File: RepeatedFormatReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer)
        throws IOException, UnsupportedAudioFileException {
    final InputStream is = new ByteArrayInputStream(buffer);
    for (int i = 0; i < 10; ++i) {
        AudioSystem.getAudioFileFormat(is);
    }
}
 
Example 12
Source File: RecognizeWaveExtensible.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 {
    final InputStream is = new ByteArrayInputStream(data);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(is);
    System.out.println("AudioFileFormat: " + aff);
    try (AudioInputStream ais = AudioSystem.getAudioInputStream(is)) {
        System.out.println("AudioFormat: " + ais.getFormat());
    }
    System.out.println("new String(data) = " + new String(data));
}
 
Example 13
Source File: ReadersExceptions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
Example 14
Source File: RecognizeHugeWaveExtFiles.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(final int[] type, final int rate,
                            final int channel, final long size)
        throws Exception {
    final byte[] header = createHeader(type, rate, channel, size);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.WAVE) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
Example 15
Source File: RecognizeHugeWaveFiles.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(final byte[] type, final int rate,
                            final int channel, final long size)
        throws Exception {
    final byte[] header = createHeader(type, rate, channel, size);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.WAVE) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
Example 16
Source File: ReadersExceptions.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
Example 17
Source File: ReadersExceptions.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
Example 18
Source File: RecognizeHugeAuFiles.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code  MAX_INT}
 * as NOT_SPECIFIED.
 */
private static void testAFF(final byte[] type, final int rate,
                            final int channel, final long size)
        throws Exception {
    final byte[] header = createHeader(type, rate, channel, size);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.AU) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (size != MAX_UNSIGNED_INT && frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }

    final long byteLength = size + AU_HEADER;
    if (byteLength <= Integer.MAX_VALUE) {
        if (aff.getByteLength() != byteLength) {
            System.err.println("Expected: " + byteLength);
            System.err.println("Actual: " + aff.getByteLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getByteLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getByteLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
Example 19
Source File: PlayerTest.java    From DTMF-Decoder with MIT License 4 votes vote down vote up
public void testPlayFile()
{
 try
 {
	if (out != null) out.println("---  Start : "+filename+"  ---");
	File file = new File(filename);
	AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
	if (out != null) out.println("Audio Type : "+aff.getType());
	AudioInputStream in= AudioSystem.getAudioInputStream(file);
	AudioInputStream din = null;
	if (in != null)
	{
	  AudioFormat baseFormat = in.getFormat();
	  if (out != null) out.println("Source Format : "+baseFormat.toString());
	  AudioFormat  decodedFormat = new AudioFormat(
		  AudioFormat.Encoding.PCM_SIGNED,
		  baseFormat.getSampleRate(),
		  16,
		  baseFormat.getChannels(),
		  baseFormat.getChannels() * 2,
		  baseFormat.getSampleRate(),
		  false);
	  if (out != null) out.println("Target Format : "+decodedFormat.toString());
	  din = AudioSystem.getAudioInputStream(decodedFormat, in);
	  if (din instanceof PropertiesContainer)
	  {
		assertTrue("PropertiesContainer : OK",true);
	  }
	  else
	  {
		assertTrue("Wrong PropertiesContainer instance",false);
	  }
	  rawplay(decodedFormat, din);
	  in.close();		
	  if (out != null) out.println("---  Stop : "+filename+"  ---");
	  assertTrue("testPlay : OK",true);
	}
 }
 catch (Exception e)
 {
	assertTrue("testPlay : "+e.getMessage(),false);
 }
}
 
Example 20
Source File: BasicPlayer.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Inits Audio ressources from URL.
 */
protected void initAudioInputStream(final URL url) throws UnsupportedAudioFileException, IOException {
	m_audioInputStream = AudioSystem.getAudioInputStream(url);
	m_audioFileFormat = AudioSystem.getAudioFileFormat(url);
}