ARAContentLogger.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //------------------------------------------------------------------------------
  2. //! \file ARAContentLogger.h
  3. //! class to conveniently log (and validate) ARA content
  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 ARAContentLogger_h
  19. #define ARAContentLogger_h
  20. #include "ARA_Library/Debug/ARADebug.h"
  21. #if ARA_VALIDATE_API_CALLS
  22. #include "ARA_Library/Debug/ARAContentValidator.h"
  23. #endif
  24. #if ARA_ENABLE_DEBUG_OUTPUT
  25. #include "ARA_Library/Dispatch/ARAContentReader.h"
  26. #include "ARA_Library/Utilities/ARAPitchInterpretation.h"
  27. #include <array>
  28. namespace ARA {
  29. /*******************************************************************************/
  30. // ContentLogger
  31. // Debugging tool to print all content reported by the API partner.
  32. /*******************************************************************************/
  33. struct ContentLogger
  34. {
  35. template <typename ControllerType, typename ModelObjectRefType>
  36. using ContentReaderRef = typename std::result_of<decltype (ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::createContentReader)(ControllerType, ModelObjectRefType, ARAContentType, const ARAContentTimeRange*)>::type;
  37. template <ARAContentType contentType, typename ControllerType, typename ModelObjectRefType>
  38. #if ARA_VALIDATE_API_CALLS
  39. using ContentValidator = ContentValidator<contentType, ControllerType, ContentReaderRef<ControllerType, ModelObjectRefType>>;
  40. #else
  41. using ContentValidator = NoContentValidator<contentType, ControllerType, ContentReaderRef<ControllerType, ModelObjectRefType>>;
  42. #endif
  43. template <ARAContentType contentType, typename ControllerType, typename ModelObjectRefType>
  44. using ContentReader = ContentReader<contentType, ControllerType, ContentReaderRef<ControllerType, ModelObjectRefType>, ContentValidator<contentType, ControllerType, ModelObjectRefType>>;
  45. // array of all content types defined in the api
  46. static inline constexpr std::array<ARAContentType, 6> getAllContentTypes () noexcept
  47. {
  48. return { { kARAContentTypeNotes,
  49. kARAContentTypeTempoEntries, kARAContentTypeBarSignatures,
  50. kARAContentTypeStaticTuning,
  51. kARAContentTypeKeySignatures, kARAContentTypeSheetChords
  52. } };
  53. }
  54. // logging of time ranges
  55. static inline double getStartOfRange (const ARAContentTimeRange* range)
  56. {
  57. return (range) ? range->start : -std::numeric_limits<ARATimePosition>::infinity ();
  58. }
  59. static inline double getEndOfRange (const ARAContentTimeRange* range)
  60. {
  61. return (range) ? range->start + range->duration : std::numeric_limits<ARATimeDuration>::infinity ();
  62. }
  63. // debug text helpers
  64. static inline const char* getEnumNameForContentType (ARAContentType contentType) noexcept
  65. {
  66. switch (contentType)
  67. {
  68. case kARAContentTypeNotes: return ContentTypeMapper<kARAContentTypeNotes>::enumName;
  69. case kARAContentTypeTempoEntries: return ContentTypeMapper<kARAContentTypeTempoEntries>::enumName;
  70. case kARAContentTypeBarSignatures: return ContentTypeMapper<kARAContentTypeBarSignatures>::enumName;
  71. case kARAContentTypeStaticTuning: return ContentTypeMapper<kARAContentTypeStaticTuning>::enumName;
  72. case kARAContentTypeKeySignatures: return ContentTypeMapper<kARAContentTypeKeySignatures>::enumName;
  73. case kARAContentTypeSheetChords: return ContentTypeMapper<kARAContentTypeSheetChords>::enumName;
  74. default: ARA_INTERNAL_ASSERT (false); return "kARAContentType???";
  75. }
  76. }
  77. static inline const char* getTypeNameForContentType (ARAContentType contentType) noexcept
  78. {
  79. switch (contentType)
  80. {
  81. case kARAContentTypeNotes: return ContentTypeMapper<kARAContentTypeNotes>::typeName;
  82. case kARAContentTypeTempoEntries: return ContentTypeMapper<kARAContentTypeTempoEntries>::typeName;
  83. case kARAContentTypeBarSignatures: return ContentTypeMapper<kARAContentTypeBarSignatures>::typeName;
  84. case kARAContentTypeStaticTuning: return ContentTypeMapper<kARAContentTypeStaticTuning>::typeName;
  85. case kARAContentTypeKeySignatures: return ContentTypeMapper<kARAContentTypeKeySignatures>::typeName;
  86. case kARAContentTypeSheetChords: return ContentTypeMapper<kARAContentTypeSheetChords>::typeName;
  87. default: ARA_INTERNAL_ASSERT (false); return "ARAContent???";
  88. }
  89. }
  90. static inline const char* getNameForContentGrade (ARAContentGrade contentGrade) noexcept
  91. {
  92. switch (contentGrade)
  93. {
  94. case kARAContentGradeInitial: return "initial";
  95. case kARAContentGradeDetected: return "detected";
  96. case kARAContentGradeAdjusted: return "adjusted";
  97. case kARAContentGradeApproved: return "approved";
  98. default: ARA_INTERNAL_ASSERT (false); return "kARAContentGrade???";
  99. }
  100. }
  101. // Logging individual content events
  102. // The value of \p idx is used only for decorating the output, e.g. when log() and its variants are iterating over content events.
  103. static inline void logEvent (ARAInt32 idx, const ARAContentNote& noteData)
  104. {
  105. if ((noteData.frequency == kARAInvalidFrequency) || (noteData.pitchNumber == kARAInvalidPitchNumber))
  106. {
  107. ARA_LOG ("%s[%i] no pitch, vol = %.3f, %.3f - %.3f - %.3f - %.3f", getTypeNameForContentType (kARAContentTypeNotes), idx,
  108. noteData.volume,
  109. noteData.startPosition, noteData.startPosition + noteData.attackDuration,
  110. noteData.startPosition + noteData.noteDuration, noteData.startPosition + noteData.signalDuration);
  111. }
  112. else
  113. {
  114. const std::array<const char*, 12> noteNames { "C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B" };
  115. ARA_LOG ("%s[%i] %.2f Hz (%i, %s), vol = %.3f, %.3f - %.3f - %.3f - %.3f", getTypeNameForContentType (kARAContentTypeNotes), idx,
  116. noteData.frequency, noteData.pitchNumber, noteNames[noteData.pitchNumber % 12], noteData.volume,
  117. noteData.startPosition, noteData.startPosition + noteData.attackDuration,
  118. noteData.startPosition + noteData.noteDuration, noteData.startPosition + noteData.signalDuration);
  119. }
  120. }
  121. static inline void logEvent (ARAInt32 idx, const ARAContentTempoEntry& tempoData)
  122. {
  123. ARA_LOG ("%s[%i] sec = %.3f, quarters = %.3f", getTypeNameForContentType (kARAContentTypeTempoEntries), idx, tempoData.timePosition, tempoData.quarterPosition);
  124. }
  125. static inline void logEvent (ARAInt32 idx, const ARAContentBarSignature& signatureData)
  126. {
  127. ARA_LOG ("%s[%i] %i/%i, quarters = %.3f", getTypeNameForContentType (kARAContentTypeBarSignatures), idx, signatureData.numerator, signatureData.denominator, signatureData.position);
  128. }
  129. static inline void logEvent (ARAInt32 idx, const ARAContentTuning& tuning)
  130. {
  131. static const PitchInterpreter interpreter { true };
  132. const auto rootName { interpreter.getNoteNameForCircleOfFifthIndex (tuning.root) };
  133. ARA_LOG ("%s[%i] %.2f, %s {%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f}",
  134. getTypeNameForContentType (kARAContentTypeStaticTuning), idx,
  135. tuning.concertPitchFrequency, rootName.c_str (),
  136. tuning.tunings[0], tuning.tunings[1], tuning.tunings[2], tuning.tunings[3],
  137. tuning.tunings[4], tuning.tunings[5], tuning.tunings[6], tuning.tunings[7],
  138. tuning.tunings[8], tuning.tunings[9], tuning.tunings[10], tuning.tunings[11]);
  139. }
  140. static inline void logEvent (ARAInt32 idx, const ARAContentKeySignature& signatureData)
  141. {
  142. const auto logGivenName { signatureData.name != nullptr };
  143. static const KeySignatureInterpreter interpreter { true };
  144. const auto parsedSignatureName { interpreter.getNameForKeySignature (signatureData) };
  145. const auto logParsedName { !parsedSignatureName.empty () && (!logGivenName || (parsedSignatureName.compare (signatureData.name) != 0)) };
  146. ARA_LOG ("%s[%i] %s%s%s, quarters = %.3f", getTypeNameForContentType (kARAContentTypeKeySignatures), idx,
  147. (logGivenName) ? signatureData.name : (logParsedName) ? parsedSignatureName.c_str () : (interpreter.getRootNoteNameForKeySignature (signatureData) + " ???").c_str (),
  148. (logGivenName && logParsedName) ? " aka " : "", (logGivenName && logParsedName) ? parsedSignatureName.c_str () : "", signatureData.position);
  149. }
  150. static inline void logEvent (ARAInt32 idx, const ARAContentChord& chordData)
  151. {
  152. const auto logGivenName { chordData.name != nullptr };
  153. static const ChordInterpreter interpreter { true };
  154. const auto parsedChordName { interpreter.getNameForChord (chordData) };
  155. const auto logParsedName { !logGivenName || (parsedChordName.compare (chordData.name) != 0) };
  156. ARA_LOG ("%s[%i] %s%s%s, quarters = %.3f", getTypeNameForContentType (kARAContentTypeSheetChords), idx,
  157. (logGivenName) ? chordData.name : parsedChordName.c_str (),
  158. (logGivenName && logParsedName) ? " aka " : "", (logGivenName && logParsedName) ? parsedChordName.c_str () : "", chordData.position);
  159. }
  160. // internal helper for log ()
  161. template <ARAContentType contentType, typename ContentReader, typename std::enable_if<contentType != kARAContentTypeTempoEntries, bool>::type = true>
  162. static inline void logEventIteration (ContentReader& reader, ARAInt32 i)
  163. {
  164. logEvent (i, reader[i]);
  165. }
  166. template <ARAContentType contentType, typename ContentReader, typename std::enable_if<contentType == kARAContentTypeTempoEntries, bool>::type = true>
  167. static inline void logEventIteration (ContentReader& reader, ARAInt32 i)
  168. {
  169. const auto event { reader[i] };
  170. if (i != 0)
  171. {
  172. const auto prevEvent { reader[i - 1] };
  173. const auto tempo { 60.0 * (event.quarterPosition - prevEvent.quarterPosition) / (event.timePosition - prevEvent.timePosition) };
  174. ARA_LOG ("tempo between entries [%i:%i] is %.2f BPM", i - 1, i, tempo);
  175. }
  176. logEvent (i, event);
  177. }
  178. // Logging Model Objects
  179. // Log content associated with the model object referenced by \p modelObjectRef.
  180. // If \p range is not nullptr, the logged content will be limited to events with \p range.
  181. // Main logging method, with \p contentType being a template parameter.
  182. // Returns true if any content of type \p contentType was available for \p modelObjectRef.
  183. template <ARAContentType contentType, typename ControllerType, typename ModelObjectRefType>
  184. static inline bool log (ControllerType& controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range = nullptr, bool logIfNotAvailable = true)
  185. {
  186. if (ContentReader<contentType, ControllerType, ModelObjectRefType> reader { &controller, modelObjectRef, range })
  187. {
  188. ARA_LOG ("%i %s of %s grade available for %s %p", reader.getEventCount (), getEnumNameForContentType (contentType),
  189. getNameForContentGrade (reader.getGrade ()), ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName, modelObjectRef);
  190. for (auto i { 0 }; i < reader.getEventCount (); ++i)
  191. logEventIteration<contentType> (reader, i);
  192. return true;
  193. }
  194. if (logIfNotAvailable)
  195. ARA_LOG ("no %s available for %s %p", getEnumNameForContentType (contentType), ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName, modelObjectRef);
  196. return false;
  197. }
  198. // Main logging methods, with \p contentType being a runtime parameter.
  199. // Returns true if any content of type \p contentType was available for \p modelObjectRef.
  200. template <typename ControllerType, typename ModelObjectRefType>
  201. static inline bool log (ControllerType& controller, ModelObjectRefType modelObjectRef, ARAContentType contentType, const ARAContentTimeRange* range = nullptr, bool logIfNotAvailable = true)
  202. {
  203. switch (contentType)
  204. {
  205. case kARAContentTypeNotes: return log<kARAContentTypeNotes> (controller, modelObjectRef, range, logIfNotAvailable);
  206. case kARAContentTypeTempoEntries: return log<kARAContentTypeTempoEntries> (controller, modelObjectRef, range, logIfNotAvailable);
  207. case kARAContentTypeBarSignatures: return log<kARAContentTypeBarSignatures> (controller, modelObjectRef, range, logIfNotAvailable);
  208. case kARAContentTypeStaticTuning: return log<kARAContentTypeStaticTuning> (controller, modelObjectRef, range, logIfNotAvailable);
  209. case kARAContentTypeKeySignatures: return log<kARAContentTypeKeySignatures> (controller, modelObjectRef, range, logIfNotAvailable);
  210. case kARAContentTypeSheetChords: return log<kARAContentTypeSheetChords> (controller, modelObjectRef, range, logIfNotAvailable);
  211. default: ARA_INTERNAL_ASSERT (false); return false;
  212. }
  213. }
  214. // Variant of log() for logging all ARA content types.
  215. template <typename ControllerType, typename ModelObjectRefType>
  216. static inline void logAllContent (ControllerType& controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range = nullptr) noexcept
  217. {
  218. ARA_LOG ("all content of %s %p in range %.3f to %.3f:", ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName,
  219. modelObjectRef, ContentLogger::getStartOfRange (range), ContentLogger::getEndOfRange (range));
  220. for (const auto& contentType : getAllContentTypes ())
  221. log (controller, modelObjectRef, contentType, range);
  222. }
  223. // Variant of log() for logging content that may have been analyzed by the plug-in.
  224. template <typename ControllerType, typename ModelObjectRefType>
  225. static inline void logAnalyzableContent (ControllerType& controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range) noexcept
  226. {
  227. ARA_LOG ("analyzable content of %s %p in range %.3f to %.3f:", ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName,
  228. modelObjectRef, ContentLogger::getStartOfRange (range), ContentLogger::getEndOfRange (range));
  229. for (auto i { 0 }; i < controller.getFactory ()->analyzeableContentTypesCount; ++i)
  230. log (controller, modelObjectRef, controller.getFactory ()->analyzeableContentTypes[i], range);
  231. }
  232. // Variant of log() for logging available content.
  233. template <typename ControllerType, typename ModelObjectRefType>
  234. static inline void logAvailableContent (ControllerType& controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range = nullptr)
  235. {
  236. ARA_LOG ("available content of %s %p in range %.3f to %.3f:", ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName,
  237. modelObjectRef, ContentLogger::getStartOfRange (range), ContentLogger::getEndOfRange (range));
  238. bool didLogContent { false };
  239. for (const auto contentType : getAllContentTypes ())
  240. didLogContent = log (controller, modelObjectRef, contentType, range, false) || didLogContent;
  241. if (!didLogContent)
  242. ARA_LOG ("no content available for %s %p", ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName, modelObjectRef);
  243. }
  244. // Variant of log() for logging content change notifications.
  245. template <typename ControllerType, typename ModelObjectRefType>
  246. static inline void logUpdatedContent (ControllerType& controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range, ContentUpdateScopes scopeFlags)
  247. {
  248. ARA_LOG ("content of %s %p was updated from %.3f to %.3f, flags 0x%X", ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::modelObjectRefTypeName,
  249. modelObjectRef, ContentLogger::getStartOfRange (range), ContentLogger::getEndOfRange (range), scopeFlags);
  250. if (scopeFlags.affectNotes ())
  251. {
  252. ARA_LOG ("note scope updated, related content is:");
  253. log<kARAContentTypeNotes> (controller, modelObjectRef, range, false);
  254. }
  255. if (scopeFlags.affectTimeline ())
  256. {
  257. ARA_LOG ("timing scope updated, related content is:");
  258. log<kARAContentTypeTempoEntries> (controller, modelObjectRef, range, false);
  259. log<kARAContentTypeBarSignatures> (controller, modelObjectRef, range, false);
  260. }
  261. if (scopeFlags.affectTuning ())
  262. {
  263. ARA_LOG ("pitch scope updated, related content is:");
  264. log<kARAContentTypeStaticTuning> (controller, modelObjectRef, range, false);
  265. }
  266. if (scopeFlags.affectHarmonies ())
  267. {
  268. ARA_LOG ("harmonic scope updated, related content is:");
  269. log<kARAContentTypeKeySignatures> (controller, modelObjectRef, range, false);
  270. log<kARAContentTypeSheetChords> (controller, modelObjectRef, range, false);
  271. }
  272. }
  273. };
  274. #endif // ARA_ENABLE_DEBUG_OUTPUT
  275. } // namespace ARA
  276. #endif // ARAContentLogger_h