Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderVocke committed Jun 24, 2024
1 parent 9511b6d commit 3627ac4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
Empty file.
Empty file.
12 changes: 10 additions & 2 deletions src/libshoopdaloop/internal/MidiStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <memory>
#include <optional>
#include <stdexcept>
#include <stdio.h>
#include <vector>
#include <fmt/format.h>
Expand Down Expand Up @@ -413,7 +414,7 @@ void MidiStorage<TimeType, SizeType>::truncate(TimeType time, TruncateType type)
auto prev_n_events = this->m_n_events;

if (type == TruncateType::TruncateHead &&
this->unsafe_at(this->m_head_start)->storage_time > time)
this->unsafe_at(this->m_head_start)->storage_time <= time)
{
this->template log<log_level_debug_trace>("truncate: head already in range");
return;
Expand Down Expand Up @@ -478,9 +479,16 @@ void MidiStorage<TimeType, SizeType>::for_each_msg_modify(
auto maybe_self = MidiStorageBase<TimeType, SizeType>::weak_from_this();
if (auto self = maybe_self.lock()) {
auto cursor = shoop_make_shared<Cursor>(shoop_static_pointer_cast<const MidiStorageBase<TimeType, SizeType>>(self));
for (cursor->reset(); cursor->valid(); cursor->next()) {
cursor->reset();
auto *first_elem = cursor->get();
decltype(first_elem) prev_elem = nullptr;
for (; cursor->valid(); cursor->next()) {
auto *elem = cursor->get();
if (elem >= first_elem && prev_elem && ((prev_elem < first_elem) || (elem < prev_elem))) {
throw std::runtime_error ("Message iterator looped back to start");
}
cb(elem->storage_time, elem->size, elem->data());
prev_elem = elem;
}
cursor = nullptr;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/libshoopdaloop/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(TEST_RUNNER_SOURCES
unit/test_DummyPorts.cpp
unit/test_InternalAudioPort.cpp
unit/test_BufferQueue.cpp
unit/test_MidiChannel.cpp
unit/test_MidiStorage.cpp
unit/test_MidiRingbuffer.cpp
integration/test_libshoopdaloop_if.cpp
Expand Down
61 changes: 61 additions & 0 deletions src/libshoopdaloop/test/unit/test_MidiChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "MidiChannel.h"
#include "MidiMessage.h"

#include <catch2/catch_test_macros.hpp>

namespace Catch {
template<>
struct StringMaker<std::vector<uint8_t>> {
static std::string convert(const std::vector<uint8_t>& vec) {
std::ostringstream oss;
oss << "[";
for (size_t i=0; i<vec.size(); i++) {
if (i>0) { oss << ", "; }
oss << (int)vec[i];
}
oss << "]";
return oss.str();
}
};

template<>
struct StringMaker<MidiMessage<uint32_t, uint32_t>> {
static std::string convert(const MidiMessage<uint32_t, uint32_t>& e) {
std::ostringstream oss;
oss << "{ t:" << e.time << ", s:" << e.size << ", d:" << StringMaker<std::vector<uint8_t>>::convert(e.data) << " }";
return oss.str();
}
};

template<>
struct StringMaker<std::vector<MidiMessage<uint32_t, uint32_t>>> {
static std::string convert(const std::vector<MidiMessage<uint32_t, uint32_t>>& vec) {
std::ostringstream oss;
oss << "[\n";
for (auto &e : vec) {
oss << " " << StringMaker<MidiMessage<uint32_t, uint32_t>>::convert(e) << "\n";
}
oss << "]";
return oss.str();
}
};
}

TEST_CASE("MidiChannel - Indifinite size", "[MidiChannel]") {
using Msg = MidiMessage<uint32_t, uint32_t>;
using Storage = MidiStorage<uint32_t, uint32_t>;
using Channel = MidiChannel<uint32_t, uint32_t>;
using Contents = Channel::Contents;

auto c = shoop_make_shared<MidiChannel<uint32_t, uint32_t>>(1, ChannelMode_Direct);

const std::vector<Msg> data = {
Msg(0, 3, {0, 1, 2}),
Msg(1, 3, {3, 4, 5}),
Msg(10, 1, {10})
};
const Contents contents = { data, {} };

c->set_contents(contents, 1000, false);
CHECK(c->retrieve_contents(false).recorded_msgs == data);
};

0 comments on commit 3627ac4

Please sign in to comment.