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.
This commit is contained in:
Earle F. Philhower, III authored and GitHub committed 2025-10-20 18:26:41 -07:00
1 parent 3e099fefe1
commit 7d99e6b7b9
31 files changed
+64533 -2836

No files matched your search

+7
View File
@@ -21,9 +21,15 @@
#ifndef _AUDIOFILESOURCESPIFFS_H
#define _AUDIOFILESOURCESPIFFS_H
#ifndef ARDUINO_ARCH_RP2040
#include <Arduino.h>
#include <FS.h>
#ifdef ESP32
#include <SPIFFS.h>
#endif
#include "AudioFileSource.h"
#include "AudioFileSourceFS.h"
@@ -37,6 +43,7 @@ public:
// Others are inherited from base
};
#endif
#endif
+53 -124
View File
@@ -58,38 +58,19 @@
#include "AudioGeneratorMIDI.h"
#if defined(ESP32) && (__GNUC__ >= 8) && (__XTENSA__)
// Do not build, Espressif's GCC8+ has a compiler bug
#else // __GNUC__ == 8
#pragma GCC optimize ("O3")
#define TSF_NO_STDIO
#define TSF_CONST_FILE
#define TSF_SAMPLES_SHORT
#define TSF_IMPLEMENTATION
#include "libtinysoundfont/tsf.h"
/**************** utility routines **********************/
/* announce a fatal MIDI file format error */
void AudioGeneratorMIDI::midi_error(const char *msg, int curpos) {
cb.st(curpos, msg);
#if 0
int ptr;
audioLogger->printf("---> MIDI file error at position %04X (%d): %s\n", (uint16_t) curpos, (uint16_t) curpos, msg);
/* print some bytes surrounding the error */
ptr = curpos - 16;
if (ptr < 0) {
ptr = 0;
}
buffer.seek(buffer.data, ptr);
for (int i = 0; i < 32; i++) {
char c;
buffer.read(buffer.data, &c, 1);
audioLogger->printf((ptr + i) == curpos ? " [%02X] " : "%02X ", (int) c & 0xff);
}
audioLogger->printf("\n");
#endif
running = false;
}
@@ -119,8 +100,8 @@ void AudioGeneratorMIDI::process_header(void) {
unsigned int time_division;
chk_bufdata(hdrptr, sizeof(struct midi_header));
buffer.seek(buffer.data, hdrptr);
buffer.read(buffer.data, &hdr, sizeof(hdr));
file->seek(hdrptr, SEEK_SET);
file->read(&hdr, sizeof(hdr));
if (!charcmp((char *) hdr.MThd, "MThd")) {
midi_error("Missing 'MThd'", hdrptr);
}
@@ -143,8 +124,10 @@ void AudioGeneratorMIDI::start_track(int tracknum) {
unsigned long tracklen;
chk_bufdata(hdrptr, sizeof(struct track_header));
buffer.seek(buffer.data, hdrptr);
buffer.read(buffer.data, &hdr, sizeof(hdr));
file->seek(hdrptr, SEEK_SET);
//buffer.seek(buffer.data, hdrptr);
file->read(&hdr, sizeof(hdr));
//buffer.read(buffer.data, &hdr, sizeof(hdr));
if (!charcmp((char *)(hdr.MTrk), "MTrk")) {
midi_error("Missing 'MTrk'", hdrptr);
}
@@ -158,22 +141,28 @@ void AudioGeneratorMIDI::start_track(int tracknum) {
unsigned char AudioGeneratorMIDI::buffer_byte(int offset) {
unsigned char c;
buffer.seek(buffer.data, offset);
buffer.read(buffer.data, &c, 1);
file->seek(offset, SEEK_SET);
//buffer.seek(buffer.data, offset);
file->read(&c, 1);
//buffer.read(buffer.data, &c, 1);
return c;
}
unsigned short AudioGeneratorMIDI::buffer_short(int offset) {
unsigned short s;
buffer.seek(buffer.data, offset);
buffer.read(buffer.data, &s, sizeof(short));
file->seek(offset, SEEK_SET);
//buffer.seek(buffer.data, offset);
file->read(&s, sizeof(short));
// buffer.read(buffer.data, &s, sizeof(short));
return s;
}
unsigned int AudioGeneratorMIDI::buffer_int32(int offset) {
uint32_t i;
buffer.seek(buffer.data, offset);
buffer.read(buffer.data, &i, sizeof(i));
file->seek(offset, SEEK_SET);
//buffer.seek(buffer.data, offset);
file->read(&i, sizeof(i));
//buffer.read(buffer.data, &i, sizeof(i));
return i;
}
@@ -345,18 +334,15 @@ note_off:
// Open file, parse headers, get ready to process MIDI
void AudioGeneratorMIDI::PrepareMIDI(AudioFileSource *src) {
MakeStreamFromAFS(src, &afsMIDI);
tsf_stream_wrap_cached(&afsMIDI, 32, 64, &buffer);
buflen = buffer.size(buffer.data);
void AudioGeneratorMIDI::PrepareMIDI() { //AudioFileSource *src) {
buflen = file->getSize(); //buffer.size(buffer.data);
/* process the MIDI file header */
hdrptr = buffer.tell(buffer.data); /* pointer to file and track headers */
hdrptr = file->getPos(); // buffer.tell(buffer.data); /* pointer to file and track headers */
process_header();
printf(" Processing %d tracks.\n", num_tracks);
if (num_tracks > MAX_TRACKS) {
midi_error("Too many tracks", buffer.tell(buffer.data));
midi_error("Too many tracks", file->getPos()); //buffer.tell(buffer.data));
}
/* initialize processing of all the tracks */
@@ -454,7 +440,7 @@ int AudioGeneratorMIDI::PlayMIDI() {
for (tgnum = 0; tgnum < num_tonegens; ++tgnum) { /* find which generator is playing it */
tg = &tonegen[tgnum];
if (tg->playing && tg->track == tracknum && tg->note == trk->note) {
tsf_note_off_fast(g_tsf, tg->instrument, tg->note, tg->playIndex);
tsf_note_off/*_fast*/(g_tsf, tg->instrument, tg->note);//, tg->playIndex);
tg->playing = false;
tg->playIndex = -1;
trk->tonegens[tgnum] = false;
@@ -489,7 +475,7 @@ int AudioGeneratorMIDI::PlayMIDI() {
if (tg->instrument != midi_chan_instrument[trk->chan]) { /* new instrument for this generator */
tg->instrument = midi_chan_instrument[trk->chan];
}
tg->playIndex = tsf_note_on_fast(g_tsf, tg->instrument, tg->note, trk->velocity / 127.0); // velocity = 0...127
tg->playIndex = tsf_note_on/*_fast*/(g_tsf, tg->instrument, tg->note, trk->velocity / 127.0f); // velocity = 0...127
} else {
++notes_skipped;
}
@@ -502,15 +488,8 @@ int AudioGeneratorMIDI::PlayMIDI() {
void AudioGeneratorMIDI::StopMIDI() {
buffer.close(buffer.data);
file->close();
tsf_close(g_tsf);
printf(" %s %d tone generators were used.\n",
num_tonegens_used < num_tonegens ? "Only" : "All", num_tonegens_used);
if (notes_skipped)
printf
(" %d notes were skipped because there weren't enough tone generators.\n", notes_skipped);
printf(" Done.\n");
}
@@ -524,18 +503,17 @@ bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) {
}
memset(midi_chan_instrument, 0, sizeof(midi_chan_instrument));
g_tsf = tsf_load(&afsSF2);
if (!g_tsf) {
return false;
}
tsf_set_output(g_tsf, TSF_MONO, freq, -10 /* dB gain -10 */);
g_tsf = _tsf;
tsf_set_output(g_tsf, TSF_STEREO_INTERLEAVED, freq, -10 /* dB gain -10 */);
if (!out->SetRate(freq)) {
return false;
}
if (!out->SetChannels(1)) {
if (!out->SetChannels(2)) {
return false;
}
if (!out->begin()) {
return false;
}
@@ -545,7 +523,7 @@ bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) {
running = true;
PrepareMIDI(src);
PrepareMIDI();//src);
samplesToPlay = 0;
numSamplesRendered = 0;
@@ -557,40 +535,37 @@ bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) {
bool AudioGeneratorMIDI::loop() {
static int c = 0;
if (!running) {
goto done; // Nothing to do here!
file->loop();
output->loop();
return running;
}
// First, try and push in the stored sample. If we can't, then punt and try later
if (!output->ConsumeSample(lastSample)) {
goto done; // Can't send, but no error detected
}
// Try and stuff the buffer one sample at a time
do {
#ifdef ESP8266
static int c = 0;
c++;
if (c % 44100 == 0) {
if (c == 44100) {
c = 0;
yield();
}
play:
#endif
if (sentSamplesRendered < numSamplesRendered) {
lastSample[AudioOutput::LEFTCHANNEL] = samplesRendered[sentSamplesRendered];
lastSample[AudioOutput::RIGHTCHANNEL] = samplesRendered[sentSamplesRendered];
sentSamplesRendered++;
auto toSend = numSamplesRendered - sentSamplesRendered;
auto ret = output->ConsumeSamples(samplesRendered + sentSamplesRendered * 2, toSend);
sentSamplesRendered += ret;
if (toSend != ret) {
break;
}
} else if (samplesToPlay) {
numSamplesRendered = sizeof(samplesRendered) / sizeof(samplesRendered[0]);
if ((int)samplesToPlay < (int)(sizeof(samplesRendered) / sizeof(samplesRendered[0]))) {
numSamplesRendered = (sizeof(samplesRendered) / sizeof(samplesRendered[0])) / 2;
if ((int)samplesToPlay < (int)(sizeof(samplesRendered) / sizeof(samplesRendered[0])) / 2) {
numSamplesRendered = samplesToPlay;
}
tsf_render_short_fast(g_tsf, samplesRendered, numSamplesRendered, 0);
lastSample[AudioOutput::LEFTCHANNEL] = samplesRendered[0];
lastSample[AudioOutput::RIGHTCHANNEL] = samplesRendered[0];
sentSamplesRendered = 1;
tsf_render_short(g_tsf, samplesRendered, numSamplesRendered, 0);
samplesToPlay -= numSamplesRendered;
sentSamplesRendered = 0;
} else {
numSamplesRendered = 0;
sentSamplesRendered = 0;
@@ -602,12 +577,10 @@ play:
sawEOF = true;
samplesToPlay = freq / 2;
}
goto play;
}
}
} while (running && output->ConsumeSample(lastSample));
} while (running);
done:
file->loop();
output->loop();
@@ -619,47 +592,3 @@ bool AudioGeneratorMIDI::stop() {
output->stop();
return true;
}
int AudioGeneratorMIDI::afs_read(void *data, void *ptr, unsigned int size) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->read(ptr, size);
}
int AudioGeneratorMIDI::afs_tell(void *data) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->getPos();
}
int AudioGeneratorMIDI::afs_skip(void *data, unsigned int count) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->seek(count, SEEK_CUR);
}
int AudioGeneratorMIDI::afs_seek(void *data, unsigned int pos) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->seek(pos, SEEK_SET);
}
int AudioGeneratorMIDI::afs_close(void *data) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->close();
}
int AudioGeneratorMIDI::afs_size(void *data) {
AudioFileSource *s = reinterpret_cast<AudioFileSource *>(data);
return s->getSize();
}
void AudioGeneratorMIDI::MakeStreamFromAFS(AudioFileSource *src, tsf_stream *afs) {
afs->data = reinterpret_cast<void*>(src);
afs->read = &afs_read;
afs->tell = &afs_tell;
afs->skip = &afs_skip;
afs->seek = &afs_seek;
afs->close = &afs_close;
afs->size = &afs_size;
}
#endif //__GNUC__ == 8
+17 -18
View File
@@ -21,22 +21,25 @@
#ifndef _AUDIOGENERATORMIDI_H
#define _AUDIOGENERATORMIDI_H
#if defined(ESP32) && (__GNUC__ >= 8) && (__XTENSA__)
#if defined(xxxESP32) && (__GNUC__ >= 8) && (__XTENSA__)
// Do not build, Espressif's GCC8+ has a compiler bug
#else // __GNUC__ == 8
#include "AudioGenerator.h"
#define TSF_NO_STDIO
#define TSF_CONST_FILE
#define TSF_SAMPLES_SHORT
#include "libtinysoundfont/tsf.h"
class AudioGeneratorMIDI : public AudioGenerator {
public:
AudioGeneratorMIDI() {
freq = 44100;
freq = 22050;
running = false;
};
virtual ~AudioGeneratorMIDI() override {};
#if 0
bool SetSoundfont(AudioFileSource *newsf2) {
if (isRunning()) {
return false;
@@ -45,6 +48,14 @@ public:
MakeStreamFromAFS(sf2, &afsSF2);
return true;
}
#endif
bool SetSoundFont(tsf *t) {
if (isRunning()) {
return false;
}
_tsf = t;
return true;
}
bool SetSampleRate(int newfreq) {
if (isRunning()) {
return false;
@@ -62,10 +73,6 @@ public:
private:
int freq;
tsf *g_tsf;
struct tsf_stream buffer;
struct tsf_stream afsMIDI;
struct tsf_stream afsSF2;
AudioFileSource *sf2;
AudioFileSource *midi;
protected:
@@ -138,7 +145,6 @@ protected:
}; /* no more data left in this track */
/* portable string length */
int strlength(const char *str) {
int i;
@@ -172,24 +178,17 @@ protected:
unsigned long get_varlen(int *ptr);
void find_note(int tracknum);
void PrepareMIDI(AudioFileSource *src);
void PrepareMIDI();//AudioFileSource *src);
int PlayMIDI();
void StopMIDI();
// tsf_stream <-> AudioFileSource
static int afs_read(void *data, void *ptr, unsigned int size);
static int afs_tell(void *data);
static int afs_skip(void *data, unsigned int count);
static int afs_seek(void *data, unsigned int pos);
static int afs_close(void *data);
static int afs_size(void *data);
void MakeStreamFromAFS(AudioFileSource *src, tsf_stream *afs);
int samplesToPlay;
bool sawEOF;
int numSamplesRendered;
int sentSamplesRendered ;
short samplesRendered[256];
short samplesRendered[256 * 2];
tsf *_tsf = nullptr;
};
#endif //__GNUC__ == 8
-1
View File
@@ -64,4 +64,3 @@ protected:
};
#endif
+1 -1
View File
@@ -69,7 +69,7 @@ bool AudioOutputPWM::begin() {
}
bool AudioOutputPWM::ConsumeSample(int16_t sample[2]) {
//Serial.printf("%d\n", sample[0]);
if (!pwmOn) {
return false;
}
File diff suppressed because it is too large. Load diff
+1 -2
View File
@@ -1,5 +1,4 @@
Copyright (C) 2017 Bernhard Schelling
Based on SFZero, Copyright (C) 2012 Steve Folta (https://github.com/stevefolta/SFZero)
Copyright (C) 2017-2023 Bernhard Schelling (Based on SFZero, Copyright (C) 2012 Steve Folta, https://github.com/stevefolta/SFZero)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
-40
View File
@@ -1,40 +0,0 @@
This is a ported and significantly modified version of TinySoundFont by
Bernhard Schelling. https://github.com/schellingb/TinySoundFont
TinySoundFont lets you use a SoundFont2 library (sampled MIDI instruments)
to play high quality music with a single #include.
Porting and modifications done by Earle F. Philhower, III
<earlephilhower@yahoo.com> and released under the GPL v3 or later.
The changes to allow it to work on the ESP8266 consisted mostly in making
it significantly less memory intensive and speeding up the inner loops by
using fixed point arithmetic instead of floating point. This change to
fixed point lets us use simple integer math, which is massively faster than
the software FP libraries on the ESP8266, but does come at the cost of a
slightly increased noise floor.
On the memory side, all data structures were converted into a "lazy"
allocation which means that instead of loading the full contents of the
header bits into RAM (which is often WAY more than the 40KB available on
the EPS8266), only those bits needed are loaded, and only when they are
needed.
Also, instead of loading the entire sample array into memory (i.e. nearly
500MB for a good piano sample), only a very small portion is loaded into a
LRU *cache* area.
Even with the caching, it was found that SPIFFS, while having great
functionality, was horrbly slow. So I wrote a new "faster" ROM filesystem
called, surprisingly, FastROMFilesystem.
https://github.com/earlephilhower/ESP8266FastROMFS
If you are getting choppy playback, try this new filesystem or using a SD
card (not tested by myself, but it'd be hard top be slower than SPIFFS).
Simply going from SPIFFS to FastROMFilesystem took my testing of
FURELISE.MID and 1MGM.SF2 from 0.5x realtime to 2.5x (i.e. from unusable
stuttering to plenty of time to do other tings on the ESP8266) for the
first 20 seconds or so tested.
-Earle F. Philhower, III
earlephilhower@yahoo.com
@@ -0,0 +1,43 @@
# Fixed-point MIDI ROM-wavetable synthesis
This branch was ported to ESP8266Audio by Earle F. Philhower, III <earlephilhower@yahoo.com>
from the original TinySoundFont by https://github.com/schellingb/TinySoundFont
Changes include converting a large portion of the operation to fixed-point
arithmetic and adding in a special ROM-mode where the SoundFont can be
stored inside a microcontroller ROM w/o needing a filesystem. This saves
RAM and makes the code faster than having to page in little bits of
waveforms or regions.
## Building ROM-wavetable structures
The SoundFont is stored in ROM using the normal C structures TSF uses, so it
requires a conversion from raw SF2 file into a header with structure members.
Do NOT use `xxd` or similar on the SoundFont, it won't work.
In the `examples` directory, run `build-linux-gcc`to compile the SF2-to-H
converted. Run it as follows:
````
$ ./dump-linux-x86_64 INPUT.SF2 OUTPUT.H
````
For example
````
$ ./# dump-linux-x86_64 Scratch2010.sf2 scratch2010.h
````
## Building the F16P16 tables
This port implements a few functions that normally require floating point
math with piecewise linear approximations. They are hardcoded in the TSF.
file already, but to regenerate them just run `make_f16p16_tables`.
## Performance
On devices that can perform hardware single-precision FP (i.e. the Pico 2,
original ESP32, others) this code can generate up to 44.1khz sounds with
over a dozen simultaneous voices without underflow and leaving some CPU
for other work.
On devices without an FPU, sample rates of up to 22050hz are possible,
depending on the MIDI and SF2 used.
+37 -37
View File
@@ -1,37 +1,37 @@
# TinySoundFont
SoundFont2 synthesizer library in a single C/C++ file
## Overview
TinySoundFont is a software synthesizer using SoundFont2 sound bank files.
The library is a single C header file so it is extremely simple to integrate in your C/C++ projects.
```c++
#define TSF_IMPLEMENTATION
#include "tsf.h"
...
tsf* TinySoundFont = tsf_load_filename("soundfont.sf2");
tsf_set_output(TinySoundFont, TSF_MONO, 44100, 0); //sample rate
tsf_note_on(TinySoundFont, 0, 60, 1.0f); //preset 0, middle C
short HalfSecond[22050]; //synthesize 0.5 seconds
tsf_render_short(TinySoundFont, HalfSecond, 22050, 0);
```
The library code is based on [SFZero by Steve Folta](https://github.com/stevefolta/SFZero).
## Documentation
The API documentation can be found on [top of the library source code](https://github.com/schellingb/TinySoundFont/blob/master/tsf.h).
There are also [examples available](https://github.com/schellingb/TinySoundFont/tree/master/examples) which come with a sample SoundFont file and build and play sound on Win32, Win64, Linux and MacOSX with no further dependencies.
## Dependencies
C standard libraries for fopen, math and malloc (can be removed by providing custom functions with #defines).
## License
TinySoundFont is available under the [MIT license](https://choosealicense.com/licenses/mit/).
# TinySoundFont
SoundFont2 synthesizer library in a single C/C++ file
## Overview
TinySoundFont is a software synthesizer using SoundFont2 sound bank files.
The library is a single C header file so it is extremely simple to integrate in your C/C++ projects.
```c++
#define TSF_IMPLEMENTATION
#include "tsf.h"
...
tsf* TinySoundFont = tsf_load_filename("soundfont.sf2");
tsf_set_output(TinySoundFont, TSF_MONO, 44100, 0); //sample rate
tsf_note_on(TinySoundFont, 0, 60, 1.0f); //preset 0, middle C
short HalfSecond[22050]; //synthesize 0.5 seconds
tsf_render_short(TinySoundFont, HalfSecond, 22050, 0);
```
The library code is based on [SFZero by Steve Folta](https://github.com/stevefolta/SFZero).
## Documentation
The API documentation can be found on [top of the library source code](https://github.com/schellingb/TinySoundFont/blob/master/tsf.h).
There are also [examples available](https://github.com/schellingb/TinySoundFont/tree/master/examples) which come with a sample SoundFont file and build and play sound on Win32, Win64, Linux and MacOSX with no further dependencies.
## Dependencies
C standard libraries for fopen, math and malloc (can be removed by providing custom functions with #defines).
## License
TinySoundFont is available under the [MIT license](https://choosealicense.com/licenses/mit/).
File diff suppressed because it is too large. Load diff
+2530 -2492
View File
File diff suppressed because it is too large. Load diff