Skip to content

Instantly share code, notes, and snippets.

@notlion
Last active December 23, 2015 04:27
Show Gist options
  • Save notlion/7b30b89376213aee1ce2 to your computer and use it in GitHub Desktop.
Save notlion/7b30b89376213aee1ce2 to your computer and use it in GitHub Desktop.
ci::audio BlockTimer
#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);
});
}
#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