Imperfector.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "Imperfector.h"
  2. #include "StdInclude.h"
  3. #include "Constants.h"
  4. #include <cmath>
  5. #include <vector>
  6. namespace {
  7. void SendNoteOffEvent(std::uint8_t iNoteNumber, std::uint16_t iFrameIndex)
  8. {
  9. JBOX_ASSERT(iNoteNumber < 128);
  10. TJBox_NoteEvent event;
  11. event.fNoteNumber = iNoteNumber;
  12. event.fVelocity = 0;
  13. event.fAtFrameIndex = iFrameIndex;
  14. JBox_OutputNoteEvent(event);
  15. }
  16. /// od tego wychodzić - debbug w reaconie dodaje nuty jeden nuty drugi często.
  17. //Tryb synchronizacji tempo-
  18. //Drugi tryb bez synchronizacji
  19. void SendNoteOnEvent(std::uint8_t iNoteNumber, std::uint8_t iVelocity, std::uint16_t iFrameIndex)
  20. {
  21. JBOX_ASSERT(iNoteNumber < 128);
  22. TJBox_NoteEvent event;
  23. event.fNoteNumber = iNoteNumber;
  24. event.fVelocity = iVelocity;
  25. event.fAtFrameIndex = iFrameIndex;
  26. JBox_OutputNoteEvent(event);
  27. }
  28. std::uint16_t PPQToFrameIndex(TJBox_Float64 iPPQ, TJBox_Float64 iTempo, TJBox_Float64 iSampleRate)
  29. {
  30. std::uint8_t frameIndex = 0;
  31. const TJBox_Float64 beatsPerSecond = iTempo / 60;
  32. const TJBox_Float64 numPPQsPerSecond = beatsPerSecond * kPPQResolution;
  33. const TJBox_Float64 seconds = iPPQ / numPPQsPerSecond;
  34. const TJBox_Float64 frames = seconds * iSampleRate;
  35. frameIndex = static_cast<std::uint16_t>(std::max<TJBox_Float64>(0, std::min<TJBox_Float64>(frames, 63)));
  36. JBOX_ASSERT(frameIndex >= 0 && frameIndex < kBatchSize);
  37. return frameIndex;
  38. }
  39. // skumać
  40. std::int64_t FindNextBeat(std::int64_t iBeatLength, std::int64_t iMaxBeatPosition)
  41. {
  42. JBOX_ASSERT(iBeatLength > 0);
  43. const std::int64_t absolutePPQ = static_cast<std::int64_t>((iMaxBeatPosition) / iBeatLength) * iBeatLength;
  44. return absolutePPQ;
  45. }
  46. std::uint8_t ConvertCVToNote(TJBox_Value iCVValue)
  47. {
  48. const TJBox_Float64 value = JBox_GetNumber(iCVValue) * 127.f + 0.1f;
  49. if (value < 0) {
  50. return 0;
  51. }
  52. else if (value > 127) {
  53. return 127;
  54. }
  55. return static_cast<std::uint8_t>(value);
  56. }
  57. bool IsInRange(std::int64_t iPosition, const TPPQRange& iRange) {
  58. if (iPosition >= iRange.first && iPosition < iRange.second) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool IsEmptyRange(const TPPQRange& iRange) {
  64. return iRange.first == iRange.second;
  65. }
  66. #if DEBUG
  67. bool IsValidRange(const TPPQRange& iRange) {
  68. return iRange.first <= iRange.second;
  69. }
  70. #endif // DEBUG
  71. }
  72. CImperfector::CImperfector(TJBox_Float64 iSampleRate) :
  73. fIsPlaying(false),
  74. fSampleRate(iSampleRate),
  75. fLastStringizeAmountNote(kInvalidNoteNumber),
  76. fLastStringizeSpeedNote(kInvalidNoteNumber),
  77. fLastPosition(0)
  78. {
  79. fCustomPropertiesRef = JBox_GetMotherboardObjectRef("/custom_properties");
  80. fTransportRef = JBox_GetMotherboardObjectRef("/transport");
  81. fNoteStates = JBox_GetMotherboardObjectRef("/note_states");
  82. fEnvironmenRef = JBox_GetMotherboardObjectRef("/environment");
  83. // CV inputs
  84. /*TJBox_ObjectRef numeratorNoteCVRef = JBox_GetMotherboardObjectRef("/cv_inputs/numerator_note_cv");
  85. fNumeratorNoteCVInputRef = JBox_MakePropertyRef(numeratorNoteCVRef, "value");
  86. fNumeratorNoteCVConnectedRef = JBox_MakePropertyRef(numeratorNoteCVRef, "connected");*/
  87. TJBox_ObjectRef stringizeAmountNoteCVRef = JBox_GetMotherboardObjectRef("/cv_inputs/stringizeAmount_note_cv");
  88. fstringizeAmountNoteCVInputRef = JBox_MakePropertyRef(stringizeAmountNoteCVRef, "value");
  89. fstringizeAmountNoteCVConnectedRef = JBox_MakePropertyRef(stringizeAmountNoteCVRef, "connected");
  90. TJBox_ObjectRef stringizeSpeedNoteCVRef = JBox_GetMotherboardObjectRef("/cv_inputs/stringizeSpeed_note_cv");
  91. fstringizeSpeedNoteCVInputRef = JBox_MakePropertyRef(stringizeSpeedNoteCVRef, "value");
  92. fstringizeSpeedNoteCVConnectedRef = JBox_MakePropertyRef(stringizeSpeedNoteCVRef, "connected");
  93. /*TJBox_ObjectRef denominatorNoteCVRef = JBox_GetMotherboardObjectRef("/cv_inputs/denominator_note_cv");
  94. fDenominatorNoteCVInputRef = JBox_MakePropertyRef(denominatorNoteCVRef, "value");
  95. fDenominatorNoteCVConnectedRef = JBox_MakePropertyRef(denominatorNoteCVRef, "connected");*/
  96. }
  97. void CImperfector::HandleDiffs(const TJBox_PropertyDiff iPropertyDiffs[], TJBox_UInt32 iDiffCount)
  98. {
  99. for (TJBox_UInt32 i = 0; i < iDiffCount; ++i) {
  100. const TJBox_PropertyDiff& diff = iPropertyDiffs[i];
  101. if (diff.fPropertyRef.fObject == fTransportRef && diff.fPropertyTag == kJBox_TransportPlaying)
  102. {
  103. fIsPlaying = JBox_GetBoolean(diff.fCurrentValue);
  104. }
  105. else if (diff.fPropertyRef.fObject == fTransportRef && diff.fPropertyTag == kJBox_TransportLoopEnabled)
  106. {
  107. fIsLooping = JBox_GetBoolean(diff.fCurrentValue);
  108. }
  109. else if (diff.fPropertyRef.fObject == fTransportRef && diff.fPropertyTag == kJBox_TransportTempo)
  110. {
  111. fTempo = JBox_GetNumber(diff.fCurrentValue);
  112. }
  113. else if (diff.fPropertyRef.fObject == fEnvironmenRef && diff.fPropertyTag == kJBox_EnvironmentPlayerBypassed)
  114. {
  115. fIsBypassedByHost = JBox_GetBoolean(diff.fCurrentValue);
  116. }
  117. else if (diff.fPropertyRef.fObject == fCustomPropertiesRef && diff.fPropertyTag == kOnOffTag)
  118. {
  119. fIsEnabled = JBox_GetBoolean(diff.fCurrentValue);
  120. }
  121. }
  122. }
  123. void CImperfector::ForwardNoteEvents(const TJBox_PropertyDiff iPropertyDiffs[], TJBox_UInt32 iDiffCount)
  124. {
  125. for (TJBox_UInt32 i = 0; i < iDiffCount; ++i) {
  126. const TJBox_PropertyDiff& propertyDiff = iPropertyDiffs[i];
  127. if (propertyDiff.fPropertyRef.fObject == fNoteStates) {
  128. const TJBox_NoteEvent& noteEvent = JBox_AsNoteEvent(propertyDiff);
  129. JBox_OutputNoteEvent(noteEvent);
  130. }
  131. }
  132. }
  133. // Zobaczy jak wyglada event od tego każde nacisniecie korzysta z tej funkcji- player po nacisnieciu opoznienie
  134. std::uint8_t CImperfector::NoteFromCV(std::uint16_t iDefaultNote,TJBox_PropertyRef iNoteCVConnectedRef, TJBox_PropertyRef iNoteCVInputRef)
  135. {
  136. if (JBox_GetBoolean(JBox_LoadMOMProperty(iNoteCVConnectedRef))) {
  137. const TJBox_Value noteCV = JBox_LoadMOMProperty(iNoteCVInputRef);
  138. std::uint8_t newNote = ConvertCVToNote(noteCV);
  139. return newNote;
  140. }
  141. return iDefaultNote;
  142. }
  143. TJBox_Float64 CImperfector::ComputeBatchLengthPPQ() const {
  144. const TJBox_Float64 batchLengthPPQ = (kBatchSize / fSampleRate) * ((fTempo / 60) * kPPQResolution);
  145. return batchLengthPPQ;
  146. }
  147. // Returns two ranges to support looping
  148. std::pair<TPPQRange, TPPQRange> CImperfector::ComputePPQRangesForCurrentBatch(TJBox_Float64 iBatchLengthPPQ) const
  149. {
  150. TPPQRange firstRange;
  151. TPPQRange secondRange; // Only used if the main seequencer has looped this batch
  152. TJBox_Float64 batchBeginPPQ = JBox_LoadMOMPropertyAsNumber(fTransportRef, kJBox_TransportPlayPos);
  153. const TJBox_Float64 batchEndPPQ = batchBeginPPQ + iBatchLengthPPQ;
  154. firstRange.first = static_cast<std::int64_t>(batchBeginPPQ);
  155. firstRange.second = static_cast<std::int64_t>(batchEndPPQ);
  156. const TJBox_Float64 absDiff = std::abs(batchBeginPPQ - fLastPosition);
  157. if (absDiff <= (iBatchLengthPPQ * 4)) {
  158. // Sequencer pos has (probably) not jumped,
  159. // adjust start of current batch to the end of previous batch
  160. firstRange.first = fLastPosition;
  161. }
  162. // Handle main sequencer looping
  163. if (fIsLooping) {
  164. // Did the sequencer loop this batch?
  165. const std::int64_t loopEndPPQ = static_cast<std::int64_t>(JBox_LoadMOMPropertyAsNumber(fTransportRef, kJBox_TransportLoopEndPos));
  166. if (IsInRange(loopEndPPQ, firstRange)) {
  167. const std::int64_t loopBeginPPQ = static_cast<std::int64_t>(JBox_LoadMOMPropertyAsNumber(fTransportRef, kJBox_TransportLoopStartPos));
  168. firstRange.second = loopEndPPQ;
  169. secondRange.first = loopBeginPPQ;
  170. const TJBox_Float64 secondRangeLength = iBatchLengthPPQ - (loopEndPPQ - batchBeginPPQ);
  171. JBOX_ASSERT(secondRangeLength >= 0);
  172. secondRange.second = loopBeginPPQ + secondRangeLength;
  173. if (IsEmptyRange(firstRange)) {
  174. // Edge case, the sequencer looped at the end of the batch,
  175. // just swap so that the first range is never empty
  176. std::swap(firstRange, secondRange);
  177. }
  178. }
  179. }
  180. JBOX_ASSERT(IsValidRange(firstRange));
  181. JBOX_ASSERT(!IsEmptyRange(firstRange) || (IsEmptyRange(firstRange) && IsEmptyRange(secondRange)));
  182. JBOX_ASSERT(IsValidRange(secondRange));
  183. return std::make_pair(firstRange, secondRange);
  184. }
  185. void CImperfector::PlayRange(const TPPQRange& iRange) {
  186. // Output the numerator beat if it happens in the current batch
  187. {
  188. const TJBox_Float64 stringizeAmount = JBox_LoadMOMPropertyAsNumber(fCustomPropertiesRef, kStringizeAmountTag) + 1;
  189. const TJBox_Float64 stringizeSpeed = JBox_LoadMOMPropertyAsNumber(fCustomPropertiesRef, kStringizeSpeedTag) + 1;
  190. const std::int64_t beatLength = static_cast<std::int64_t>(kPPQResolution * (stringizeSpeed / stringizeAmount));
  191. const std::int64_t nextBeat = FindNextBeat(beatLength, iRange.second);
  192. if (IsInRange(nextBeat, iRange)) {
  193. const std::int64_t offsetPPQ = nextBeat - iRange.first;
  194. const std::uint16_t frameIndex = PPQToFrameIndex(offsetPPQ, fTempo, fSampleRate);
  195. const std::uint8_t note = NoteFromCV(kDefaultNumeratorNote, fstringizeAmountNoteCVConnectedRef, fstringizeAmountNoteCVInputRef);
  196. // Send a note off for the previously played note.
  197. // This is a naive way of handling note off events to keep this example minimal.
  198. if (fLastStringizeAmountNote != kInvalidNoteNumber) {
  199. SendNoteOffEvent(fLastStringizeAmountNote, 0);
  200. }
  201. fLastStringizeAmountNote = note;
  202. SendNoteOnEvent(note, kNoteVelocity, frameIndex);
  203. }
  204. }
  205. // Output the denominator beat if it happens in the current batch
  206. {
  207. const std::int64_t beatLength = kPPQResolution; // Denominator is always on quarter note beats
  208. const std::int64_t nextBeat = FindNextBeat(beatLength, iRange.second);
  209. if (IsInRange(nextBeat, iRange)) {
  210. const std::int64_t offsetPPQ = nextBeat - iRange.first;
  211. const std::uint16_t frameIndex = PPQToFrameIndex(offsetPPQ, fTempo, fSampleRate);
  212. const std::uint8_t note = NoteFromCV(kDefaultDenominatorNote, fstringizeSpeedNoteCVConnectedRef, fstringizeSpeedNoteCVInputRef);
  213. if (fLastStringizeSpeedNote != kInvalidNoteNumber) {
  214. SendNoteOffEvent(fLastStringizeSpeedNote, 0);
  215. }
  216. fLastStringizeSpeedNote = note;
  217. SendNoteOnEvent(note, kNoteVelocity, frameIndex);
  218. }
  219. }
  220. fLastPosition = iRange.second;
  221. }
  222. void CImperfector::StringizeAmount(TJBox_Float64 value, char* output)
  223. {
  224. {
  225. const TJBox_Float64 stringizeAmount = JBox_LoadMOMPropertyAsNumber(fCustomPropertiesRef, kStringizeAmountTag) + 1;
  226. }
  227. }
  228. void CImperfector::RenderBatch(const TJBox_PropertyDiff iPropertyDiffs[], TJBox_UInt32 iDiffCount)
  229. {
  230. HandleDiffs(iPropertyDiffs, iDiffCount);
  231. if (fIsBypassedByHost) {
  232. // We don't have to do anything if the player is bypassed by the host
  233. return;
  234. }
  235. if (!fIsEnabled || !fIsPlaying) {
  236. if (fLastStringizeAmountNote != kInvalidNoteNumber) {
  237. SendNoteOffEvent(fLastStringizeAmountNote, 0);
  238. fLastStringizeAmountNote = kInvalidNoteNumber;
  239. }
  240. if (fLastStringizeSpeedNote != kInvalidNoteNumber) {
  241. SendNoteOffEvent(fLastStringizeSpeedNote, 0);
  242. fLastStringizeSpeedNote = kInvalidNoteNumber;
  243. }
  244. // Forward all note events if the player is turned off
  245. // or if sequencer is not playing
  246. ForwardNoteEvents(iPropertyDiffs, iDiffCount);
  247. return;
  248. }
  249. // This player only adds note events, so we simply
  250. // forward all incoming events
  251. ForwardNoteEvents(iPropertyDiffs, iDiffCount);
  252. const TJBox_Float64 batchLength = ComputeBatchLengthPPQ();
  253. const std::pair<TPPQRange, TPPQRange> ranges = ComputePPQRangesForCurrentBatch(batchLength);
  254. PlayRange(ranges.first);
  255. if (!IsEmptyRange(ranges.second)) {
  256. PlayRange(ranges.second);
  257. }
  258. }