Class AudioTranscoder

java.lang.Object
org.openhab.core.audio.transcode.AudioTranscoder

@NonNullByDefault public final class AudioTranscoder extends Object
Utility class for transcoding AudioStreams using the Java Sound API.

Supports transcoding between AudioFormat.WAV, AudioFormat.PCM_SIGNED, AudioFormat.FLAC, and all PCM variations supported by the Java Sound API (signed/unsigned PCM, A-law, μ-law), including sample rate resampling, bit-depth conversion, and endianness changes.

Author:
Florian Hotze - Initial contribution
  • Field Details

    • SUPPORTED_SOURCE_FORMATS

      public static final Set<AudioFormat> SUPPORTED_SOURCE_FORMATS
    • SUPPORTED_TARGET_FORMATS

      public static final Set<AudioFormat> SUPPORTED_TARGET_FORMATS
  • Method Details

    • getSupportedSourceFormats

      public static Set<AudioFormat> getSupportedSourceFormats()
      Gets the set of general audio formats supported for decoding (reading as source stream).
      Returns:
      supported source audio formats
    • getSupportedTargetFormats

      public static Set<AudioFormat> getSupportedTargetFormats()
      Gets the set of general audio formats supported for encoding (writing as target stream).
      Returns:
      supported target audio formats
    • isSupported

      public static boolean isSupported(@Nullable AudioFormat format)
      Checks if the given audio format is supported by this transcoder (either for decoding or encoding).
      Parameters:
      format - the format to check
      Returns:
      true if the format is supported, false otherwise
    • isSourceSupported

      public static boolean isSourceSupported(@Nullable AudioFormat format)
      Checks if the given audio format can be decoded from a source stream.
      Parameters:
      format - the format to check
      Returns:
      true if the format can be decoded, false otherwise
    • isTargetSupported

      public static boolean isTargetSupported(@Nullable AudioFormat format)
      Checks if the given audio format can be encoded to a target stream.
      Parameters:
      format - the format to check
      Returns:
      true if the format can be encoded, false otherwise
    • canTranscode

      public static boolean canTranscode(@Nullable AudioFormat sourceFormat, @Nullable AudioFormat targetFormat)
      Checks whether this transcoder can transcode from the source format to the target format.
      Parameters:
      sourceFormat - the source format
      targetFormat - the target format
      Returns:
      true if transcoding is possible, false otherwise
    • transcode

      public static AudioStream transcode(AudioStream source, AudioFormat targetFormat) throws AudioTranscodingException
      Transcodes the given audio stream to the requested target format.

      Favors streaming via PipedAudioStream for lower latency without disk buffering.

      Parameters:
      source - the audio stream to transcode
      targetFormat - the desired target format
      Returns:
      a new audio stream in the requested target format
      Throws:
      AudioTranscodingException - if transcoding fails or formats are unsupported
    • transcode

      public static AudioStream transcode(AudioStream source, AudioFormat targetFormat, Set<Class<? extends AudioStream>> supportedStreams) throws AudioTranscodingException
      Transcodes the given audio stream to the requested target format, taking into account supported stream types.
      Parameters:
      source - the audio stream to transcode
      targetFormat - the desired target format
      supportedStreams - the set of stream classes supported by the consumer/sink
      Returns:
      a new audio stream in the requested target format
      Throws:
      AudioTranscodingException - if transcoding fails or formats are unsupported
    • transcode

      public static AudioStream transcode(AudioStream source, AudioFormat targetFormat, boolean requireSizeable) throws AudioTranscodingException
      Transcodes the given audio stream to the requested target format.
      Parameters:
      source - the audio stream to transcode
      targetFormat - the desired target format
      requireSizeable - true to buffer to disk and return a SizeableAudioStream, false to stream via pipe
      Returns:
      a new audio stream in the requested target format
      Throws:
      AudioTranscodingException - if transcoding fails or formats are unsupported
    • transcodeToSupported

      public static AudioStream transcodeToSupported(AudioStream source, Set<AudioFormat> candidateFormats) throws AudioTranscodingException
      Transcodes the audio stream to a format supported by candidateFormats (e.g. from an AudioSink).
      Parameters:
      source - the audio stream to transcode
      candidateFormats - the acceptable target formats
      Returns:
      an audio stream compatible with one of the candidateFormats
      Throws:
      AudioTranscodingException - if no route to any candidate format exists or transcoding fails
    • transcodeToSupported

      public static AudioStream transcodeToSupported(AudioStream source, Set<AudioFormat> candidateFormats, Set<Class<? extends AudioStream>> supportedStreams) throws AudioTranscodingException
      Transcodes the audio stream to a format and stream type supported by candidateFormats and supportedStreams (e.g. from an AudioSink).
      Parameters:
      source - the audio stream to transcode
      candidateFormats - the acceptable target formats
      supportedStreams - the set of stream classes supported by the consumer/sink
      Returns:
      an audio stream compatible with one of the candidateFormats
      Throws:
      AudioTranscodingException - if no route to any candidate format exists or transcoding fails
    • transcodeToSupported

      public static AudioStream transcodeToSupported(AudioStream source, Set<AudioFormat> candidateFormats, boolean requireSizeable) throws AudioTranscodingException
      Transcodes the audio stream to a format supported by candidateFormats (e.g. from an AudioSink).
      Parameters:
      source - the audio stream to transcode
      candidateFormats - the acceptable target formats
      requireSizeable - true to buffer to disk and return a SizeableAudioStream, false to stream via pipe
      Returns:
      an audio stream compatible with one of the candidateFormats
      Throws:
      AudioTranscodingException - if no route to any candidate format exists or transcoding fails