AVPKit
AudioSamples.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef AUDIOSAMPLES_H_
21 #define AUDIOSAMPLES_H_
22 
23 #include <com/avpkit/ferry/RefPointer.h>
24 #include <com/avpkit/core/AVPKit.h>
25 #include <com/avpkit/core/IAudioSamples.h>
26 #include <com/avpkit/ferry/IBuffer.h>
27 #include <com/avpkit/core/IRational.h>
28 
29 namespace com { namespace avpkit { namespace core
30 {
31 
32  class AudioSamples : public IAudioSamples
33  {
34  VS_JNIUTILS_REFCOUNTED_OBJECT_PRIVATE_MAKE(AudioSamples)
35  public:
36  // IMediaData
37  virtual int64_t getTimeStamp() { return getPts(); }
38  virtual void setTimeStamp(int64_t aTimeStamp) { setPts(aTimeStamp); }
39  virtual IRational* getTimeBase() { return mTimeBase.get(); }
40  virtual void setTimeBase(IRational *aBase) { mTimeBase.reset(aBase, true); }
41  virtual int32_t getSize() { return getNumSamples()*getSampleSize(); }
42  virtual bool isKey() { return true; }
43 
44  // IAudioSamples
45  virtual bool isComplete();
46  virtual int32_t getSampleRate();
47  virtual int32_t getChannels();
48 
49  virtual IAudioSamples::ChannelLayout getChannelLayout();
50 
51  virtual Format getFormat();
52  virtual int32_t getSampleBitDepth();
53 
54  virtual int32_t getNumSamples();
55  virtual int32_t getMaxBufferSize();
56  virtual int32_t getSampleSize();
57  virtual int32_t getMaxSamples();
59  virtual int64_t getPts();
60  virtual void setPts(int64_t aValue);
61  virtual int64_t getNextPts();
62  virtual int32_t setSample(int32_t sampleIndex, int32_t channel, Format format, int32_t sample);
63  virtual int32_t getSample(int32_t sampleIndex, int32_t channel, Format format);
64  virtual void setData(com::avpkit::ferry::IBuffer* buffer);
65  virtual void setComplete(bool complete, int32_t numSamples,
66  int32_t sampleRate, int32_t channels, Format sampleFmt,
67  int64_t pts);
68 
69  virtual void setComplete(bool complete, int32_t numSamples,
70  int32_t sampleRate, int32_t channels, ChannelLayout channelLayout,
71  Format format, int64_t pts);
72 
73 
74 
75  /*
76  * Convenience method that from C++ returns the buffer
77  * managed by getData() above.
78  *
79  * @param startingSample The sample to start the array at.
80  * That means that only getNumSamples()-startingSample
81  * samples are available in this AudioSamples collection.
82  */
83  virtual short *getRawSamples(uint32_t startingSample);
84 
88  virtual int32_t ensureCapacity(int32_t capacityInBytes);
89 
90  /*
91  * This creates an audio sample.
92  */
93  static AudioSamples* make(int32_t numSamples,
94  int32_t numChannels);
95  static AudioSamples* make(int32_t numSamples,
96  int32_t numChannels, IAudioSamples::Format);
97 
98  static AudioSamples* make(com::avpkit::ferry::IBuffer* buffer, int32_t channels,
99  IAudioSamples::Format format);
100  protected:
101  AudioSamples();
102  virtual ~AudioSamples();
103  private:
104  void allocInternalSamples();
105  static void setBufferType(IAudioSamples::Format format,
106  com::avpkit::ferry::IBuffer * buffer);
109  int32_t mNumSamples;
110  int32_t mRequestedSamples;
111  int32_t mSampleRate;
112  int32_t mChannels;
113  ChannelLayout mChannelLayout;
114  int32_t mIsComplete;
115  Format mSampleFmt;
116  int64_t mPts;
117  };
118 
119 }}}
120 
121 #endif /*AUDIOSAMPLES_H_*/
virtual int32_t getSampleRate()
Find the sample rate of the samples in this audio buffer.
virtual int64_t getTimeStamp()
Get the time stamp of this object in getTimeBase() units.
Definition: AudioSamples.h:37
virtual Format getFormat()
Find the Format of the samples in this buffer.
virtual int32_t getSize()
Get the size in bytes of the raw data available for this object.
Definition: AudioSamples.h:41
virtual int32_t getChannels()
Return the number of channels of the samples in this buffer.
virtual IRational * getTimeBase()
Get the time base that time stamps of this object are represented in.
Definition: AudioSamples.h:39
virtual com::avpkit::ferry::IBuffer * getData()
Get any underlying raw data available for this object.
virtual int32_t ensureCapacity(int32_t capacityInBytes)
Called by decoder before decoding to ensure sufficient space.
virtual void setTimeBase(IRational *aBase)
Set the time base that time stamps of this object are represented in.
Definition: AudioSamples.h:40
virtual bool isKey()
Is this object a key object? i.e.
Definition: AudioSamples.h:42
virtual void setComplete(bool complete, int32_t numSamples, int32_t sampleRate, int32_t channels, Format sampleFmt, int64_t pts)
Call this if you modify the samples and are now done.
virtual int64_t getPts()
What is the Presentation Time Stamp of this set of audio samples.
virtual int32_t getMaxBufferSize()
virtual void setPts(int64_t aValue)
Set the Presentation Time Stamp for this set of samples.
virtual int32_t setSample(int32_t sampleIndex, int32_t channel, Format format, int32_t sample)
Sets the sample at the given index and channel to the sample.
virtual void setTimeStamp(int64_t aTimeStamp)
Set the time stamp for this object in getTimeBase() units.
Definition: AudioSamples.h:38
virtual int32_t getSample(int32_t sampleIndex, int32_t channel, Format format)
Get the sample at the given sampleIndex and channel, and return it in the asked for format.
virtual int32_t getNumSamples()
Get the number of samples in this video.
virtual int32_t getSampleBitDepth()
Find out the bit-depth of the samples in this buffer.
virtual void setData(com::avpkit::ferry::IBuffer *buffer)
Sets the underlying buffer used by this object.
virtual bool isComplete()
Returns whether or not we think this buffer has been filled with data.
virtual int64_t getNextPts()
What would be the next Presentation Time Stamp after all the samples in this buffer were played?
A set of raw (decoded) samples, plus a timestamp for when to play those samples relative to other ite...
Definition: IAudioSamples.h:38
Format
The format we use to represent audio.
Definition: IAudioSamples.h:46
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...