ARAContentValidator.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //------------------------------------------------------------------------------
  2. //! \file ARAContentValidator.h
  3. //! utility classes vor validating content readers
  4. //! \project ARA SDK Library
  5. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  6. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  7. //! you may not use this file except in compliance with the License.
  8. //! You may obtain a copy of the License at
  9. //!
  10. //! http://www.apache.org/licenses/LICENSE-2.0
  11. //!
  12. //! Unless required by applicable law or agreed to in writing, software
  13. //! distributed under the License is distributed on an "AS IS" BASIS,
  14. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. //! See the License for the specific language governing permissions and
  16. //! limitations under the License.
  17. //------------------------------------------------------------------------------
  18. #ifndef ARAContentValidator_h
  19. #define ARAContentValidator_h
  20. #include "ARA_Library/Debug/ARADebug.h"
  21. #if ARA_VALIDATE_API_CALLS
  22. #include "ARA_Library/Dispatch/ARAContentReader.h"
  23. namespace ARA {
  24. /*******************************************************************************/
  25. // ContentReaderValidatorImplementation
  26. // Internal helper template class for validating content reader implementations,
  27. // to be used only by ContentReaderValidator.
  28. /*******************************************************************************/
  29. template <ARAContentType contentType>
  30. struct ContentReaderValidatorImplementation;
  31. template <>
  32. struct ContentReaderValidatorImplementation<kARAContentTypeNotes>
  33. {
  34. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount >= 0); }
  35. static inline void validateEvent (const ARAContentNote* event)
  36. {
  37. if ((event->frequency == kARAInvalidFrequency) || (event->pitchNumber == kARAInvalidPitchNumber))
  38. ARA_VALIDATE_API_CONDITION ((event->frequency == kARAInvalidFrequency) && (event->pitchNumber == kARAInvalidPitchNumber));
  39. else
  40. ARA_VALIDATE_API_CONDITION ((event->frequency != kARAInvalidFrequency) && (event->pitchNumber != kARAInvalidPitchNumber));
  41. ARA_VALIDATE_API_CONDITION (event->volume >= 0.0f);
  42. ARA_VALIDATE_API_CONDITION (event->attackDuration >= 0.0);
  43. ARA_VALIDATE_API_CONDITION (event->noteDuration >= 0.0);
  44. ARA_VALIDATE_API_CONDITION (event->signalDuration >= 0.0);
  45. }
  46. static inline void validateEventSequence (const ARAContentNote* event, const ARAContentNote* prevEvent)
  47. {
  48. ARA_VALIDATE_API_CONDITION (prevEvent->startPosition <= event->startPosition);
  49. }
  50. };
  51. template <>
  52. struct ContentReaderValidatorImplementation<kARAContentTypeTempoEntries>
  53. {
  54. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount > 1); }
  55. static inline void validateEvent (const ARAContentTempoEntry* /*event*/) {}
  56. static inline void validateEventSequence (const ARAContentTempoEntry* event, const ARAContentTempoEntry* prevEvent)
  57. {
  58. ARA_VALIDATE_API_CONDITION ((prevEvent->timePosition < event->timePosition) && (prevEvent->quarterPosition < event->quarterPosition));
  59. }
  60. };
  61. template <>
  62. struct ContentReaderValidatorImplementation<kARAContentTypeBarSignatures>
  63. {
  64. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount > 0); }
  65. static inline void validateEvent (const ARAContentBarSignature* event)
  66. {
  67. ARA_VALIDATE_API_CONDITION (event->numerator > 0);
  68. ARA_VALIDATE_API_CONDITION (event->denominator > 0);
  69. }
  70. static inline void validateEventSequence (const ARAContentBarSignature* event, const ARAContentBarSignature* prevEvent)
  71. {
  72. ARA_VALIDATE_API_CONDITION (prevEvent->position < event->position);
  73. const auto barLength { static_cast<double> (prevEvent->numerator) / static_cast<double> (prevEvent->denominator) };
  74. const auto signatureDuration { event->position - prevEvent->position };
  75. constexpr auto roundingWindow { 1.0 / 32.0 }; // a 1/128th note in quarter units
  76. ARA_VALIDATE_API_CONDITION ((std::fmod (signatureDuration, barLength) < roundingWindow) ||
  77. (std::fmod (signatureDuration, barLength) > barLength - roundingWindow));
  78. }
  79. };
  80. template <>
  81. struct ContentReaderValidatorImplementation<kARAContentTypeStaticTuning>
  82. {
  83. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount == 1); }
  84. static inline void validateEvent (const ARAContentTuning* event)
  85. {
  86. ARA_VALIDATE_API_CONDITION (event->concertPitchFrequency > 0.0);
  87. }
  88. static inline void validateEventSequence (const ARAContentTuning* /*event*/, const ARAContentTuning* /*prevEvent*/) {}
  89. };
  90. template <>
  91. struct ContentReaderValidatorImplementation<kARAContentTypeKeySignatures>
  92. {
  93. // this may fail in older versions of Studio One if no key has been set for the song - fixed in version 5.2.1
  94. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount > 0); }
  95. static inline void validateEvent (const ARAContentKeySignature* /*event*/) {}
  96. static inline void validateEventSequence (const ARAContentKeySignature* event, const ARAContentKeySignature* prevEvent)
  97. {
  98. ARA_VALIDATE_API_CONDITION (prevEvent->position < event->position);
  99. }
  100. };
  101. template <>
  102. struct ContentReaderValidatorImplementation<kARAContentTypeSheetChords>
  103. {
  104. static inline void validateEventCount (ARAInt32 eventCount) { ARA_VALIDATE_API_CONDITION (eventCount >= 0); }
  105. static inline void validateEvent (const ARAContentChord* /*event*/) {}
  106. static inline void validateEventSequence (const ARAContentChord* event, const ARAContentChord* prevEvent)
  107. {
  108. ARA_VALIDATE_API_CONDITION (prevEvent->position < event->position);
  109. }
  110. };
  111. /*******************************************************************************/
  112. // ContentReaderValidator
  113. // Template class for validating content reader implementations.
  114. // Not called directly, but used as argument for the ContentReader template.
  115. /*******************************************************************************/
  116. template <ARAContentType contentType, typename ControllerType, typename ContentReaderRefType>
  117. class ContentValidator
  118. {
  119. public:
  120. using DataType = typename ContentTypeMapper<contentType>::DataType;
  121. inline void validateEventCount (ARAInt32 eventCount)
  122. {
  123. ContentReaderValidatorImplementation<contentType>::validateEventCount (eventCount);
  124. }
  125. inline void prepareValidateEvent (ControllerType* controller, ContentReaderRefType ref, ARAInt32 eventIndex)
  126. {
  127. if ((eventIndex > 0) && (_cachedEventIndex != eventIndex - 1))
  128. _cachedData = *static_cast<const DataType*> (controller->getContentReaderDataForEvent (ref, eventIndex - 1));
  129. }
  130. inline void validateEvent (const DataType* dataPtr, ARAInt32 eventIndex)
  131. {
  132. if (ARA_VALIDATE_API_CONDITION (dataPtr != nullptr))
  133. {
  134. ContentReaderValidatorImplementation<contentType>::validateEvent (dataPtr);
  135. if (eventIndex > 0)
  136. ContentReaderValidatorImplementation<contentType>::validateEventSequence (dataPtr, &_cachedData);
  137. _cachedData = *dataPtr;
  138. _cachedEventIndex = eventIndex;
  139. }
  140. }
  141. private:
  142. ARAInt32 _cachedEventIndex { -1 };
  143. DataType _cachedData {};
  144. };
  145. } // namespace ARA
  146. #endif // ARA_VALIDATE_API_CALLS
  147. #endif // ARAContentValidator_h