PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

122 lines
3.4KB

  1. /***************************************************
  2. This is an example for the Adafruit VS1053 Codec Breakout
  3. Designed specifically to work with the Adafruit VS1053 Codec Breakout
  4. ----> https://www.adafruit.com/products/1381
  5. Adafruit invests time and resources providing this open source code,
  6. please support Adafruit and open-source hardware by purchasing
  7. products from Adafruit!
  8. Written by Limor Fried/Ladyada for Adafruit Industries.
  9. BSD license, all text above must be included in any redistribution
  10. ****************************************************/
  11. #include <SoftwareSerial.h>
  12. // define the pins used
  13. #define VS1053_RX 2 // This is the pin that connects to the RX pin on VS1053
  14. #define VS1053_RESET 9 // This is the pin that connects to the RESET pin on VS1053
  15. // If you have the Music Maker shield, you don't need to connect the RESET pin!
  16. // If you're using the VS1053 breakout:
  17. // Don't forget to connect the GPIO #0 to GROUND and GPIO #1 pin to 3.3V
  18. // If you're using the Music Maker shield:
  19. // Don't forget to connect the GPIO #1 pin to 3.3V and the RX pin to digital #2
  20. // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 31
  21. #define VS1053_BANK_DEFAULT 0x00
  22. #define VS1053_BANK_DRUMS1 0x78
  23. #define VS1053_BANK_DRUMS2 0x7F
  24. #define VS1053_BANK_MELODY 0x79
  25. // See http://www.vlsi.fi/fileadmin/datasheets/vs1053.pdf Pg 32 for more!
  26. #define VS1053_GM1_OCARINA 80
  27. #define MIDI_NOTE_ON 0x90
  28. #define MIDI_NOTE_OFF 0x80
  29. #define MIDI_CHAN_MSG 0xB0
  30. #define MIDI_CHAN_BANK 0x00
  31. #define MIDI_CHAN_VOLUME 0x07
  32. #define MIDI_CHAN_PROGRAM 0xC0
  33. SoftwareSerial VS1053_MIDI(0, 2); // TX only, do not use the 'rx' side
  34. // on a Mega/Leonardo you may have to change the pin to one that
  35. // software serial support uses OR use a hardware serial port!
  36. void setup() {
  37. Serial.begin(9600);
  38. Serial.println("VS1053 MIDI test");
  39. VS1053_MIDI.begin(31250); // MIDI uses a 'strange baud rate'
  40. pinMode(VS1053_RESET, OUTPUT);
  41. digitalWrite(VS1053_RESET, LOW);
  42. delay(10);
  43. digitalWrite(VS1053_RESET, HIGH);
  44. delay(10);
  45. midiSetChannelBank(0, VS1053_BANK_MELODY);
  46. midiSetInstrument(0, VS1053_GM1_OCARINA);
  47. midiSetChannelVolume(0, 127);
  48. }
  49. void loop() {
  50. for (uint8_t i=60; i<69; i++) {
  51. midiNoteOn(0, i, 127);
  52. delay(100);
  53. midiNoteOff(0, i, 127);
  54. }
  55. delay(1000);
  56. }
  57. void midiSetInstrument(uint8_t chan, uint8_t inst) {
  58. if (chan > 15) return;
  59. inst --; // page 32 has instruments starting with 1 not 0 :(
  60. if (inst > 127) return;
  61. VS1053_MIDI.write(MIDI_CHAN_PROGRAM | chan);
  62. VS1053_MIDI.write(inst);
  63. }
  64. void midiSetChannelVolume(uint8_t chan, uint8_t vol) {
  65. if (chan > 15) return;
  66. if (vol > 127) return;
  67. VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
  68. VS1053_MIDI.write(MIDI_CHAN_VOLUME);
  69. VS1053_MIDI.write(vol);
  70. }
  71. void midiSetChannelBank(uint8_t chan, uint8_t bank) {
  72. if (chan > 15) return;
  73. if (bank > 127) return;
  74. VS1053_MIDI.write(MIDI_CHAN_MSG | chan);
  75. VS1053_MIDI.write((uint8_t)MIDI_CHAN_BANK);
  76. VS1053_MIDI.write(bank);
  77. }
  78. void midiNoteOn(uint8_t chan, uint8_t n, uint8_t vel) {
  79. if (chan > 15) return;
  80. if (n > 127) return;
  81. if (vel > 127) return;
  82. VS1053_MIDI.write(MIDI_NOTE_ON | chan);
  83. VS1053_MIDI.write(n);
  84. VS1053_MIDI.write(vel);
  85. }
  86. void midiNoteOff(uint8_t chan, uint8_t n, uint8_t vel) {
  87. if (chan > 15) return;
  88. if (n > 127) return;
  89. if (vel > 127) return;
  90. VS1053_MIDI.write(MIDI_NOTE_OFF | chan);
  91. VS1053_MIDI.write(n);
  92. VS1053_MIDI.write(vel);
  93. }