Browse Source

Use higher resolution clock for system::getNanoseconds() on Windows.

Ban `long` from codebase. Use int64_t instead.
tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
306e454f0c
10 changed files with 39 additions and 28 deletions
  1. +3
    -3
      include/engine/Engine.hpp
  2. +1
    -1
      include/midi.hpp
  3. +3
    -3
      include/system.hpp
  4. +2
    -2
      src/core/MIDI_CC.cpp
  5. +2
    -2
      src/core/MIDI_CV.cpp
  6. +2
    -2
      src/core/MIDI_Gate.cpp
  7. +2
    -2
      src/core/MIDI_Map.cpp
  8. +9
    -9
      src/engine/Engine.cpp
  9. +2
    -2
      src/logger.cpp
  10. +13
    -2
      src/system.cpp

+ 3
- 3
include/engine/Engine.hpp View File

@@ -37,11 +37,11 @@ struct Engine {
void yieldWorkers();
/** Returns the number of audio samples since the Engine's first sample.
*/
long getFrame();
int64_t getFrame();
/** Returns the frame when step() was last called. */
long getStepFrame();
int64_t getStepFrame();
/** Returns the timestamp in nanoseconds when step() was last called. */
long getStepTime();
int64_t getStepTime();
/** Returns the total number of frames in the current step() call. */
int getStepFrames();



+ 1
- 1
include/midi.hpp View File

@@ -18,7 +18,7 @@ struct Message {
/** Initialized to 3 empty bytes. */
std::vector<uint8_t> bytes;
/** Timestamp of MIDI message in nanoseconds. Negative if not set. */
long timestamp = -1;
int64_t timestamp = -1;

Message() : bytes(3) {}



+ 3
- 3
include/system.hpp View File

@@ -36,10 +36,10 @@ int getLogicalCoreCount();
void setThreadName(const std::string& name);
/** Returns the caller's human-readable stack trace with "\n"-separated lines. */
std::string getStackTrace();
/** Returns the current number of nanoseconds since the epoch.
Currently uses std::chrono::high_resolution_clock.
/** Returns the current number of nanoseconds since the epoch with the highest precion available on the OS.
The epoch is undefined and could be the UNIX epoch, time since boot, time since launch, etc.
*/
long getNanoseconds();
int64_t getNanoseconds();
/** Opens a URL, also happens to work with PDFs and folders.
Shell injection is possible, so make sure the URL is trusted or hard coded.
May block, so open in a new thread.


+ 2
- 2
src/core/MIDI_CC.cpp View File

@@ -51,8 +51,8 @@ struct MIDI_CC : Module {
// Process MIDI messages only if they arrived `stepFrames` frames ago.
while (!midiInput.queue.empty()) {
midi::Message& msg = midiInput.queue.front();
long msgTime = msg.timestamp + long(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
long frameTime = APP->engine->getStepTime() + long((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
int64_t msgTime = msg.timestamp + int64_t(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
int64_t frameTime = APP->engine->getStepTime() + int64_t((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
if (msgTime > frameTime)
break;
processMessage(msg);


+ 2
- 2
src/core/MIDI_CV.cpp View File

@@ -122,8 +122,8 @@ struct MIDI_CV : Module {
// Process MIDI messages only if they arrived `stepFrames` frames ago.
while (!midiInput.queue.empty()) {
midi::Message& msg = midiInput.queue.front();
long msgTime = msg.timestamp + long(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
long frameTime = APP->engine->getStepTime() + long((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
int64_t msgTime = msg.timestamp + int64_t(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
int64_t frameTime = APP->engine->getStepTime() + int64_t((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
if (msgTime > frameTime)
break;
processMessage(msg);


+ 2
- 2
src/core/MIDI_Gate.cpp View File

@@ -58,8 +58,8 @@ struct MIDI_Gate : Module {
// Process MIDI messages only if they arrived `stepFrames` frames ago.
while (!midiInput.queue.empty()) {
midi::Message& msg = midiInput.queue.front();
long msgTime = msg.timestamp + long(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
long frameTime = APP->engine->getStepTime() + long((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
int64_t msgTime = msg.timestamp + int64_t(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
int64_t frameTime = APP->engine->getStepTime() + int64_t((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
if (msgTime > frameTime)
break;
processMessage(msg);


+ 2
- 2
src/core/MIDI_Map.cpp View File

@@ -81,8 +81,8 @@ struct MIDI_Map : Module {
// Process MIDI messages only if they arrived `stepFrames` frames ago.
while (!midiInput.queue.empty()) {
midi::Message& msg = midiInput.queue.front();
long msgTime = msg.timestamp + long(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
long frameTime = APP->engine->getStepTime() + long((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
int64_t msgTime = msg.timestamp + int64_t(APP->engine->getStepFrames() * APP->engine->getSampleTime() * 1e9);
int64_t frameTime = APP->engine->getStepTime() + int64_t((APP->engine->getFrame() - APP->engine->getStepFrame()) * APP->engine->getSampleTime() * 1e9);
if (msgTime > frameTime)
break;
processMessage(msg);


+ 9
- 9
src/engine/Engine.cpp View File

@@ -151,9 +151,9 @@ struct Engine::Internal {

float sampleRate = 0.f;
float sampleTime = 0.f;
long frame = 0;
long stepFrame = 0;
long stepTime = 0;
int64_t frame = 0;
int64_t stepFrame = 0;
int64_t stepTime = 0;
int stepFrames = 0;
Module* primaryModule = NULL;

@@ -224,7 +224,7 @@ static void Engine_stepModulesWorker(Engine* that, int threadId) {
Module* module = internal->modules[i];

// Start CPU timer
long startTime;
int64_t startTime;
if (cpuMeter) {
startTime = system::getNanoseconds();
}
@@ -237,8 +237,8 @@ static void Engine_stepModulesWorker(Engine* that, int threadId) {

// Stop CPU timer
if (cpuMeter) {
long endTime = system::getNanoseconds();
float duration = (endTime - startTime) / 1e9;
int64_t endTime = system::getNanoseconds();
float duration = (endTime - startTime) * 1e-9f;

// Smooth CPU time
const float cpuTau = 2.f /* seconds */;
@@ -521,19 +521,19 @@ void Engine::yieldWorkers() {
}


long Engine::getFrame() {
int64_t Engine::getFrame() {
// No lock, for performance
return internal->frame;
}


long Engine::getStepFrame() {
int64_t Engine::getStepFrame() {
// No lock, for performance
return internal->stepFrame;
}


long Engine::getStepTime() {
int64_t Engine::getStepTime() {
// No lock, for performance
return internal->stepTime;
}


+ 2
- 2
src/logger.cpp View File

@@ -10,7 +10,7 @@ namespace logger {


static FILE* outputFile = NULL;
static long startTime = 0;
static int64_t startTime = 0;
static std::mutex logMutex;


@@ -50,7 +50,7 @@ static const int levelColors[] = {
static void logVa(Level level, const char* filename, int line, const char* format, va_list args) {
std::lock_guard<std::mutex> lock(logMutex);

long nowTime = system::getNanoseconds();
int64_t nowTime = system::getNanoseconds();
double duration = (nowTime - startTime) / 1e9;
if (outputFile == stderr)
fprintf(outputFile, "\x1B[%dm", levelColors[level]);


+ 13
- 2
src/system.cpp View File

@@ -215,13 +215,24 @@ std::string getStackTrace() {
}


long getNanoseconds() {
int64_t getNanoseconds() {
#if defined ARCH_WIN
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
// TODO Check if this is always an integer factor on all CPUs
int64_t nsPerTick = 1000000000LL / frequency.QuadPart;
int64_t time = counter.QuadPart * nsPerTick;
return time;
#else
using clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::time_point<clock>;
time_point now = clock::now();
using duration = std::chrono::duration<long, std::nano>;
using duration = std::chrono::duration<int64_t, std::nano>;
duration d = now.time_since_epoch();
return d.count();
#endif
}




Loading…
Cancel
Save