Launchpad.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "Launchpad.h"
  2. #include "RtMidi.h"
  3. #include "MidiMessage.h"
  4. LaunchpadColor::LaunchpadColor()
  5. : red(0), green(0)
  6. {
  7. }
  8. LaunchpadColor::LaunchpadColor(unsigned char r, unsigned char g)
  9. : red(r), green(g)
  10. {
  11. }
  12. bool LaunchpadColor::operator==(const LaunchpadColor& color) const
  13. {
  14. return red == color.red && green == color.green;
  15. }
  16. Launchpad::Launchpad()
  17. : midiin(), midiout(), keyPressedCallback(0), keyReleasedCallback(0)
  18. {
  19. midiin.setCallback(midiMessageCallback, (void*) this);
  20. midiout.openPort(1);
  21. midiin.openPort(0);
  22. }
  23. void Launchpad::midiMessageCallback(double timeStamp, MidiMessage &message, void *userData)
  24. {
  25. Launchpad *launchpad = (Launchpad*) userData;
  26. // use runtime type information to check for the message type.
  27. // this requires the specified class to have a virtual member.
  28. NoteMessage *noteMessage = dynamic_cast<NoteMessage*>(&message);
  29. if(noteMessage)
  30. {
  31. unsigned char x = noteMessage->pitch % 16;
  32. unsigned char y = 7 - noteMessage->pitch / 16;
  33. if(dynamic_cast<NoteOnMessage*>(noteMessage))
  34. {
  35. launchpad->keyPressed(x, y);
  36. }
  37. else
  38. {
  39. launchpad->keyReleased(x, y);
  40. }
  41. }
  42. else
  43. {
  44. ControlChangeMessage *controlChangeMessage = dynamic_cast<ControlChangeMessage*>(&message);
  45. if(controlChangeMessage)
  46. {
  47. unsigned char x = controlChangeMessage->control - 104;
  48. unsigned char y = 8;
  49. if(controlChangeMessage->value == 127)
  50. {
  51. launchpad->keyPressed(x, y);
  52. }
  53. else
  54. {
  55. launchpad->keyReleased(x, y);
  56. }
  57. }
  58. }
  59. }
  60. bool Launchpad::issetColor(unsigned char x, unsigned char y) const
  61. {
  62. return colorSet[x][y];
  63. }
  64. const LaunchpadColor& Launchpad::getColor(unsigned char x, unsigned char y) const
  65. {
  66. return colors[x][y];
  67. }
  68. void Launchpad::setColor(unsigned char x, unsigned char y, const LaunchpadColor& color)
  69. {
  70. if(x >= width || y >= height || (x == 8 && y == 8))
  71. {
  72. throw RtMidiError(
  73. "specified launchpad position is out of range",
  74. RtMidiError::INVALID_PARAMETER
  75. );
  76. }
  77. if(color.red > 3 || color.green > 3)
  78. {
  79. throw RtMidiError(
  80. "specified launchpad color is out of range",
  81. RtMidiError::INVALID_PARAMETER
  82. );
  83. }
  84. unsigned char velocity = (color.green << 4) + color.red;
  85. if(y == 8)
  86. {
  87. midiout.sendMessage(ControlChangeMessage(
  88. 0,
  89. 104 + x,
  90. velocity
  91. ));
  92. }
  93. else
  94. {
  95. midiout.sendMessage(NoteOnMessage(
  96. 0,
  97. ((7 - y) << 4) + x,
  98. velocity
  99. ));
  100. }
  101. colors[x][y] = color;
  102. colorSet[x][y] = true;
  103. }
  104. void Launchpad::keyPressed(unsigned char x, unsigned char y)
  105. {
  106. if(keyPressedCallback)
  107. {
  108. keyPressedCallback(x, y);
  109. }
  110. }
  111. void Launchpad::keyReleased(unsigned char x, unsigned char y)
  112. {
  113. if(keyReleasedCallback)
  114. {
  115. keyReleasedCallback(x, y);
  116. }
  117. }