Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Adrien Beraud <adrien.beraud@wisdomvibes.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU 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 : * This program 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 General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #pragma once 22 : 23 : extern "C" { 24 : #include <libavutil/samplefmt.h> 25 : } 26 : 27 : #include <fmt/core.h> 28 : #include <sstream> 29 : #include <string> 30 : #include <cstddef> // for size_t 31 : 32 : namespace jami { 33 : 34 : /** 35 : * Structure to hold sample rate and channel number associated with audio data. 36 : */ 37 : struct AudioFormat 38 : { 39 : unsigned sample_rate; 40 : unsigned nb_channels; 41 : AVSampleFormat sampleFormat; 42 : 43 1841 : constexpr AudioFormat(unsigned sr, unsigned c, AVSampleFormat f = AV_SAMPLE_FMT_S16) 44 1841 : : sample_rate(sr) 45 1841 : , nb_channels(c) 46 1841 : , sampleFormat(f) 47 1841 : {} 48 : 49 417 : inline bool operator==(const AudioFormat& b) const 50 : { 51 230 : return ((b.sample_rate == sample_rate) && (b.nb_channels == nb_channels) 52 647 : && (b.sampleFormat == sampleFormat)); 53 : } 54 : 55 305 : inline bool operator!=(const AudioFormat& b) const { return !(*this == b); } 56 : 57 112 : inline std::string toString() const 58 : { 59 224 : return fmt::format("{{{}, {} channels, {}Hz}}", av_get_sample_fmt_name(sampleFormat), nb_channels, sample_rate); 60 : } 61 : 62 0 : inline AudioFormat withSampleFormat(AVSampleFormat format) 63 : { 64 0 : return {sample_rate, nb_channels, format}; 65 : } 66 : 67 : 68 : /** 69 : * Returns bytes necessary to hold one frame of audio data. 70 : */ 71 : inline size_t getBytesPerFrame() const { return av_get_bytes_per_sample(sampleFormat) * nb_channels; } 72 : 73 : /** 74 : * Bytes per second (default), or bytes necessary 75 : * to hold delay_ms milliseconds of audio data. 76 : */ 77 : inline size_t getBandwidth(unsigned delay_ms = 1000) const 78 : { 79 : return (getBytesPerFrame() * sample_rate * delay_ms) / 1000; 80 : } 81 : 82 : static const constexpr unsigned DEFAULT_SAMPLE_RATE = 48000; 83 37 : static const constexpr AudioFormat DEFAULT() { return AudioFormat {16000, 1}; } 84 : static const constexpr AudioFormat NONE() { return AudioFormat {0, 0}; } 85 4 : static const constexpr AudioFormat MONO() { return AudioFormat {DEFAULT_SAMPLE_RATE, 1}; } 86 4 : static const constexpr AudioFormat STEREO() { return AudioFormat {DEFAULT_SAMPLE_RATE, 2}; } 87 : }; 88 : 89 : } // namespace jami