Skip to content

Extended audio file format support for the Java Platform, through plugins for the javax.sound.* package.

Notifications You must be signed in to change notification settings

jseproject/jse-spi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaSound Enhancement Project - Service Provider Interface

JSE-SPI provides extended audio file format support for the Java Platform, through plugins for the javax.sound.* package.

The main goal of this project is to provide support for formats not covered by the JRE itself. Support for these formats is important, to be able to read data found "in the wild", as well as to maintain access to data in legacy formats. As there is lots of modern data out there, we see the need for open implementations of readers for popular formats.

Note that it's NOT recommended to use codecs bundled in this library on non-standard JREs such as Android Runtime. Use solutions more suitable for these platforms!

API

Basic Usage

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;

public class PlaybackExample {
    
    public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException {

        // Step 1. get the encoded AudioInputStream
        try (AudioInputStream encodedInputStream = AudioSystem.getAudioInputStream(new File("/path/to/your/audio"))) {

            // Step 2. decode the input AudioInputStream
            AudioFormat encodedFormat = encodedInputStream.getFormat();
            AudioInputStream decodedInputStream = AudioSystem.getAudioInputStream(
                    new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, encodedFormat.getSampleRate(), 16,
                            encodedFormat.getChannels(), 2 * encodedFormat.getChannels(), encodedFormat.getFrameRate(),
                            encodedFormat.isBigEndian(), encodedFormat.properties()),
                    encodedInputStream);

            // Step 3. resample for playback
            AudioFormat decodedFormat = decodedInputStream.getFormat();
            AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(
                    new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, decodedFormat.getSampleSizeInBits(),
                            decodedFormat.getChannels(), decodedFormat.getFrameSize(), 44100,
                            decodedFormat.isBigEndian(), decodedFormat.properties()),
                    decodedInputStream);

            // Step 4. playback
            AudioFormat playbackFormat = playbackInputStream.getFormat();
            SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(playbackFormat);
            sourceDataLine.open();
            sourceDataLine.start();
            byte[] buffer = new byte[44100];
            for (int read = playbackInputStream.read(buffer); read != -1; read = playbackInputStream.read(buffer)) {
                sourceDataLine.write(buffer, 0, read);
            }
            sourceDataLine.drain();
            sourceDataLine.close();

        }

    }
    
}

SPI

Sampled

Module Format Encoder Decoder Channel Sample Rate (kHz) Codec PCM File Types
Container Formats
jse-spi-flac FLAC YES YES 1-8 0.001-48 u8, s16, u24
  • FLAC (.flac)
  • Ogg (.ogg)
jse-spi-opus Opus YES YES 1-2 8, 12, 16, 24, 48 s16
  • Ogg (.opus, .ogg)
jse-spi-vorbis Vorbis YES YES 1-2 8, 11.025, 12, 16, 22.05, 24, 32, 44.1, 48 s16
  • Ogg (.ogg)
jse-spi-speex Speex-NB YES YES 1-2 8 s16
  • Ogg (.spx, .ogg)
Speex-WB YES YES 1-2 16 s16
Speex-UWB YES YES 1-2 32 s16
jse-spi-mp3 MP3 YES YES 1-2 8, 11.025, 12, 16, 22.05, 24, 32, 44.1, 48 u8, s16, u24
  • MP3 (.mp3)
MP2 YES 1-2 u8, s16, u24
  • MP2 (.mp2)
MP1 YES 1-2 u8, s16, u24
  • MP1 (.mp1)
jse-spi-aac AAC LC YES 1-8 8, 11.025, 12, 16, 22.05, 24, 44.1, 48, 64, 88.2, 96 s16
  • ADTS raw AAC (.aac)
HE-AACv1
(AAC+)
YES 1-8 s16
HE-AACv2
(enhanced
AAC+)
YES 1-8 s16

Installing

Maven

<dependencies>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-flac</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-opus</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-vorbis</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-speex</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-mp3</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>io.github.jseproject</groupId>
        <artifactId>jse-spi-aac</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

Gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.github.jseproject:jse-spi-flac:1.0.1'
    implementation 'io.github.jseproject:jse-spi-opus:1.0.1'
    implementation 'io.github.jseproject:jse-spi-vorbis:1.0.1'
    implementation 'io.github.jseproject:jse-spi-speex:1.0.1'
    implementation 'io.github.jseproject:jse-spi-mp3:1.0.2'
    implementation 'io.github.jseproject:jse-spi-aac:1.0.2'
}

License

Module License
jse-spi-flac Xiph.Org Variant of the BSD License
jse-spi-opus Xiph.Org Variant of the BSD License
jse-spi-vorbis Xiph.Org Variant of the BSD License
jse-spi-speex Xiph.Org Variant of the BSD License
jse-spi-mp3 LGPL-2.1
jse-spi-aac BSD 2-Clause

Dependencies

Module Dependency License Compile Runtime
ALL JavaSound Enhancement Project API BSD 3-Clause YES YES
ALL Tritonus Share LGPL-2.1 YES YES
jse-spi-flac VorbisJava Core Apache-2.0 YES YES
jse-spi-opus
jse-spi-vorbis
jse-spi-speex

About

Extended audio file format support for the Java Platform, through plugins for the javax.sound.* package.

Topics

Resources

Stars

Watchers

Forks

Languages