foobar2000 SDK  2015-08-03
VisUtils.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 
3 namespace VisUtils {
4  void PrepareFFTChunk(audio_chunk const & source, audio_chunk & out, double centerOffset) {
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  }
23 
24  bool IsValidFFTSize(t_size p_size) {
25  return p_size >= 2 && (p_size & (p_size - 1)) == 0;
26  }
27 
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  }
36 };
unsigned get_sample_rate() const
Helper, same as get_srate().
Definition: audio_chunk.h:103
Interface to container of a chunk of audio data. See audio_chunk_impl for an implementation.
Definition: audio_chunk.h:5
virtual unsigned get_channel_config() const =0
Retrieves channel map of contained audio data. Conditions where number of channels specified by chann...
void 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...
Definition: VisUtils.cpp:4
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
bool IsValidFFTSize(t_size p_size)
Definition: VisUtils.cpp:24
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
unsigned get_channel_count() const
Helper - for consistency - same as get_channels().
Definition: audio_chunk.h:88
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...
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