Ring Daemon
Loading...
Searching...
No Matches
audio_format.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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
19extern "C" {
20#include <libavutil/samplefmt.h>
21}
22
23#include <fmt/core.h>
24#include <string>
25#include <cstddef> // for size_t
26
27namespace jami {
28
33{
34 unsigned sample_rate;
35 unsigned nb_channels;
37
38 constexpr AudioFormat(unsigned sr, unsigned c, AVSampleFormat f = AV_SAMPLE_FMT_S16)
40 , nb_channels(c)
42 {}
43
44 inline bool operator==(const AudioFormat& b) const
45 {
46 return ((b.sample_rate == sample_rate) && (b.nb_channels == nb_channels) && (b.sampleFormat == sampleFormat));
47 }
48
49 inline bool operator!=(const AudioFormat& b) const { return !(*this == b); }
50
51 inline std::string toString() const
52 {
53 return fmt::format("{{{}, {} channels, {}Hz}}", av_get_sample_fmt_name(sampleFormat), nb_channels, sample_rate);
54 }
55
57
61 inline size_t getBytesPerFrame() const
62 {
63 return static_cast<size_t>(av_get_bytes_per_sample(sampleFormat)) * nb_channels;
64 }
65
70 inline size_t getBandwidth(unsigned delay_ms = 1000) const
71 {
72 return (getBytesPerFrame() * sample_rate * delay_ms) / 1000;
73 }
74
75 static const constexpr unsigned DEFAULT_SAMPLE_RATE = 48000;
76 static const constexpr AudioFormat DEFAULT() { return AudioFormat {16000, 1}; }
77 static const constexpr AudioFormat NONE() { return AudioFormat {0, 0}; }
78 static const constexpr AudioFormat MONO() { return AudioFormat {DEFAULT_SAMPLE_RATE, 1}; }
79 static const constexpr AudioFormat STEREO() { return AudioFormat {DEFAULT_SAMPLE_RATE, 2}; }
80};
81
82} // namespace jami
void emitSignal(Args... args)
Definition jami_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat
static const constexpr AudioFormat NONE()
bool operator!=(const AudioFormat &b) const
static const constexpr AudioFormat STEREO()
static const constexpr AudioFormat DEFAULT()
constexpr AudioFormat(unsigned sr, unsigned c, AVSampleFormat f=AV_SAMPLE_FMT_S16)
size_t getBytesPerFrame() const
Returns bytes necessary to hold one frame of audio data.
AudioFormat withSampleFormat(AVSampleFormat format)
std::string toString() const
size_t getBandwidth(unsigned delay_ms=1000) const
Bytes per second (default), or bytes necessary to hold delay_ms milliseconds of audio data.
static const constexpr AudioFormat MONO()
static const constexpr unsigned DEFAULT_SAMPLE_RATE
bool operator==(const AudioFormat &b) const