UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
audio_frame2 Class Reference

#include <types.h>

Collaboration diagram for audio_frame2:
Collaboration graph

Classes

struct  channel
 

Public Member Functions

 audio_frame2 ()=default
 
 audio_frame2 (audio_frame2 &&)=default
 
 audio_frame2 (audio_frame2 const &)=delete
 
 audio_frame2 (const struct audio_frame *)
 creates audio_frame2 from POD audio_frame
 
 ~audio_frame2 ()=default
 
void append (audio_frame2 const &frame)
 
void append (int channel, const char *data, size_t length)
 
void change_bps (int new_bps)
 
int get_bps () const
 
int get_channel_count () const
 
audio_codec_t get_codec () const
 
char * get_data (int channel)
 
const char * get_data (int channel) const
 
size_t get_data_len () const
 
size_t get_data_len (int channel) const
 
struct audio_desc get_desc () const
 
double get_duration () const
 
fec_desc const & get_fec_params (int channel) const
 
int get_sample_count () const
 
int get_sample_rate () const
 
int64_t get_timestamp () const
 
bool has_same_prop_as (audio_frame2 const &frame) const
 
void init (int nr_channels, audio_codec_t codec, int bps, int sample_rate)
 Initializes audio_frame2 for use. If already initialized, data are dropped.
 
 operator bool () const
 
audio_frame2 & operator= (audio_frame2 &&)=default
 
audio_frame2 & operator= (audio_frame2 const &)=delete
 
void replace (int channel, size_t offset, const char *data, size_t length)
 replaces portion of data of specified channel. If the size of the channel is not sufficient, it is extended and old data are copied.
 
bool resample (audio_frame2_resampler &resampler_state, int new_sample_rate)
 
std::tuple< bool, audio_frame2 > resample_fake (audio_frame2_resampler &resampler_state, int new_sample_rate_num, int new_sample_rate_den)
 @ resamples to new sample rate while keeping nominal sample rate intact
 
void reserve (size_t len)
 
void reset ()
 
void resize (int channel, size_t len)
 
void set_duration (double duration)
 
void set_fec_params (int channel, fec_desc const &)
 
void set_timestamp (int64_t ts)
 

Static Public Member Functions

static audio_frame2 copy_with_bps_change (audio_frame2 const &frame, int new_bps)
 

Private Member Functions

void reserve (int channel, size_t len)
 

Private Attributes

std::vector< channel > channels
 
struct audio_desc desc = {}
 desc.ch_count not set, use channels.size() instead
 
double duration = 0.0
 for compressed formats where this cannot be directly determined from samples/sample_rate
 
int64_t timestamp = -1
 

Friends

class audio_frame2_resampler
 
class soxr_resampler
 
class speex_resampler
 

Detailed Description

More versatile than audio_frame

Can hold also compressed audio data. Audio channels are non-interleaved.

Definition at line 144 of file types.h.

Constructor & Destructor Documentation

◆ audio_frame2() [1/4]

audio_frame2::audio_frame2 ( )
default

◆ ~audio_frame2()

audio_frame2::~audio_frame2 ( )
default

◆ audio_frame2() [2/4]

audio_frame2::audio_frame2 ( audio_frame2 const & )
delete

◆ audio_frame2() [3/4]

audio_frame2::audio_frame2 ( audio_frame2 && )
default

◆ audio_frame2() [4/4]

audio_frame2::audio_frame2 ( const struct audio_frame * old)
explicit

creates audio_frame2 from POD audio_frame

Definition at line 87 of file types.cpp.

87 :
88 channels(old ? old->ch_count : 0)
89{
90 if (old == nullptr) {
91 return;
92 }
93 desc.bps = old->bps;
96 for (int i = 0; i < old->ch_count; i++) {
97 resize(i, old->data_len / old->ch_count);
98 char *data = channels[i].data.get();
99 demux_channel(data, old->data, old->bps, old->data_len,
100 old->ch_count, i);
101 }
102 if ((old->flags & TIMESTAMP_VALID) != 0) {
103 timestamp = old->timestamp;
104 }
105}
@ AC_PCM
Definition types.h:53
int64_t timestamp
Definition types.h:210
void resize(int channel, size_t len)
Definition types.cpp:183
std::vector< channel > channels
Definition types.h:208
struct audio_desc desc
desc.ch_count not set, use channels.size() instead
Definition types.h:207
int sample_rate
Definition types.h:78
int bps
Definition types.h:77
audio_codec_t codec
Definition types.h:80
int sample_rate
Definition types.h:101
int64_t timestamp
Definition types.h:109
char * data
Definition types.h:102
int ch_count
Definition types.h:104
int data_len
Definition types.h:103
int flags
Definition types.h:111
int bps
Definition types.h:100
@ TIMESTAMP_VALID
timestamp set by source (in 90 kHz clock)
Definition types.h:223
void demux_channel(char *out, char *in, int bps, int in_len, int in_stream_channels, int pos_in_stream)
Definition utils.cpp:334

