hendriks73 / ffsampledsp

FFmpeg based service provider for javax.sound.sampled.
GNU Lesser General Public License v2.1
24 stars 5 forks source link
audio audio-decoder ffmpeg java java-library macos sound ubuntu windows

LGPL 2.1 Maven Central Build and Test CodeCov

FFSampledSP

FFSampledSP is an implementation of the javax.sound.sampled service provider interfaces based on FFmpeg, a complete, cross-platform solution to record, convert and stream audio and video. FFSampledSP is part of the SampledSP collection of javax.sound.sampled libraries.

Its main purpose is to decode audio files or streams to signed linear PCM.

Supported platforms are currently:

FFSampledSP makes use of the tagtraum FFmpeg package.

Binaries and more info can be found at its tagtraum home.

Installation

FFSampledSP is released via Maven. You can install it via the following dependency:

<dependencies>
    <dependency>
        <groupId>com.tagtraum</groupId>
        <artifactId>ffsampledsp-complete</artifactId>
    </dependency>
</dependencies>

Usage

To use the library, simply use javax.sound.sampled like you normally would.

Note that opening an AudioInputStream of compressed audio (e.g. mp3), does not decode the stream. To obtain PCM you still have to transcode to PCM like this:

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class DecodeExample {
    public static void main(final String[] args) {
        // compressed stream
        final AudioInputStream mp3In = AudioSystem.getAudioInputStream(new File(args[0]));
        // AudioFormat describing the compressed stream
        final AudioFormat mp3Format = mp3In.getFormat();
        // AudioFormat describing the desired decompressed stream 
        final AudioFormat pcmFormat = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            mp3Format.getSampleRate(),
            16,
            mp3Format.getChannels(),
            16 * mp3Format.getChannels() / 8,
            mp3Format.getSampleRate(),
            mp3Format.isBigEndian()
            );
        // actually decompressed stream (signed PCM)
        final AudioInputStream pcmIn = AudioSystem.getAudioInputStream(mp3In, pcmFormat);
        // do something with the raw audio stream pcmIn... 
    }
}

Build

You can build this library locally on macOS, Windows, or Linux (Ubuntu is tested). When doing so, only the appropriate native libraries are included in the "complete" jar. The GitHub-based build also adds native libraries for other platforms.

To do so, you also need:

Note, that the C sources in the ffsampledsp-x86_64-macos module are expected to compile on all supported platforms. In fact, the very same sources are compiled in the modules for other platforms.

Release Notes

You can find the release notes/history here.