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.

121 lines
2.7KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 3 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. /**
  18. * This file is an edited version of VCVRack's midi.hpp
  19. * Copyright (C) 2016-2021 VCV.
  20. *
  21. * This program is free software: you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 3 of
  24. * the License, or (at your option) any later version.
  25. */
  26. #pragma once
  27. #include "choc/choc_SmallVector.h"
  28. namespace rack {
  29. /** Abstraction for all MIDI drivers in Rack */
  30. namespace midi {
  31. struct Message {
  32. /** Initialized to 3 empty bytes. */
  33. choc::SmallVector<uint8_t, 3> bytes;
  34. /** The Engine frame timestamp of the Message.
  35. For output messages, the frame when the message was generated.
  36. For input messages, the frame when it is intended to be processed.
  37. -1 for undefined, to be sent or processed immediately.
  38. */
  39. int64_t frame = -1;
  40. Message() {
  41. bytes.resize(3);
  42. }
  43. int getSize() const {
  44. return bytes.size();
  45. }
  46. void setSize(int size) {
  47. bytes.resize(size);
  48. }
  49. uint8_t getChannel() const {
  50. if (bytes.size() < 1)
  51. return 0;
  52. return bytes[0] & 0xf;
  53. }
  54. void setChannel(uint8_t channel) {
  55. if (bytes.size() < 1)
  56. return;
  57. bytes[0] = (bytes[0] & 0xf0) | (channel & 0xf);
  58. }
  59. uint8_t getStatus() const {
  60. if (bytes.size() < 1)
  61. return 0;
  62. return bytes[0] >> 4;
  63. }
  64. void setStatus(uint8_t status) {
  65. if (bytes.size() < 1)
  66. return;
  67. bytes[0] = (bytes[0] & 0xf) | (status << 4);
  68. }
  69. uint8_t getNote() const {
  70. if (bytes.size() < 2)
  71. return 0;
  72. return bytes[1];
  73. }
  74. void setNote(uint8_t note) {
  75. if (bytes.size() < 2)
  76. return;
  77. bytes[1] = note & 0x7f;
  78. }
  79. uint8_t getValue() const {
  80. if (bytes.size() < 3)
  81. return 0;
  82. return bytes[2];
  83. }
  84. void setValue(uint8_t value) {
  85. if (bytes.size() < 3)
  86. return;
  87. bytes[2] = value & 0x7f;
  88. }
  89. std::string toString() const;
  90. int64_t getFrame() const {
  91. return frame;
  92. }
  93. void setFrame(int64_t frame) {
  94. this->frame = frame;
  95. }
  96. };
  97. /* NOTE all the other MIDI stuff (drivers, ports etc) is purposefully missing here, unwanted in Cardinal
  98. */
  99. struct Port;
  100. } // namespace midi
  101. } // namespace rack