Time.h (2028B)
1 /* 2 ZynAddSubFX - a software synthesizer 3 4 Time.h - Frame Tracker 5 Copyright (C) 2016 Mark McCurry 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 as published by the Free Software Foundation; either version 2 10 of the License, or (at your option) any later version. 11 */ 12 #pragma once 13 #include <stdint.h> 14 #include "../globals.h" 15 16 namespace zyn { 17 18 class AbsTime 19 { 20 public: 21 AbsTime(const SYNTH_T &synth) 22 :tempo(120), 23 hostSamples(0), 24 bar(0), 25 beat(0), 26 tick(0.0f), 27 bpm(120.0f), 28 ppq(1920.0f), 29 frames(0), 30 s(synth) {}; 31 void operator++(){++frames;}; 32 void operator++(int){frames++;}; 33 int64_t time() const {return frames;}; 34 unsigned int tempo; 35 int hostSamples; 36 int bar; 37 int beat; 38 float tick; 39 float beatsPerBar; 40 float beatType; 41 float bpm; 42 float ppq; 43 bool playing; 44 float dt() const { return s.dt(); } 45 float framesPerSec() const { return 1/s.dt();} 46 int samplesPerFrame() const {return s.buffersize;} 47 int samplerate() const {return s.buffersize / s.dt();} 48 private: 49 int64_t frames; 50 const SYNTH_T &s; 51 52 }; 53 54 //Marker for an event relative to some position of the absolute timer 55 class RelTime 56 { 57 public: 58 RelTime(const AbsTime &t_, float sec) 59 :t(t_) 60 { 61 //Calculate time of event 62 float deltaFrames = sec*t.framesPerSec(); 63 int64_t tmp = (int64_t)deltaFrames; 64 frame = t.time() + tmp; 65 sample = (int32_t)(t.samplesPerFrame()*(deltaFrames-tmp)); 66 } 67 bool inThisFrame() {return t.time() == frame;}; 68 bool inPast() {return t.time() > frame;} 69 bool inFuture() {return t.time() < frame;} 70 private: 71 int64_t frame; 72 int32_t sample; 73 const AbsTime &t; 74 }; 75 76 }