foobar2000 SDK  2015-08-03
Functions
VisUtils Namespace Reference

Functions

bool IsValidFFTSize (t_size p_size)
 
t_size MatchFFTSize (t_size samples)
 
void PrepareFFTChunk (audio_chunk const &source, audio_chunk &out, double centerOffset)
 

Function Documentation

bool VisUtils::IsValidFFTSize ( t_size  p_size)

Definition at line 24 of file VisUtils.cpp.

24  {
25  return p_size >= 2 && (p_size & (p_size - 1)) == 0;
26  }
t_size VisUtils::MatchFFTSize ( t_size  samples)

Definition at line 28 of file VisUtils.cpp.

28  {
29  if (samples <= 2) return 2;
30  t_size mask = 1;
31  while(!IsValidFFTSize(samples)) {
32  samples &= ~mask; mask <<= 1;
33  }
34  return samples;
35  }
size_t t_size
Definition: int_types.h:48
bool IsValidFFTSize(t_size p_size)
Definition: VisUtils.cpp:24
void VisUtils::PrepareFFTChunk ( audio_chunk const &  source,
audio_chunk out,
double  centerOffset 
)

Turns an arbitrary audio_chunk into a valid chunk to run FFT on, with proper sample count etc.

Parameters
centerOffsetTime offset (in seconds) inside the source chunk to center the output on, in case the FFT window is smaller than input data.

Definition at line 4 of file VisUtils.cpp.

4  {
5  const t_uint32 channels = source.get_channel_count();
6  const t_uint32 sampleRate = source.get_sample_rate();
7  FB2K_DYNAMIC_ASSERT( sampleRate > 0 );
8  out.set_channels(channels, source.get_channel_config());
9  out.set_sample_rate(sampleRate);
10  const t_size inSize = source.get_sample_count();
11  const t_size fftSize = MatchFFTSize(inSize);
12  out.set_sample_count(fftSize);
13  out.set_data_size(fftSize * channels);
14  if (fftSize >= inSize) { //rare case with *REALLY* small input
15  pfc::memcpy_t( out.get_data(), source.get_data(), inSize * channels );
16  pfc::memset_null_t( out.get_data() + inSize * channels, (fftSize - inSize) * channels );
17  } else { //inSize > fftSize, we're using a subset of source chunk for the job, pick a subset around centerOffset.
18  const double baseOffset = pfc::max_t<double>(0, centerOffset - 0.5 * (double)fftSize / (double)sampleRate);
19  const t_size baseSample = pfc::min_t<t_size>( (t_size) audio_math::time_to_samples(baseOffset, sampleRate), inSize - fftSize);
20  pfc::memcpy_t( out.get_data(), source.get_data() + baseSample * channels, fftSize * channels);
21  }
22  }
size_t t_size
Definition: int_types.h:48
virtual audio_sample * get_data()=0
Retrieves audio data buffer pointer (non-const version). Returned pointer is for temporary use only; ...
void memset_null_t(T *p_buffer, t_size p_count)
Definition: primitives.h:638
t_size MatchFFTSize(t_size samples)
Definition: VisUtils.cpp:28
virtual void set_channels(unsigned p_count, unsigned p_config)=0
Sets channel count / channel map.
virtual void set_data_size(t_size p_new_size)=0
Resizes audio data buffer to specified size. Throws std::bad_alloc on failure.
void memcpy_t(t_dst *p_dst, const t_src *p_src, t_size p_count)
Definition: primitives.h:611
virtual void set_sample_count(t_size val)=0
Sets number of valid samples in the buffer. WARNING: sample count * channel count should never be abo...
void set_sample_rate(unsigned val)
Helper, same as set_srate().
Definition: audio_chunk.h:105
uint32_t t_uint32
Definition: int_types.h:5