sources and files for the bombatuino project
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.

100 lines
2.5 KiB

  1. #include "Arduino.h"
  2. #include "bombatuino_MIDI.h"
  3. void MIDI::begin(int channel) {
  4. #ifdef DEBUG
  5. Serial.begin(9600);
  6. #else
  7. Serial.begin(31250);
  8. #endif
  9. //if given channel is not valid, set default channel to 1
  10. if (channel > MIDI_MAX_CHANNEL)
  11. channel = MIDI_DEFAULT_CHANNEL;
  12. _channel = channel;
  13. }
  14. bool MIDI::message(int status, int data, int data2, int channel) {
  15. //check if status byte is valid
  16. if (status > MIDI_MAX_STATUS) return false;
  17. //check if first data byte is valid
  18. if (data > MIDI_MAX_DATA) return false;
  19. //check if second data byte is valid
  20. if (data2 > MIDI_MAX_DATA) return false;
  21. //if no specific channel given, use default channel
  22. if (channel == MIDI_NULL_CHANNEL) channel = _channel;
  23. //check if channel is valid
  24. if (channel > MIDI_MAX_CHANNEL) return false;
  25. //write bytes to serial
  26. #ifdef DEBUG
  27. Serial.print("status: ");
  28. Serial.print(status+channel);
  29. Serial.print(" data: ");
  30. Serial.print(data);
  31. #else
  32. Serial.write(status+channel);
  33. Serial.write(data);
  34. #endif
  35. //check if second data byte should be send
  36. if (status != MIDI_PROGRAMM_CHANGE && status != MIDI_CHANNEL_PRESSURE) {
  37. #ifdef DEBUG
  38. Serial.print(" data2: ");
  39. Serial.print(data2);
  40. #else
  41. Serial.write(data2);
  42. #endif
  43. }
  44. #ifdef DEBUG
  45. Serial.println();
  46. #endif
  47. return true;
  48. }
  49. bool MIDI::noteOff(int note, int velocity, int channel) {
  50. #ifdef DEBUG
  51. Serial.print(" noteOff ");
  52. #endif
  53. return message(MIDI_NOTE_OFF,note,velocity,channel);
  54. }
  55. bool MIDI::noteOn(int note, int velocity, int channel) {
  56. #ifdef DEBUG
  57. Serial.print(" noteOn ");
  58. #endif
  59. return message(MIDI_NOTE_ON,note,velocity,channel);
  60. }
  61. bool MIDI::polyphonicKeyPressure(int note, int velocity, int channel) {
  62. #ifdef DEBUG
  63. Serial.print(" polyphonicKeyPressure ");
  64. #endif
  65. return message(MIDI_POLYPHONIC_KEY_PRESSURE,note,velocity,channel);
  66. }
  67. bool MIDI::controlChange(int controller, int value, int channel) {
  68. #ifdef DEBUG
  69. Serial.print(" controlChange ");
  70. #endif
  71. return message(MIDI_CONTROL_CHANGE,controller,value,channel);
  72. }
  73. bool MIDI::programChange(int programm, int channel) {
  74. #ifdef DEBUG
  75. Serial.print(" programChange ");
  76. #endif
  77. return message(MIDI_PROGRAMM_CHANGE,programm,0,channel);
  78. }
  79. bool MIDI::channelPressure(int pressure, int channel) {
  80. #ifdef DEBUG
  81. Serial.print(" channelPressure ");
  82. #endif
  83. return message(MIDI_CHANNEL_PRESSURE,pressure,0,channel);
  84. }
  85. bool MIDI::pitchWheelChange(int last, int most, int channel) {
  86. #ifdef DEBUG
  87. Serial.print(" pitchWheelChange ");
  88. #endif
  89. return message(MIDI_PITCH_WHEEL_CHANGE,last,most,channel);
  90. }