Class IAudioSamples


  • public class IAudioSamples
    extends IMediaData
    A set of raw (decoded) samples, plus a timestamp for when to play those
    samples relative to other items in a given {IContainer}.

    The timestamp value in decoded data is always in Microseonds.
    • Constructor Detail

      • IAudioSamples

        protected IAudioSamples​(long cPtr,
                                boolean cMemoryOwn)
        Internal Only.
      • IAudioSamples

        protected IAudioSamples​(long cPtr,
                                boolean cMemoryOwn,
                                java.util.concurrent.atomic.AtomicLong ref)
        Internal Only.
    • Method Detail

      • getCPtr

        public static long getCPtr​(IAudioSamples obj)
        Internal Only. Not part of public API. Get the raw value of the native object that obj is proxying for.
        Parameters:
        obj - The java proxy object for a native object.
        Returns:
        The raw pointer obj is proxying for.
      • getMyCPtr

        public long getMyCPtr()
        Internal Only. Not part of public API. Get the raw value of the native object that we're proxying for.
        Overrides:
        getMyCPtr in class IMediaData
        Returns:
        The raw pointer we're proxying for.
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares two values, returning true if the underlying objects in native code are the same object. That means you can have two different Java objects, but when you do a comparison, you'll find out they are the EXACT same object.
        Overrides:
        equals in class IMediaData
        Returns:
        True if the underlying native object is the same. False otherwise.
      • hashCode

        public int hashCode()
        Get a hashable value for this object.
        Overrides:
        hashCode in class IMediaData
        Returns:
        the hashable value.
      • toString

        public java.lang.String toString()
        info about this packet
        Overrides:
        toString in class java.lang.Object
        Returns:
        information about this packet
      • isComplete

        public boolean isComplete()
        Returns whether or not we think this buffer has been filled
        with data.


        Returns:
        Has setComplete() been called and the buffer populated.
      • getSampleRate

        public int getSampleRate()
        Find the sample rate of the samples in this audio buffer.


        Returns:
        The Sampling Rate of the samples in this buffer (e.g. 22050).
      • getChannels

        public int getChannels()
        Return the number of channels of the samples in this buffer. For example,
        1 is mono, 2 is stereo.


        Returns:
        The number of channels.
      • getSampleBitDepth

        public int getSampleBitDepth()
        Find out the bit-depth of the samples in this buffer.


        Returns:
        Number of bits in a raw sample (per channel)
      • getFormat

        public IAudioSamples.Format getFormat()
        Find the Format of the samples in this buffer. Right now
        only FMT_S16 is supported.


        Returns:
        The format of the samples.
      • getNumSamples

        public int getNumSamples()
        Get the number of samples in this video.


        For example, if you have 100 bytes of stereo (2-channel) 16-bit
        audio in this buffer, there are 25 samples. If you have
        100 bytes of mono (1-channel) 16-bit audio in this buffer, you
        have 50 samples.


        Returns:
        The number of samples.
      • getMaxBufferSize

        public int getMaxBufferSize()
        Returns:
        Maximum number of bytes that can be put in
        this buffer. To get the number of samples you can
        put in this IAudioSamples instance, do the following
        num_samples = getMaxBufferSize() / (getSampleSize())
      • getMaxSamples

        public int getMaxSamples()
        Returns:
        Maximum number of samples this buffer can hold.
      • getSampleSize

        public int getSampleSize()
        Returns:
        Number of bytes in a single sample of audio (including channels).
        You can also get this by getSampleBitDepth()*getChannels()/8.
      • getPts

        public long getPts()
        What is the Presentation Time Stamp of this set of audio samples.

        Returns:
        the presentation time stamp (pts)
      • setPts

        public void setPts​(long aValue)
        Set the Presentation Time Stamp for this set of samples.

        Parameters:
        aValue - the new value
      • getNextPts

        public long getNextPts()
        What would be the next Presentation Time Stamp after all the
        samples in this buffer were played?

        Returns:
        the next presentation time stamp (pts)
      • setComplete

        public void setComplete​(boolean complete,
                                int numSamples,
                                int sampleRate,
                                int channels,
                                IAudioSamples.Format format,
                                long pts)
        Call this if you modify the samples and are now done. This
        updates the pertinent information in the structure.

        Parameters:
        complete - Is this set of samples complete?
        numSamples - Number of samples in this update (note that
        4 shorts of 16-bit audio in stereo is actually 1 sample).
        sampleRate - The sample rate (in Hz) of this set of samples.
        channels - The number of channels in this set of samples.
        format - The sample-format of this set of samples.
        pts - The presentation time stamp of the starting sample in this buffer.
        Caller must ensure pts is in units of 1/1,000,000 of a second
      • setSample

        public int setSample​(int sampleIndex,
                             int channel,
                             IAudioSamples.Format format,
                             int sample)
        Sets the sample at the given index and channel to the sample. In
        theory we assume input is the given Format, and will convert
        if needed, but right now we only support FMT_S16 anyway.


        Parameters:
        sampleIndex - The zero-based index into the set of samples
        channel - The zero-based channel number. If this set of samples doesn't
        have that given channel, an error is returned.
        format - The format of the given sample
        sample - The actual sample

        Returns:
        >= 0 on success; -1 on error.
      • getSample

        public int getSample​(int sampleIndex,
                             int channel,
                             IAudioSamples.Format format)
        Get the sample at the given sampleIndex and channel, and return it in
        the asked for format.

        Parameters:
        sampleIndex - The zero-based index into this set of samples.
        channel - The zero-based channel to get the sample from
        format - The format to return in

        Returns:
        The sample if available. If that sample is not available
        (e.g. because the channel doesn't exist, or the samples have not
        been #setComplete(bool, int32_t, int32_t, int32_t, Format, int64_t)),
        then this method returns 0. It is up to the caller to ensure
        the inputs are valid given that 0 is also a valid sample value.
      • findSampleBitDepth

        public static int findSampleBitDepth​(IAudioSamples.Format format)
        A convenience method that returns the # of bits in a given
        format. Be aware that right now this library only supports
        16-bit audio.

        Parameters:
        format - The format you want to find the number of bits in.


        Returns:
        The number of bits (not bytes) in the passed in format.
      • make

        public static IAudioSamples make​(int numSamples,
                                         int numChannels)
        Get a new audio samples buffer.


        Note that any buffers this objects needs will be
        lazily allocated (i.e. we won't actually grab all
        the memory until we need it).


        Parameters:
        numSamples - The minimum number of samples you're
        going to want to put in this buffer. We may (and probably
        will) return a larger buffer, but you cannot assume that.
        numChannels - The number of channels in the audio you'll
        want to put in this buffer.
        Returns:
        A new object, or null if we can't allocate one.
      • samplesToDefaultPts

        public static long samplesToDefaultPts​(long samples,
                                               int sampleRate)
        Converts a number of samples at a given sampleRate into
        Microseconds.
        Parameters:
        samples - Number of samples.
        sampleRate - sample rate that those samples are recorded at.
        Returns:
        number of microseconds it would take to play that audio.
      • defaultPtsToSamples

        public static long defaultPtsToSamples​(long duration,
                                               int sampleRate)
        Converts a duration in microseconds into
        a number of samples, assuming a given sampleRate.
        Parameters:
        duration - The duration in microseconds.
        sampleRate - sample rate that you want to use.
        Returns:
        The number of samples it would take (at the given sampleRate) to take duration microseconds to play.
      • make

        public static IAudioSamples make​(IBuffer buffer,
                                         int channels,
                                         IAudioSamples.Format format)
        Creates an {IAudioSamples} object by wrapping an
        {com.avpkit.ferry.IBuffer object}.


        If you are decoding into this buffer, the buffer must be at least
        192k*channels large (an FFmpeg requirement) or the decodeAudio
        call on {IStreamCoder} will fail with an error.
        If you are encoding from, any size should do.


        Parameters:
        buffer - the buffer to wrap
        channels - the number of channels of audio you will put it the buffer
        format - the audio sample format

        Returns:
        a new {IAudioSamples} object, or null on error.
      • make

        public static IAudioSamples make​(int numSamples,
                                         int numChannels,
                                         IAudioSamples.Format format)
        Get a new audio samples buffer.


        Note that any buffers this objects needs will be
        lazily allocated (i.e. we won't actually grab all
        the memory until we need it).


        Parameters:
        numSamples - The minimum number of samples you're
        going to want to put in this buffer. We may (and probably
        will) return a larger buffer, but you cannot assume that.
        numChannels - The number of channels in the audio you'll
        want to put in this buffer.
        format - The format of this buffer
        Returns:
        A new object, or null if we can't allocate one.