References AC_PCM, audio_desc::bps, audio_frame::bps, audio_frame::ch_count, channels, audio_desc::codec, audio_frame::data, audio_frame::data_len, demux_channel(), desc, audio_frame::flags, resize(), audio_desc::sample_rate, audio_frame::sample_rate, and TIMESTAMP_VALID.

Here is the call graph for this function:

Member Function Documentation

◆ append() [1/2]

void audio_frame2::append ( audio_frame2 const & frame)

Definition at line 125 of file types.cpp.

126{
127 if (desc.bps != src.desc.bps || desc.sample_rate != src.desc.sample_rate ||
128 channels.size() != src.channels.size()) {
129 throw std::logic_error(
130 "Trying to append frame with different parameters!");
131 }
132
133 for (size_t i = 0; i < channels.size(); i++) {
134 append(i, src.get_data(i), src.get_data_len(i));
135 }
136}
void append(audio_frame2 const &frame)
Definition types.cpp:125

References append(), audio_desc::bps, channels, desc, get_data(), get_data_len(), and audio_desc::sample_rate.

Referenced by append(), audio_codec_compress(), audio_codec_decompress(), decode_audio_frame(), decode_audio_frame_mulaw(), rs::encode(), and process_statistics().

Here is the call graph for this function:

◆ append() [2/2]

void audio_frame2::append ( int channel,
const char * data,
size_t length )

Definition at line 138 of file types.cpp.

139{
140 // allocate twice as much as we need to avoid frequent reallocations
141 // when append is called repeatedly
142 reserve(channel, 2 * (channels[channel].len + length));
143 copy(data, data + length, channels[channel].data.get() + channels[channel].len);
144 channels[channel].len += length;
145}
int void size_t * len
void reserve(size_t len)
Definition types.cpp:161

References channels, len, and reserve().

Here is the call graph for this function:

◆ change_bps()

void audio_frame2::change_bps ( int new_bps)

Definition at line 313 of file types.cpp.

314{
315 if (new_bps == desc.bps) {
316 return;
317 }
318
319 vector<channel> new_channels(channels.size());
320
321 for (size_t i = 0; i < channels.size(); i++) {
322 size_t new_size = channels[i].len / desc.bps * new_bps;
323 new_channels[i] = {unique_ptr<char []>(new char[new_size]), new_size, new_size, {}};
324 }
325
326 for (size_t i = 0; i < channels.size(); i++) {
327 ::change_bps(new_channels[i].data.get(), new_bps, get_data(i), get_bps(),
328 get_data_len(i));
329 }
330
331 desc.bps = new_bps;
332 channels = std::move(new_channels);
333}
void change_bps(int new_bps)
Definition types.cpp:313
char * get_data(int channel)
Definition types.cpp:218
int get_bps() const
Definition types.cpp:200
size_t get_data_len() const
Definition types.cpp:236

References audio_desc::bps, change_bps(), channels, desc, get_bps(), get_data(), and get_data_len().

Referenced by audio_sender_thread(), change_bps(), copy_with_bps_change(), and decode_audio_frame().

Here is the call graph for this function:

◆ copy_with_bps_change()

audio_frame2 audio_frame2::copy_with_bps_change ( audio_frame2 const & frame,
int new_bps )
staticnodiscard

Definition at line 298 of file types.cpp.

299{
300 audio_frame2 ret;
301 ret.init(frame.get_channel_count(), frame.get_codec(), new_bps, frame.get_sample_rate());
302
303 for (size_t i = 0; i < ret.channels.size(); i++) {
304 ret.channels[i].len = frame.get_data_len(i) / frame.get_bps() * new_bps;
305 ret.channels[i].data = unique_ptr<char []>(new char[ret.channels[i].len]);
306 ::change_bps(ret.channels[i].data.get(), new_bps, frame.get_data(i), frame.get_bps(),
307 frame.get_data_len(i));
308 }
309
310 return ret;
311}
struct video_frame * frame
void init(int nr_channels, audio_codec_t codec, int bps, int sample_rate)
Initializes audio_frame2 for use. If already initialized, data are dropped.
Definition types.cpp:115

