Files
ESP8266Audio/examples/PlayMIDIFromROM/PlayMIDIFromROM.ino
T
Earle F. Philhower, III 7d99e6b7b9 BREAKING: Re-port MIDI, faster, lower memory, ESP32 support (#790)
* Pull untouched upstream TinySoundFont

Start from the upstream TinySoundFont by @schellingb at
https://github.com/schellingb/TinySoundFont and rearchitect the MIDI
processing.

* Convert to PROGMEM-based SoundFont structures.

This makes it much faster to access the SoundFont and also removes all the
static RAM usage.  The samples, the regions, the instruments are all now
in ROM.  Only the dynamic voices and related info takes up RAM.  No caching.

* (Re)convert to fixed point math.

TSF upstream is built for systems with real floating point units (and
produces great sound with it!), but processors like the Pico, ESP8266,
and several models of ESP32 don't have one so emulate FP in *extremely*
slow SW.  Use fixed point (integer) representations for the inner loop
and wherever possible to avoid `float` math.

For processors like the Pico 2 or original ESP32 with hardware FPUs this
still increases performance since integer is still much faster.

* Redo the example and add default SF2s

Rewrite the MIDI examples to use PROGMEM for both SF2 and MID file.
It simplifies life for the casual user and me.

* Breaking change

The MIDI object now has a `midi::SetSoundFont(tsf *)` instead of a
`midi::SetSoundfont(AudioFileSource *)`.  It no longer supports reading
SF2 from LittleFS or SD.
2025-10-20 18:26:41 -07:00

74 lines
2.1 KiB
Arduino

// PlayMIDIFromROM
// Released to the public domain (2025) by Earle F. Philhower, III <earlephilhower@yahoo.com>
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ESP8266Audio.h>
// Define the SF2 (SoundFont) to use. Will be stored in PROGMEM as global _tsf
#ifdef ESP8266
// The ESP8266 has a 1MB ROM limit so use a smaller SF2 file
#include <libtinysoundfont/scratch2010.h>
#else
// Pico and ESP32 can have up to 16MB ROMs, use the 1M General MIDI (1mgm) font or your own
// For ESP32 be sure to use HUGE APP or another partition mode which allows you to have sketches larger than 1-2MB
#include <libtinysoundfont/1mgm.h>
#endif
// Select one of the following in order of CPU limitations. You can also use your own.
//#include "furelise_mid.h"
//#include "venture_mid.h"
#include "jm_mozdi_mid.h"
// Note that the .MID file does NOT need to be in PROGMEM, it just makes the examples easier to use (no FS upload required).
AudioFileSourcePROGMEM *midfile;
// Any AudioOutput* would work here (AudioOutputPWM, AudioOutputI2SNoDAT, etc.)
AudioOutputI2S *dac;
// The MIDI wavetable synthesizer
AudioGeneratorMIDI *midi;
void setup() {
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
Serial.println("Starting up...\n");
audioLogger = &Serial;
midfile = new AudioFileSourcePROGMEM(_mid, sizeof(_mid));
dac = new AudioOutputI2S();
dac->SetPinout(0, 1, 2); // Adjust to your I2S DAC pinout
dac->SetGain(0.5);
midi = new AudioGeneratorMIDI();
midi->SetSoundFont(&_tsf);
// If you get audio clicks/break up then you can decrease the sample rate. You can also try increasing this if your MIDs are simple enough and the chip fast enough
midi->SetSampleRate(22050);
Serial.printf("BEGIN...\n");
midi->begin(midfile, dac);
}
void loop() {
static int cnt, lcnt;
if (midi->isRunning()) {
if (!midi->loop()) {
midi->stop();
} else {
if (++cnt == 10000) {
Serial.printf("Loop %d\n", ++lcnt * 1000);
cnt = 0;
}
}
} else {
Serial.printf("MIDI done\n");
delay(1000);
}
}