Ring Daemon
16.0.0
Loading...
Searching...
No Matches
audio_format.h
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3
*
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16
*/
17
#pragma once
18
19
extern
"C"
{
20
#include <libavutil/samplefmt.h>
21
}
22
23
#include <fmt/core.h>
24
#include <sstream>
25
#include <string>
26
#include <cstddef>
// for size_t
27
28
namespace
jami
{
29
33
struct
AudioFormat
34
{
35
unsigned
sample_rate
;
36
unsigned
nb_channels
;
37
AVSampleFormat
sampleFormat
;
38
39
constexpr
AudioFormat
(
unsigned
sr
,
unsigned
c,
AVSampleFormat
f
=
AV_SAMPLE_FMT_S16
)
40
:
sample_rate
(
sr
)
41
,
nb_channels
(c)
42
,
sampleFormat
(
f
)
43
{}
44
45
inline
bool
operator==
(
const
AudioFormat
&
b
)
const
46
{
47
return
((
b
.sample_rate ==
sample_rate
) && (
b
.nb_channels ==
nb_channels
)
48
&& (
b
.sampleFormat ==
sampleFormat
));
49
}
50
51
inline
bool
operator!=
(
const
AudioFormat
&
b
)
const
{
return
!(*
this
==
b
); }
52
53
inline
std::string
toString
()
const
54
{
55
return
fmt::format(
"{{{}, {} channels, {}Hz}}"
,
av_get_sample_fmt_name
(
sampleFormat
),
nb_channels
,
sample_rate
);
56
}
57
58
inline
AudioFormat
withSampleFormat
(
AVSampleFormat
format)
59
{
60
return
{
sample_rate
,
nb_channels
, format};
61
}
62
63
67
inline
size_t
getBytesPerFrame
()
const
{
return
av_get_bytes_per_sample
(
sampleFormat
) *
nb_channels
; }
68
73
inline
size_t
getBandwidth
(
unsigned
delay_ms
= 1000)
const
74
{
75
return
(
getBytesPerFrame
() *
sample_rate
*
delay_ms
) / 1000;
76
}
77
78
static
const
constexpr
unsigned
DEFAULT_SAMPLE_RATE
= 48000;
79
static
const
constexpr
AudioFormat
DEFAULT
() {
return
AudioFormat
{16000, 1}; }
80
static
const
constexpr
AudioFormat
NONE
() {
return
AudioFormat
{0, 0}; }
81
static
const
constexpr
AudioFormat
MONO
() {
return
AudioFormat
{
DEFAULT_SAMPLE_RATE
, 1}; }
82
static
const
constexpr
AudioFormat
STEREO
() {
return
AudioFormat
{
DEFAULT_SAMPLE_RATE
, 2}; }
83
};
84
85
}
// namespace jami
jami
Definition
account.cpp:61
jami::emitSignal
void emitSignal(Args... args)
Definition
ring_signal.h:64
jami::AudioFormat
Structure to hold sample rate and channel number associated with audio data.
Definition
audio_format.h:34
jami::AudioFormat::sampleFormat
AVSampleFormat sampleFormat
Definition
audio_format.h:37
jami::AudioFormat::NONE
static const constexpr AudioFormat NONE()
Definition
audio_format.h:80
jami::AudioFormat::operator!=
bool operator!=(const AudioFormat &b) const
Definition
audio_format.h:51
jami::AudioFormat::STEREO
static const constexpr AudioFormat STEREO()
Definition
audio_format.h:82
jami::AudioFormat::DEFAULT
static const constexpr AudioFormat DEFAULT()
Definition
audio_format.h:79
jami::AudioFormat::AudioFormat
constexpr AudioFormat(unsigned sr, unsigned c, AVSampleFormat f=AV_SAMPLE_FMT_S16)
Definition
audio_format.h:39
jami::AudioFormat::getBytesPerFrame
size_t getBytesPerFrame() const
Returns bytes necessary to hold one frame of audio data.
Definition
audio_format.h:67
jami::AudioFormat::nb_channels
unsigned nb_channels
Definition
audio_format.h:36
jami::AudioFormat::withSampleFormat
AudioFormat withSampleFormat(AVSampleFormat format)
Definition
audio_format.h:58
jami::AudioFormat::toString
std::string toString() const
Definition
audio_format.h:53
jami::AudioFormat::getBandwidth
size_t getBandwidth(unsigned delay_ms=1000) const
Bytes per second (default), or bytes necessary to hold delay_ms milliseconds of audio data.
Definition
audio_format.h:73
jami::AudioFormat::MONO
static const constexpr AudioFormat MONO()
Definition
audio_format.h:81
jami::AudioFormat::sample_rate
unsigned sample_rate
Definition
audio_format.h:35
jami::AudioFormat::DEFAULT_SAMPLE_RATE
static const constexpr unsigned DEFAULT_SAMPLE_RATE
Definition
audio_format.h:78
jami::AudioFormat::operator==
bool operator==(const AudioFormat &b) const
Definition
audio_format.h:45
src
media
audio
audio_format.h
Generated on Thu Jun 19 2025 15:50:37 for Ring Daemon by
1.9.8