foobar2000 SDK  2015-08-03
Public Member Functions | Private Types | Private Attributes
duration_counter

#include <audio_chunk.h>

Public Member Functions

 duration_counter ()
 
void add (double v)
 
void add (const audio_chunk &c)
 
void add (t_uint64 sampleCount, t_uint32 sampleRate)
 
void add (const duration_counter &other)
 
template<typename t_source >
duration_counteroperator+= (const t_source &source)
 
template<typename t_source >
duration_counteroperator-= (const t_source &source)
 
template<typename t_source >
duration_counteroperator= (const t_source &source)
 
double query () const
 
uint64_t queryAsSampleCount (uint32_t rate)
 
void reset ()
 
void set (double v)
 
void subtract (double v)
 
void subtract (const duration_counter &other)
 
void subtract (t_uint64 sampleCount, t_uint32 sampleRate)
 
void subtract (const audio_chunk &c)
 

Private Types

typedef pfc::map_t< t_uint32, t_uint64t_map
 

Private Attributes

double m_offset
 
t_map m_sampleCounts
 

Detailed Description

Duration counter class - accumulates duration using sample values, without any kind of rounding error accumulation.

Definition at line 313 of file audio_chunk.h.

Member Typedef Documentation

Definition at line 374 of file audio_chunk.h.

Constructor & Destructor Documentation

duration_counter::duration_counter ( )
inline

Definition at line 315 of file audio_chunk.h.

315  : m_offset() {
316  }

Member Function Documentation

void duration_counter::add ( double  v)
inline

Definition at line 325 of file audio_chunk.h.

325 {m_offset += v;}
void duration_counter::add ( const audio_chunk c)
inline

Definition at line 331 of file audio_chunk.h.

331  {
333  }
unsigned get_sample_rate() const
Helper, same as get_srate().
Definition: audio_chunk.h:103
void add(double v)
Definition: audio_chunk.h:325
virtual t_size get_sample_count() const =0
Retrieves number of valid samples in the buffer. Note that a "sample" means a unit of interleaved PC...
void duration_counter::add ( t_uint64  sampleCount,
t_uint32  sampleRate 
)
inline

Definition at line 334 of file audio_chunk.h.

334  {
335  PFC_ASSERT( sampleRate > 0 );
336  if (sampleRate > 0 && sampleCount > 0) {
337  m_sampleCounts.find_or_add(sampleRate) += sampleCount;
338  }
339  }
t_storage_value & find_or_add(_t_key const &p_key)
Definition: map.h:29
void duration_counter::add ( const duration_counter other)
inline

Definition at line 340 of file audio_chunk.h.

340  {
341  add(other.m_offset);
342  for(t_map::const_iterator walk = other.m_sampleCounts.first(); walk.is_valid(); ++walk) {
343  add(walk->m_value, walk->m_key);
344  }
345  }
bool is_valid() const
Definition: iterators.h:24
void add(double v)
Definition: audio_chunk.h:325
iterator first()
Definition: map.h:229
t_content::const_iterator const_iterator
Definition: map.h:226
template<typename t_source >
duration_counter& duration_counter::operator+= ( const t_source &  source)
inline

Definition at line 369 of file audio_chunk.h.

369 {add(source); return *this;}
void add(double v)
Definition: audio_chunk.h:325
template<typename t_source >
duration_counter& duration_counter::operator-= ( const t_source &  source)
inline

Definition at line 370 of file audio_chunk.h.

370 {subtract(source); return *this;}
void subtract(double v)
Definition: audio_chunk.h:326
template<typename t_source >
duration_counter& duration_counter::operator= ( const t_source &  source)
inline

Definition at line 371 of file audio_chunk.h.

371 {reset(); add(source); return *this;}
void add(double v)
Definition: audio_chunk.h:325
double duration_counter::query ( ) const

Definition at line 598 of file audio_chunk.cpp.

598  {
599  double acc = m_offset;
600  for(t_map::const_iterator walk = m_sampleCounts.first(); walk.is_valid(); ++walk) {
601  acc += audio_math::samples_to_time(walk->m_value, walk->m_key);
602  }
603  return acc;
604 }
bool is_valid() const
Definition: iterators.h:24
iterator first()
Definition: map.h:229
t_content::const_iterator const_iterator
Definition: map.h:226
uint64_t duration_counter::queryAsSampleCount ( uint32_t  rate)

Definition at line 606 of file audio_chunk.cpp.

606  {
607  uint64_t samples = 0;
608  double acc = m_offset;
609  for(t_map::const_iterator walk = m_sampleCounts.first(); walk.is_valid(); ++walk) {
610  if (walk->m_key == rate) samples += walk->m_value;
611  else acc += audio_math::samples_to_time(walk->m_value, walk->m_key);
612  }
613  return samples + audio_math::time_to_samples(acc, rate );
614 }
bool is_valid() const
Definition: iterators.h:24
iterator first()
Definition: map.h:229
t_content::const_iterator const_iterator
Definition: map.h:226
void duration_counter::reset ( )
inline

Definition at line 321 of file audio_chunk.h.

321  {
322  set(0);
323  }
void duration_counter::set ( double  v)
inline

Definition at line 317 of file audio_chunk.h.

317  {
319  m_offset = v;
320  }
void remove_all()
Definition: map.h:139
void duration_counter::subtract ( double  v)
inline

Definition at line 326 of file audio_chunk.h.

326 {m_offset -= v;}
void duration_counter::subtract ( const duration_counter other)
inline

Definition at line 346 of file audio_chunk.h.

346  {
347  subtract(other.m_offset);
348  for(t_map::const_iterator walk = other.m_sampleCounts.first(); walk.is_valid(); ++walk) {
349  subtract(walk->m_value, walk->m_key);
350  }
351  }
void subtract(double v)
Definition: audio_chunk.h:326
bool is_valid() const
Definition: iterators.h:24
iterator first()
Definition: map.h:229
t_content::const_iterator const_iterator
Definition: map.h:226
void duration_counter::subtract ( t_uint64  sampleCount,
t_uint32  sampleRate 
)
inline

Definition at line 352 of file audio_chunk.h.

352  {
353  PFC_ASSERT( sampleRate > 0 );
354  if (sampleRate > 0 && sampleCount > 0) {
355  t_uint64 * val = m_sampleCounts.query_ptr(sampleRate);
356  if (val == NULL) throw pfc::exception_invalid_params();
357  if (*val < sampleCount) throw pfc::exception_invalid_params();
358  else if (*val == sampleCount) {
359  m_sampleCounts.remove(sampleRate);
360  } else {
361  *val -= sampleCount;
362  }
363 
364  }
365  }
uint64_t t_uint64
Definition: int_types.h:3
const t_storage_value * query_ptr(const _t_key &p_key) const
Definition: map.h:64
bool remove(const _t_key &p_key)
Definition: map.h:120
void duration_counter::subtract ( const audio_chunk c)
inline

Definition at line 366 of file audio_chunk.h.

366  {
368  }
unsigned get_sample_rate() const
Helper, same as get_srate().
Definition: audio_chunk.h:103
void subtract(double v)
Definition: audio_chunk.h:326
virtual t_size get_sample_count() const =0
Retrieves number of valid samples in the buffer. Note that a "sample" means a unit of interleaved PC...

Field Documentation

double duration_counter::m_offset
private

Definition at line 373 of file audio_chunk.h.

t_map duration_counter::m_sampleCounts
private

Definition at line 375 of file audio_chunk.h.


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