1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
|
/*
* tuning.cpp
* ----------
* Purpose: Alternative sample tuning.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "tuning.h"
#include "mpt/io/io.hpp"
#include "mpt/io/io_stdstream.hpp"
#include "../common/serialization_utils.h"
#include "../common/misc_util.h"
#include <string>
#include <cmath>
OPENMPT_NAMESPACE_BEGIN
namespace Tuning {
static RATIOTYPE SanitizeGroupRatio(RATIOTYPE ratio)
{
return std::clamp(std::abs(ratio), 1e-15f, 1e+07f);
}
namespace CTuningS11n
{
void ReadStr(std::istream &iStrm, mpt::ustring &ustr, const std::size_t dummy, mpt::Charset charset);
void ReadNoteMap(std::istream &iStrm, std::map<NOTEINDEXTYPE, mpt::ustring> &m, const std::size_t dummy, mpt::Charset charset);
void ReadRatioTable(std::istream& iStrm, std::vector<RATIOTYPE>& v, const size_t);
void WriteNoteMap(std::ostream &oStrm, const std::map<NOTEINDEXTYPE, mpt::ustring> &m);
void WriteStr(std::ostream &oStrm, const mpt::ustring &ustr);
struct RatioWriter
{
RatioWriter(uint16 nWriteCount = s_nDefaultWriteCount) : m_nWriteCount(nWriteCount) {}
void operator()(std::ostream& oStrm, const std::vector<float>& v);
uint16 m_nWriteCount;
enum : uint16 { s_nDefaultWriteCount = (uint16_max >> 2) };
};
}
using namespace CTuningS11n;
/*
Version history:
4->5: Lots of changes, finestep interpretation revamp, fileformat revamp.
3->4: Changed sizetypes in serialisation from size_t(uint32) to
smaller types (uint8, USTEPTYPE) (March 2007)
*/
/*
Version changes:
3->4: Finetune related internal structure and serialization revamp.
2->3: The type for the size_type in the serialisation changed
from default(size_t, uint32) to unsigned STEPTYPE. (March 2007)
*/
static_assert(CTuning::s_RatioTableFineSizeMaxDefault < static_cast<USTEPINDEXTYPE>(FINESTEPCOUNT_MAX));
CTuning::CTuning()
{
#if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(12, 3, 0)
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109455
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100366
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif
m_RatioTable.resize(s_RatioTableSizeDefault, 1);
#if MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(12, 3, 0)
#pragma GCC diagnostic pop
#endif
}
bool CTuning::operator==(const CTuning &other) const noexcept
{
return m_TuningType == other.m_TuningType
&& m_NoteMin == other.m_NoteMin
&& m_GroupSize == other.m_GroupSize
&& m_GroupRatio == other.m_GroupRatio
&& m_FineStepCount == other.m_FineStepCount
&& m_RatioTable == other.m_RatioTable
&& m_RatioTableFine == other.m_RatioTableFine
&& m_TuningName == other.m_TuningName
&& m_NoteNameMap == other.m_NoteNameMap;
}
bool CTuning::CreateGroupGeometric(const NOTEINDEXTYPE &s, const RATIOTYPE &r, const NOTEINDEXTYPE &startindex)
{
if(s < 1 || !IsValidRatio(r) || startindex < GetNoteRange().first)
{
return false;
}
std::vector<RATIOTYPE> v;
v.reserve(s);
for(NOTEINDEXTYPE i = startindex; i < startindex + s; i++)
{
v.push_back(GetRatio(i));
}
return CreateGroupGeometric(v, r, GetNoteRange(), startindex);
}
bool CTuning::CreateGroupGeometric(const std::vector<RATIOTYPE> &v, const RATIOTYPE &r, const NoteRange &range, const NOTEINDEXTYPE &ratiostartpos)
{
if(range.first > range.last || v.size() == 0)
{
return false;
}
if(ratiostartpos < range.first || range.last < ratiostartpos || static_cast<UNOTEINDEXTYPE>(range.last - ratiostartpos) < static_cast<UNOTEINDEXTYPE>(v.size() - 1))
{
return false;
}
if(GetFineStepCount() > FINESTEPCOUNT_MAX)
{
return false;
}
for(size_t i = 0; i < v.size(); i++)
{
if(v[i] < 0)
{
return false;
}
}
if(r <= 0)
{
return false;
}
m_TuningType = Type::GROUPGEOMETRIC;
m_NoteMin = range.first;
m_GroupSize = mpt::saturate_cast<NOTEINDEXTYPE>(v.size());
m_GroupRatio = std::fabs(r);
m_RatioTable.resize(range.last - range.first + 1);
std::copy(v.begin(), v.end(), m_RatioTable.begin() + (ratiostartpos - range.first));
for(int32 i = ratiostartpos - 1; i >= m_NoteMin && ratiostartpos > NOTEINDEXTYPE_MIN; i--)
{
m_RatioTable[i - m_NoteMin] = m_RatioTable[i - m_NoteMin + m_GroupSize] / m_GroupRatio;
}
for(int32 i = ratiostartpos + m_GroupSize; i <= range.last && ratiostartpos <= (NOTEINDEXTYPE_MAX - m_GroupSize); i++)
{
m_RatioTable[i - m_NoteMin] = m_GroupRatio * m_RatioTable[i - m_NoteMin - m_GroupSize];
}
UpdateFineStepTable();
return true;
}
bool CTuning::CreateGeometric(const UNOTEINDEXTYPE &p, const RATIOTYPE &r)
{
return CreateGeometric(p, r, GetNoteRange());
}
bool CTuning::CreateGeometric(const UNOTEINDEXTYPE &s, const RATIOTYPE &r, const NoteRange &range)
{
if(range.first > range.last)
{
return false;
}
if(s < 1 || !IsValidRatio(r))
{
return false;
}
if(range.last - range.first + 1 > NOTEINDEXTYPE_MAX)
{
return false;
}
m_TuningType = Type::GEOMETRIC;
m_RatioTable.clear();
m_NoteMin = s_NoteMinDefault;
m_RatioTable.resize(s_RatioTableSizeDefault, static_cast<RATIOTYPE>(1.0));
m_GroupSize = 0;
m_GroupRatio = 0;
m_RatioTableFine.clear();
m_NoteMin = range.first;
m_GroupSize = mpt::saturate_cast<NOTEINDEXTYPE>(s);
m_GroupRatio = std::fabs(r);
const RATIOTYPE stepRatio = std::pow(m_GroupRatio, static_cast<RATIOTYPE>(1.0) / static_cast<RATIOTYPE>(m_GroupSize));
m_RatioTable.resize(range.last - range.first + 1);
for(int32 i = range.first; i <= range.last; i++)
{
m_RatioTable[i - m_NoteMin] = std::pow(stepRatio, static_cast<RATIOTYPE>(i));
}
UpdateFineStepTable();
return true;
}
mpt::ustring CTuning::GetNoteName(const NOTEINDEXTYPE &x, bool addOctave) const
{
if(!IsValidNote(x))
{
return mpt::ustring();
}
if(GetGroupSize() < 1)
{
const auto i = m_NoteNameMap.find(x);
if(i != m_NoteNameMap.end())
return i->second;
else
return mpt::ufmt::val(x);
}
else
{
const NOTEINDEXTYPE pos = static_cast<NOTEINDEXTYPE>(mpt::wrapping_modulo(x, m_GroupSize));
const NOTEINDEXTYPE middlePeriodNumber = 5;
mpt::ustring rValue;
const auto nmi = m_NoteNameMap.find(pos);
if(nmi != m_NoteNameMap.end())
{
rValue = nmi->second;
if(addOctave)
{
rValue += mpt::ufmt::val(middlePeriodNumber + mpt::wrapping_divide(x, m_GroupSize));
}
}
else
{
//By default, using notation nnP for notes; nn <-> note character starting
//from 'A' with char ':' as fill char, and P is period integer. For example:
//C:5, D:3, R:7
if(m_GroupSize <= 26)
{
rValue = mpt::ToUnicode(mpt::Charset::UTF8, std::string(1, static_cast<char>(pos + 'A')));
rValue += UL_(":");
} else
{
rValue = mpt::ufmt::HEX0<1>(pos % 16) + mpt::ufmt::HEX0<1>((pos / 16) % 16);
if(pos > 0xff)
{
rValue = mpt::ToUnicode(mpt::Charset::UTF8, mpt::ToLowerCaseAscii(mpt::ToCharset(mpt::Charset::UTF8, rValue)));
}
}
if(addOctave)
{
rValue += mpt::ufmt::val(middlePeriodNumber + mpt::wrapping_divide(x, m_GroupSize));
}
}
return rValue;
}
}
void CTuning::SetNoteName(const NOTEINDEXTYPE &n, const mpt::ustring &str)
{
const NOTEINDEXTYPE pos = (GetGroupSize() < 1) ? n : static_cast<NOTEINDEXTYPE>(mpt::wrapping_modulo(n, m_GroupSize));
if(!str.empty())
{
m_NoteNameMap[pos] = str;
} else
{
const auto iter = m_NoteNameMap.find(pos);
if(iter != m_NoteNameMap.end())
{
m_NoteNameMap.erase(iter);
}
}
}
// Without finetune
RATIOTYPE CTuning::GetRatio(const NOTEINDEXTYPE note) const
{
if(!IsValidNote(note))
{
return s_DefaultFallbackRatio;
}
const auto ratio = m_RatioTable[note - m_NoteMin];
if(ratio <= 1e-15f)
{
return s_DefaultFallbackRatio;
}
return ratio;
}
// With finetune
RATIOTYPE CTuning::GetRatio(const NOTEINDEXTYPE baseNote, const STEPINDEXTYPE baseFineSteps) const
{
const STEPINDEXTYPE fineStepCount = static_cast<STEPINDEXTYPE>(GetFineStepCount());
if(fineStepCount == 0 || baseFineSteps == 0)
{
return GetRatio(static_cast<NOTEINDEXTYPE>(baseNote + baseFineSteps));
}
// If baseFineSteps is more than the number of finesteps between notes, note is increased.
// So first figuring out what note and fineStep values to actually use.
// Interpreting finestep==-1 on note x so that it is the same as finestep==fineStepCount on note x-1.
// Note: If fineStepCount is n, n+1 steps are needed to get to next note.
const NOTEINDEXTYPE note = static_cast<NOTEINDEXTYPE>(baseNote + mpt::wrapping_divide(baseFineSteps, (fineStepCount + 1)));
const STEPINDEXTYPE fineStep = mpt::wrapping_modulo(baseFineSteps, (fineStepCount + 1));
if(!IsValidNote(note))
{
return s_DefaultFallbackRatio;
}
if(fineStep == 0)
{
return m_RatioTable[note - m_NoteMin];
}
RATIOTYPE fineRatio = static_cast<RATIOTYPE>(1.0);
if(GetType() == Type::GEOMETRIC && m_RatioTableFine.size() > 0)
{
fineRatio = m_RatioTableFine[fineStep - 1];
} else if(GetType() == Type::GROUPGEOMETRIC && m_RatioTableFine.size() > 0)
{
fineRatio = m_RatioTableFine[GetRefNote(note) * fineStepCount + fineStep - 1];
} else
{
// Geometric finestepping
fineRatio = std::pow(GetRatio(note + 1) / GetRatio(note), static_cast<RATIOTYPE>(fineStep) / static_cast<RATIOTYPE>(fineStepCount + 1));
}
return m_RatioTable[note - m_NoteMin] * fineRatio;
}
bool CTuning::SetRatio(const NOTEINDEXTYPE& s, const RATIOTYPE& r)
{
if(GetType() != Type::GROUPGEOMETRIC && GetType() != Type::GENERAL)
{
return false;
}
//Creating ratio table if doesn't exist.
if(m_RatioTable.empty())
{
m_RatioTable.assign(s_RatioTableSizeDefault, 1);
m_NoteMin = s_NoteMinDefault;
}
if(!IsValidNote(s))
{
return false;
}
m_RatioTable[s - m_NoteMin] = std::fabs(r);
if(GetType() == Type::GROUPGEOMETRIC)
{ // update other groups
for(NOTEINDEXTYPE n = m_NoteMin; n < m_NoteMin + static_cast<NOTEINDEXTYPE>(m_RatioTable.size()); ++n)
{
if(n == s)
{
// nothing
} else if(std::abs(n - s) % m_GroupSize == 0)
{
m_RatioTable[n - m_NoteMin] = std::pow(m_GroupRatio, static_cast<RATIOTYPE>(n - s) / static_cast<RATIOTYPE>(m_GroupSize)) * m_RatioTable[s - m_NoteMin];
}
}
UpdateFineStepTable();
}
return true;
}
void CTuning::SetFineStepCount(const USTEPINDEXTYPE& fs)
{
m_FineStepCount = std::clamp(mpt::saturate_cast<STEPINDEXTYPE>(fs), STEPINDEXTYPE(0), FINESTEPCOUNT_MAX);
UpdateFineStepTable();
}
void CTuning::UpdateFineStepTable()
{
if(m_FineStepCount <= 0)
{
m_RatioTableFine.clear();
return;
}
if(GetType() == Type::GEOMETRIC)
{
if(m_FineStepCount > s_RatioTableFineSizeMaxDefault)
{
m_RatioTableFine.clear();
return;
}
m_RatioTableFine.resize(m_FineStepCount);
const RATIOTYPE q = GetRatio(GetNoteRange().first + 1) / GetRatio(GetNoteRange().first);
const RATIOTYPE rFineStep = std::pow(q, static_cast<RATIOTYPE>(1) / static_cast<RATIOTYPE>(m_FineStepCount + 1));
for(USTEPINDEXTYPE i = 1; i<=m_FineStepCount; i++)
m_RatioTableFine[i-1] = std::pow(rFineStep, static_cast<RATIOTYPE>(i));
return;
}
if(GetType() == Type::GROUPGEOMETRIC)
{
const UNOTEINDEXTYPE p = GetGroupSize();
if(p > s_RatioTableFineSizeMaxDefault / m_FineStepCount)
{
//In case fineratiotable would become too large, not using
//table for it.
m_RatioTableFine.clear();
return;
}
else
{
//Creating 'geometric' finestepping between notes.
m_RatioTableFine.resize(p * m_FineStepCount);
const NOTEINDEXTYPE startnote = GetRefNote(GetNoteRange().first);
for(UNOTEINDEXTYPE i = 0; i<p; i++)
{
const NOTEINDEXTYPE refnote = GetRefNote(static_cast<NOTEINDEXTYPE>(startnote + i));
const RATIOTYPE rFineStep = std::pow(GetRatio(refnote+1) / GetRatio(refnote), static_cast<RATIOTYPE>(1) / static_cast<RATIOTYPE>(m_FineStepCount + 1));
for(UNOTEINDEXTYPE j = 1; j<=m_FineStepCount; j++)
{
m_RatioTableFine[m_FineStepCount * refnote + (j-1)] = std::pow(rFineStep, static_cast<RATIOTYPE>(j));
}
}
return;
}
}
if(GetType() == Type::GENERAL)
{
//Not using table with tuning of type general.
m_RatioTableFine.clear();
return;
}
//Should not reach here.
m_RatioTableFine.clear();
m_FineStepCount = 0;
}
bool CTuning::Multiply(const RATIOTYPE r)
{
if(!IsValidRatio(r))
{
return false;
}
for(auto & ratio : m_RatioTable)
{
ratio *= r;
}
return true;
}
bool CTuning::ChangeGroupsize(const NOTEINDEXTYPE& s)
{
if(s < 1)
return false;
if(m_TuningType == Type::GROUPGEOMETRIC)
return CreateGroupGeometric(s, GetGroupRatio(), 0);
if(m_TuningType == Type::GEOMETRIC)
return CreateGeometric(s, GetGroupRatio());
return false;
}
bool CTuning::ChangeGroupRatio(const RATIOTYPE& r)
{
if(!IsValidRatio(r))
return false;
if(m_TuningType == Type::GROUPGEOMETRIC)
return CreateGroupGeometric(GetGroupSize(), r, 0);
if(m_TuningType == Type::GEOMETRIC)
return CreateGeometric(GetGroupSize(), r);
return false;
}
SerializationResult CTuning::InitDeserialize(std::istream &iStrm, mpt::Charset defaultCharset)
{
// Note: OpenMPT since at least r323 writes version number (4<<24)+4 while it
// reads version number (5<<24)+4 or earlier.
// We keep this behaviour.
if(iStrm.fail())
return SerializationResult::Failure;
srlztn::SsbRead ssb(iStrm);
ssb.BeginRead("CTB244RTI", (5 << 24) + 4); // version
int8 use_utf8 = 0;
ssb.ReadItem(use_utf8, "UTF8");
const mpt::Charset charset = use_utf8 ? mpt::Charset::UTF8 : defaultCharset;
ssb.ReadItem(m_TuningName, "0", [charset](std::istream &iStrm, mpt::ustring &ustr, const std::size_t dummy){ return ReadStr(iStrm, ustr, dummy, charset); });
uint16 dummyEditMask = 0xffff;
ssb.ReadItem(dummyEditMask, "1");
std::underlying_type<Type>::type type = 0;
ssb.ReadItem(type, "2");
m_TuningType = static_cast<Type>(type);
ssb.ReadItem(m_NoteNameMap, "3", [charset](std::istream &iStrm, std::map<NOTEINDEXTYPE, mpt::ustring> &m, const std::size_t dummy){ return ReadNoteMap(iStrm, m, dummy, charset); });
ssb.ReadItem(m_FineStepCount, "4");
// RTI entries.
ssb.ReadItem(m_RatioTable, "RTI0", ReadRatioTable);
ssb.ReadItem(m_NoteMin, "RTI1");
ssb.ReadItem(m_GroupSize, "RTI2");
ssb.ReadItem(m_GroupRatio, "RTI3");
UNOTEINDEXTYPE ratiotableSize = 0;
ssb.ReadItem(ratiotableSize, "RTI4");
m_GroupRatio = SanitizeGroupRatio(m_GroupRatio);
if(!std::isfinite(m_GroupRatio))
{
return SerializationResult::Failure;
}
for(auto ratio : m_RatioTable)
{
if(!std::isfinite(ratio))
return SerializationResult::Failure;
}
// If reader status is ok and m_NoteMin is somewhat reasonable, process data.
if(ssb.HasFailed() || (m_NoteMin < -300) || (m_NoteMin > 300))
{
return SerializationResult::Failure;
}
// reject unknown types
if(m_TuningType != Type::GENERAL && m_TuningType != Type::GROUPGEOMETRIC && m_TuningType != Type::GEOMETRIC)
{
return SerializationResult::Failure;
}
if(m_GroupSize < 0)
{
return SerializationResult::Failure;
}
m_FineStepCount = std::clamp(mpt::saturate_cast<STEPINDEXTYPE>(m_FineStepCount), STEPINDEXTYPE(0), FINESTEPCOUNT_MAX);
if(m_RatioTable.size() > static_cast<size_t>(NOTEINDEXTYPE_MAX))
{
return SerializationResult::Failure;
}
if((GetType() == Type::GROUPGEOMETRIC) || (GetType() == Type::GEOMETRIC))
{
if(ratiotableSize < 1 || ratiotableSize > NOTEINDEXTYPE_MAX)
{
return SerializationResult::Failure;
}
if(GetType() == Type::GEOMETRIC)
{
if(!CreateGeometric(GetGroupSize(), GetGroupRatio(), NoteRange{m_NoteMin, static_cast<NOTEINDEXTYPE>(m_NoteMin + ratiotableSize - 1)}))
{
return SerializationResult::Failure;
}
} else
{
if(!CreateGroupGeometric(m_RatioTable, GetGroupRatio(), NoteRange{m_NoteMin, static_cast<NOTEINDEXTYPE>(m_NoteMin + ratiotableSize - 1)}, m_NoteMin))
{
return SerializationResult::Failure;
}
}
} else
{
UpdateFineStepTable();
}
return SerializationResult::Success;
}
template<class T, class SIZETYPE, class Tdst>
static bool VectorFromBinaryStream(std::istream& inStrm, std::vector<Tdst>& v, const SIZETYPE maxSize = (std::numeric_limits<SIZETYPE>::max)())
{
if(!inStrm.good())
return false;
SIZETYPE size = 0;
mpt::IO::ReadIntLE<SIZETYPE>(inStrm, size);
if(size > maxSize)
return false;
v.resize(size);
for(std::size_t i = 0; i<size; i++)
{
T tmp = T();
mpt::IO::Read(inStrm, tmp);
v[i] = tmp;
}
return inStrm.good();
}
SerializationResult CTuning::InitDeserializeOLD(std::istream &inStrm, mpt::Charset defaultCharset)
{
if(!inStrm.good())
return SerializationResult::Failure;
const std::streamoff startPos = static_cast<std::streamoff>(inStrm.tellg());
//First checking is there expected begin sequence.
char begin[8];
MemsetZero(begin);
inStrm.read(begin, sizeof(begin));
if(std::memcmp(begin, "CTRTI_B.", 8))
{
//Returning stream position if beginmarker was not found.
inStrm.seekg(startPos, std::ios::beg);
return SerializationResult::Failure;
}
//Version
int16 version = 0;
mpt::IO::ReadIntLE<int16>(inStrm, version);
if(version != 2 && version != 3)
return SerializationResult::Failure;
char begin2[8];
MemsetZero(begin2);
inStrm.read(begin2, sizeof(begin2));
if(std::memcmp(begin2, "CT<sfs>B", 8))
{
return SerializationResult::Failure;
}
int16 version2 = 0;
mpt::IO::ReadIntLE<int16>(inStrm, version2);
if(version2 != 3 && version2 != 4)
{
return SerializationResult::Failure;
}
//Tuning name
if(version2 <= 3)
{
std::string tmpName;
if(!mpt::IO::ReadSizedStringLE<uint32>(inStrm, tmpName, 0xffff))
{
return SerializationResult::Failure;
}
m_TuningName = mpt::ToUnicode(defaultCharset, tmpName);
} else
{
std::string tmpName;
if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, tmpName))
{
return SerializationResult::Failure;
}
m_TuningName = mpt::ToUnicode(defaultCharset, tmpName);
}
//Const mask
int16 em = 0;
mpt::IO::ReadIntLE<int16>(inStrm, em);
//Tuning type
int16 tt = 0;
mpt::IO::ReadIntLE<int16>(inStrm, tt);
m_TuningType = static_cast<Type>(tt);
//Notemap
uint16 size = 0;
if(version2 <= 3)
{
uint32 tempsize = 0;
mpt::IO::ReadIntLE<uint32>(inStrm, tempsize);
if(tempsize > 0xffff)
{
return SerializationResult::Failure;
}
size = mpt::saturate_cast<uint16>(tempsize);
} else
{
mpt::IO::ReadIntLE<uint16>(inStrm, size);
}
for(UNOTEINDEXTYPE i = 0; i<size; i++)
{
std::string str;
int16 n = 0;
mpt::IO::ReadIntLE<int16>(inStrm, n);
if(version2 <= 3)
{
if(!mpt::IO::ReadSizedStringLE<uint32>(inStrm, str, 0xffff))
{
return SerializationResult::Failure;
}
} else
{
if(!mpt::IO::ReadSizedStringLE<uint8>(inStrm, str))
{
return SerializationResult::Failure;
}
}
m_NoteNameMap[n] = mpt::ToUnicode(defaultCharset, str);
}
//End marker
char end2[8];
MemsetZero(end2);
inStrm.read(end2, sizeof(end2));
if(std::memcmp(end2, "CT<sfs>E", 8))
{
return SerializationResult::Failure;
}
// reject unknown types
if(m_TuningType != Type::GENERAL && m_TuningType != Type::GROUPGEOMETRIC && m_TuningType != Type::GEOMETRIC)
{
return SerializationResult::Failure;
}
//Ratiotable
if(version <= 2)
{
if(!VectorFromBinaryStream<IEEE754binary32LE, uint32>(inStrm, m_RatioTable, 0xffff))
{
return SerializationResult::Failure;
}
} else
{
if(!VectorFromBinaryStream<IEEE754binary32LE, uint16>(inStrm, m_RatioTable))
{
return SerializationResult::Failure;
}
}
for(auto ratio : m_RatioTable)
{
if(!std::isfinite(ratio))
return SerializationResult::Failure;
}
//Fineratios
if(version <= 2)
{
if(!VectorFromBinaryStream<IEEE754binary32LE, uint32>(inStrm, m_RatioTableFine, 0xffff))
{
return SerializationResult::Failure;
}
} else
{
if(!VectorFromBinaryStream<IEEE754binary32LE, uint16>(inStrm, m_RatioTableFine))
{
return SerializationResult::Failure;
}
}
for(auto ratio : m_RatioTableFine)
{
if(!std::isfinite(ratio))
return SerializationResult::Failure;
}
m_FineStepCount = mpt::saturate_cast<USTEPINDEXTYPE>(m_RatioTableFine.size());
// m_NoteMin
int16 notemin = 0;
mpt::IO::ReadIntLE<int16>(inStrm, notemin);
m_NoteMin = notemin;
if(m_NoteMin < -200 || m_NoteMin > 200)
{
return SerializationResult::Failure;
}
//m_GroupSize
int16 groupsize = 0;
mpt::IO::ReadIntLE<int16>(inStrm, groupsize);
m_GroupSize = groupsize;
if(m_GroupSize < 0)
{
return SerializationResult::Failure;
}
//m_GroupRatio
IEEE754binary32LE groupratio = IEEE754binary32LE(0.0f);
mpt::IO::Read(inStrm, groupratio);
m_GroupRatio = SanitizeGroupRatio(groupratio);
if(!std::isfinite(m_GroupRatio))
{
return SerializationResult::Failure;
}
std::array<char, 8> end = {};
mpt::IO::Read(inStrm, end);
if(std::memcmp(end.data(), "CTRTI_E.", 8))
{
return SerializationResult::Failure;
}
// reject corrupt tunings
if(m_RatioTable.size() > static_cast<std::size_t>(NOTEINDEXTYPE_MAX))
{
return SerializationResult::Failure;
}
if((m_GroupSize <= 0 || m_GroupRatio <= 0) && m_TuningType != Type::GENERAL)
{
return SerializationResult::Failure;
}
if(m_TuningType == Type::GROUPGEOMETRIC || m_TuningType == Type::GEOMETRIC)
{
if(m_RatioTable.size() < static_cast<std::size_t>(m_GroupSize))
{
return SerializationResult::Failure;
}
}
// convert old finestepcount
if(m_FineStepCount > 0)
{
m_FineStepCount -= 1;
}
m_FineStepCount = std::clamp(mpt::saturate_cast<STEPINDEXTYPE>(m_FineStepCount), STEPINDEXTYPE(0), FINESTEPCOUNT_MAX);
UpdateFineStepTable();
if(m_TuningType == Type::GEOMETRIC)
{
// Convert old geometric to new groupgeometric because old geometric tunings
// can have ratio(0) != 1.0, which would get lost when saving nowadays.
if(mpt::saturate_cast<NOTEINDEXTYPE>(m_RatioTable.size()) >= m_GroupSize - m_NoteMin)
{
std::vector<RATIOTYPE> ratios;
for(NOTEINDEXTYPE n = 0; n < m_GroupSize; ++n)
{
ratios.push_back(m_RatioTable[n - m_NoteMin]);
}
CreateGroupGeometric(ratios, m_GroupRatio, GetNoteRange(), 0);
}
}
return SerializationResult::Success;
}
Tuning::SerializationResult CTuning::Serialize(std::ostream& outStrm) const
{
// Note: OpenMPT since at least r323 writes version number (4<<24)+4 while it
// reads version number (5<<24)+4.
// We keep this behaviour.
srlztn::SsbWrite ssb(outStrm);
ssb.BeginWrite("CTB244RTI", (4 << 24) + 4); // version
ssb.WriteItem(int8(1), "UTF8");
if (m_TuningName.length() > 0)
ssb.WriteItem(m_TuningName, "0", WriteStr);
uint16 dummyEditMask = 0xffff;
ssb.WriteItem(dummyEditMask, "1");
ssb.WriteItem(mpt::to_underlying(m_TuningType), "2");
if (m_NoteNameMap.size() > 0)
ssb.WriteItem(m_NoteNameMap, "3", WriteNoteMap);
if (GetFineStepCount() > 0)
ssb.WriteItem(m_FineStepCount, "4");
const Tuning::Type tt = GetType();
if (GetGroupRatio() > 0)
ssb.WriteItem(m_GroupRatio, "RTI3");
if (tt == Type::GROUPGEOMETRIC)
ssb.WriteItem(m_RatioTable, "RTI0", RatioWriter(GetGroupSize()));
if (tt == Type::GENERAL)
ssb.WriteItem(m_RatioTable, "RTI0", RatioWriter());
if (tt == Type::GEOMETRIC)
ssb.WriteItem(m_GroupSize, "RTI2");
if(tt == Type::GEOMETRIC || tt == Type::GROUPGEOMETRIC)
{ //For Groupgeometric this data is the number of ratios in ratiotable.
UNOTEINDEXTYPE ratiotableSize = static_cast<UNOTEINDEXTYPE>(m_RatioTable.size());
ssb.WriteItem(ratiotableSize, "RTI4");
}
// m_NoteMin
ssb.WriteItem(m_NoteMin, "RTI1");
ssb.FinishWrite();
return ssb.HasFailed() ? Tuning::SerializationResult::Failure : Tuning::SerializationResult::Success;
}
#ifdef MODPLUG_TRACKER
bool CTuning::WriteSCL(std::ostream &f, const mpt::PathString &filename) const
{
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT("! {}")(mpt::ToCharset(mpt::Charset::ISO8859_1, (filename.GetFilenameBase() + filename.GetFilenameExtension()).ToUnicode())));
mpt::IO::WriteTextCRLF(f, "!");
std::string name = mpt::ToCharset(mpt::Charset::ISO8859_1, GetName());
for(auto & c : name) { if(static_cast<uint8>(c) < 32) c = ' '; } // remove control characters
if(name.length() >= 1 && name[0] == '!') name[0] = '?'; // do not confuse description with comment
mpt::IO::WriteTextCRLF(f, name);
if(GetType() == Type::GEOMETRIC)
{
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {}")(m_GroupSize));
mpt::IO::WriteTextCRLF(f, "!");
for(NOTEINDEXTYPE n = 0; n < m_GroupSize; ++n)
{
double ratio = std::pow(static_cast<double>(m_GroupRatio), static_cast<double>(n + 1) / static_cast<double>(m_GroupSize));
double cents = std::log2(ratio) * 1200.0;
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {} ! {}")(
mpt::afmt::fix(cents),
mpt::ToCharset(mpt::Charset::ISO8859_1, GetNoteName((n + 1) % m_GroupSize, false))
));
}
} else if(GetType() == Type::GROUPGEOMETRIC)
{
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {}")(m_GroupSize));
mpt::IO::WriteTextCRLF(f, "!");
for(NOTEINDEXTYPE n = 0; n < m_GroupSize; ++n)
{
bool last = (n == (m_GroupSize - 1));
double baseratio = static_cast<double>(GetRatio(0));
double ratio = static_cast<double>(last ? m_GroupRatio : GetRatio(n + 1)) / baseratio;
double cents = std::log2(ratio) * 1200.0;
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {} ! {}")(
mpt::afmt::fix(cents),
mpt::ToCharset(mpt::Charset::ISO8859_1, GetNoteName((n + 1) % m_GroupSize, false))
));
}
} else if(GetType() == Type::GENERAL)
{
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {}")(m_RatioTable.size() + 1));
mpt::IO::WriteTextCRLF(f, "!");
double baseratio = 1.0;
for(NOTEINDEXTYPE n = 0; n < mpt::saturate_cast<NOTEINDEXTYPE>(m_RatioTable.size()); ++n)
{
baseratio = std::min(baseratio, static_cast<double>(m_RatioTable[n]));
}
for(NOTEINDEXTYPE n = 0; n < mpt::saturate_cast<NOTEINDEXTYPE>(m_RatioTable.size()); ++n)
{
double ratio = static_cast<double>(m_RatioTable[n]) / baseratio;
double cents = std::log2(ratio) * 1200.0;
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {} ! {}")(
mpt::afmt::fix(cents),
mpt::ToCharset(mpt::Charset::ISO8859_1, GetNoteName(n + m_NoteMin, false))
));
}
mpt::IO::WriteTextCRLF(f, MPT_AFORMAT(" {} ! {}")(
mpt::afmt::val(1),
std::string()
));
} else
{
return false;
}
return true;
}
#endif
namespace CTuningS11n
{
void RatioWriter::operator()(std::ostream& oStrm, const std::vector<float>& v)
{
const std::size_t nWriteCount = std::min(v.size(), static_cast<std::size_t>(m_nWriteCount));
mpt::IO::WriteAdaptiveInt64LE(oStrm, nWriteCount);
for(size_t i = 0; i < nWriteCount; i++)
mpt::IO::Write(oStrm, IEEE754binary32LE(v[i]));
}
void ReadNoteMap(std::istream &iStrm, std::map<NOTEINDEXTYPE, mpt::ustring> &m, const std::size_t dummy, mpt::Charset charset)
{
MPT_UNREFERENCED_PARAMETER(dummy);
uint64 val;
mpt::IO::ReadAdaptiveInt64LE(iStrm, val);
LimitMax(val, 256u); // Read 256 at max.
for(size_t i = 0; i < val; i++)
{
int16 key;
mpt::IO::ReadIntLE<int16>(iStrm, key);
std::string str;
mpt::IO::ReadSizedStringLE<uint8>(iStrm, str);
m[key] = mpt::ToUnicode(charset, str);
}
}
void ReadRatioTable(std::istream& iStrm, std::vector<RATIOTYPE>& v, const size_t)
{
uint64 val;
mpt::IO::ReadAdaptiveInt64LE(iStrm, val);
v.resize(std::min(mpt::saturate_cast<std::size_t>(val), std::size_t(256))); // Read 256 vals at max.
for(size_t i = 0; i < v.size(); i++)
{
IEEE754binary32LE tmp(0.0f);
mpt::IO::Read(iStrm, tmp);
v[i] = tmp;
}
}
void ReadStr(std::istream &iStrm, mpt::ustring &ustr, const std::size_t dummy, mpt::Charset charset)
{
MPT_UNREFERENCED_PARAMETER(dummy);
std::string str;
uint64 val;
mpt::IO::ReadAdaptiveInt64LE(iStrm, val);
size_t nSize = (val > 255) ? 255 : static_cast<size_t>(val); // Read 255 characters at max.
str.clear();
str.resize(nSize);
for(size_t i = 0; i < nSize; i++)
mpt::IO::ReadIntLE(iStrm, str[i]);
if(str.find_first_of('\0') != std::string::npos)
{ // trim \0 at the end
str.resize(str.find_first_of('\0'));
}
ustr = mpt::ToUnicode(charset, str);
}
void WriteNoteMap(std::ostream &oStrm, const std::map<NOTEINDEXTYPE, mpt::ustring> &m)
{
mpt::IO::WriteAdaptiveInt64LE(oStrm, m.size());
for(auto &mi : m)
{
mpt::IO::WriteIntLE<int16>(oStrm, mi.first);
mpt::IO::WriteSizedStringLE<uint8>(oStrm, mpt::ToCharset(mpt::Charset::UTF8, mi.second));
}
}
void WriteStr(std::ostream &oStrm, const mpt::ustring &ustr)
{
std::string str = mpt::ToCharset(mpt::Charset::UTF8, ustr);
mpt::IO::WriteAdaptiveInt64LE(oStrm, str.size());
oStrm.write(str.c_str(), str.size());
}
} // namespace CTuningS11n.
} // namespace Tuning
OPENMPT_NAMESPACE_END
|