AVPKit
IAudioSamples.cpp
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 #include "IAudioSamples.h"
21 #include "Global.h"
22 #include "AudioSamples.h"
23 
24 #include "FfmpegIncludes.h"
25 
26 namespace com { namespace avpkit { namespace core
27 {
28 
29  IAudioSamples :: IAudioSamples()
30  {
31  }
32 
33  IAudioSamples :: ~IAudioSamples()
34  {
35  }
36 
37  int32_t
39  {
40  int bits = av_get_bytes_per_sample((enum AVSampleFormat) format)<<3;
41  return bits;
42  }
43 
45  IAudioSamples :: make(int32_t numSamples, int32_t numChannels)
46  {
47  Global::init();
48  return AudioSamples::make(numSamples, numChannels);
49  }
50 
52  IAudioSamples :: make(int32_t numSamples, int32_t numChannels,
53  IAudioSamples::Format format)
54  {
55  Global::init();
56  return AudioSamples::make(numSamples, numChannels, format);
57  }
58 
59 
62  IAudioSamples::Format format)
63  {
64  Global::init();
65  return AudioSamples::make(buffer, channels, format);
66  }
67 
68  int64_t
69  IAudioSamples :: samplesToDefaultPts(int64_t samples, int sampleRate)
70  {
71  // Note: These need to always round up! a "partial sample" actually must
72  // be whole (and similar with time stamps).
73  int64_t retval = Global::NO_PTS;
74  Global::init();
75  if (sampleRate > 0)
76  {
77  int64_t num = samples * Global::DEFAULT_PTS_PER_SECOND;
78  int64_t den = sampleRate;
79  long double calc = ((long double)num)/((long double)den);
80  retval = (int64_t)(calc);
81  }
82  return retval;
83  }
84 
85  int64_t
86  IAudioSamples :: defaultPtsToSamples(int64_t duration, int sampleRate)
87  {
88  int64_t retval = 0;
89  Global::init();
90  if (duration != Global::NO_PTS)
91  {
92  int64_t num = duration * sampleRate;
93  int64_t den = Global::DEFAULT_PTS_PER_SECOND;
94  long double calc = ((long double)num)/((long double)den);
95  retval = (int64_t)(calc);
96  }
97  return retval;
98  }
99 
100 
101 }}}
static const int64_t DEFAULT_PTS_PER_SECOND
The default time units per second that we use for decoded IAudioSamples and IVideoPicture objects.
Definition: Global.h:57
static const int64_t NO_PTS
A value that means no time stamp is set for a given object.
Definition: Global.h:50
static void init()
Internal Only.
Definition: Global.cpp:157
A set of raw (decoded) samples, plus a timestamp for when to play those samples relative to other ite...
Definition: IAudioSamples.h:38
static int32_t findSampleBitDepth(Format format)
A convenience method that returns the # of bits in a given format.
static int64_t defaultPtsToSamples(int64_t duration, int sampleRate)
Converts a duration in microseconds into a number of samples, assuming a given sampleRate.
Format
The format we use to represent audio.
Definition: IAudioSamples.h:46
static IAudioSamples * make(int32_t numSamples, int32_t numChannels)
Get a new audio samples buffer.
static int64_t samplesToDefaultPts(int64_t samples, int sampleRate)
Converts a number of samples at a given sampleRate into Microseconds.
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...