Skip to content

Instantly share code, notes, and snippets.

@seph14
Last active February 27, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seph14/2cdb25fe32333871cbdcdc23b4cb4cca to your computer and use it in GitHub Desktop.
Save seph14/2cdb25fe32333871cbdcdc23b4cb4cca to your computer and use it in GitHub Desktop.
MultiChannelSpectralNode.h
#include "cinder/audio/audio.h"
//class for getting individual channel data
typedef std::shared_ptr<class MultiChannelSpectralNode> MultiChannelSpectralNodeRef;
class MultiChannelSpectralNode : public cinder::audio::MonitorNode {
public:
struct Format : public ci::audio::MonitorNode::Format {
Format() : ci::audio::MonitorNode::Format(), mFftSize( 0 ), mWindowType( ci::audio::dsp::WindowType::BLACKMAN ) {}
Format& fftSize( size_t size ) { mFftSize = size; return *this; }
Format& windowType( ci::audio::dsp::WindowType type ) { mWindowType = type; return *this; }
Format& windowSize( size_t size ) { ci::audio::MonitorNode::Format::windowSize( size ); return *this; }
size_t getFftSize() const { return mFftSize; }
ci::audio::dsp::WindowType getWindowType() const { return mWindowType; }
Format& channels( size_t ch ) { ci::audio::Node::Format::channels( ch ); return *this; }
Format& channelMode( ChannelMode mode ) { ci::audio::Node::Format::channelMode( mode ); return *this; }
Format& autoEnable( bool autoEnable = true ) { ci::audio::Node::Format::autoEnable( autoEnable ); return *this; }
protected:
size_t mFftSize;
ci::audio::dsp::WindowType mWindowType;
};
MultiChannelSpectralNode( const Format &format = Format() );
virtual ~MultiChannelSpectralNode();
void prepareProcessing();
const std::vector<float>& getMagSpectrum(size_t channel);
float getSpectralCentroid(size_t channel);
size_t getNumBins() const { return mFftSize / 2; }
size_t getFftSize() const { return mFftSize; }
float getFreqForBin( size_t bin );
float getSmoothingFactor() const { return mSmoothingFactor; }
void setSmoothingFactor( float factor );
protected:
void initialize() override;
private:
std::unique_ptr<ci::audio::dsp::Fft> mFft;
ci::audio::Buffer mFftBuffer; // windowed samples before transform
ci::audio::BufferSpectral mBufferSpectral; // transformed samples
std::vector<std::vector<float>> mMagSpectrum; // computed magnitude spectrum from frequency-domain samples
ci::audio::AlignedArrayPtr mWindowingTable;
size_t mFftSize;
ci::audio::dsp::WindowType mWindowType;
float mSmoothingFactor;
uint64_t mLastFrameMagSpectrumComputed;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment