You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
5.3KB

  1. #include "MidiEvent.h"
  2. #include "asserts.h"
  3. static void testType()
  4. {
  5. MidiNoteEvent note;
  6. assert(note.type == MidiEvent::Type::Note);
  7. MidiEndEvent end;
  8. assert(end.type == MidiEvent::Type::End);
  9. }
  10. static void testCast()
  11. {
  12. MidiNoteEventPtr note = std::make_shared<MidiNoteEvent>();
  13. MidiEventPtr evn(note);
  14. MidiEndEventPtr end = std::make_shared<MidiEndEvent>();
  15. MidiEventPtr eve(end);
  16. assert(safe_cast<MidiNoteEvent>(evn));
  17. assert(safe_cast<MidiEndEvent>(eve));
  18. assert(!safe_cast<MidiNoteEvent>(eve));
  19. assert(!safe_cast<MidiEndEvent>(evn));
  20. assert(!safe_cast<MidiNoteEvent>(end));
  21. assert(!safe_cast<MidiEndEvent>(note));
  22. assert(safe_cast<MidiEvent>(note));
  23. assert(safe_cast<MidiEvent>(end));
  24. }
  25. static void testEqual()
  26. {
  27. MidiNoteEventPtr note = std::make_shared<MidiNoteEvent>();
  28. MidiNoteEventPtr note2 = std::make_shared<MidiNoteEvent>();
  29. MidiEventPtr evn(note);
  30. MidiEndEventPtr end = std::make_shared<MidiEndEvent>();
  31. MidiEventPtr eve(end);
  32. assertEQ(note, evn);
  33. assertEQ(end, eve);
  34. assert (*note == *note2);
  35. assert(note != note2);
  36. assert(!(*note == *end));
  37. assert(*note != *end);
  38. assert(*note != *eve);
  39. assert(*evn != *end);
  40. assert(*note != *end);
  41. note2->pitchCV = 50;
  42. assert(*note != *note2);
  43. }
  44. static void testPitch()
  45. {
  46. MidiNoteEvent n;
  47. n.pitchCV = 0; // C4 in VCV
  48. auto pitch = n.getPitch();
  49. assert(pitch.first == 4);
  50. assert(pitch.second == 0);
  51. n.setPitch(pitch.first, pitch.second);
  52. assertEQ(n.pitchCV, 0);
  53. //************************************************
  54. n.pitchCV = 1.f / 12.f;
  55. pitch = n.getPitch();
  56. assert(pitch.first == 4);
  57. assert(pitch.second == 1);
  58. n.setPitch(pitch.first, pitch.second);
  59. assertClose(n.pitchCV, 1.f / 12.f, .0001);
  60. //********************************************************
  61. n.pitchCV = 3 + 1.f / 12.f;
  62. pitch = n.getPitch();
  63. assertEQ(pitch.first, 4.0f + 3);
  64. assertEQ(pitch.second, 1);
  65. n.setPitch(pitch.first, pitch.second);
  66. assertClose(n.pitchCV, 3 + 1.f / 12.f, .0001);
  67. //********************************************************
  68. n.pitchCV = 7.f / 12.f;
  69. pitch = n.getPitch();
  70. assertEQ(pitch.first, 4.0f);
  71. assertEQ(pitch.second, 7);
  72. n.setPitch(pitch.first, pitch.second);
  73. assertClose(n.pitchCV, 7.f / 12.f, .0001);
  74. //********************************************************
  75. n.pitchCV = 3 + 7.f / 12.f;
  76. pitch = n.getPitch();
  77. assertEQ(pitch.first, 4.0f+3);
  78. assertEQ(pitch.second, 7);
  79. n.setPitch(pitch.first, pitch.second);
  80. assertClose(n.pitchCV,3 + 7.f / 12.f, .0001);
  81. }
  82. static void testPitch2()
  83. {
  84. assertNoMidi(); // check for leaks
  85. MidiNoteEvent n;
  86. n.pitchCV = -1; // C3 in VCV
  87. auto pitch = n.getPitch();
  88. assert(pitch.first == 3);
  89. assert(pitch.second == 0);
  90. n.setPitch(pitch.first, pitch.second);
  91. assertClose(n.pitchCV, -1, .0001);
  92. //******************************************************************************
  93. n.pitchCV = 0 + 1.f / 12.f + 1.f / 25.f; // D4 plus less than half semi
  94. pitch = n.getPitch();
  95. assertEQ(pitch.first, 4);
  96. assertEQ(pitch.second, 1);
  97. n.setPitch(pitch.first, pitch.second);
  98. assertClose(n.pitchCV, 1.f / 12.f, .0001);
  99. //**********************************************************
  100. n.pitchCV = 0 + 1.f / 12.f + 1.f / 23.f; // D4 plus more than half semi
  101. pitch = n.getPitch();
  102. assertEQ(pitch.first, 4);
  103. assertEQ(pitch.second, 2);
  104. n.setPitch(pitch.first, pitch.second);
  105. assertClose(n.pitchCV, 2.f / 12.f, .0001);
  106. }
  107. static void testCopyCtor()
  108. {
  109. assertNoMidi(); // check for leaks
  110. {
  111. MidiNoteEvent note;
  112. note.pitchCV = 1.12f;
  113. note.duration = 33.45f;
  114. note.startTime = 15.3f;
  115. MidiNoteEvent note2(note);
  116. assert(note == note2);
  117. MidiNoteEvent note3 = note;
  118. assert(note == note3);
  119. MidiEndEvent end;
  120. end.startTime = 234.5f;
  121. MidiEndEvent end2(end);
  122. assert(end == end2);
  123. MidiEndEvent end3 = end;
  124. assert(end == end3);
  125. }
  126. assertNoMidi(); // check for leaks
  127. }
  128. static void testEqualNote()
  129. {
  130. MidiNoteEvent note1;
  131. MidiNoteEvent note2;
  132. assert(note1 == note2);
  133. note2.pitchCV = .5;
  134. assert(note1 != note2);
  135. note2 = note1;
  136. assert(note1 == note2);
  137. note2.duration = 5;
  138. assert(note1 != note2);
  139. MidiNoteEvent note3(note2);
  140. assert(note3 == note2);
  141. }
  142. static void testEqualEnd()
  143. {
  144. MidiEndEvent end1;
  145. MidiEndEvent end2;
  146. assert(end1 == end2);
  147. end2.startTime = 55;
  148. assert(end1 != end2);
  149. }
  150. static void testClone()
  151. {
  152. MidiNoteEvent note;
  153. note.pitchCV = 4.1f;
  154. note.duration = .6f;
  155. note.startTime = .22f;
  156. MidiEventPtr evt = note.clone();
  157. MidiNoteEventPtr note2 = note.clonen();
  158. assert(note == *evt);
  159. assert(note == *note2);
  160. MidiEndEvent end;
  161. end.startTime = 102345;
  162. MidiEventPtr end2 = end.clone();
  163. assert(end == *end2);
  164. }
  165. void testMidiEvents()
  166. {
  167. assertNoMidi(); // check for leaks
  168. testType();
  169. testCast();
  170. testEqual();
  171. testPitch();
  172. testPitch2();
  173. testCopyCtor();
  174. testEqualNote();
  175. testEqualEnd();
  176. testClone();
  177. assertNoMidi(); // check for leaks
  178. }