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.

SimpleSynth.ino 2.4KB

3 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <MIDI.h>
  2. #include "noteList.h"
  3. #include "pitches.h"
  4. MIDI_CREATE_DEFAULT_INSTANCE();
  5. #ifdef ARDUINO_SAM_DUE // Due has no tone function (yet), overriden to prevent build errors.
  6. #define tone(...)
  7. #define noTone(...)
  8. #endif
  9. // This example shows how to make a simple synth out of an Arduino, using the
  10. // tone() function. It also outputs a gate signal for controlling external
  11. // analog synth components (like envelopes).
  12. static const unsigned sGatePin = 13;
  13. static const unsigned sAudioOutPin = 10;
  14. static const unsigned sMaxNumNotes = 16;
  15. MidiNoteList<sMaxNumNotes> midiNotes;
  16. // -----------------------------------------------------------------------------
  17. inline void handleGateChanged(bool inGateActive)
  18. {
  19. digitalWrite(sGatePin, inGateActive ? HIGH : LOW);
  20. }
  21. inline void pulseGate()
  22. {
  23. handleGateChanged(false);
  24. delay(1);
  25. handleGateChanged(true);
  26. }
  27. // -----------------------------------------------------------------------------
  28. void handleNotesChanged(bool isFirstNote = false)
  29. {
  30. if (midiNotes.empty())
  31. {
  32. handleGateChanged(false);
  33. noTone(sAudioOutPin); // Remove to keep oscillator running during envelope release.
  34. }
  35. else
  36. {
  37. // Possible playing modes:
  38. // Mono Low: use midiNotes.getLow
  39. // Mono High: use midiNotes.getHigh
  40. // Mono Last: use midiNotes.getLast
  41. byte currentNote = 0;
  42. if (midiNotes.getLast(currentNote))
  43. {
  44. tone(sAudioOutPin, sNotePitches[currentNote]);
  45. if (isFirstNote)
  46. {
  47. handleGateChanged(true);
  48. }
  49. else
  50. {
  51. pulseGate(); // Retrigger envelopes. Remove for legato effect.
  52. }
  53. }
  54. }
  55. }
  56. // -----------------------------------------------------------------------------
  57. void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
  58. {
  59. const bool firstNote = midiNotes.empty();
  60. midiNotes.add(MidiNote(inNote, inVelocity));
  61. handleNotesChanged(firstNote);
  62. }
  63. void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
  64. {
  65. midiNotes.remove(inNote);
  66. handleNotesChanged();
  67. }
  68. // -----------------------------------------------------------------------------
  69. void setup()
  70. {
  71. pinMode(sGatePin, OUTPUT);
  72. pinMode(sAudioOutPin, OUTPUT);
  73. MIDI.setHandleNoteOn(handleNoteOn);
  74. MIDI.setHandleNoteOff(handleNoteOff);
  75. MIDI.begin();
  76. }
  77. void loop()
  78. {
  79. MIDI.read();
  80. }