diff options
author | haslersn <sebastian.hasler@gmx.net> | 2019-07-04 01:48:54 +0200 |
---|---|---|
committer | haslersn <sebastian.hasler@gmx.net> | 2019-07-08 00:39:02 +0200 |
commit | 0d8d4de2d35703754b9a5637d2c280679235a2c5 (patch) | |
tree | a6d69ba7c948d6d0920c897905af9be8af5911b4 /src | |
parent | 0a79f05c92d655e492d797b0a9d023ef80764338 (diff) |
enginelibrary: Move normalize_beatgrid to djinterop/enginelibrary.hpp
Diffstat (limited to 'src')
-rw-r--r-- | src/djinterop/enginelibrary.cpp | 63 | ||||
-rw-r--r-- | src/djinterop/performance_data.cpp | 88 |
2 files changed, 62 insertions, 89 deletions
diff --git a/src/djinterop/enginelibrary.cpp b/src/djinterop/enginelibrary.cpp index 7c5b3d4..41a6b91 100644 --- a/src/djinterop/enginelibrary.cpp +++ b/src/djinterop/enginelibrary.cpp @@ -1,4 +1,5 @@ #include <sys/stat.h> +#include <cmath> #include <string> #if defined(_WIN32) #include <direct.h> @@ -18,7 +19,7 @@ database load_database(std::string directory) } database make_database( - std::string directory, const semantic_version& default_version) + std::string directory, const semantic_version &default_version) { if (!is_supported(default_version)) { @@ -69,5 +70,65 @@ database make_database( return load_database(std::move(directory)); } +std::vector<beatgrid_marker> normalize_beatgrid( + std::vector<beatgrid_marker> beatgrid, int64_t sample_count) +{ + if (beatgrid.empty()) + { + return beatgrid; // Named RVO + } + + { + auto last_marker_iter = std::find_if( + beatgrid.begin(), beatgrid.end(), + [sample_count](const beatgrid_marker &marker) { + return marker.sample_offset > sample_count; + }); + if (last_marker_iter != beatgrid.end()) + { + beatgrid.erase(last_marker_iter + 1, beatgrid.end()); + } + } + + { + auto after_first_marker_iter = std::find_if( + beatgrid.begin(), beatgrid.end(), + [](const beatgrid_marker &marker) { + return marker.sample_offset > 0; + }); + if (after_first_marker_iter != beatgrid.begin()) + { + beatgrid.erase(beatgrid.begin(), after_first_marker_iter - 1); + } + } + + if (beatgrid.size() < 2) + { + throw std::invalid_argument{ + "Attempted to normalize a misplaced beadgrid"}; + } + + { + double samples_per_beat = + (beatgrid[1].sample_offset - beatgrid[0].sample_offset) / + (beatgrid[1].index - beatgrid[0].index); + beatgrid[0].sample_offset -= (4 + beatgrid[0].index) * samples_per_beat; + beatgrid[0].index = -4; + } + + { + int32_t last = static_cast<int32_t>(beatgrid.size() - 1); + double samples_per_beat = + (beatgrid[last].sample_offset - beatgrid[last - 1].sample_offset) / + (beatgrid[last].index - beatgrid[last - 1].index); + int32_t index_adjustment = static_cast<int32_t>(std::ceil( + (sample_count - beatgrid[last].sample_offset) / samples_per_beat)); + beatgrid[last].sample_offset += index_adjustment * samples_per_beat; + beatgrid[last].index += index_adjustment; + } + + return beatgrid; // Named RVO +} + } // namespace enginelibrary } // namespace djinterop diff --git a/src/djinterop/performance_data.cpp b/src/djinterop/performance_data.cpp index 6656919..e69de29 100644 --- a/src/djinterop/performance_data.cpp +++ b/src/djinterop/performance_data.cpp @@ -1,88 +0,0 @@ -/* - This file is part of libdjinterop. - - libdjinterop is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libdjinterop is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with libdjinterop. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <algorithm> -#include <cmath> - -#include <djinterop/djinterop.hpp> - -namespace djinterop -{ -namespace enginelibrary -{ -std::vector<beatgrid_marker> normalize_beatgrid( - std::vector<beatgrid_marker> beatgrid, int64_t sample_count) -{ - if (beatgrid.empty()) - { - return beatgrid; // Named RVO - } - - { - auto last_marker_iter = std::find_if( - beatgrid.begin(), beatgrid.end(), - [sample_count](const beatgrid_marker &marker) { - return marker.sample_offset > sample_count; - }); - if (last_marker_iter != beatgrid.end()) - { - beatgrid.erase(last_marker_iter + 1, beatgrid.end()); - } - } - - { - auto after_first_marker_iter = std::find_if( - beatgrid.begin(), beatgrid.end(), - [](const beatgrid_marker &marker) { - return marker.sample_offset > 0; - }); - if (after_first_marker_iter != beatgrid.begin()) - { - beatgrid.erase(beatgrid.begin(), after_first_marker_iter - 1); - } - } - - if (beatgrid.size() < 2) - { - throw std::invalid_argument{ - "Attempted to normalize a misplaced beadgrid"}; - } - - { - double samples_per_beat = - (beatgrid[1].sample_offset - beatgrid[0].sample_offset) / - (beatgrid[1].index - beatgrid[0].index); - beatgrid[0].sample_offset -= (4 + beatgrid[0].index) * samples_per_beat; - beatgrid[0].index = -4; - } - - { - int32_t last = static_cast<int32_t>(beatgrid.size() - 1); - double samples_per_beat = - (beatgrid[last].sample_offset - beatgrid[last - 1].sample_offset) / - (beatgrid[last].index - beatgrid[last - 1].index); - int32_t index_adjustment = static_cast<int32_t>(std::ceil( - (sample_count - beatgrid[last].sample_offset) / samples_per_beat)); - beatgrid[last].sample_offset += index_adjustment * samples_per_beat; - beatgrid[last].index += index_adjustment; - } - - return beatgrid; // Named RVO -} - -} // namespace enginelibrary -} // namespace djinterop |