References change_bps(), channels, frame, and init().

Here is the call graph for this function:

◆ get_bps()

int audio_frame2::get_bps ( ) const
nodiscard

◆ get_channel_count()

int audio_frame2::get_channel_count ( ) const
nodiscard

◆ get_codec()

audio_codec_t audio_frame2::get_codec ( ) const
nodiscard

Definition at line 213 of file types.cpp.

214{
215 return desc.codec;
216}

References audio_desc::codec, and desc.

Referenced by audio_tx_send_standard(), decode_audio_frame(), rs::encode(), process_statistics(), and validate_std_audio().

◆ get_data() [1/2]

char * audio_frame2::get_data ( int channel)
nodiscard

Definition at line 218 of file types.cpp.

219{
220 return channels[channel].data.get();
221}

References channels.

Referenced by append(), audio_codec_decompress(), audio_tx_send_chan(), audio_tx_send_standard(), change_bps(), decode_audio_frame(), and rs::encode().

◆ get_data() [2/2]

const char * audio_frame2::get_data ( int channel) const
nodiscard

Definition at line 223 of file types.cpp.

224{
225 return channels[channel].data.get();
226}

References channels.

◆ get_data_len() [1/2]

size_t audio_frame2::get_data_len ( ) const
nodiscard

Returns length of all channels in bytes

Definition at line 236 of file types.cpp.

237{
238 size_t len = 0;
239 for (int i = 0; i < get_channel_count(); ++i) {
240 len += get_data_len(i);
241 }
242
243 return len;
244}
int get_channel_count() const
Definition types.cpp:260

References get_channel_count(), get_data_len(), and len.

Referenced by change_bps(), and get_data_len().

Here is the call graph for this function:

◆ get_data_len() [2/2]

size_t audio_frame2::get_data_len ( int channel) const
nodiscard

Definition at line 228 of file types.cpp.

229{
230 return channels[channel].len;
231}

References channels.

Referenced by append(), audio_codec_decompress(), audio_tx_send_chan(), audio_tx_send_standard(), decode_audio_frame(), rs::encode(), and validate_std_audio().

◆ get_desc()

struct audio_desc audio_frame2::get_desc ( ) const

Definition at line 205 of file types.cpp.

206{
207 struct audio_desc ret = desc;
208 ret.ch_count =
209 get_channel_count(); // ch_count is not set in audio_frame2::desc
210 return ret;
211}
int ch_count
Definition types.h:79

References audio_desc::ch_count, and desc.

Referenced by set_audio_spec_to_vrxtx().

◆ get_duration()

double audio_frame2::get_duration ( ) const
nodiscard

Definition at line 246 of file types.cpp.

247{
248 if (desc.codec == AC_PCM) {
249 int samples = get_sample_count();
250 return (double) samples / get_sample_rate();
251 }
252 return duration;
253}
int get_sample_rate() const
Definition types.cpp:275
double duration
for compressed formats where this cannot be directly determined from samples/sample_rate
Definition types.h:209
int get_sample_count() const
Definition types.cpp:265

References AC_PCM, audio_desc::codec, desc, duration, get_sample_count(), and get_sample_rate().

Referenced by audio_tx_send_standard().

Here is the call graph for this function:

◆ get_fec_params()

fec_desc const & audio_frame2::get_fec_params ( int channel) const
nodiscard

Definition at line 255 of file types.cpp.

256{
257 return channels[channel].fec_params;
258}

References channels.

Referenced by audio_tx_send_chan().

◆ get_sample_count()

int audio_frame2::get_sample_count ( ) const
nodiscard

Definition at line 265 of file types.cpp.

266{
267 // for PCM, we can deduce samples count from length of the data
268 if (desc.codec == AC_PCM) {
269 return channels[0].len / get_bps();
270 } else {
271 throw logic_error("Unknown sample count for compressed audio!");
272 }
273}

References AC_PCM, channels, audio_desc::codec, desc, and get_bps().

Referenced by get_duration().

Here is the call graph for this function:

◆ get_sample_rate()

int audio_frame2::get_sample_rate ( ) const
nodiscard

◆ get_timestamp()

int64_t audio_frame2::get_timestamp ( ) const
nodiscard

Definition at line 340 of file types.cpp.

340{ return timestamp; }

References timestamp.

Referenced by audio_tx_send(), and decode_audio_frame().

◆ has_same_prop_as()

