Last active
December 23, 2015 04:27
-
-
Save notlion/7b30b89376213aee1ce2 to your computer and use it in GitHub Desktop.
ci::audio BlockTimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "BlockTimer.hpp" | |
using namespace cinder; | |
class FnNode : public audio::Node { | |
std::function<void()> mFunc; | |
protected: | |
void process(audio::Buffer *buffer) override { | |
mFunc(); | |
} | |
public: | |
FnNode(std::function<void()> &&func, const Format &format = {}) | |
: audio::Node{ format } | |
, mFunc{ func } {} | |
}; | |
static double calcSecondsPerBlock(audio::Context &ctx) { | |
return double(ctx.getFramesPerBlock()) / double(ctx.getSampleRate()); | |
} | |
BlockTimer::BlockTimer(audio::Context *ctx) { | |
mAverageSeconds = mSeconds = 10.0 * calcSecondsPerBlock(*ctx); | |
startNode = ctx->makeNode<FnNode>([this] { mTimer.start(); }); | |
stopNode = ctx->makeNode<FnNode>([this] { | |
assert(!mTimer.isStopped()); | |
mTimer.stop(); | |
mSeconds = mTimer.getSeconds(); | |
mAverageSeconds = math::mix<double>(mAverageSeconds, mSeconds, 0.025); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class BlockTimer { | |
double mSeconds; | |
std::atomic<double> mAverageSeconds; | |
ci::Timer mTimer; | |
public: | |
ci::audio::NodeRef startNode, stopNode; | |
BlockTimer(ci::audio::Context *ctx); | |
double getAverageSecondsPerBlock() const { return mAverageSeconds; }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment