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.

113 lines
2.8KB

  1. #include "asserts.h"
  2. #include "MidiEditorContext.h"
  3. #include "NoteScreenScale.h"
  4. // basic test of x coordinates
  5. static void test0()
  6. {
  7. // viewport holds single quarter note
  8. MidiEditorContextPtr vp = std::make_shared<MidiEditorContext>(nullptr);
  9. vp->setStartTime(0);
  10. vp->setEndTime(1);
  11. // let's make one quarter note fill the whole screen
  12. MidiNoteEvent note;
  13. note.setPitch(3, 0);
  14. vp->setPitchRange(note.pitchCV, note.pitchCV);
  15. vp->setCursorPitch(note.pitchCV);
  16. NoteScreenScale n(vp, 100, 100);
  17. float left = n.midiTimeToX(note);
  18. float right = left + n.midiTimeTodX(1.0f);
  19. assertEQ(left, 0);
  20. assertEQ(right, 100);
  21. float l2 = n.midiTimeToX(note.startTime);
  22. assertEQ(left, l2);
  23. auto bounds = n.midiTimeToHBounds(note);
  24. assertEQ(bounds.first, 0);
  25. assertEQ(bounds.second, 100);
  26. }
  27. // basic test of y coordinates
  28. static void test1()
  29. {
  30. // viewport holds single quarter note
  31. MidiEditorContextPtr vp = std::make_shared<MidiEditorContext>(nullptr);
  32. vp->setTimeRange(0, 1);
  33. // let's make one quarter note fill the whole screen
  34. MidiNoteEvent note;
  35. note.setPitch(3, 0);
  36. vp->setPitchRange(note.pitchCV, note.pitchCV);
  37. vp->setCursorPitch(note.pitchCV);
  38. NoteScreenScale n(vp, 100, 100);
  39. auto y = n.midiPitchToY(note);
  40. auto h = n.noteHeight();
  41. assertClose(y, 0, .001);
  42. assertClose(h, 100, .001);
  43. }
  44. // test of offset x coordinates
  45. // viewport = 1 bar, have an eight not on beat 4
  46. static void test2()
  47. {
  48. printf("test2\n");
  49. // viewport holds one bar of 4/4
  50. MidiEditorContextPtr vp = std::make_shared<MidiEditorContext>(nullptr);
  51. vp->setTimeRange(0, 4);
  52. // let's make one eight note
  53. MidiNoteEvent note;
  54. note.startTime = 3.f;
  55. note.duration = .5f;
  56. note.setPitch(3, 0);
  57. vp->setPitchRange(note.pitchCV, note.pitchCV);
  58. vp->setCursorPitch(note.pitchCV);
  59. NoteScreenScale n(vp, 100, 100);
  60. auto bounds = n.midiTimeToHBounds(note);
  61. assertEQ(bounds.first, 75.f);
  62. assertEQ(bounds.second, 75.f + (100.0 / 8));
  63. float x = n.midiTimeToX(note);
  64. float x2 = n.midiTimeToX(note.startTime);
  65. assertEQ(x, x2);
  66. }
  67. // basic test of y coordinates
  68. static void test3()
  69. {
  70. // viewport holds two pitches
  71. MidiEditorContextPtr vp = std::make_shared<MidiEditorContext>(nullptr);
  72. vp->setTimeRange(0, 1);
  73. MidiNoteEvent note1, note2;
  74. note1.setPitch(3, 0);
  75. note2.setPitch(3, 1);
  76. vp->setPitchRange(note1.pitchCV, note2.pitchCV);
  77. vp->setCursorPitch(note1.pitchCV);
  78. NoteScreenScale n(vp, 100, 100);
  79. auto h = n.noteHeight();
  80. assertClose(h, 50, .001);
  81. // hight pitch should be at top
  82. auto y = n.midiPitchToY(note2);
  83. assertClose(y, 0, .001);
  84. }
  85. void testNoteScreenScale()
  86. {
  87. test0();
  88. test1();
  89. test2();
  90. test3();
  91. }