Skip to content

Commit

Permalink
[quantification] Add a test and fix some quantification issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 3, 2024
1 parent 41e79d4 commit f327672
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/ossia/dataflow/token_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ struct token_request

if(start_quant != end_quant)
{
if(end_quant == end_quarter * musical_quant_dur)
{
// We want quantization on start, not on end
return std::nullopt;
}
// Date to quantify is the next one :
const double musical_tick_duration
= musical_end_position - musical_start_position;
Expand All @@ -250,9 +255,9 @@ struct token_request

quantification_date = prev_date + quantified_duration * ratio;
}
else if(musical_start_position == 0. && musical_end_position > 0.)
else if(start_quant == start_quarter * musical_quant_dur)
{
// Special first bar case
// We start on a signature change
return prev_date;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ossia/editor/scenario/detail/scenario_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void scenario::run_interval(
{
interval.tick_offset_speed_precomputed(max_tick, offset, tk);
}
else if(max_tick == 0_tv)
else if(cst_max_dur == 0_tv)
{
interval.tick_offset_speed_precomputed(max_tick, offset, tk);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ossia/editor/scenario/time_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ void time_interval::tick_impl(
m_musical_start_position = num_quarters;
}

if(new_date.impl - 1 > old_date.impl)
if(new_date.impl > old_date.impl)
{
auto d = ossia::time_value{new_date.impl - 1};
auto d = ossia::time_value{new_date.impl};
const double num_quarters = d.impl / m_quarter_duration;

auto [time, sig] = *ossia::last_before(m_timeSignature, d);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ if(OSSIA_EDITOR)
ossia_add_test(ExpressionPulseTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/ExpressionPulseTest.cpp")
ossia_add_test(MapperTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/MapperTest.cpp")
ossia_add_test(MessageTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/MessageTest.cpp")
ossia_add_test(QuantificationTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/QuantificationTest.cpp")
ossia_add_test(StateTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/StateTest.cpp")
ossia_add_test(TimeIntervalTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/TimeIntervalTest.cpp")
ossia_add_test(TimeEventTest "${CMAKE_CURRENT_SOURCE_DIR}/Editor/TimeEventTest.cpp")
Expand Down
138 changes: 138 additions & 0 deletions tests/Editor/QuantificationTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

#include "TestUtils.hpp"
#include "include_catch.hpp"

#include <ossia/detail/config.hpp>

#include <ossia/editor/scenario/time_interval.hpp>

using namespace ossia;
using namespace std::placeholders;

TEST_CASE("get_quantification_date_0", "get_quantification_date_0")
{
ossia::token_request req{0_tv, 100_tv, 15000_tv, 0_tv, 1., {4, 4}, ossia::root_tempo};
REQUIRE(req.get_quantification_date(0.25));
REQUIRE(req.get_quantification_date(0.25) == 0_tv);
REQUIRE(req.get_quantification_date(1.));
REQUIRE(req.get_quantification_date(1.) == 0_tv);
REQUIRE(req.get_quantification_date(2.));
REQUIRE(req.get_quantification_date(2.) == 0_tv);
REQUIRE(req.get_quantification_date(100.));
REQUIRE(req.get_quantification_date(100.) == 0_tv);
}

TEST_CASE("get_quantification_date_some", "get_quantification_date_some")
{
ossia::token_request req{100_tv, 200_tv, 15000_tv, 0_tv,
1., {4, 4}, ossia::root_tempo};
GIVEN("quantization in the middle")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 0.95;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.05;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 150_tv);
}
GIVEN("quantization before the beginning 1")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 0.99;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.05;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 116_tv);
}
GIVEN("quantization before the beginning 2")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 0.999;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.05;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 101_tv);
}
GIVEN("quantization before the beginning 3")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 0.999999999;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.05;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 100_tv);
}
GIVEN("quantization before the beginning 4")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = std::nextafter(1., 0.);
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.05;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 100_tv);
}
GIVEN("quantization at the beginning")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 1.;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.10;
REQUIRE(req.get_quantification_date(4.));
REQUIRE(req.get_quantification_date(4.) == 100_tv);
}

GIVEN("quantization before the end")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 1.90;
req.musical_end_last_bar = 0.;
req.musical_end_position = 1.999999999999999;
REQUIRE(!req.get_quantification_date(4.));
}
GIVEN("quantization at the end")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = 1.90;
req.musical_end_last_bar = 0.;
req.musical_end_position = 2.;
req.get_quantification_date(4.);
REQUIRE(!req.get_quantification_date(4.));
//REQUIRE(!req.get_quantification_date(4.) == 200_tv);
}
GIVEN("quantization after the end")
{
req.musical_start_last_signature = 0.;
req.musical_start_last_bar = 0.;
req.musical_start_position = std::nextafter(2., 3.);
REQUIRE(req.musical_start_position != 2.);
req.musical_end_last_bar = 0.;
req.musical_end_position = 2.1;
REQUIRE(!req.get_quantification_date(4.));
}
}
TEST_CASE("test_quant", "test_quant")
{
std::shared_ptr<ossia::time_sync> start_node{std::make_shared<ossia::time_sync>()};
std::shared_ptr<ossia::time_sync> end_node{std::make_shared<ossia::time_sync>()};

std::shared_ptr<ossia::time_event> start_event{std::make_shared<ossia::time_event>(
ossia::time_event::exec_callback{}, *start_node,
ossia::expressions::make_expression_true())};
std::shared_ptr<ossia::time_event> end_event{std::make_shared<ossia::time_event>(
ossia::time_event::exec_callback{}, *end_node,
ossia::expressions::make_expression_true())};

std::shared_ptr<ossia::time_interval> interval{ossia::time_interval::create(
{}, *start_event, *end_event, ossia::time_value{15000}, ossia::time_value{15000},
ossia::time_value{15000})};

//
}
5 changes: 5 additions & 0 deletions tests/Editor/TestUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ std::ostream& operator<<(std::ostream& d, const ossia::time_value& t)
d << t.impl;
return d;
}
inline std::ostream&
operator<<(std::ostream& d, const std::optional<ossia::time_value>& t)
{
return t ? (d << t->impl) : (d << "nullopt");
}
inline
std::ostream& operator<<(std::ostream& d, const ossia::token_request& t)
{
Expand Down

0 comments on commit f327672

Please sign in to comment.