bool audio_frame2::has_same_prop_as ( audio_frame2 const & frame) const
nodiscard

Definition at line 280 of file types.cpp.

281{
282 return desc.bps == frame.desc.bps &&
283 desc.sample_rate == frame.desc.sample_rate &&
284 desc.codec == frame.desc.codec &&
285 channels.size() == frame.channels.size();
286}

References audio_desc::bps, channels, audio_desc::codec, desc, frame, and audio_desc::sample_rate.

Referenced by process_statistics().

◆ init()

void audio_frame2::init ( int nr_channels,
audio_codec_t codec,
int bps,
int sample_rate )

Initializes audio_frame2 for use. If already initialized, data are dropped.

Definition at line 115 of file types.cpp.

116{
117 channels.clear();
118 channels.resize(nr_channels);
119 desc.bps = b;
120 desc.codec = c;
121 desc.sample_rate = sr;
122 duration = 0.0;
123}
steady_clock::duration duration
Definition echo.cpp:67

References audio_desc::bps, channels, audio_desc::codec, desc, and audio_desc::sample_rate.

Referenced by audio_codec_compress(), audio_codec_decompress(), audio_decoder_reconfigure(), copy_with_bps_change(), decode_audio_frame(), decode_audio_frame_mulaw(), rs::encode(), and process_statistics().

◆ operator bool()

audio_frame2::operator bool ( ) const
explicit

Definition at line 107 of file types.cpp.

108{
109 return desc.codec != AC_NONE;
110}
@ AC_NONE
Definition types.h:52

References AC_NONE, and desc.

◆ operator=() [1/2]

audio_frame2 & audio_frame2::operator= ( audio_frame2 && )
default

◆ operator=() [2/2]

audio_frame2 & audio_frame2::operator= ( audio_frame2 const & )
delete

◆ replace()

void audio_frame2::replace ( int channel,
size_t offset,
const char * data,
size_t length )

replaces portion of data of specified channel. If the size of the channel is not sufficient, it is extended and old data are copied.

Definition at line 152 of file types.cpp.

153{
154 resize(channel, offset + length);
155 copy(data, data + length, channels[channel].data.get() + offset);
156}
static __device__ const float offset
Definition cuda_dxt.cu:139

References channels, offset, and resize().

Referenced by audio_fec_decode(), and decode_audio_frame().

Here is the call graph for this function:

◆ resample()

bool audio_frame2::resample ( audio_frame2_resampler & resampler_state,
int new_sample_rate )
Note
bps of the frame needs to be 16 bits!
Parameters
resampler_stateopaque state that can holds resampler that dosn't need to be reinitalized during calls on various audio frames. It reinitializes itself when needed (when source or new sample rate changes). Therefore, it is very recommended to use it only in a stream that may change sometimes but do not eg. share it between two streams that has different properties.
Return values
falseif SpeexDSP was not compiled in

Definition at line 361 of file types.cpp.

362{
363 auto [ret, remainder] = resample_fake(resampler_state, new_sample_rate, 1);
364 if (!ret) {
365 return false;
366 }
367 if (remainder.get_data_len() > 0) {
368 LOG(LOG_LEVEL_WARNING) << "Audio frame resampler: not all samples resampled!\n";
369 }
370 desc.sample_rate = new_sample_rate;
371
372 return true;
373}
std::tuple< bool, audio_frame2 > resample_fake(audio_frame2_resampler &resampler_state, int new_sample_rate_num, int new_sample_rate_den)
@ resamples to new sample rate while keeping nominal sample rate intact
Definition types.cpp:342
#define LOG(level)
Definition debug.h:313
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60

References desc, LOG, LOG_LEVEL_WARNING, resample_fake(), and audio_desc::sample_rate.

Referenced by audio_sender_thread(), and decode_audio_frame().

Here is the call graph for this function:

◆ resample_fake()

tuple< bool, audio_frame2 > audio_frame2::resample_fake ( audio_frame2_resampler & resampler_state,
int new_sample_rate_num,
int new_sample_rate_den )

@ resamples to new sample rate while keeping nominal sample rate intact

Definition at line 342 of file types.cpp.

