Class SimpleAudioConversion


  • public class SimpleAudioConversion
    extends Object
    Performs rudimentary audio format conversion.

    Example usage:

    
     AudioInputStream ais = ... ;
     SourceDataLine  line = ... ;
     AudioFormat      fmt = ... ;
    
     // do prep
    
     for (int blen = 0; (blen = ais.read(bytes)) > -1;) {
         int slen;
         slen = SimpleAudioConversion.unpack(bytes, samples, blen, fmt);
    
         // do something with samples
    
         blen = SimpleAudioConversion.pack(samples, bytes, slen, fmt);
         line.write(bytes, 0, blen);
     }
     
    Author:
    Radiodef
    See Also:
    Overview on StackOverflow.com
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int bytesPerSample​(int bitsPerSample)
      Computes the block-aligned bytes per sample of the audio format, with (int) ceil(bitsPerSample / 8.0).
      static double fullScale​(int bitsPerSample)
      Computes the largest magnitude representable by the audio format, with pow(2.0, bitsPerSample - 1).
      static int pack​(float[] samples, byte[] bytes, int slen, AudioFormat fmt)
      Converts: from an audio sample array (float[]) to a byte array (byte[]).
      static int unpack​(byte[] bytes, gnu.trove.list.TFloatList samples, int blen, AudioFormat fmt)
      Converts: from a byte array (byte[]) to an audio sample array (float[]).
    • Method Detail

      • unpack

        public static int unpack​(byte[] bytes,
                                 gnu.trove.list.TFloatList samples,
                                 int blen,
                                 AudioFormat fmt)
        Converts:
        • from a byte array (byte[])
        • to an audio sample array (float[]).
        Parameters:
        bytes - the byte array, filled by the InputStream.
        samples - an array to fill up with audio samples.
        blen - the return value of InputStream.read.
        fmt - the source AudioFormat.
        Returns:
        the number of valid audio samples converted.
        Throws:
        NullPointerException - if bytes, samples or fmt is null
        ArrayIndexOutOfBoundsException - if (bytes.length < blen) or (samples.length < blen / bytesPerSample(fmt.getBitsPerSample())).
      • pack

        public static int pack​(float[] samples,
                               byte[] bytes,
                               int slen,
                               AudioFormat fmt)
        Converts:
        • from an audio sample array (float[])
        • to a byte array (byte[]).
        Parameters:
        samples - an array of audio samples to encode.
        bytes - an array to fill up with bytes.
        slen - the return value of unpack.
        fmt - the destination AudioFormat.
        Returns:
        the number of valid bytes converted.
        Throws:
        NullPointerException - if samples, bytes or fmt is null
        ArrayIndexOutOfBoundsException - if (samples.length < slen) or (bytes.length < slen * bytesPerSample(fmt.getSampleSizeInBits()))
      • bytesPerSample

        public static int bytesPerSample​(int bitsPerSample)
        Computes the block-aligned bytes per sample of the audio format, with (int) ceil(bitsPerSample / 8.0).

        This is generally equivalent to the optimization ((bitsPerSample + 7) >>> 3). (Except for the invalid argument bitsPerSample <= 0.)

        Round towards the ceiling because formats that allow bit depths in non-integral multiples of 8 typically pad up to the nearest integral multiple of 8. So for example, a 31-bit AIFF file will actually store 32-bit blocks.

        Parameters:
        bitsPerSample - the return value of AudioFormat.getSampleSizeInBits.
        Returns:
        The block-aligned bytes per sample of the audio format.
      • fullScale

        public static double fullScale​(int bitsPerSample)
        Computes the largest magnitude representable by the audio format, with pow(2.0, bitsPerSample - 1).

        For bitsPerSample < 64, this is generally equivalent to the optimization (1L << (bitsPerSample - 1L)). (Except for the invalid argument bitsPerSample <= 0.)

        The result is returned as a double because, in the case that bitsPerSample == 64, a long would overflow.

        Parameters:
        bitsPerSample - the return value of AudioFormat.getBitsPerSample.
        Returns:
        the largest magnitude representable by the audio format.