Audacity 3.2.0
Public Member Functions | Private Attributes | List of all members
MIR::StftFrameProvider Class Reference

#include <StftFrameProvider.h>

Collaboration diagram for MIR::StftFrameProvider:
[legend]

Public Member Functions

 StftFrameProvider (const MirAudioReader &source)
 
bool GetNextFrame (PffftFloatVector &frame)
 
int GetNumFrames () const
 
int GetSampleRate () const
 
double GetFrameRate () const
 
int GetFftSize () const
 

Private Attributes

const MirAudioReadermAudio
 
const int mFftSize
 
const double mHopSize
 
const std::vector< float > mWindow
 
const int mNumFrames
 
const long long mNumSamples
 
int mNumFramesProvided = 0
 

Detailed Description

Utility class to provide time-domain frames ready for FFT. The returned frames have power-of-two size and are already windowed with a Hann window, scaled such that it sums to unity. Also, GetNumFrames() is the closest power of two that satifsies a hop size of 10ms. This property facilitates the FFT analysis of transformation of the STFT frames to some scalar, e.g. the novelty values of an onset detection function.

Definition at line 28 of file StftFrameProvider.h.

Constructor & Destructor Documentation

◆ StftFrameProvider()

MIR::StftFrameProvider::StftFrameProvider ( const MirAudioReader source)

Definition at line 46 of file StftFrameProvider.cpp.

47 : mAudio { audio }
48 , mFftSize { GetFrameSize(audio.GetSampleRate()) }
49 , mHopSize { GetHopSize(audio.GetSampleRate(), audio.GetNumSamples()) }
51 , mNumFrames { mHopSize > 0 ? static_cast<int>(std::round(
52 audio.GetNumSamples() / mHopSize)) :
53 0 }
54 , mNumSamples { audio.GetNumSamples() }
55{
56 assert(mNumFrames == 0 || IsPowOfTwo(mNumFrames));
57}
MockedAudio audio
const long long mNumSamples
const MirAudioReader & mAudio
const std::vector< float > mWindow
double GetHopSize(int sampleRate, long long numSamples)
std::vector< float > GetNormalizedHann(int size)
Definition: MirUtils.cpp:80
constexpr auto IsPowOfTwo(int x)
Definition: MirUtils.h:28
fastfloat_really_inline void round(adjusted_mantissa &am, callback cb) noexcept
Definition: fast_float.h:2512

References MIR::IsPowOfTwo(), and mNumFrames.

Here is the call graph for this function:

Member Function Documentation

◆ GetFftSize()

int MIR::StftFrameProvider::GetFftSize ( ) const

Definition at line 100 of file StftFrameProvider.cpp.

101{
102 return mFftSize;
103}

References mFftSize.

◆ GetFrameRate()

double MIR::StftFrameProvider::GetFrameRate ( ) const

Definition at line 95 of file StftFrameProvider.cpp.

96{
97 return 1. * mAudio.GetSampleRate() / mHopSize;
98}
virtual double GetSampleRate() const =0

References MIR::MirAudioReader::GetSampleRate(), mAudio, and mHopSize.

Here is the call graph for this function:

◆ GetNextFrame()

bool MIR::StftFrameProvider::GetNextFrame ( PffftFloatVector frame)

Definition at line 59 of file StftFrameProvider.cpp.

60{
62 return false;
63 frame.resize(mFftSize, 0.f);
64 const int firstReadPosition = mHopSize - mFftSize;
65 int start = std::round(firstReadPosition + mNumFramesProvided * mHopSize);
66 while (start < 0)
67 start += mNumSamples;
68 const auto end = std::min<long long>(start + mFftSize, mNumSamples);
69 const auto numToRead = end - start;
70 mAudio.ReadFloats(frame.data(), start, numToRead);
71 // It's not impossible that some user drops a file so short that `mFftSize >
72 // mNumSamples`. In that case we won't be returning a meaningful
73 // STFT, but that's a use case we're not interested in. We just need to make
74 // sure we don't crash.
75 const auto numRemaining = std::min(mFftSize - numToRead, mNumSamples);
76 if (numRemaining > 0)
77 mAudio.ReadFloats(frame.data() + numToRead, 0, numRemaining);
78 std::transform(
79 frame.begin(), frame.end(), mWindow.begin(), frame.begin(),
80 std::multiplies<float>());
82 return true;
83}
int min(int a, int b)
virtual void ReadFloats(float *buffer, long long where, size_t numFrames) const =0
const char * end(const char *str) noexcept
Definition: StringUtils.h:106

References details::end(), mAudio, mFftSize, mHopSize, min(), mNumFrames, mNumFramesProvided, mNumSamples, mWindow, MIR::MirAudioReader::ReadFloats(), and fast_float::round().

Here is the call graph for this function:

◆ GetNumFrames()

int MIR::StftFrameProvider::GetNumFrames ( ) const

Definition at line 85 of file StftFrameProvider.cpp.

86{
87 return mNumFrames;
88}

References mNumFrames.

◆ GetSampleRate()

int MIR::StftFrameProvider::GetSampleRate ( ) const

Definition at line 90 of file StftFrameProvider.cpp.

91{
92 return mAudio.GetSampleRate();
93}

References MIR::MirAudioReader::GetSampleRate(), and mAudio.

Here is the call graph for this function:

Member Data Documentation

◆ mAudio

const MirAudioReader& MIR::StftFrameProvider::mAudio
private

Definition at line 39 of file StftFrameProvider.h.

Referenced by GetFrameRate(), GetNextFrame(), and GetSampleRate().

◆ mFftSize

const int MIR::StftFrameProvider::mFftSize
private

Definition at line 40 of file StftFrameProvider.h.

Referenced by GetFftSize(), and GetNextFrame().

◆ mHopSize

const double MIR::StftFrameProvider::mHopSize
private

Definition at line 41 of file StftFrameProvider.h.

Referenced by GetFrameRate(), and GetNextFrame().

◆ mNumFrames

const int MIR::StftFrameProvider::mNumFrames
private

Definition at line 43 of file StftFrameProvider.h.

Referenced by GetNextFrame(), GetNumFrames(), and StftFrameProvider().

◆ mNumFramesProvided

int MIR::StftFrameProvider::mNumFramesProvided = 0
private

Definition at line 45 of file StftFrameProvider.h.

Referenced by GetNextFrame().

◆ mNumSamples

const long long MIR::StftFrameProvider::mNumSamples
private

Definition at line 44 of file StftFrameProvider.h.

Referenced by GetNextFrame().

◆ mWindow

const std::vector<float> MIR::StftFrameProvider::mWindow
private

Definition at line 42 of file StftFrameProvider.h.

Referenced by GetNextFrame().


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