343{
344 vector<channel> new_channels(channels.size());
345 for (size_t i = 0; i < channels.size(); i++) {
346 // allocate new storage + 10 ms headroom
347 size_t new_size = (long long) channels[i].len * new_sample_rate_num / desc.sample_rate / new_sample_rate_den
348 + new_sample_rate_num * desc.bps / 100 / new_sample_rate_den;
349 new_channels[i] = {unique_ptr<char []>(new char[new_size]), new_size, new_size, {}};
350 }
351
352 auto [ret, remainder] = resampler_state.resample(*this, new_channels, new_sample_rate_num, new_sample_rate_den);
353 if (!ret) {
354 return {false, audio_frame2{}};
355 }
356
357 channels = std::move(new_channels);
358 return {ret, std::move(remainder)};
359}
std::tuple< bool, audio_frame2 > resample(audio_frame2 &a, std::vector< audio_frame2::channel > &out, int new_sample_rate_num, int new_sample_rate_den)
Definition resampler.cpp:76

References audio_desc::bps, channels, desc, len, audio_frame2_resampler::resample(), and audio_desc::sample_rate.

Referenced by decode_audio_frame(), and resample().

Here is the call graph for this function:

◆ reserve() [1/2]

void audio_frame2::reserve ( int channel,
size_t len )
private

Definition at line 168 of file types.cpp.

169{
170 if (channels[channel].max_len < length) {
171 unique_ptr<char []> new_data(new char[length]);
172 copy(channels[channel].data.get(), channels[channel].data.get() +
173 channels[channel].len, new_data.get());
174
175 channels[channel].max_len = length;
176 channels[channel].data = std::move(new_data);
177 }
178}

References channels.

◆ reserve() [2/2]

void audio_frame2::reserve ( size_t length)

Reserves data for every channel with the specified length.

Definition at line 161 of file types.cpp.

162{
163 for (size_t channel = 0; channel < channels.size(); ++channel) {
164 reserve(channel, length);
165 }
166}

References channels, and reserve().

Referenced by append(), audio_decoder_reconfigure(), rs::encode(), reserve(), and resize().

Here is the call graph for this function:

◆ reset()

void audio_frame2::reset ( )

Removes all data from audio_frame2.

Definition at line 192 of file types.cpp.

193{
194 for (size_t i = 0; i < channels.size(); i++) {
195 channels[i].len = 0;
196 }
197 duration = 0.0;
198}

References channels.

◆ resize()

void audio_frame2::resize ( int channel,
size_t length )

Changes actual size of channel.

Definition at line 183 of file types.cpp.

184{
185 reserve(channel, length);
186 channels[channel].len = length;
187}

References channels, and reserve().

Referenced by audio_frame2(), audio_codec_decompress(), decode_audio_frame(), rs::encode(), and replace().

Here is the call graph for this function:

◆ set_duration()

void audio_frame2::set_duration ( double duration)

Definition at line 288 of file types.cpp.

289{
290 duration = new_duration;
291}

Referenced by audio_codec_compress().

◆ set_fec_params()

void audio_frame2::set_fec_params ( int channel,
fec_desc const & fec_params )

Definition at line 293 of file types.cpp.

294{
295 channels[channel].fec_params = fec_params;
296}

References channels, and video_frame::fec_params.

Referenced by rs::encode().

◆ set_timestamp()

void audio_frame2::set_timestamp ( int64_t ts)

Definition at line 335 of file types.cpp.

336{
337 assert(ts >= -1);
338 timestamp = ts;
339}
uint32_t ts
Definition tfrc.c:101

References timestamp, and ts.

Referenced by audio_codec_compress(), audio_codec_decompress(), and decode_audio_frame().

Friends And Related Symbol Documentation

◆ audio_frame2_resampler

friend class audio_frame2_resampler
friend

Definition at line 212 of file types.h.

◆ soxr_resampler

friend class soxr_resampler
friend

Definition at line 213 of file types.h.

◆ speex_resampler

friend class speex_resampler
friend

Definition at line 214 of file types.h.

Member Data Documentation

◆ channels

◆ desc

struct audio_desc audio_frame2::desc = {}
private

desc.ch_count not set, use channels.size() instead

Definition at line 207 of file types.h.

207{};

Referenced by audio_frame2(), append(), change_bps(), get_bps(), get_codec(), get_duration(), get_sample_count(), get_sample_rate(), has_same_prop_as(), init(), resample(), and resample_fake().

◆ duration

double audio_frame2::duration = 0.0
private

for compressed formats where this cannot be directly determined from samples/sample_rate

Definition at line 209 of file types.h.

Referenced by get_duration().

◆ timestamp

int64_t audio_frame2::timestamp = -1
private

Definition at line 210 of file types.h.

Referenced by get_timestamp(), and set_timestamp().


The documentation for this class was generated from the following files: