diff --git a/include/modules/dmf.h b/include/modules/dmf.h index 7a7b055..7b88e37 100644 --- a/include/modules/dmf.h +++ b/include/modules/dmf.h @@ -234,6 +234,7 @@ struct PCMSample uint32_t size; std::string name; uint8_t rate, pitch, amp, bits; + uint32_t cut_start, cut_end; uint16_t* data; }; diff --git a/src/modules/dmf.cpp b/src/modules/dmf.cpp index d81fd30..3964dcf 100644 --- a/src/modules/dmf.cpp +++ b/src/modules/dmf.cpp @@ -33,7 +33,7 @@ using namespace d2m; static constexpr uint8_t kDMFFileVersionMin = 17; // DMF files as old as version 17 (0x11) are supported -static constexpr uint8_t kDMFFileVersionMax = 26; // DMF files as new as version 26 (0x1a) are supported +static constexpr uint8_t kDMFFileVersionMax = 27; // DMF files as new as version 27 (0x1b) are supported // DMF format magic numbers //static constexpr int kDMFNoInstrument = -1; @@ -931,10 +931,37 @@ auto DMF::Importer::LoadPCMSample() -> dmf::PCMSample sample.bits = fin_.ReadInt(); } - sample.data = new uint16_t[sample.size]; - for (uint32_t i = 0; i < sample.size; i++) + if (dmf_format_version >= 27) // DMF version 27 (0x1b) and newer { - sample.data[i] = fin_.ReadInt(); + sample.cut_start = fin_.ReadInt(); + sample.cut_end = fin_.ReadInt(); + if (sample.cut_start < 0 || sample.cut_start > sample.size) + { + throw ModuleException(ModuleException::Category::kImport, DMF::ImportError::kUnspecifiedError, "Sample cut start is out of range"); + } + if (sample.cut_end < 0 || sample.cut_end > sample.size) + { + throw ModuleException(ModuleException::Category::kImport, DMF::ImportError::kUnspecifiedError, "Sample cut end is out of range"); + } + if (sample.cut_end < sample.cut_start) + { + throw ModuleException(ModuleException::Category::kImport, DMF::ImportError::kUnspecifiedError, "Sample cut end is before sample cut start"); + } + } + else + { + sample.cut_start = 0; + sample.cut_end = sample.size; + } + + sample.data = nullptr; + if (sample.size > 0) + { + sample.data = new uint16_t[sample.size]; + for (uint32_t i = 0; i < sample.size; i++) + { + sample.data[i] = fin_.ReadInt(); + } } return sample;