AVPKit
IAudioSamples.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 IAUDIOSAMPLES_H_
21 #define IAUDIOSAMPLES_H_
22 
23 #include <com/avpkit/core/AVPKit.h>
24 #include <com/avpkit/core/IMediaData.h>
25 #include <com/avpkit/core/IRational.h>
26 #include <com/avpkit/ferry/IBuffer.h>
27 namespace com { namespace avpkit { namespace core
28 {
29 
37  class VS_API_AVPKIT IAudioSamples : public IMediaData
38  {
39  public:
40  //IAudioSamples
41 
46  typedef enum Format {
47  FMT_NONE = -1,
53 
59  } Format;
60 
68  virtual bool isComplete()=0;
69 
76  virtual int32_t getSampleRate()=0;
77 
85  virtual int32_t getChannels()=0;
86 
93  virtual int32_t getSampleBitDepth()=0;
94 
102  virtual Format getFormat()=0;
103 
116  virtual int32_t getNumSamples()=0;
117 
124  virtual int32_t getMaxBufferSize()=0;
125 
129  virtual int32_t getMaxSamples()=0;
130 
135  virtual int32_t getSampleSize()=0;
136 
142  virtual int64_t getPts()=0;
143 
149  virtual void setPts(int64_t aValue)=0;
150 
157  virtual int64_t getNextPts()=0;
158 
172  virtual void setComplete(bool complete, int32_t numSamples,
173  int32_t sampleRate, int32_t channels, Format format,
174  int64_t pts)=0;
175 
176 #ifndef SWIG
177  /*
178  * Convenience method that from C++ returns the buffer
179  * managed by getData() above.
180  *
181  * @param startingSample The sample to start the array at.
182  * That means that only getNumSamples()-startingSample
183  * samples are available in this AudioSamples collection.
184  */
185  virtual short *getRawSamples(uint32_t startingSample) = 0;
186 #endif
187 
202  virtual int32_t setSample(int32_t sampleIndex, int32_t channel, Format format, int32_t sample)=0;
203 
218  virtual int32_t getSample(int32_t sampleIndex, int32_t channel, Format format)=0;
219 
230  static int32_t findSampleBitDepth(Format format);
231 
246  static IAudioSamples* make(int32_t numSamples,
247  int32_t numChannels);
248 
249 
257  static int64_t samplesToDefaultPts(int64_t samples, int sampleRate);
258 
266  static int64_t defaultPtsToSamples(int64_t duration, int sampleRate);
267 
268  protected:
269  IAudioSamples();
270  virtual ~IAudioSamples();
271 
272  /*
273  * Added for 2.1
274  */
275  public:
276  enum class ChannelLayout : uint64_t {
277 CH_NONE= 0,
278 CH_FRONT_LEFT= 0x00000001,
279 CH_FRONT_RIGHT= 0x00000002,
280 CH_FRONT_CENTER= 0x00000004,
281 CH_LOW_FREQUENCY= 0x00000008,
282 CH_BACK_LEFT= 0x00000010,
283 CH_BACK_RIGHT= 0x00000020,
284 CH_FRONT_LEFT_OF_CENTER= 0x00000040,
285 CH_FRONT_RIGHT_OF_CENTER= 0x00000080,
286 CH_BACK_CENTER= 0x00000100,
287 CH_SIDE_LEFT= 0x00000200,
288 CH_SIDE_RIGHT= 0x00000400,
289 CH_TOP_CENTER= 0x00000800,
290 CH_TOP_FRONT_LEFT= 0x00001000,
291 CH_TOP_FRONT_CENTER= 0x00002000,
292 CH_TOP_FRONT_RIGHT= 0x00004000,
293 CH_TOP_BACK_LEFT= 0x00008000,
294 CH_TOP_BACK_CENTER= 0x00010000,
295 CH_TOP_BACK_RIGHT= 0x00020000,
296 CH_STEREO_LEFT= 0x20000000,
297 CH_STEREO_RIGHT= 0x40000000,
298 CH_WIDE_LEFT= 0x0000000080000000ULL,
299 CH_WIDE_RIGHT= 0x0000000100000000ULL,
300 CH_SURROUND_DIRECT_LEFT= 0x0000000200000000ULL,
301 CH_SURROUND_DIRECT_RIGHT= 0x0000000400000000ULL,
302 CH_LOW_FREQUENCY_2= 0x0000000800000000ULL,
303 
307 CH_LAYOUT_NATIVE= 0x8000000000000000ULL,
308 
314 CH_LAYOUT_MONO= (CH_FRONT_CENTER),
315 CH_LAYOUT_STEREO= (CH_FRONT_LEFT|CH_FRONT_RIGHT),
316 CH_LAYOUT_2POINT1= (CH_LAYOUT_STEREO|CH_LOW_FREQUENCY),
317 CH_LAYOUT_2_1= (CH_LAYOUT_STEREO|CH_BACK_CENTER),
318 CH_LAYOUT_SURROUND= (CH_LAYOUT_STEREO|CH_FRONT_CENTER),
319 CH_LAYOUT_3POINT1= (CH_LAYOUT_SURROUND|CH_LOW_FREQUENCY),
320 CH_LAYOUT_4POINT0= (CH_LAYOUT_SURROUND|CH_BACK_CENTER),
321 CH_LAYOUT_4POINT1= (CH_LAYOUT_4POINT0|CH_LOW_FREQUENCY),
322 CH_LAYOUT_2_2= (CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT),
323 CH_LAYOUT_QUAD= (CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT),
324 CH_LAYOUT_5POINT0= (CH_LAYOUT_SURROUND|CH_SIDE_LEFT|CH_SIDE_RIGHT),
325 CH_LAYOUT_5POINT1= (CH_LAYOUT_5POINT0|CH_LOW_FREQUENCY),
326 CH_LAYOUT_5POINT0_BACK= (CH_LAYOUT_SURROUND|CH_BACK_LEFT|CH_BACK_RIGHT),
327 CH_LAYOUT_5POINT1_BACK= (CH_LAYOUT_5POINT0_BACK|CH_LOW_FREQUENCY),
328 CH_LAYOUT_6POINT0= (CH_LAYOUT_5POINT0|CH_BACK_CENTER),
329 CH_LAYOUT_6POINT0_FRONT= (CH_LAYOUT_2_2|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER),
330 CH_LAYOUT_HEXAGONAL= (CH_LAYOUT_5POINT0_BACK|CH_BACK_CENTER),
331 CH_LAYOUT_6POINT1= (CH_LAYOUT_5POINT1|CH_BACK_CENTER),
332 CH_LAYOUT_6POINT1_BACK= (CH_LAYOUT_5POINT1_BACK|CH_BACK_CENTER),
333 CH_LAYOUT_6POINT1_FRONT= (CH_LAYOUT_6POINT0_FRONT|CH_LOW_FREQUENCY),
334 CH_LAYOUT_7POINT0= (CH_LAYOUT_5POINT0|CH_BACK_LEFT|CH_BACK_RIGHT),
335 CH_LAYOUT_7POINT0_FRONT= (CH_LAYOUT_5POINT0|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER),
336 CH_LAYOUT_7POINT1= (CH_LAYOUT_5POINT1|CH_BACK_LEFT|CH_BACK_RIGHT),
337 CH_LAYOUT_7POINT1_WIDE= (CH_LAYOUT_5POINT1|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER),
338 CH_LAYOUT_7POINT1_WIDE_BACK= (CH_LAYOUT_5POINT1_BACK|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER),
339 CH_LAYOUT_OCTAGONAL= (CH_LAYOUT_5POINT0|CH_BACK_LEFT|CH_BACK_CENTER|CH_BACK_RIGHT),
340 CH_LAYOUT_HEXADECAGONAL= (CH_LAYOUT_OCTAGONAL|CH_WIDE_LEFT|CH_WIDE_RIGHT|CH_TOP_BACK_LEFT|CH_TOP_BACK_RIGHT|CH_TOP_BACK_CENTER|CH_TOP_FRONT_CENTER|CH_TOP_FRONT_LEFT|CH_TOP_FRONT_RIGHT),
341 CH_LAYOUT_STEREO_DOWNMIX= (CH_STEREO_LEFT|CH_STEREO_RIGHT),
342 
343  } ;
344 
345 
346  virtual ChannelLayout getChannelLayout() = 0;
347 
348  virtual void setComplete(bool complete, int32_t numSamples,
349  int32_t sampleRate, int32_t channels, ChannelLayout channelLayout, Format format,
350  int64_t pts)=0;
351 
352  /*
353  * Added for 3.1
354  */
355 
371  static IAudioSamples* make(
372  com::avpkit::ferry::IBuffer* buffer, int channels,
373  IAudioSamples::Format format);
374 
375  /*
376  * Added for 3.2
377  *
378  */
394  static IAudioSamples* make(int32_t numSamples,
395  int32_t numChannels,
396  IAudioSamples::Format format);
397 
398  };
399 
400 }}}
401 
402 #endif /*IAUDIOSAMPLES_H_*/
A set of raw (decoded) samples, plus a timestamp for when to play those samples relative to other ite...
Definition: IAudioSamples.h:38
virtual int32_t setSample(int32_t sampleIndex, int32_t channel, Format format, int32_t sample)=0
Sets the sample at the given index and channel to the sample.
virtual int32_t getChannels()=0
Return the number of channels of the samples in this buffer.
virtual void setComplete(bool complete, int32_t numSamples, int32_t sampleRate, int32_t channels, Format format, int64_t pts)=0
Call this if you modify the samples and are now done.
virtual int32_t getSampleRate()=0
Find the sample rate of the samples in this audio buffer.
virtual int32_t getSample(int32_t sampleIndex, int32_t channel, Format format)=0
Get the sample at the given sampleIndex and channel, and return it in the asked for format.
virtual bool isComplete()=0
Returns whether or not we think this buffer has been filled with data.
virtual void setPts(int64_t aValue)=0
Set the Presentation Time Stamp for this set of samples.
virtual int32_t getMaxSamples()=0
virtual int64_t getNextPts()=0
What would be the next Presentation Time Stamp after all the samples in this buffer were played?
virtual int32_t getSampleBitDepth()=0
Find out the bit-depth of the samples in this buffer.
virtual Format getFormat()=0
Find the Format of the samples in this buffer.
virtual int32_t getNumSamples()=0
Get the number of samples in this video.
virtual int32_t getMaxBufferSize()=0
Format
The format we use to represent audio.
Definition: IAudioSamples.h:46
@ FMT_S32P
signed 32 bits, planar
Definition: IAudioSamples.h:56
@ FMT_U8P
unsigned 8 bits, planar
Definition: IAudioSamples.h:54
@ FMT_S16P
signed 16 bits, planar
Definition: IAudioSamples.h:55
virtual int64_t getPts()=0
What is the Presentation Time Stamp of this set of audio samples.
virtual int32_t getSampleSize()=0
The parent class of all media objects than can be gotten from an IStream.
Definition: IMediaData.h:34
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...