Skip to content

Commit

Permalink
In FlexASIOTest, dump the time data in bufferSwitchTimeInfo.
Browse files Browse the repository at this point in the history
See #7.
  • Loading branch information
dechamps committed Oct 24, 2018
1 parent 8407c32 commit 057b028
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions FlexASIOTest/test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <condition_variable>
#include <iostream>
#include <functional>
Expand Down Expand Up @@ -30,6 +31,25 @@ namespace flexasio_test {
return std::nullopt;
}

// Ensures that chars are printed as numbers, not characters.
template <typename T, typename = std::enable_if_t<!std::is_arithmetic<std::decay_t<T>>::value>> auto Printable(T&& value) -> decltype(std::forward<T>(value)) { return std::forward<T>(value); }
template <typename T, typename = std::enable_if_t<std::is_arithmetic<std::decay_t<T>>::value>> auto Printable(T&& value) -> decltype(+std::forward<T>(value)) { return +std::forward<T>(value); }

template <typename Items> void Join(const Items& items, std::string_view delimiter, std::ostream& result) {
auto it = std::begin(items);
if (it == std::end(items)) return;
for (;;) {
result << Printable(*it);
if (++it == std::end(items)) break;
result << delimiter;
}
}
template <typename Items> std::string Join(const Items& items, std::string_view delimiter) {
std::stringstream result;
Join(items, delimiter, result);
return result.str();
}

template <typename Enum> std::string EnumToString(Enum value, std::initializer_list<std::pair<Enum, std::string_view>> enumStrings) {
std::stringstream result;
result << value;
Expand All @@ -38,6 +58,27 @@ namespace flexasio_test {
return result.str();
}

template <typename Bitfield> std::string BitfieldToString(Bitfield bitfield, std::initializer_list<std::pair<Bitfield, std::string_view>> bitStrings) {
std::vector<std::string_view> bits;
std::stringstream result;
result << bitfield;
for (const auto& bitString : bitStrings) {
if (bitfield & bitString.first) bits.push_back(bitString.second);
}
if (!bits.empty()) {
result << " ";
Join(bits, " ", result);
}
return result.str();
}

template <typename ASIOInt64> int64_t ASIOToInt64(ASIOInt64 asioInt64) {
int64_t result;
static_assert(sizeof asioInt64 == sizeof result);
memcpy(&result, &asioInt64, sizeof result);
return result;
}

std::string GetASIOErrorString(ASIOError error) {
return EnumToString(error, {
{ASE_OK, "ASE_OK"},
Expand Down Expand Up @@ -98,11 +139,41 @@ namespace flexasio_test {
});
}

std::string GetAsioTimeInfoFlagsString(unsigned long timeInfoFlags) {
return BitfieldToString(timeInfoFlags, {
{kSystemTimeValid, "kSystemTimeValid"},
{kSamplePositionValid, "kSamplePositionValid"},
{kSampleRateValid, "kSampleRateValid"},
{kSpeedValid, "kSpeedValid"},
{kSampleRateChanged, "kSampleRateChanged"},
{kClockSourceChanged, "kClockSourceChanged"},
});
}

std::string GetASIOTimeCodeFlagsString(unsigned long timeCodeFlags) {
return BitfieldToString(timeCodeFlags, {
{kTcValid, "kTcValid"},
{kTcRunning, "kTcRunning"},
{kTcReverse, "kTcReverse"},
{kTcOnspeed, "kTcOnspeed"},
{kTcStill, "kTcStill"},
{kTcSpeedValid, "kTcSpeedValid"},
});
}

ASIOError PrintError(ASIOError error) {
std::cout << "-> " << GetASIOErrorString(error) << std::endl;
return error;
}

void PrintASIOTime(const ASIOTime& asioTime) {
std::cout << "ASIOTime::reserved = " << Join(asioTime.reserved, " ") << std::endl;
const auto& timeInfo = asioTime.timeInfo;
std::cout << "ASIOTime::timeInfo: speed = " << timeInfo.speed << " systemTime = " << ASIOToInt64(timeInfo.systemTime) << " samplePosition = " << ASIOToInt64(timeInfo.samplePosition) << " sampleRate = " << timeInfo.sampleRate << " flags = " << GetAsioTimeInfoFlagsString(timeInfo.flags) << " reserved = " << Join(timeInfo.reserved, " ") << std::endl;
const auto& timeCode = asioTime.timeCode;
std::cout << "ASIOTime::timeCode: speed = " << timeCode.speed << " timeCodeSamples = " << ASIOToInt64(timeCode.timeCodeSamples) << " flags = " << GetASIOTimeCodeFlagsString(timeCode.flags) << " future = " << Join(timeCode.future, " ") << std::endl;
}

std::optional<ASIODriverInfo> Init() {
ASIODriverInfo asioDriverInfo = { 0 };
asioDriverInfo.asioVersion = 2;
Expand Down Expand Up @@ -356,6 +427,7 @@ namespace flexasio_test {
};
callbacks.bufferSwitchTimeInfo = [&](ASIOTime* params, long doubleBufferIndex, ASIOBool directProcess) {
std::cout << "bufferSwitchTimeInfo(params = " << params << ", doubleBufferIndex = " << doubleBufferIndex << ", directProcess = " << directProcess << ")" << std::endl;
if (params != nullptr) PrintASIOTime(*params);
incrementBufferSwitchCount();
std::cout << "<- nullptr" << std::endl;
return nullptr;
Expand Down

0 comments on commit 057b028

Please sign in to comment.