Play music using the BANK_0_ACOUSTIC_GUITAR_STEEL_STRING tone.
#if defined(ESP32)
#include <HardwareSerial.h>
#else
#include <SoftwareSerial.h>
#endif
namespace {
constexpr uint8_t kChannel = 0;
constexpr uint8_t kChannelVolume = 110;
constexpr uint8_t kReverberationVolume = 80;
constexpr uint8_t kReverberationDelayFeedback = 0;
constexpr uint16_t kQuarterNoteDuration = 300;
constexpr uint16_t kEighthNoteDuration = 150;
constexpr uint16_t kDottedQuarterNoteDuration = 450;
#if defined(ESP32)
constexpr gpio_num_t kMidiPin = GPIO_NUM_17;
constexpr uart_port_t kUartPort = UART_NUM_2;
HardwareSerial g_midi_serial(kUartPort);
#else
constexpr uint8_t kMidiPin = 4;
SoftwareSerial g_midi_serial(-1, kMidiPin);
#endif
void PlayNote(const uint8_t midi_note, const uint16_t duration, const uint8_t note_velocity = 90) {
g_midi.
NoteOn(kChannel, midi_note, note_velocity);
delay(duration);
g_midi.
NoteOff(kChannel, midi_note);
delay(30);
}
}
void setup() {
#if defined(ESP32)
g_midi_serial.begin(31250, SERIAL_8N1, -1, kMidiPin);
#else
g_midi_serial.begin(31250);
#endif
g_midi.SetChannelVolume(kChannel, kChannelVolume);
}
void loop() {
delay(kQuarterNoteDuration);
delay(kQuarterNoteDuration);
delay(kQuarterNoteDuration);
delay(5000);
}
Midi is a driver class for the MIDI module, used for synthesizing music.
void NoteOff(const uint8_t channel, const uint8_t midi_note)
Close the note in the specified channel, which was previously opened at a given pitch using the noteO...
void NoteOn(const uint8_t channel, const uint8_t midi_note, const uint8_t z)
Generate the given MIDI note on the designated channel (0-15).
#define EM_MIDI_REVERBERATION_ROOM_2
Room reverberation type 2.
#define EM_MIDI_NOTE_F_5
F5 (note F at octave 5)
#define EM_MIDI_NOTE_G_5
G5 (note G at octave 5)
#define EM_MIDI_NOTE_E_5
E5 (note E at octave 5)
#define EM_MIDI_NOTE_B_4
B4 (note B at octave 4)
#define EM_MIDI_NOTE_A_4
A4 (note A at octave 4, standard pitch 440Hz)
#define EM_MIDI_NOTE_D_5
D5 (note D at octave 5)
#define EM_MIDI_NOTE_G_4
G4 (note G at octave 4)
#define EM_MIDI_NOTE_C_5
C5 (note C at octave 5)
#define EM_MIDI_TIMBRE_BANK_0
Standard GM Sound Bank.
#define EM_MIDI_TIMBRE_BANK_0_ACOUSTIC_GUITAR_STEEL_STRING
Acoustic Guitar Steel String.