diff --git a/examples/PlayMIDIFromFastROMFS/PlayMIDIFromFastROMFS.ino b/examples/PlayMIDIFromFastROMFS/PlayMIDIFromFastROMFS.ino new file mode 100644 index 0000000..392c8a6 --- /dev/null +++ b/examples/PlayMIDIFromFastROMFS/PlayMIDIFromFastROMFS.ino @@ -0,0 +1,52 @@ +#include +#include + +#include +#include +#include +#include + +AudioFileSourceFastROMFS *sf2; +AudioFileSourceFastROMFS *mid; +AudioOutputI2SDAC *dac; +AudioGeneratorMIDI *midi; +FastROMFilesystem *fs; + +void setup() +{ + const char *soundfont = "1mgm.sf2"; + const char *midifile = "furelise.mid"; + + WiFi.forceSleepBegin(); + + Serial.begin(115200); + Serial.println("Starting up...\n"); + + fs = new FastROMFilesystem(); + fs->mount(); + + sf2 = new AudioFileSourceFastROMFS(fs, soundfont); + mid = new AudioFileSourceFastROMFS(fs, midifile); + + dac = new AudioOutputI2SDAC(); + midi = new AudioGeneratorMIDI(); + midi->SetSoundfont(sf2); + midi->SetSampleRate(22050); + Serial.printf("BEGIN...\n"); + midi->begin(mid, dac); +} + +void loop() +{ + if (midi->isRunning()) { + if (!midi->loop()) { + uint32_t e = millis(); + midi->stop(); + } + } else { + Serial.printf("MIDI done\n"); + delay(1000); + } +} + + diff --git a/examples/PlayMIDIFromFastROMFS/data/1mgm.sf2 b/examples/PlayMIDIFromFastROMFS/data/1mgm.sf2 new file mode 100644 index 0000000..00862e9 Binary files /dev/null and b/examples/PlayMIDIFromFastROMFS/data/1mgm.sf2 differ diff --git a/examples/PlayMIDIFromFastROMFS/data/furelise.mid b/examples/PlayMIDIFromFastROMFS/data/furelise.mid new file mode 100644 index 0000000..b50146c Binary files /dev/null and b/examples/PlayMIDIFromFastROMFS/data/furelise.mid differ diff --git a/keywords.txt b/keywords.txt index b66a009..a9f0c95 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,6 +7,7 @@ AudioFileSourceSD KEYWORD1 AudioGenerator KEYWORD1 AudioGeneratorFLAC KEYWORD1 AudioGeneratorMOD KEYWORD1 +AudioGeneratorMIDI KEYWORD1 AudioGeneratorMP3 KEYWORD1 AudioGeneratorWAV KEYWORD1 AudioOutput KEYWORD1 diff --git a/src/AudioGeneratorMIDI.cpp b/src/AudioGeneratorMIDI.cpp new file mode 100644 index 0000000..6f622da --- /dev/null +++ b/src/AudioGeneratorMIDI.cpp @@ -0,0 +1,628 @@ +/* + AudioGeneratorMIDI + Audio output generator that plays MIDI files using a SF2 SoundFont + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + The MIDI processing engine is a heavily modified version of MIDITONES, + by Len Shustek, https://github.com/LenShustek/miditones . + Whereas MIDITONES original simply parsed a file beforehand to a byte + stream to be played by another program, this does the parsing and + playback in real-time. + + Here's his original header/readme w/MIT license, which is subsumed by the + GPL license of the ESP8266Audio project. +*/ + +/*************************************************************************** + + MIDITONES: Convert a MIDI file into a simple bytestream of notes + + ------------------------------------------------------------------------- + The MIT License (MIT) + Copyright (c) 2011,2013,2015,2016, Len Shustek + + 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 the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR + IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +**************************************************************************/ + + +#include "AudioGeneratorMIDI.h" + +#pragma GCC optimize ("O3") + +#define TSF_NO_STDIO +#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) +{ + int ptr; + Serial.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); + Serial.printf((ptr + i) == curpos ? " [%02X] " : "%02X ", (int) c & 0xff); + } + Serial.printf("\n"); + running = false; +} + +/* check that we have a specified number of bytes left in the buffer */ + +void AudioGeneratorMIDI::chk_bufdata (int ptr, unsigned long int len) { + if ((unsigned) (ptr + len) > buflen) + midi_error ("data missing", ptr); +} + +/* fetch big-endian numbers */ + +uint16_t AudioGeneratorMIDI::rev_short (uint16_t val) { + return ((val & 0xff) << 8) | ((val >> 8) & 0xff); +} + +uint32_t AudioGeneratorMIDI::rev_long (uint32_t val) { + return (((rev_short ((uint16_t) val) & 0xffff) << 16) | + (rev_short ((uint16_t) (val >> 16)) & 0xffff)); +} + +/************** process the MIDI file header *****************/ + +void AudioGeneratorMIDI::process_header (void) { + struct midi_header hdr; + unsigned int time_division; + + chk_bufdata (hdrptr, sizeof (struct midi_header)); + buffer.seek (buffer.data, hdrptr); + buffer.read (buffer.data, &hdr, sizeof (hdr)); + if (!charcmp ((char *) hdr.MThd, "MThd")) + midi_error ("Missing 'MThd'", hdrptr); + num_tracks = rev_short (hdr.number_of_tracks); + time_division = rev_short (hdr.time_division); + if (time_division < 0x8000) + ticks_per_beat = time_division; + else + ticks_per_beat = ((time_division >> 8) & 0x7f) /* SMTE frames/sec */ *(time_division & 0xff); /* ticks/SMTE frame */ + hdrptr += rev_long (hdr.header_size) + 8; /* point past header to track header, presumably. */ + return; +} + + +/**************** Process a MIDI track header *******************/ + +void AudioGeneratorMIDI::start_track (int tracknum) { + struct track_header hdr; + unsigned long tracklen; + + chk_bufdata (hdrptr, sizeof (struct track_header)); + buffer.seek (buffer.data, hdrptr); + buffer.read (buffer.data, &hdr, sizeof (hdr)); + if (!charcmp ((char *) (hdr.MTrk), "MTrk")) + midi_error ("Missing 'MTrk'", hdrptr); + tracklen = rev_long (hdr.track_size); + hdrptr += sizeof (struct track_header); /* point past header */ + chk_bufdata (hdrptr, tracklen); + track[tracknum].trkptr = hdrptr; + hdrptr += tracklen; /* point to the start of the next track */ + track[tracknum].trkend = hdrptr; /* the point past the end of the track */ +} + +unsigned char AudioGeneratorMIDI::buffer_byte (int offset) { + unsigned char c; + buffer.seek (buffer.data, offset); + 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)); + return s; +} + +unsigned int AudioGeneratorMIDI::buffer_int32 (int offset) { + uint32_t i; + buffer.seek (buffer.data, offset); + buffer.read (buffer.data, &i, sizeof (i)); + return i; +} + +/* Get a MIDI-style variable-length integer */ + +unsigned long AudioGeneratorMIDI::get_varlen (int *ptr) { + /* Get a 1-4 byte variable-length value and adjust the pointer past it. + These are a succession of 7-bit values with a MSB bit of zero marking the end */ + + unsigned long val; + int i, byte; + + val = 0; + for (i = 0; i < 4; ++i) { + byte = buffer_byte ((*ptr)++); + val = (val << 7) | (byte & 0x7f); + if (!(byte & 0x80)) + return val; + } + return val; +} + + +/*************** Process the MIDI track data ***************************/ + +/* Skip in the track for the next "note on", "note off" or "set tempo" command, + then record that information in the track status block and return. */ + +void AudioGeneratorMIDI::find_note (int tracknum) { + unsigned long int delta_time; + int event, chan; + int note, velocity, controller, pressure, pitchbend, instrument; + int meta_cmd, meta_length; + unsigned long int sysex_length; + struct track_status *t; + const char *tag; + + /* process events */ + + t = &track[tracknum]; /* our track status structure */ + while (t->trkptr < t->trkend) { + + delta_time = get_varlen (&t->trkptr); + t->time += delta_time; + if (buffer_byte (t->trkptr) < 0x80) + event = t->last_event; /* using "running status": same event as before */ + else { /* otherwise get new "status" (event type) */ + event = buffer_byte (t->trkptr++); + } + if (event == 0xff) { /* meta-event */ + meta_cmd = buffer_byte (t->trkptr++); + meta_length = buffer_byte (t->trkptr++); + switch (meta_cmd) { + case 0x00: + break; + case 0x01: + tag = "description"; + goto show_text; + case 0x02: + tag = "copyright"; + goto show_text; + case 0x03: + tag = "track name"; + goto show_text; + case 0x04: + tag = "instrument name"; + goto show_text; + case 0x05: + tag = "lyric"; + goto show_text; + case 0x06: + tag = "marked point"; + goto show_text; + case 0x07: + tag = "cue point"; +show_text: + break; + case 0x20: + break; + case 0x2f: + break; + case 0x51: /* tempo: 3 byte big-endian integer! */ + t->cmd = CMD_TEMPO; + t->tempo = rev_long (buffer_int32 (t->trkptr - 1)) & 0xffffffL; + t->trkptr += meta_length; + return; + case 0x54: + break; + case 0x58: + break; + case 0x59: + break; + case 0x7f: + tag = "sequencer data"; + goto show_hex; + default: /* unknown meta command */ + tag = "???"; +show_hex: + break; + } + t->trkptr += meta_length; + } + + else if (event < 0x80) + midi_error ("Unknown MIDI event type", t->trkptr); + + else { + if (event < 0xf0) + t->last_event = event; // remember "running status" if not meta or sysex event + chan = event & 0xf; + t->chan = chan; + switch (event >> 4) { + case 0x8: + t->note = buffer_byte (t->trkptr++); + velocity = buffer_byte (t->trkptr++); +note_off: + t->cmd = CMD_STOPNOTE; + return; /* stop processing and return */ + case 0x9: + t->note = buffer_byte (t->trkptr++); + velocity = buffer_byte (t->trkptr++); + if (velocity == 0) /* some scores use note-on with zero velocity for off! */ + goto note_off; + t->velocity = velocity; + t->cmd = CMD_PLAYNOTE; + return; /* stop processing and return */ + case 0xa: + note = buffer_byte (t->trkptr++); + velocity = buffer_byte (t->trkptr++); + break; + case 0xb: + controller = buffer_byte (t->trkptr++); + velocity = buffer_byte (t->trkptr++); + break; + case 0xc: + instrument = buffer_byte (t->trkptr++); + midi_chan_instrument[chan] = instrument; // record new instrument for this channel + break; + case 0xd: + pressure = buffer_byte (t->trkptr++); + break; + case 0xe: + pitchbend = buffer_byte (t->trkptr) | (buffer_byte (t->trkptr + 1) << 7); + t->trkptr += 2; + break; + case 0xf: + sysex_length = get_varlen (&t->trkptr); + t->trkptr += sysex_length; + break; + default: + midi_error ("Unknown MIDI command", t->trkptr); + } + } + } + t->cmd = CMD_TRACKDONE; /* no more notes to process */ + ++tracks_done; + + // Remove unused warnings..maybe some day we'll look at these + (void)note; + (void)controller; + (void)pressure; + (void)pitchbend; + (void)tag; +} + + +// Open file, parse headers, get ready tio process MIDI +void AudioGeneratorMIDI::PrepareMIDI(AudioFileSource *src) +{ + MakeStreamFromAFS(src, &afsMIDI); + tsf_stream_wrap_cached(&afsMIDI, 32, 64, &buffer); + buflen = buffer.size (buffer.data); + + /* process the MIDI file header */ + + hdrptr = 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)); + + /* initialize processing of all the tracks */ + + for (tracknum = 0; tracknum < num_tracks; ++tracknum) { + start_track (tracknum); /* process the track header */ + find_note (tracknum); /* position to the first note on/off */ + } + + notes_skipped = 0; + tracknum = 0; + earliest_tracknum = 0; + earliest_time = 0; +} + +// Parses the note on/offs ujntil we are ready to render some more samples. Then return the +// total number of samples to render before we need to be called again +int AudioGeneratorMIDI::PlayMIDI() +{ + /* Continue processing all tracks, in an order based on the simulated time. + This is not unlike multiway merging used for tape sorting algoritms in the 50's! */ + + do { /* while there are still track notes to process */ + static struct track_status *trk; + static struct tonegen_status *tg; + static int tgnum; + static int count_tracks; + static unsigned long delta_time, delta_msec; + + /* Find the track with the earliest event time, + and output a delay command if time has advanced. + + A potential improvement: If there are multiple tracks with the same time, + first do the ones with STOPNOTE as the next command, if any. That would + help avoid running out of tone generators. In practice, though, most MIDI + files do all the STOPNOTEs first anyway, so it won't have much effect. + */ + + earliest_time = 0x7fffffff; + + /* Usually we start with the track after the one we did last time (tracknum), + so that if we run out of tone generators, we have been fair to all the tracks. + The alternate "strategy1" says we always start with track 0, which means + that we favor early tracks over later ones when there aren't enough tone generators. + */ + + count_tracks = num_tracks; + do { + if (++tracknum >= num_tracks) + tracknum = 0; + trk = &track[tracknum]; + if (trk->cmd != CMD_TRACKDONE && trk->time < earliest_time) { + earliest_time = trk->time; + earliest_tracknum = tracknum; + } + } while (--count_tracks); + + tracknum = earliest_tracknum; /* the track we picked */ + trk = &track[tracknum]; + if (earliest_time < timenow) + midi_error ("INTERNAL: time went backwards", trk->trkptr); + + /* If time has advanced, output a "delay" command */ + + delta_time = earliest_time - timenow; + if (delta_time) { + /* Convert ticks to milliseconds based on the current tempo */ + unsigned long long temp; + temp = ((unsigned long long) delta_time * tempo) / ticks_per_beat; + delta_msec = temp / 1000; // get around LCC compiler bug + if (delta_msec > 0x7fff) + midi_error ("INTERNAL: time delta too big", trk->trkptr); + int samples = (((int) delta_msec) * freq) / 1000; + timenow = earliest_time; + return samples; + } + timenow = earliest_time; + + /* If this track event is "set tempo", just change the global tempo. + That affects how we generate "delay" commands. */ + + if (trk->cmd == CMD_TEMPO) { + tempo = trk->tempo; + find_note (tracknum); + } + + /* If this track event is "stop note", process it and all subsequent "stop notes" for this track + that are happening at the same time. Doing so frees up as many tone generators as possible. */ + + else if (trk->cmd == CMD_STOPNOTE) + do { + // stop a note + 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 (g_tsf, tg->instrument, tg->note); + tg->playing = false; + trk->tonegens[tgnum] = false; + } + } + find_note (tracknum); // use up the note + } while (trk->cmd == CMD_STOPNOTE && trk->time == timenow); + + /* If this track event is "start note", process only it. + Don't do more than one, so we allow other tracks their chance at grabbing tone generators. */ + + else if (trk->cmd == CMD_PLAYNOTE) { + bool foundgen = false; + /* if not, then try for any free tone generator */ + if (!foundgen) + for (tgnum = 0; tgnum < num_tonegens; ++tgnum) { + tg = &tonegen[tgnum]; + if (!tg->playing) { + foundgen = true; + break; + } + } + if (foundgen) { + if (tgnum + 1 > num_tonegens_used) + num_tonegens_used = tgnum + 1; + tg->playing = true; + tg->track = tracknum; + tg->note = trk->note; + trk->tonegens[tgnum] = true; + trk->preferred_tonegen = tgnum; + if (tg->instrument != midi_chan_instrument[trk->chan]) { /* new instrument for this generator */ + tg->instrument = midi_chan_instrument[trk->chan]; + } + tsf_note_on (g_tsf, tg->instrument, tg->note, trk->velocity / 256.0); + } else { + ++notes_skipped; + } + find_note (tracknum); // use up the note + } + } + while (tracks_done < num_tracks); + return -1; // EOF +} + + +void AudioGeneratorMIDI::StopMIDI() +{ + + buffer.close(buffer.data); + 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"); +} + + +bool AudioGeneratorMIDI::begin(AudioFileSource *src, AudioOutput *out) +{ + // Clear out status variables + for (int i=0; iSetRate( freq )) return false; + if (!out->SetBitsPerSample( 16 )) return false; + if (!out->SetChannels( 1 )) return false; + if (!out->begin()) return false; + + output = out; + + running = true; + + PrepareMIDI(src); + + samplesToPlay = 0; + numSamplesRendered = 0; + sentSamplesRendered = 0; + + sawEOF = false; + return running; +} + + +bool AudioGeneratorMIDI::loop() +{ + static int c = 0; + if (!running) return true; // Nothing to do here! + + // First, try and push in the stored sample. If we can't, then punt and try later + if (!output->ConsumeSample(lastSample)) return true; // Can't send, but no error detected + + // Try and stuff the buffer one sample at a time + do { + c++; + if (c%44100 == 0) yield(); + +play: + + if (sentSamplesRendered < numSamplesRendered) { + lastSample[AudioOutput::LEFTCHANNEL] = samplesRendered[sentSamplesRendered]; + lastSample[AudioOutput::RIGHTCHANNEL] = samplesRendered[sentSamplesRendered]; + sentSamplesRendered++; + } else if (samplesToPlay) { + numSamplesRendered = sizeof(samplesRendered)/sizeof(samplesRendered[0]); + if ((int)samplesToPlay < (int)(sizeof(samplesRendered)/sizeof(samplesRendered[0]))) numSamplesRendered = samplesToPlay; + tsf_render_short_fast(g_tsf, samplesRendered, numSamplesRendered, 0); + lastSample[AudioOutput::LEFTCHANNEL] = samplesRendered[0]; + lastSample[AudioOutput::RIGHTCHANNEL] = samplesRendered[0]; + sentSamplesRendered = 1; + samplesToPlay -= numSamplesRendered; + } else { + numSamplesRendered = 0; + sentSamplesRendered = 0; + if (sawEOF) { + running = false; + } else { + samplesToPlay = PlayMIDI(); + if (samplesToPlay == -1) { + sawEOF = true; + samplesToPlay = freq / 2; + } + goto play; + } + } + } while (running && output->ConsumeSample(lastSample)); + + return running; +} + +bool AudioGeneratorMIDI::stop() +{ + return true; +} + + +int AudioGeneratorMIDI::afs_read(void *data, void *ptr, unsigned int size) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->read(ptr, size); +} + +int AudioGeneratorMIDI::afs_tell(void *data) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->getPos(); +} + +int AudioGeneratorMIDI::afs_skip(void *data, unsigned int count) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->seek(count, SEEK_CUR); +} + +int AudioGeneratorMIDI::afs_seek(void *data, unsigned int pos) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->seek(pos, SEEK_SET); +} + +int AudioGeneratorMIDI::afs_close(void *data) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->close(); +} + +int AudioGeneratorMIDI::afs_size(void *data) +{ + AudioFileSource *s = reinterpret_cast(data); + return s->getSize(); +} + +void AudioGeneratorMIDI::MakeStreamFromAFS(AudioFileSource *src, tsf_stream *afs) +{ + afs->data = reinterpret_cast(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; +} + diff --git a/src/AudioGeneratorMIDI.h b/src/AudioGeneratorMIDI.h new file mode 100644 index 0000000..fb93dbb --- /dev/null +++ b/src/AudioGeneratorMIDI.h @@ -0,0 +1,181 @@ +/* + AudioGeneratorMIDI + Audio output generator that plays MIDI files using a SF2 SoundFont + + Copyright (C) 2017 Earle F. Philhower, III + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _AUDIOGENERATORMOD_H +#define _AUDIOGENERATORMOD_H + +#include "AudioGenerator.h" + +#define TSF_NO_STDIO +#include "libtinysoundfont/tsf.h" + +class AudioGeneratorMIDI : AudioGenerator +{ + public: + AudioGeneratorMIDI() { freq=44100; }; + virtual ~AudioGeneratorMIDI() override {}; + bool SetSoundfont(AudioFileSource *newsf2) { + if (isRunning()) return false; + sf2 = newsf2; + MakeStreamFromAFS(sf2, &afsSF2); + return true; + } + bool SetSampleRate(int newfreq) { + if (isRunning()) return false; + freq = newfreq; + return true; + } + virtual bool begin(AudioFileSource *mid, AudioOutput *output) override; + virtual bool loop() override; + virtual bool stop() override; + virtual bool isRunning() override { return running; }; + + private: + int freq; + tsf *g_tsf; + struct tsf_stream buffer; + struct tsf_stream afsMIDI; + struct tsf_stream afsSF2; + AudioFileSource *sf2; + AudioFileSource *midi; + + protected: + struct midi_header { + int8_t MThd[4]; + uint32_t header_size; + uint16_t format_type; + uint16_t number_of_tracks; + uint16_t time_division; + }; + + struct track_header { + int8_t MTrk[4]; + uint32_t track_size; + }; + + enum { MAX_TONEGENS = 32, /* max tone generators: tones we can play simultaneously */ + MAX_TRACKS = 24 + }; /* max number of MIDI tracks we will process */ + + int hdrptr; + unsigned long buflen; + int num_tracks; + int tracks_done = 0; + int num_tonegens = MAX_TONEGENS; + int num_tonegens_used = 0; + unsigned int ticks_per_beat = 240; + unsigned long timenow = 0; + unsigned long tempo; /* current tempo in usec/qnote */ + // State needed for PlayMID() + int notes_skipped = 0; + int tracknum = 0; + int earliest_tracknum = 0; + unsigned long earliest_time = 0; + + struct tonegen_status { /* current status of a tone generator */ + bool playing; /* is it playing? */ + char track; /* if so, which track is the note from? */ + char note; /* what note is playing? */ + char instrument; /* what instrument? */ + } tonegen[MAX_TONEGENS]; + + struct track_status { /* current processing point of a MIDI track */ + int trkptr; /* ptr to the next note change */ + int trkend; /* ptr past the end of the track */ + unsigned long time; /* what time we're at in the score */ + unsigned long tempo; /* the tempo last set, in usec per qnote */ + unsigned int preferred_tonegen; /* for strategy2, try to use this generator */ + unsigned char cmd; /* CMD_xxxx next to do */ + unsigned char note; /* for which note */ + unsigned char chan; /* from which channel it was */ + unsigned char velocity; /* the current volume */ + unsigned char last_event; /* the last event, for MIDI's "running status" */ + bool tonegens[MAX_TONEGENS]; /* which tone generators our notes are playing on */ + } track[MAX_TRACKS]; + + int midi_chan_instrument[16]; /* which instrument is currently being played on each channel */ + + /* output bytestream commands, which are also stored in track_status.cmd */ + enum { CMD_PLAYNOTE = 0x90, /* play a note: low nibble is generator #, note is next byte */ + CMD_STOPNOTE = 0x80, /* stop a note: low nibble is generator # */ + CMD_INSTRUMENT = 0xc0, /* change instrument; low nibble is generator #, instrument is next byte */ + CMD_RESTART = 0xe0, /* restart the score from the beginning */ + CMD_STOP = 0xf0, /* stop playing */ + CMD_TEMPO = 0xFE, /* tempo in usec per quarter note ("beat") */ + CMD_TRACKDONE = 0xFF + }; /* no more data left in this track */ + + + + /* portable string length */ + int strlength (const char *str) { + int i; + for (i = 0; str[i] != '\0'; ++i); + return i; + } + + + /* match a constant character sequence */ + + int charcmp (const char *buf, const char *match) { + int len, i; + len = strlength (match); + for (i = 0; i < len; ++i) + if (buf[i] != match[i]) + return 0; + return 1; + } + + unsigned char buffer_byte (int offset); + unsigned short buffer_short (int offset); + unsigned int buffer_int32 (int offset); + + void midi_error (const char *msg, int curpos); + void chk_bufdata (int ptr, unsigned long int len); + uint16_t rev_short (uint16_t val); + uint32_t rev_long (uint32_t val); + void process_header (void); + void start_track (int tracknum); + + unsigned long get_varlen (int *ptr); + void find_note (int tracknum); + 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]; +}; + + +#endif + diff --git a/src/libtinysoundfont/LICENSE b/src/libtinysoundfont/LICENSE new file mode 100644 index 0000000..066642d --- /dev/null +++ b/src/libtinysoundfont/LICENSE @@ -0,0 +1,20 @@ +Copyright (C) 2017 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 +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/libtinysoundfont/README.ESP8266 b/src/libtinysoundfont/README.ESP8266 new file mode 100644 index 0000000..e88104d --- /dev/null +++ b/src/libtinysoundfont/README.ESP8266 @@ -0,0 +1,40 @@ +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 + 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 filesytem 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 + diff --git a/src/libtinysoundfont/README.md b/src/libtinysoundfont/README.md new file mode 100644 index 0000000..7e030ec --- /dev/null +++ b/src/libtinysoundfont/README.md @@ -0,0 +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/). diff --git a/src/libtinysoundfont/tsf.h b/src/libtinysoundfont/tsf.h new file mode 100644 index 0000000..6ca0e24 --- /dev/null +++ b/src/libtinysoundfont/tsf.h @@ -0,0 +1,1638 @@ +/* TinySoundFont - v0.7 - SoundFont2 synthesizer - https://github.com/schellingb/TinySoundFont + no warranty implied; use at your own risk + Do this: + #define TSF_IMPLEMENTATION + before you include this file in *one* C or C++ file to create the implementation. + // i.e. it should look like this: + #include ... + #include ... + #define TSF_IMPLEMENTATION + #include "tsf.h" + + [OPTIONAL] #define TSF_NO_STDIO to remove stdio dependency + [OPTIONAL] #define TSF_MALLOC, TSF_REALLOC, and TSF_FREE to avoid stdlib.h + [OPTIONAL] #define TSF_MEMCPY, TSF_MEMSET to avoid string.h + [OPTIONAL] #define TSF_POW, TSF_POWF, TSF_EXPF, TSF_LOG, TSF_TAN, TSF_LOG10, TSF_SQRT to avoid math.h + + NOT YET IMPLEMENTED + - Lower level voice interface to render single voices/presets + - Support for ChorusEffectsSend and ReverbEffectsSend generators + - Better low-pass filter without lowering performance too much + - Support for modulators + + LICENSE (MIT) + + Copyright (C) 2017 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 the Software + without restriction, including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons + to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#ifndef TSF_INCLUDE_TSF_INL +#define TSF_INCLUDE_TSF_INL + +#ifdef __cplusplus +extern "C" { +# define CPP_DEFAULT0 = 0 +#else +# define CPP_DEFAULT0 +#endif + +//define this if you want the API functions to be static +#ifdef TSF_STATIC +#define TSFDEF static +#else +#define TSFDEF extern +#endif + +// The load functions will return a pointer to a struct tsf which all functions +// thereafter take as the first parameter. +// On error the tsf_load* functions will return NULL most likely due to invalid +// data (or if the file did not exist in tsf_load_filename). +typedef struct tsf tsf; + +#ifndef TSF_NO_STDIO +// Directly load a SoundFont from a .sf2 file path +TSFDEF tsf* tsf_load_filename(const char* filename); +#endif + +// Load a SoundFont from a block of memory +TSFDEF tsf* tsf_load_memory(const void* buffer, int size); + +// Stream structure for the generic loading +struct tsf_stream +{ + // Custom data given to the functions as the first parameter + void* data; + + // Function pointer will be called to read 'size' bytes into ptr + int (*read)(void* data, void* ptr, unsigned int size); + + // Function pointer will be called to skip ahead over 'count' bytes + int (*tell)(void* data); + + // Function pointer will be called to skip ahead over 'count' bytes + int (*skip)(void* data, unsigned int count); + + // Function pointer will be called to seek to absolute position + int (*seek)(void* data, unsigned int pos); + + // Function pointer will be called to skip ahead over 'count' bytes + int (*close)(void* data); + + // Function pointer will be called to skip ahead over 'count' bytes + int (*size)(void* data); +}; + +// Generic SoundFont loading method using the stream structure above +TSFDEF tsf* tsf_load(struct tsf_stream* stream); + +// Free the memory related to this tsf instance +TSFDEF void tsf_close(tsf* f); + +// Returns the number of presets in the loaded SoundFont +TSFDEF int tsf_get_presetcount(tsf* f); + +// Returns the name of a preset index >= 0 and < tsf_get_presetcount() +TSFDEF const char* tsf_get_presetname(tsf* f, int preset); + +// Supported output modes by the render methods +enum TSFOutputMode +{ + // Two channels with single left/right samples one after another + TSF_STEREO_INTERLEAVED, + // Two channels with all samples for the left channel first then right + TSF_STEREO_UNWEAVED, + // A single channel (stereo instruments are mixed into center) + TSF_MONO +}; + +// Thread safety: +// Your audio output which calls the tsf_render* functions will most likely +// run on a different thread than where the playback tsf_note* functions +// are called. In which case some sort of concurrency control like a +// mutex needs to be used so they are not called at the same time. + +// Setup the parameters for the voice render methods +// outputmode: if mono or stereo and how stereo channel data is ordered +// samplerate: the number of samples per second (output frequency) +// globalgaindb: volume gain in decibels (>0 means higher, <0 means lower) +TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float globalgaindb CPP_DEFAULT0); + +// Start playing a note +// preset: preset index >= 0 and < tsf_get_presetcount() +// key: note value between 0 and 127 (60 being middle C) +// vel: velocity as a float between 0.0 (equal to note off) and 1.0 (full) +TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel); + +// Stop playing a note +TSFDEF void tsf_note_off(tsf* f, int preset, int key); + +// Render output samples into a buffer +// You can either render as signed 16-bit values (tsf_render_short) or +// as 32-bit float values (tsf_render_float) +// buffer: target buffer of size samples * output_channels * sizeof(type) +// samples: number of samples to render +// flag_mixing: if 0 clear the buffer first, otherwise mix into existing data +TSFDEF void tsf_render_short(tsf* f, short* buffer, int samples, int flag_mixing CPP_DEFAULT0); +TSFDEF void tsf_render_float(tsf* f, float* buffer, int samples, int flag_mixing CPP_DEFAULT0); + +#ifdef __cplusplus +# undef CPP_DEFAULT0 +} +#endif + +// end header +// --------------------------------------------------------------------------------------------------------- +#endif //TSF_INCLUDE_TSF_INL + +#ifdef TSF_IMPLEMENTATION + +// The lower this block size is the more accurate the effects are. +// Increasing the value significantly lowers the CPU usage of the voice rendering. +// If LFO affects the low-pass filter it can be hearable even as low as 8. +#ifndef TSF_RENDER_EFFECTSAMPLEBLOCK +#define TSF_RENDER_EFFECTSAMPLEBLOCK 64 +#endif + +// Grace release time for quick voice off (avoid clicking noise) +#define TSF_FASTRELEASETIME 0.01f +#if !defined(TSF_MALLOC) || !defined(TSF_FREE) || !defined(TSF_REALLOC) +# include +# define TSF_MALLOC malloc +# define TSF_FREE free +# define TSF_REALLOC realloc +#endif + +#if !defined(TSF_MEMCPY) || !defined(TSF_MEMSET) +# include +# define TSF_MEMCPY memcpy +# define TSF_MEMSET memset +#endif + +#if !defined(TSF_POW) || !defined(TSF_POWF) || !defined(TSF_EXPF) || !defined(TSF_LOG) || !defined(TSF_TAN) || !defined(TSF_LOG10) || !defined(TSF_SQRT) +# include +# if !defined(__cplusplus) && !defined(NAN) && !defined(powf) && !defined(expf) +# define powf (float)pow // deal with old math.h files that +# define expf (float)exp // come without powf and expf +# endif +# define TSF_POW pow +# define TSF_POWF powf +# define TSF_EXPF expf +# define TSF_LOG log +# define TSF_TAN tan +# define TSF_LOG10 log10 +# define TSF_SQRT sqrt +#endif + +#ifndef TSF_NO_STDIO +# include +#endif + +#define TSF_TRUE 1 +#define TSF_FALSE 0 +#define TSF_BOOL char +#define TSF_PI 3.14159265358979323846264338327950288 +#define TSF_NULL 0 + +#ifdef __cplusplus +extern "C" { +#endif + +typedef char tsf_fourcc[4]; +typedef signed char tsf_s8; +typedef unsigned char tsf_u8; +typedef unsigned short tsf_u16; +typedef signed short tsf_s16; +typedef unsigned int tsf_u32; +typedef char tsf_char20[20]; + +#define TSF_FourCCEquals(value1, value2) (value1[0] == value2[0] && value1[1] == value2[1] && value1[2] == value2[2] && value1[3] == value2[3]) + +// Samples cache, number and sample count +#define TSF_BUFFS 16 +#define TSF_BUFFSIZE 512 + +struct tsf +{ + struct tsf_preset* presets; + int presetNum; + + int fontSamplesOffset; + int fontSampleCount; + + struct tsf_voice *voices; + int voiceNum; + + float outSampleRate; + enum TSFOutputMode outputmode; + float globalGainDB; + + float* outputSamples; + int outputSampleSize; + + struct tsf_hydra *hydra; + + // Cached sample read + short *buffer[TSF_BUFFS]; + int offset[TSF_BUFFS]; + int timestamp[TSF_BUFFS]; + int epoch; +}; + +struct tsf_stream_cached_data { + int buffs; + int buffsize; + struct tsf_stream *stream; + unsigned int size, pos; + unsigned char **buffer; + unsigned int *offset; + unsigned int *timestamp; + unsigned int epoch; + unsigned int hit; + unsigned int miss; +}; + +static int tsf_stream_cached_read(void* v, void* ptr, unsigned int size) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + unsigned char *p = (unsigned char *)ptr; + + while (size) { + if (d->pos >= d->size) return 0; // EOF + for (int i=0; i < d->buffs; i++) { + if ((d->offset[i] <= d->pos) && ((d->offset[i] + d->buffsize) > d->pos) ) { + d->timestamp[i] = d->epoch++; + // Handle case of epoch rollover by just setting low, random epochs + if (d->epoch==0) { + for (int i=0; ibuffs; i++) d->timestamp[i] = d->epoch++; + } + unsigned int startOffset = d->pos - d->offset[i]; + unsigned int len = d->buffsize - (d->pos % d->buffsize); + if (len > size) len = size; + TSF_MEMCPY(p, &d->buffer[i][startOffset], len); + size -= len; + d->pos += len; + p += len; + d->hit++; + if (size == 0) return 1; + i = -1; // Restart the for() at block 0 after postincrement + } + } + + int repl = 0; + for (int i=1; i < d->buffs; i++) { + if (d->timestamp[i] < d->timestamp[repl]) repl = i; + } + int readOff = d->pos - (d->pos % d->buffsize); + d->stream->seek(d->stream->data, readOff); + d->stream->read(d->stream->data, d->buffer[repl], d->buffsize); + d->timestamp[repl] = d->epoch++; + d->offset[repl] = readOff; + d->miss++; + d->hit--; // Avoid counting this as a hit on next loop that returns data + // Don't actually do anything yet, we'll retry the search on next loop where it will notice new data + } + return 1; +} + +static int tsf_stream_cached_tell(void* v) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + return d->pos; +} + +static int tsf_stream_cached_size(void* v) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + return d->size; +} + +static int tsf_stream_cached_skip(void* v, unsigned int count) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + if ((d->pos + count) < d->size) { + d->pos += count; + return 1; + } + return 0; +} + +static int tsf_stream_cached_seek(void* v, unsigned int pos) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + if (pos < d->size) { + d->pos = pos; + return 1; + } + return 0; +} + +static int tsf_stream_cached_close(void* v) +{ + struct tsf_stream_cached_data *d = (struct tsf_stream_cached_data*)v; + free(d->timestamp); + free(d->offset); + for (int i = d->buffs - 1; i >=0; i--) free(d->buffer[i]); + free(d->buffer); + int ret = d->stream->close(d->stream->data); + free(d); + return ret; +} + +/* Wraps an existing stream with a caching layer. First create the stream you need, then + call this to create a new stream. Use this new stream only, and when done close() will + close both this stream as well as the wrapped one. */ +TSFDEF struct tsf_stream *tsf_stream_wrap_cached(struct tsf_stream *stream, int buffs, int buffsize, struct tsf_stream *dest) +{ + struct tsf_stream_cached_data *s = (struct tsf_stream_cached_data*)TSF_MALLOC(sizeof(*s)); + s->buffs = buffs; + s->buffsize = buffsize; + s->stream = stream; + s->size = stream->size(stream->data); + s->pos = stream->tell(stream->data); + s->buffer = (unsigned char **)TSF_MALLOC(sizeof(*s->buffer) * buffs); + s->offset = (unsigned int *)TSF_MALLOC(sizeof(s->offset) * buffs); + s->timestamp = (unsigned int *)TSF_MALLOC(sizeof(s->timestamp) * buffs); + s->epoch = 0; + s->hit = 0; + s->miss = 0; + for (int i=0; ibuffer[i] = (unsigned char *)TSF_MALLOC(buffsize); + s->offset[i] = 0xfffffff; + s->timestamp[i] = 0; + } + dest->data = (void*)s; + dest->read = &tsf_stream_cached_read; + dest->tell = &tsf_stream_cached_tell; + dest->skip = &tsf_stream_cached_skip; + dest->seek = &tsf_stream_cached_seek; + dest->size = &tsf_stream_cached_size; + dest->close = &tsf_stream_cached_close; + return dest; +} + + +#ifndef TSF_NO_STDIO +static int tsf_stream_stdio_read(FILE* f, void* ptr, unsigned int size) { return (int)fread(ptr, 1, size, f); } +static int tsf_stream_stdio_tell(FILE* f) { return ftell(f); } +static int tsf_stream_stdio_size(FILE* f) { int p = ftell(f); fseek(f, 0, SEEK_END); int e = ftell(f); fseek(f, p, SEEK_SET); return e; } +static int tsf_stream_stdio_skip(FILE* f, unsigned int count) { return !fseek(f, count, SEEK_CUR); } +static int tsf_stream_stdio_seek(FILE* f, unsigned int count) { return !fseek(f, count, SEEK_SET); } +static int tsf_stream_stdio_close(FILE* f) { return !fclose(f); } +TSFDEF tsf* tsf_load_filename(const char* filename) +{ + tsf* res; + struct tsf_stream stream = { TSF_NULL, (int(*)(void*,void*,unsigned int))&tsf_stream_stdio_read, (int(*)(void*))&tsf_stream_stdio_tell, (int(*)(void*,unsigned int))&tsf_stream_stdio_skip, (int(*)(void*,unsigned int))&tsf_stream_stdio_seek, (int(*)(void*))&tsf_stream_stdio_close, (int(*)(void*))&tsf_stream_stdio_size }; + #if __STDC_WANT_SECURE_LIB__ + FILE* f = TSF_NULL; fopen_s(&f, filename, "rb"); + #else + FILE* f = fopen(filename, "rb"); + #endif + if (!f) + { + //if (e) *e = TSF_FILENOTFOUND; + return TSF_NULL; + } + stream.data = f; + res = tsf_load(&stream); + //fclose(f); + return res; +} +#endif + +struct tsf_stream_memory { const char* buffer; unsigned int total, pos; }; +static int tsf_stream_memory_read(struct tsf_stream_memory* m, void* ptr, unsigned int size) { if (size > m->total - m->pos) size = m->total - m->pos; TSF_MEMCPY(ptr, m->buffer+m->pos, size); m->pos += size; return size; } +static int tsf_stream_memory_tell(struct tsf_stream_memory* m) { return m->pos; } +static int tsf_stream_memory_size(struct tsf_stream_memory* m) { return m->total; } +static int tsf_stream_memory_skip(struct tsf_stream_memory* m, unsigned int count) { if (m->pos + count > m->total) return 0; m->pos += count; return 1; } +static int tsf_stream_memory_seek(struct tsf_stream_memory* m, unsigned int pos) { if (pos > m->total) return 0; else m->pos = pos; return 1; } +static int tsf_stream_memory_close(struct tsf_stream_memory* m) { (void)m; return 1; } +TSFDEF tsf* tsf_load_memory(const void* buffer, int size) +{ + struct tsf_stream stream = { TSF_NULL, (int(*)(void*,void*,unsigned int))&tsf_stream_memory_read, (int(*)(void*))&tsf_stream_memory_tell, (int(*)(void*,unsigned int))&tsf_stream_memory_skip, (int(*)(void*,unsigned int))&tsf_stream_memory_seek, (int(*)(void*))&tsf_stream_memory_close, (int(*)(void*))&tsf_stream_memory_size }; + struct tsf_stream_memory f = { 0, 0, 0 }; + f.buffer = (const char*)buffer; + f.total = size; + stream.data = &f; + return tsf_load(&stream); +} + +enum { TSF_LOOPMODE_NONE, TSF_LOOPMODE_CONTINUOUS, TSF_LOOPMODE_SUSTAIN }; + +enum { TSF_SEGMENT_NONE, TSF_SEGMENT_DELAY, TSF_SEGMENT_ATTACK, TSF_SEGMENT_HOLD, TSF_SEGMENT_DECAY, TSF_SEGMENT_SUSTAIN, TSF_SEGMENT_RELEASE, TSF_SEGMENT_DONE }; + +struct tsf_hydra +{ + struct tsf_stream *stream; + int phdrOffset, pbagOffset, pmodOffset, pgenOffset, instOffset, ibagOffset, imodOffset, igenOffset, shdrOffset; + int phdrNum, pbagNum, pmodNum, pgenNum, instNum, ibagNum, imodNum, igenNum, shdrNum; +}; + +union tsf_hydra_genamount { struct { tsf_u8 lo, hi; } range; tsf_s16 shortAmount; tsf_u16 wordAmount; }; +struct tsf_hydra_phdr { tsf_char20 presetName; tsf_u16 preset, bank, presetBagNdx; tsf_u32 library, genre, morphology; }; +struct tsf_hydra_pbag { tsf_u16 genNdx, modNdx; }; +struct tsf_hydra_pmod { tsf_u16 modSrcOper, modDestOper; tsf_s16 modAmount; tsf_u16 modAmtSrcOper, modTransOper; }; +struct tsf_hydra_pgen { tsf_u16 genOper; union tsf_hydra_genamount genAmount; }; +struct tsf_hydra_inst { tsf_char20 instName; tsf_u16 instBagNdx; }; +struct tsf_hydra_ibag { tsf_u16 instGenNdx, instModNdx; }; +struct tsf_hydra_imod { tsf_u16 modSrcOper, modDestOper; tsf_s16 modAmount; tsf_u16 modAmtSrcOper, modTransOper; }; +struct tsf_hydra_igen { tsf_u16 genOper; union tsf_hydra_genamount genAmount; }; +struct tsf_hydra_shdr { tsf_char20 sampleName; tsf_u32 start, end, startLoop, endLoop, sampleRate; tsf_u8 originalPitch; tsf_s8 pitchCorrection; tsf_u16 sampleLink, sampleType; }; + +#define TSFR(FIELD) stream->read(stream->data, &i->FIELD, sizeof(i->FIELD)); +static void tsf_hydra_read_phdr(struct tsf_hydra_phdr* i, struct tsf_stream* stream) { TSFR(presetName) TSFR(preset) TSFR(bank) TSFR(presetBagNdx) TSFR(library) TSFR(genre) TSFR(morphology) } +static void tsf_hydra_read_pbag(struct tsf_hydra_pbag* i, struct tsf_stream* stream) { TSFR(genNdx) TSFR(modNdx) } +//static void tsf_hydra_read_pmod(struct tsf_hydra_pmod* i, struct tsf_stream* stream) { TSFR(modSrcOper) TSFR(modDestOper) TSFR(modAmount) TSFR(modAmtSrcOper) TSFR(modTransOper) } +static void tsf_hydra_read_pgen(struct tsf_hydra_pgen* i, struct tsf_stream* stream) { TSFR(genOper) TSFR(genAmount) } +static void tsf_hydra_read_inst(struct tsf_hydra_inst* i, struct tsf_stream* stream) { TSFR(instName) TSFR(instBagNdx) } +static void tsf_hydra_read_ibag(struct tsf_hydra_ibag* i, struct tsf_stream* stream) { TSFR(instGenNdx) TSFR(instModNdx) } +//static void tsf_hydra_read_imod(struct tsf_hydra_imod* i, struct tsf_stream* stream) { TSFR(modSrcOper) TSFR(modDestOper) TSFR(modAmount) TSFR(modAmtSrcOper) TSFR(modTransOper) } +static void tsf_hydra_read_igen(struct tsf_hydra_igen* i, struct tsf_stream* stream) { TSFR(genOper) TSFR(genAmount) } +static void tsf_hydra_read_shdr(struct tsf_hydra_shdr* i, struct tsf_stream* stream) { TSFR(sampleName) TSFR(start) TSFR(end) TSFR(startLoop) TSFR(endLoop) TSFR(sampleRate) TSFR(originalPitch) TSFR(pitchCorrection) TSFR(sampleLink) TSFR(sampleType) } +#undef TSFR +enum +{ + phdrSizeInFile = 38, pbagSizeInFile = 4, pmodSizeInFile = 10, + pgenSizeInFile = 4, instSizeInFile = 22, ibagSizeInFile = 4, + imodSizeInFile = 10, igenSizeInFile = 4, shdrSizeInFile = 46 +}; + +#define TGET(TYPE) \ +static struct tsf_hydra_##TYPE *get_##TYPE(struct tsf_hydra *t, int idx, struct tsf_hydra_##TYPE *data) \ +{ \ + t->stream->seek(t->stream->data, t->TYPE##Offset + TYPE##SizeInFile * idx); \ + tsf_hydra_read_##TYPE(data, t->stream); \ + return data; \ +} + +TGET(phdr) +TGET(pbag) +//TGET(pmod) +TGET(pgen) +TGET(inst) +TGET(ibag) +//TGET(imod) +TGET(igen) +TGET(shdr) +#undef TGET + +typedef int64_t fixed32p32; +typedef int32_t fixed30p2; +typedef int32_t fixed24p8; +typedef int32_t fixed16p16; +typedef int32_t fixed8p24; + + + +struct tsf_riffchunk { tsf_fourcc id; tsf_u32 size; }; +struct tsf_envelope { float delay, start, attack, hold, decay, sustain, release, keynumToHold, keynumToDecay; }; +struct tsf_voice_envelope { float level, slope; int samplesUntilNextSegment; int segment; struct tsf_envelope parameters; TSF_BOOL segmentIsExponential, exponentialDecay; }; +struct tsf_voice_lowpass { double QInv, a0, a1, b1, b2, z1, z2; TSF_BOOL active; }; +struct tsf_voice_lfo { int samplesUntil; float level, delta; }; + +struct tsf_region +{ + int loop_mode; + unsigned int sample_rate; + unsigned char lokey, hikey, lovel, hivel; + unsigned int group, offset, end, loop_start, loop_end; + int transpose, tune, pitch_keycenter, pitch_keytrack; + float volume, pan; + struct tsf_envelope ampenv, modenv; + int initialFilterQ, initialFilterFc; + int modEnvToPitch, modEnvToFilterFc, modLfoToFilterFc, modLfoToVolume; + float delayModLFO; + int freqModLFO, modLfoToPitch; + float delayVibLFO; + int freqVibLFO, vibLfoToPitch; +}; + +struct tsf_preset +{ + tsf_char20 presetName; + tsf_u16 preset, bank; + struct tsf_region* regions; + int regionNum; +}; + +struct tsf_voice +{ + int playingPreset, playingKey, curPitchWheel; + struct tsf_region* region; + double pitchInputTimecents, pitchOutputFactor; + double sourceSamplePosition; + fixed32p32 sourceSamplePositionF32P32; + float noteGainDB, panFactorLeft, panFactorRight; + unsigned int sampleEnd, loopStart, loopEnd; + struct tsf_voice_envelope ampenv, modenv; + struct tsf_voice_lowpass lowpass; + struct tsf_voice_lfo modlfo, viblfo; +}; + +static double tsf_timecents2Secsd(double timecents) { return TSF_POW(2.0, timecents / 1200.0); } +static float tsf_timecents2Secsf(float timecents) { return TSF_POWF(2.0f, timecents / 1200.0f); } +static float tsf_cents2Hertz(float cents) { return 8.176f * TSF_POWF(2.0f, cents / 1200.0f); } +static float tsf_decibelsToGain(float db) { return (db > -100.f ? TSF_POWF(10.0f, db * 0.05f) : 0); } + +static TSF_BOOL tsf_riffchunk_read(struct tsf_riffchunk* parent, struct tsf_riffchunk* chunk, struct tsf_stream* stream) +{ + TSF_BOOL IsRiff, IsList; + if (parent && sizeof(tsf_fourcc) + sizeof(tsf_u32) > parent->size) return TSF_FALSE; + if (!stream->read(stream->data, &chunk->id, sizeof(tsf_fourcc)) || *chunk->id <= ' ' || *chunk->id >= 'z') return TSF_FALSE; + if (!stream->read(stream->data, &chunk->size, sizeof(tsf_u32))) return TSF_FALSE; + if (parent && sizeof(tsf_fourcc) + sizeof(tsf_u32) + chunk->size > parent->size) return TSF_FALSE; + if (parent) parent->size -= sizeof(tsf_fourcc) + sizeof(tsf_u32) + chunk->size; + IsRiff = TSF_FourCCEquals(chunk->id, "RIFF"), IsList = TSF_FourCCEquals(chunk->id, "LIST"); + if (IsRiff && parent) return TSF_FALSE; //not allowed + if (!IsRiff && !IsList) return TSF_TRUE; //custom type without sub type + if (!stream->read(stream->data, &chunk->id, sizeof(tsf_fourcc)) || *chunk->id <= ' ' || *chunk->id >= 'z') return TSF_FALSE; + chunk->size -= sizeof(tsf_fourcc); + return TSF_TRUE; +} + +static void tsf_region_clear(struct tsf_region* i, TSF_BOOL for_relative) +{ + TSF_MEMSET(i, 0, sizeof(struct tsf_region)); + i->hikey = i->hivel = 127; + i->pitch_keycenter = 60; // C4 + if (for_relative) return; + + i->pitch_keytrack = 100; + + i->pitch_keycenter = -1; + + // SF2 defaults in timecents. + i->ampenv.delay = i->ampenv.attack = i->ampenv.hold = i->ampenv.decay = i->ampenv.release = -12000.0f; + i->modenv.delay = i->modenv.attack = i->modenv.hold = i->modenv.decay = i->modenv.release = -12000.0f; + + i->initialFilterFc = 13500; + + i->delayModLFO = -12000.0f; + i->delayVibLFO = -12000.0f; +} + +static void tsf_region_operator(struct tsf_region* region, tsf_u16 genOper, union tsf_hydra_genamount* amount) +{ + enum + { + StartAddrsOffset, EndAddrsOffset, StartloopAddrsOffset, EndloopAddrsOffset, StartAddrsCoarseOffset, ModLfoToPitch, VibLfoToPitch, ModEnvToPitch, + InitialFilterFc, InitialFilterQ, ModLfoToFilterFc, ModEnvToFilterFc, EndAddrsCoarseOffset, ModLfoToVolume, Unused1, ChorusEffectsSend, + ReverbEffectsSend, Pan, Unused2, Unused3, Unused4, DelayModLFO, FreqModLFO, DelayVibLFO, FreqVibLFO, DelayModEnv, AttackModEnv, HoldModEnv, + DecayModEnv, SustainModEnv, ReleaseModEnv, KeynumToModEnvHold, KeynumToModEnvDecay, DelayVolEnv, AttackVolEnv, HoldVolEnv, DecayVolEnv, + SustainVolEnv, ReleaseVolEnv, KeynumToVolEnvHold, KeynumToVolEnvDecay, Instrument, Reserved1, KeyRange, VelRange, StartloopAddrsCoarseOffset, + Keynum, Velocity, InitialAttenuation, Reserved2, EndloopAddrsCoarseOffset, CoarseTune, FineTune, SampleID, SampleModes, Reserved3, ScaleTuning, + ExclusiveClass, OverridingRootKey, Unused5, EndOper + }; + switch (genOper) + { + case StartAddrsOffset: region->offset += amount->shortAmount; break; + case EndAddrsOffset: region->end += amount->shortAmount; break; + case StartloopAddrsOffset: region->loop_start += amount->shortAmount; break; + case EndloopAddrsOffset: region->loop_end += amount->shortAmount; break; + case StartAddrsCoarseOffset: region->offset += amount->shortAmount * 32768; break; + case ModLfoToPitch: region->modLfoToPitch = amount->shortAmount; break; + case VibLfoToPitch: region->vibLfoToPitch = amount->shortAmount; break; + case ModEnvToPitch: region->modEnvToPitch = amount->shortAmount; break; + case InitialFilterFc: region->initialFilterFc = amount->shortAmount; break; + case InitialFilterQ: region->initialFilterQ = amount->shortAmount; break; + case ModLfoToFilterFc: region->modLfoToFilterFc = amount->shortAmount; break; + case ModEnvToFilterFc: region->modEnvToFilterFc = amount->shortAmount; break; + case EndAddrsCoarseOffset: region->end += amount->shortAmount * 32768; break; + case ModLfoToVolume: region->modLfoToVolume = amount->shortAmount; break; + case Pan: region->pan = amount->shortAmount * (2.0f / 10.0f); break; + case DelayModLFO: region->delayModLFO = amount->shortAmount; break; + case FreqModLFO: region->freqModLFO = amount->shortAmount; break; + case DelayVibLFO: region->delayVibLFO = amount->shortAmount; break; + case FreqVibLFO: region->freqVibLFO = amount->shortAmount; break; + case DelayModEnv: region->modenv.delay = amount->shortAmount; break; + case AttackModEnv: region->modenv.attack = amount->shortAmount; break; + case HoldModEnv: region->modenv.hold = amount->shortAmount; break; + case DecayModEnv: region->modenv.decay = amount->shortAmount; break; + case SustainModEnv: region->modenv.sustain = amount->shortAmount; break; + case ReleaseModEnv: region->modenv.release = amount->shortAmount; break; + case KeynumToModEnvHold: region->modenv.keynumToHold = amount->shortAmount; break; + case KeynumToModEnvDecay: region->modenv.keynumToDecay = amount->shortAmount; break; + case DelayVolEnv: region->ampenv.delay = amount->shortAmount; break; + case AttackVolEnv: region->ampenv.attack = amount->shortAmount; break; + case HoldVolEnv: region->ampenv.hold = amount->shortAmount; break; + case DecayVolEnv: region->ampenv.decay = amount->shortAmount; break; + case SustainVolEnv: region->ampenv.sustain = amount->shortAmount; break; + case ReleaseVolEnv: region->ampenv.release = amount->shortAmount; break; + case KeynumToVolEnvHold: region->ampenv.keynumToHold = amount->shortAmount; break; + case KeynumToVolEnvDecay: region->ampenv.keynumToDecay = amount->shortAmount; break; + case KeyRange: region->lokey = amount->range.lo; region->hikey = amount->range.hi; break; + case VelRange: region->lovel = amount->range.lo; region->hivel = amount->range.hi; break; + case StartloopAddrsCoarseOffset: region->loop_start += amount->shortAmount * 32768; break; + case InitialAttenuation: region->volume += -amount->shortAmount / 100.0f; break; + case EndloopAddrsCoarseOffset: region->loop_end += amount->shortAmount * 32768; break; + case CoarseTune: region->transpose += amount->shortAmount; break; + case FineTune: region->tune += amount->shortAmount; break; + case SampleModes: region->loop_mode = ((amount->wordAmount&3) == 3 ? TSF_LOOPMODE_SUSTAIN : ((amount->wordAmount&3) == 1 ? TSF_LOOPMODE_CONTINUOUS : TSF_LOOPMODE_NONE)); break; + case ScaleTuning: region->pitch_keytrack = amount->shortAmount; break; + case ExclusiveClass: region->group = amount->wordAmount; break; + case OverridingRootKey: region->pitch_keycenter = amount->shortAmount; break; + //case gen_endOper: break; // Ignore. + //default: addUnsupportedOpcode(generator_name); + } +} + +static void tsf_region_envtosecs(struct tsf_envelope* p, TSF_BOOL sustainIsGain) +{ + // EG times need to be converted from timecents to seconds. + // Pin very short EG segments. Timecents don't get to zero, and our EG is + // happier with zero values. + p->delay = (p->delay < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->delay)); + p->attack = (p->attack < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->attack)); + p->release = (p->release < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->release)); + + // If we have dynamic hold or decay times depending on key number we need + // to keep the values in timecents so we can calculate it during startNote + if (!p->keynumToHold) p->hold = (p->hold < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->hold)); + if (!p->keynumToDecay) p->decay = (p->decay < -11950.0f ? 0.0f : tsf_timecents2Secsf(p->decay)); + + if (p->sustain < 0.0f) p->sustain = 0.0f; + else if (sustainIsGain) p->sustain = 100.0f * tsf_decibelsToGain(-p->sustain / 10.0f); + else p->sustain = p->sustain / 10.0f; +} + +static void tsf_load_preset(tsf* res, struct tsf_hydra *hydra, int presetToLoad) +{ + enum { GenInstrument = 41, GenSampleID = 53 }; + // Read each preset. + struct tsf_hydra_phdr phdr; + int phdrIdx, phdrMaxIdx; + for (phdrIdx = presetToLoad, get_phdr(hydra, phdrIdx, &phdr), phdrMaxIdx = presetToLoad + 1 /*hydra->phdrNum - 1*/; phdrIdx != phdrMaxIdx; phdrIdx++, get_phdr(hydra, phdrIdx, &phdr)) + { + int sortedIndex = 0, region_index = 0; + struct tsf_hydra_phdr otherPhdr; + int otherPhdrIdx; + struct tsf_preset* preset; + for (otherPhdrIdx = 0, get_phdr(hydra, otherPhdrIdx, &otherPhdr); otherPhdrIdx != phdrMaxIdx; otherPhdrIdx++, get_phdr(hydra, otherPhdrIdx, &otherPhdr)) + { + if (otherPhdrIdx == phdrIdx || otherPhdr.bank > phdr.bank) continue; + else if (otherPhdr.bank < phdr.bank) sortedIndex++; + else if (otherPhdr.preset > phdr.preset) continue; + else if (otherPhdr.preset < phdr.preset) sortedIndex++; + else if (otherPhdrIdx < phdrIdx) sortedIndex++; + } + + preset = &res->presets[sortedIndex]; + TSF_MEMCPY(preset->presetName, phdr.presetName, sizeof(preset->presetName)); + preset->presetName[sizeof(preset->presetName)-1] = '\0'; //should be zero terminated in source file but make sure + preset->bank = phdr.bank; + preset->preset = phdr.preset; + preset->regionNum = 0; + + struct tsf_hydra_phdr phdrNext; + get_phdr(hydra, phdrIdx + 1, &phdrNext); + + //count regions covered by this preset + struct tsf_hydra_pbag pbag; + int pbagIdx, pbagEndIdx; + for (pbagIdx = phdr.presetBagNdx, get_pbag(hydra, pbagIdx, &pbag), pbagEndIdx = phdrNext.presetBagNdx; pbagIdx != pbagEndIdx; pbagIdx++, get_pbag(hydra, pbagIdx, &pbag)) + { + struct tsf_hydra_pbag pbagNext; + get_pbag(hydra, pbagIdx + 1, &pbagNext); + struct tsf_hydra_pgen pgen; + int pgenIdx, pgenEndIdx; + for (pgenIdx = pbag.genNdx, get_pgen(hydra, pgenIdx, &pgen), pgenEndIdx = pbagNext.genNdx; pgenIdx != pgenEndIdx; pgenIdx++, get_pgen(hydra, pgenIdx, &pgen)) + { + if (pgen.genOper != GenInstrument) continue; + if (pgen.genAmount.wordAmount >= hydra->instNum) continue; + struct tsf_hydra_inst inst, instNext; + get_inst(hydra, pgen.genAmount.wordAmount, &inst); + get_inst(hydra, pgen.genAmount.wordAmount+1, &instNext); + struct tsf_hydra_ibag ibag; + int ibagIdx, ibagEndIdx; + for (ibagIdx = inst.instBagNdx, get_ibag(hydra, ibagIdx, &ibag), ibagEndIdx = instNext.instBagNdx; ibagIdx != ibagEndIdx; ibagIdx++, get_ibag(hydra, ibagIdx, &ibag) ) + { + struct tsf_hydra_ibag ibagNext; + get_ibag(hydra, ibagIdx + 1, &ibagNext); + struct tsf_hydra_igen igen; + int igenIdx, igenEndIdx; + for (igenIdx = ibag.instGenNdx, get_igen(hydra, igenIdx, &igen), igenEndIdx = ibagNext.instGenNdx; igenIdx != igenEndIdx; igenIdx++, get_igen(hydra, igenIdx, &igen)) + if (igen.genOper == GenSampleID) + preset->regionNum++; + } + } + } + + preset->regions = (struct tsf_region*)TSF_MALLOC(preset->regionNum * sizeof(struct tsf_region)); + + // Zones. + //*** TODO: Handle global zone (modulators only). + for (pbagIdx = phdr.presetBagNdx, get_pbag(hydra, pbagIdx, &pbag), pbagEndIdx = phdrNext.presetBagNdx; pbagIdx != pbagEndIdx; pbagIdx++, get_pbag(hydra, pbagIdx, &pbag)) + { + struct tsf_region presetRegion; + tsf_region_clear(&presetRegion, TSF_TRUE); + struct tsf_hydra_pbag pbagNext; + get_pbag(hydra, pbagIdx + 1, &pbagNext); + struct tsf_hydra_pgen pgen; + int pgenIdx, pgenEndIdx; + + // Generators. + for (pgenIdx = pbag.genNdx, get_pgen(hydra, pgenIdx, &pgen), pgenEndIdx = pbagNext.genNdx; pgenIdx != pgenEndIdx; pgenIdx++, get_pgen(hydra, pgenIdx, &pgen)) + { + // Instrument. + if (pgen.genOper == GenInstrument) + { + struct tsf_region instRegion; + tsf_u16 whichInst = pgen.genAmount.wordAmount; + if (whichInst >= hydra->instNum) continue; + + tsf_region_clear(&instRegion, TSF_FALSE); + // Preset generators are supposed to be "relative" modifications of + // the instrument settings, but that makes no sense for ranges. + // For those, we'll have the instrument's generator take + // precedence, though that may not be correct. + instRegion.lokey = presetRegion.lokey; + instRegion.hikey = presetRegion.hikey; + instRegion.lovel = presetRegion.lovel; + instRegion.hivel = presetRegion.hivel; + + struct tsf_hydra_inst inst, instNext; + get_inst(hydra, whichInst, &inst); + get_inst(hydra, whichInst + 1, &instNext); + struct tsf_hydra_ibag ibag; + int ibagIdx, ibagEndIdx; + for (ibagIdx = inst.instBagNdx, get_ibag(hydra, ibagIdx, &ibag), ibagEndIdx = instNext.instBagNdx; ibagIdx != ibagEndIdx; ibagIdx++, get_ibag(hydra, ibagIdx, &ibag)) + { + // Generators. + struct tsf_region zoneRegion = instRegion; + int hadSampleID = 0; + struct tsf_hydra_ibag ibagNext; + get_ibag(hydra, ibagIdx + 1, &ibagNext); + struct tsf_hydra_igen igen; + int igenIdx, igenEndIdx; + for (igenIdx = ibag.instGenNdx, get_igen(hydra, igenIdx, &igen), igenEndIdx = ibagNext.instGenNdx; igenIdx != igenEndIdx; igenIdx++, get_igen(hydra, igenIdx, &igen)) + { + if (igen.genOper == GenSampleID) + { + struct tsf_hydra_shdr shdr; + get_shdr(hydra, igen.genAmount.wordAmount, &shdr); + + //sum regions + zoneRegion.offset += presetRegion.offset; + zoneRegion.end += presetRegion.end; + zoneRegion.loop_start += presetRegion.loop_start; + zoneRegion.loop_end += presetRegion.loop_end; + zoneRegion.transpose += presetRegion.transpose; + zoneRegion.tune += presetRegion.tune; + zoneRegion.pitch_keytrack += presetRegion.pitch_keytrack; + zoneRegion.volume += presetRegion.volume; + zoneRegion.pan += presetRegion.pan; + zoneRegion.ampenv.delay += presetRegion.ampenv.delay; + zoneRegion.ampenv.attack += presetRegion.ampenv.attack; + zoneRegion.ampenv.hold += presetRegion.ampenv.hold; + zoneRegion.ampenv.decay += presetRegion.ampenv.decay; + zoneRegion.ampenv.sustain += presetRegion.ampenv.sustain; + zoneRegion.ampenv.release += presetRegion.ampenv.release; + zoneRegion.modenv.delay += presetRegion.modenv.delay; + zoneRegion.modenv.attack += presetRegion.modenv.attack; + zoneRegion.modenv.hold += presetRegion.modenv.hold; + zoneRegion.modenv.decay += presetRegion.modenv.decay; + zoneRegion.modenv.sustain += presetRegion.modenv.sustain; + zoneRegion.modenv.release += presetRegion.modenv.release; + zoneRegion.initialFilterQ += presetRegion.initialFilterQ; + zoneRegion.initialFilterFc += presetRegion.initialFilterFc; + zoneRegion.modEnvToPitch += presetRegion.modEnvToPitch; + zoneRegion.modEnvToFilterFc += presetRegion.modEnvToFilterFc; + zoneRegion.delayModLFO += presetRegion.delayModLFO; + zoneRegion.freqModLFO += presetRegion.freqModLFO; + zoneRegion.modLfoToPitch += presetRegion.modLfoToPitch; + zoneRegion.modLfoToFilterFc += presetRegion.modLfoToFilterFc; + zoneRegion.modLfoToVolume += presetRegion.modLfoToVolume; + zoneRegion.delayVibLFO += presetRegion.delayVibLFO; + zoneRegion.freqVibLFO += presetRegion.freqVibLFO; + zoneRegion.vibLfoToPitch += presetRegion.vibLfoToPitch; + + // EG times need to be converted from timecents to seconds. + tsf_region_envtosecs(&zoneRegion.ampenv, TSF_TRUE); + tsf_region_envtosecs(&zoneRegion.modenv, TSF_FALSE); + + // LFO times need to be converted from timecents to seconds. + zoneRegion.delayModLFO = (zoneRegion.delayModLFO < -11950.0f ? 0.0f : tsf_timecents2Secsf(zoneRegion.delayModLFO)); + zoneRegion.delayVibLFO = (zoneRegion.delayVibLFO < -11950.0f ? 0.0f : tsf_timecents2Secsf(zoneRegion.delayVibLFO)); + + // Pin values to their ranges. + if (zoneRegion.pan < -100.0f) zoneRegion.pan = -100.0f; + else if (zoneRegion.pan > 100.0f) zoneRegion.pan = 100.0f; + if (zoneRegion.initialFilterQ < 1500 || zoneRegion.initialFilterQ > 13500) zoneRegion.initialFilterQ = 0; + + zoneRegion.offset += shdr.start; + zoneRegion.end += shdr.end; + zoneRegion.loop_start += shdr.startLoop; + zoneRegion.loop_end += shdr.endLoop; + if (shdr.endLoop > 0) zoneRegion.loop_end -= 1; + if (zoneRegion.pitch_keycenter == -1) zoneRegion.pitch_keycenter = shdr.originalPitch; + zoneRegion.tune += shdr.pitchCorrection; + + // Pin initialAttenuation to max +6dB. + if (zoneRegion.volume > 6.0f) + { + zoneRegion.volume = 6.0f; + //addUnsupportedOpcode("extreme gain in initialAttenuation"); + } + + preset->regions[region_index] = zoneRegion; + preset->regions[region_index].sample_rate = shdr.sampleRate; + region_index++; + hadSampleID = 1; + } + else tsf_region_operator(&zoneRegion, igen.genOper, &igen.genAmount); + } + + // Handle instrument's global zone. + if (ibagIdx == inst.instBagNdx && !hadSampleID) + instRegion = zoneRegion; + + // Modulators (TODO) + //if (ibag->instModNdx < ibag[1].instModNdx) addUnsupportedOpcode("any modulator"); + } + } + else tsf_region_operator(&presetRegion, pgen.genOper, &pgen.genAmount); + } + + // Modulators (TODO) + //if (pbag->modNdx < pbag[1].modNdx) addUnsupportedOpcode("any modulator"); + } + } +} + +static void tsf_load_samples(int *fontSamplesOffset, int* fontSampleCount, struct tsf_riffchunk *chunkSmpl, struct tsf_stream* stream) +{ + // Read sample data into float format buffer. + unsigned int samplesLeft; + samplesLeft = *fontSampleCount = chunkSmpl->size / sizeof(short); + *fontSamplesOffset = stream->tell(stream->data); + stream->skip(stream->data, samplesLeft * sizeof(short)); +} + +static void tsf_voice_envelope_nextsegment(struct tsf_voice_envelope* e, int active_segment, float outSampleRate) +{ + switch (active_segment) + { + case TSF_SEGMENT_NONE: + e->samplesUntilNextSegment = (int)(e->parameters.delay * outSampleRate); + if (e->samplesUntilNextSegment > 0) + { + e->segment = TSF_SEGMENT_DELAY; + e->segmentIsExponential = TSF_FALSE; + e->level = 0.0; + e->slope = 0.0; + return; + } + case TSF_SEGMENT_DELAY: + e->samplesUntilNextSegment = (int)(e->parameters.attack * outSampleRate); + if (e->samplesUntilNextSegment > 0) + { + e->segment = TSF_SEGMENT_ATTACK; + e->segmentIsExponential = TSF_FALSE; + e->level = e->parameters.start / 100.0f; + e->slope = 1.0f / e->samplesUntilNextSegment; + return; + } + case TSF_SEGMENT_ATTACK: + e->samplesUntilNextSegment = (int)(e->parameters.hold * outSampleRate); + if (e->samplesUntilNextSegment > 0) + { + e->segment = TSF_SEGMENT_HOLD; + e->segmentIsExponential = TSF_FALSE; + e->level = 1.0; + e->slope = 0.0; + return; + } + case TSF_SEGMENT_HOLD: + e->samplesUntilNextSegment = (int)(e->parameters.decay * outSampleRate); + if (e->samplesUntilNextSegment > 0) + { + e->segment = TSF_SEGMENT_DECAY; + e->level = 1.0; + if (e->exponentialDecay) + { + // I don't truly understand this; just following what LinuxSampler does. + float mysterySlope = -9.226f / e->samplesUntilNextSegment; + e->slope = TSF_EXPF(mysterySlope); + e->segmentIsExponential = TSF_TRUE; + if (e->parameters.sustain > 0.0f) + { + // Again, this is following LinuxSampler's example, which is similar to + // SF2-style decay, where "decay" specifies the time it would take to + // get to zero, not to the sustain level. The SFZ spec is not that + // specific about what "decay" means, so perhaps it's really supposed + // to specify the time to reach the sustain level. + e->samplesUntilNextSegment = (int)(TSF_LOG((e->parameters.sustain / 100.0) / e->level) / mysterySlope); + } + } + else + { + e->slope = (e->parameters.sustain / 100.0f - 1.0f) / e->samplesUntilNextSegment; + e->segmentIsExponential = TSF_FALSE; + } + return; + } + case TSF_SEGMENT_DECAY: + e->segment = TSF_SEGMENT_SUSTAIN; + e->level = e->parameters.sustain / 100.0f; + e->slope = 0.0f; + e->samplesUntilNextSegment = 0x7FFFFFFF; + e->segmentIsExponential = TSF_FALSE; + return; + case TSF_SEGMENT_SUSTAIN: + e->segment = TSF_SEGMENT_RELEASE; + e->samplesUntilNextSegment = (int)((e->parameters.release <= 0 ? TSF_FASTRELEASETIME : e->parameters.release) * outSampleRate); + if (e->exponentialDecay) + { + // I don't truly understand this; just following what LinuxSampler does. + float mysterySlope = -9.226f / e->samplesUntilNextSegment; + e->slope = TSF_EXPF(mysterySlope); + e->segmentIsExponential = TSF_TRUE; + } + else + { + e->slope = -e->level / e->samplesUntilNextSegment; + e->segmentIsExponential = TSF_FALSE; + } + return; + case TSF_SEGMENT_RELEASE: + default: + e->segment = TSF_SEGMENT_DONE; + e->segmentIsExponential = TSF_FALSE; + e->level = e->slope = 0; + e->samplesUntilNextSegment = 0x7FFFFFF; + } +} + +static void tsf_voice_envelope_setup(struct tsf_voice_envelope* e, struct tsf_envelope* new_parameters, int midiNoteNumber, TSF_BOOL setExponentialDecay, float outSampleRate) +{ + e->parameters = *new_parameters; + if (e->parameters.keynumToHold) + { + e->parameters.hold += e->parameters.keynumToHold * (60.0f - midiNoteNumber); + e->parameters.hold = (e->parameters.hold < -10000.0f ? 0.0f : tsf_timecents2Secsf(e->parameters.hold)); + } + if (e->parameters.keynumToDecay) + { + e->parameters.decay += e->parameters.keynumToDecay * (60.0f - midiNoteNumber); + e->parameters.decay = (e->parameters.decay < -10000.0f ? 0.0f : tsf_timecents2Secsf(e->parameters.decay)); + } + e->exponentialDecay = setExponentialDecay; + tsf_voice_envelope_nextsegment(e, TSF_SEGMENT_NONE, outSampleRate); +} + +static void tsf_voice_envelope_process(struct tsf_voice_envelope* e, int numSamples, float outSampleRate) +{ + if (e->slope) + { + if (e->segmentIsExponential) e->level *= TSF_POWF(e->slope, (float)numSamples); + else e->level += (e->slope * numSamples); + } + if ((e->samplesUntilNextSegment -= numSamples) <= 0) + tsf_voice_envelope_nextsegment(e, e->segment, outSampleRate); +} + +static void tsf_voice_lowpass_setup(struct tsf_voice_lowpass* e, float Fc) +{ + // Lowpass filter from http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/ + double K = TSF_TAN(TSF_PI * Fc), KK = K * K; + double norm = 1 / (1 + K * e->QInv + KK); + e->a0 = KK * norm; + e->a1 = 2 * e->a0; + e->b1 = 2 * (KK - 1) * norm; + e->b2 = (1 - K * e->QInv + KK) * norm; +} + +static float tsf_voice_lowpass_process(struct tsf_voice_lowpass* e, double In) +{ + double Out = In * e->a0 + e->z1; e->z1 = In * e->a1 + e->z2 - e->b1 * Out; e->z2 = In * e->a0 - e->b2 * Out; return (float)Out; +} + +static void tsf_voice_lfo_setup(struct tsf_voice_lfo* e, float delay, int freqCents, float outSampleRate) +{ + e->samplesUntil = (int)(delay * outSampleRate); + e->delta = (4.0f * tsf_cents2Hertz((float)freqCents) / outSampleRate); + e->level = 0; +} + +static void tsf_voice_lfo_process(struct tsf_voice_lfo* e, int blockSamples) +{ + if (e->samplesUntil > blockSamples) { e->samplesUntil -= blockSamples; return; } + e->level += e->delta * blockSamples; + if (e->level > 1.0f) { e->delta = -e->delta; e->level = 2.0f - e->level; } + else if (e->level < -1.0f) { e->delta = -e->delta; e->level = -2.0f - e->level; } +} + +static void tsf_voice_kill(struct tsf_voice* v) +{ + v->region = TSF_NULL; + v->playingPreset = -1; +} + +static void tsf_voice_end(struct tsf_voice* v, float outSampleRate) +{ + tsf_voice_envelope_nextsegment(&v->ampenv, TSF_SEGMENT_SUSTAIN, outSampleRate); + tsf_voice_envelope_nextsegment(&v->modenv, TSF_SEGMENT_SUSTAIN, outSampleRate); + if (v->region->loop_mode == TSF_LOOPMODE_SUSTAIN) + { + // Continue playing, but stop looping. + v->loopEnd = v->loopStart; + } +} + +static void tsf_voice_endquick(struct tsf_voice* v, float outSampleRate) +{ + v->ampenv.parameters.release = 0.0f; tsf_voice_envelope_nextsegment(&v->ampenv, TSF_SEGMENT_SUSTAIN, outSampleRate); + v->modenv.parameters.release = 0.0f; tsf_voice_envelope_nextsegment(&v->modenv, TSF_SEGMENT_SUSTAIN, outSampleRate); +} + +static void tsf_voice_calcpitchratio(struct tsf_voice* v, float outSampleRate) +{ + double note = v->playingKey, adjustedPitch; + note += v->region->transpose; + note += v->region->tune / 100.0; + + adjustedPitch = v->region->pitch_keycenter + (note - v->region->pitch_keycenter) * (v->region->pitch_keytrack / 100.0); + if (v->curPitchWheel != 8192) adjustedPitch += ((4.0 * v->curPitchWheel / 16383.0) - 2.0); + + v->pitchInputTimecents = adjustedPitch * 100.0; + v->pitchOutputFactor = v->region->sample_rate / (tsf_timecents2Secsd(v->region->pitch_keycenter * 100.0) * outSampleRate); +} + +short tsf_read_short_cached(tsf *f, int pos) +{ + static int hits = 0; + static int misses = 0; +// static int call =0; +// call++; +// if ((call % 88000) ==0) printf("Hit: %d, Miss: %d, Ratio: %f\n", hits, misses, (double)hits/(double)(misses+hits)); + + for (int i=0; ioffset[i] <= pos) && ((f->offset[i] + TSF_BUFFSIZE) > pos) ) { + f->timestamp[i] = f->epoch++; + if (f->epoch==0) { + for (int i=0; itimestamp[i] = f->epoch++; + } + hits++; + return f->buffer[i][pos - f->offset[i]]; + } + } + int repl = 0; + for (int i=1; itimestamp[i] < f->timestamp[repl]) repl = i; + } + int readOff = pos - (pos % TSF_BUFFSIZE); +// for (int i=0; ibuffer[repl][i] = i; } + f->hydra->stream->seek(f->hydra->stream->data, readOff * sizeof(short)); + f->hydra->stream->read(f->hydra->stream->data, f->buffer[repl], TSF_BUFFSIZE * sizeof(short)); +//static uint32_t *bp = NULL; if (!bp) bp = (uint32_t*)malloc(512); +//printf("off=%08x, buff=%p, len=%08x\n", readOff * sizeof(short), bp, TSF_BUFFSIZE); spi_flash_read(0x0000, bp, 512/4-4) ; + f->timestamp[repl] = f->epoch++; + f->offset[repl] = readOff; + misses++; + return f->buffer[repl][pos - readOff]; +} + +static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, int numSamples) +{ + struct tsf_region* region = v->region; + float* outL = outputBuffer; + float* outR = (f->outputmode == TSF_STEREO_UNWEAVED ? outL + numSamples : TSF_NULL); + + // Cache some values, to give them at least some chance of ending up in registers. + TSF_BOOL updateModEnv = (region->modEnvToPitch || region->modEnvToFilterFc); + TSF_BOOL updateModLFO = (v->modlfo.delta && (region->modLfoToPitch || region->modLfoToFilterFc || region->modLfoToVolume)); + TSF_BOOL updateVibLFO = (v->viblfo.delta && (region->vibLfoToPitch)); + TSF_BOOL isLooping = (v->loopStart < v->loopEnd); + unsigned int tmpLoopStart = v->loopStart, tmpLoopEnd = v->loopEnd; + double tmpSampleEndDbl = (double)v->sampleEnd, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0; + double tmpSourceSamplePosition = v->sourceSamplePosition; + struct tsf_voice_lowpass tmpLowpass = v->lowpass; + + TSF_BOOL dynamicLowpass = (region->modLfoToFilterFc || region->modEnvToFilterFc); + float tmpSampleRate, tmpInitialFilterFc, tmpModLfoToFilterFc, tmpModEnvToFilterFc; + + TSF_BOOL dynamicPitchRatio = (region->modLfoToPitch || region->modEnvToPitch || region->vibLfoToPitch); + double pitchRatio; + float tmpModLfoToPitch, tmpVibLfoToPitch, tmpModEnvToPitch; + + TSF_BOOL dynamicGain = (region->modLfoToVolume != 0); + float noteGain, tmpModLfoToVolume; + + if (dynamicLowpass) tmpSampleRate = f->outSampleRate, tmpInitialFilterFc = (float)region->initialFilterFc, tmpModLfoToFilterFc = (float)region->modLfoToFilterFc, tmpModEnvToFilterFc = (float)region->modEnvToFilterFc; + else tmpSampleRate = 0, tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0; + + if (dynamicPitchRatio) pitchRatio = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch; + else pitchRatio = tsf_timecents2Secsd(v->pitchInputTimecents) * v->pitchOutputFactor, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0; + + if (dynamicGain) noteGain = 0, tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f; + else noteGain = tsf_decibelsToGain(v->noteGainDB), tmpModLfoToVolume = 0; + + while (numSamples) + { + float gainMono, gainLeft, gainRight; + int blockSamples = (numSamples > TSF_RENDER_EFFECTSAMPLEBLOCK ? TSF_RENDER_EFFECTSAMPLEBLOCK : numSamples); + numSamples -= blockSamples; + + if (dynamicLowpass) + { + float fres = tmpInitialFilterFc + v->modlfo.level * tmpModLfoToFilterFc + v->modenv.level * tmpModEnvToFilterFc; + tmpLowpass.active = (fres <= 13500.0f); + if (tmpLowpass.active) tsf_voice_lowpass_setup(&tmpLowpass, tsf_cents2Hertz(fres) / tmpSampleRate); + } + + if (dynamicPitchRatio) + pitchRatio = tsf_timecents2Secsd(v->pitchInputTimecents + (v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch)) * v->pitchOutputFactor; + + if (dynamicGain) + noteGain = tsf_decibelsToGain(v->noteGainDB + (v->modlfo.level * tmpModLfoToVolume)); + + gainMono = noteGain * v->ampenv.level; + + // Update EG. + tsf_voice_envelope_process(&v->ampenv, blockSamples, f->outSampleRate); + if (updateModEnv) tsf_voice_envelope_process(&v->modenv, blockSamples, f->outSampleRate); + + // Update LFOs. + if (updateModLFO) tsf_voice_lfo_process(&v->modlfo, blockSamples); + if (updateVibLFO) tsf_voice_lfo_process(&v->viblfo, blockSamples); + + switch (f->outputmode) + { + case TSF_STEREO_INTERLEAVED: + gainLeft = gainMono * v->panFactorLeft, gainRight = gainMono * v->panFactorRight; + while (blockSamples-- && tmpSourceSamplePosition < tmpSampleEndDbl) + { + unsigned int pos = (unsigned int)tmpSourceSamplePosition, nextPos = (pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1); + float inputPos, inputNextPos; + inputPos = (float)(tsf_read_short_cached(f, pos) / 32767.0); + inputNextPos = (float)(tsf_read_short_cached(f, nextPos) / 32767.0); + // Simple linear interpolation. + float alpha = (float)(tmpSourceSamplePosition - pos), val = (inputPos * (1.0f - alpha) + inputNextPos * alpha); + + // Low-pass filter. + if (tmpLowpass.active) val = tsf_voice_lowpass_process(&tmpLowpass, val); + + *outL++ += val * gainLeft; + *outL++ += val * gainRight; + + // Next sample. + tmpSourceSamplePosition += pitchRatio; + if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) tmpSourceSamplePosition -= (tmpLoopEnd - tmpLoopStart + 1.0); + } + break; + + case TSF_STEREO_UNWEAVED: + gainLeft = gainMono * v->panFactorLeft, gainRight = gainMono * v->panFactorRight; + while (blockSamples-- && tmpSourceSamplePosition < tmpSampleEndDbl) + { + unsigned int pos = (unsigned int)tmpSourceSamplePosition, nextPos = (pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1); + float inputPos, inputNextPos; + inputPos = (float)(tsf_read_short_cached(f, pos) / 32767.0); + inputNextPos = (float)(tsf_read_short_cached(f, nextPos) / 32767.0); + + // Simple linear interpolation. + float alpha = (float)(tmpSourceSamplePosition - pos), val = (inputPos * (1.0f - alpha) + inputNextPos * alpha); + + // Low-pass filter. + if (tmpLowpass.active) val = tsf_voice_lowpass_process(&tmpLowpass, val); + + *outL++ += val * gainLeft; + *outR++ += val * gainRight; + + // Next sample. + tmpSourceSamplePosition += pitchRatio; + if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) tmpSourceSamplePosition -= (tmpLoopEnd - tmpLoopStart + 1.0); + } + break; + + case TSF_MONO: + while (blockSamples-- && tmpSourceSamplePosition < tmpSampleEndDbl) + { + unsigned int pos = (unsigned int)tmpSourceSamplePosition, nextPos = (pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1); + float inputPos, inputNextPos; + inputPos = (float)(tsf_read_short_cached(f, pos) / 32767.0); + inputNextPos = (float)(tsf_read_short_cached(f, nextPos) / 32767.0); + + // Simple linear interpolation. + float alpha = (float)(tmpSourceSamplePosition - pos), val = (inputPos * (1.0f - alpha) + inputNextPos * alpha); + + // Low-pass filter. + if (tmpLowpass.active) val = tsf_voice_lowpass_process(&tmpLowpass, val); + + *outL++ += val * gainMono; + + // Next sample. + tmpSourceSamplePosition += pitchRatio; + if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) tmpSourceSamplePosition -= (tmpLoopEnd - tmpLoopStart + 1.0); + } + break; + } + + if (tmpSourceSamplePosition >= tmpSampleEndDbl || v->ampenv.segment == TSF_SEGMENT_DONE) + { + tsf_voice_kill(v); + return; + } + } + + v->sourceSamplePosition = tmpSourceSamplePosition; + if (tmpLowpass.active || dynamicLowpass) v->lowpass = tmpLowpass; +} + + +void DumpF32P32(char *name, long long x) { + printf("%s = %08x.%08x\n", name, (int32)((x>>32)&0xffffffff), (int32)(x&0xffffffff)); +} +static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuffer, int numSamples) +{ + struct tsf_region* region = v->region; + short* outL = outputBuffer; + short* outR = (f->outputmode == TSF_STEREO_UNWEAVED ? outL + numSamples : TSF_NULL); + + // Cache some values, to give them at least some chance of ending up in registers. + TSF_BOOL updateModEnv = (region->modEnvToPitch || region->modEnvToFilterFc); + TSF_BOOL updateModLFO = (v->modlfo.delta && (region->modLfoToPitch || region->modLfoToFilterFc || region->modLfoToVolume)); + TSF_BOOL updateVibLFO = (v->viblfo.delta && (region->vibLfoToPitch)); + TSF_BOOL isLooping = (v->loopStart < v->loopEnd); + unsigned int tmpLoopStart = v->loopStart, tmpLoopEnd = v->loopEnd; + //double tmpSampleEndDbl = (double)v->sampleEnd, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0; + //double tmpSourceSamplePosition = v->sourceSamplePosition; + fixed32p32 tmpSampleEndF32P32 = ((fixed32p32)(v->sampleEnd)) << 32; + fixed32p32 tmpLoopEndF32P32 = ((fixed32p32)(tmpLoopEnd + 1)) << 32; + fixed32p32 tmpSourceSamplePositionF32P32 = v->sourceSamplePositionF32P32; + struct tsf_voice_lowpass tmpLowpass = v->lowpass; + + TSF_BOOL dynamicLowpass = (region->modLfoToFilterFc || region->modEnvToFilterFc); + float tmpSampleRate, tmpInitialFilterFc, tmpModLfoToFilterFc, tmpModEnvToFilterFc; + + TSF_BOOL dynamicPitchRatio = (region->modLfoToPitch || region->modEnvToPitch || region->vibLfoToPitch); + //double pitchRatio; + fixed32p32 pitchRatioF32P32; + float tmpModLfoToPitch, tmpVibLfoToPitch, tmpModEnvToPitch; + + TSF_BOOL dynamicGain = (region->modLfoToVolume != 0); + float noteGain, tmpModLfoToVolume; + + if (dynamicLowpass) tmpSampleRate = f->outSampleRate, tmpInitialFilterFc = (float)region->initialFilterFc, tmpModLfoToFilterFc = (float)region->modLfoToFilterFc, tmpModEnvToFilterFc = (float)region->modEnvToFilterFc; + else tmpSampleRate = 0, tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0; + + if (dynamicPitchRatio) pitchRatioF32P32 = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch; + else { + double pr = tsf_timecents2Secsd(v->pitchInputTimecents) * v->pitchOutputFactor; + fixed32p32 adj = 1LL<<32; + pr *= adj; + pitchRatioF32P32 = (int64_t)pr, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0; + } + + if (dynamicGain) noteGain = 0, tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f; + else noteGain = tsf_decibelsToGain(v->noteGainDB), tmpModLfoToVolume = 0; + + while (numSamples) + { + float gainMono; + int blockSamples = (numSamples > TSF_RENDER_EFFECTSAMPLEBLOCK ? TSF_RENDER_EFFECTSAMPLEBLOCK : numSamples); + numSamples -= blockSamples; + + if (dynamicLowpass) + { + float fres = tmpInitialFilterFc + v->modlfo.level * tmpModLfoToFilterFc + v->modenv.level * tmpModEnvToFilterFc; + tmpLowpass.active = (fres <= 13500.0f); + if (tmpLowpass.active) tsf_voice_lowpass_setup(&tmpLowpass, tsf_cents2Hertz(fres) / tmpSampleRate); + } + + if (dynamicPitchRatio) { + pitchRatioF32P32 = tsf_timecents2Secsd(v->pitchInputTimecents + (v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch)) * v->pitchOutputFactor * (1LL<<32); + } + + if (dynamicGain) + noteGain = tsf_decibelsToGain(v->noteGainDB + (v->modlfo.level * tmpModLfoToVolume)); + + gainMono = noteGain * v->ampenv.level; + short gainMonoFP = gainMono * 32767; + + // Update EG. + tsf_voice_envelope_process(&v->ampenv, blockSamples, f->outSampleRate); + if (updateModEnv) tsf_voice_envelope_process(&v->modenv, blockSamples, f->outSampleRate); + + // Update LFOs. + if (updateModLFO) tsf_voice_lfo_process(&v->modlfo, blockSamples); + if (updateVibLFO) tsf_voice_lfo_process(&v->viblfo, blockSamples); + + while (blockSamples-- && tmpSourceSamplePositionF32P32 < tmpSampleEndF32P32) + { + unsigned int pos = (unsigned int)(tmpSourceSamplePositionF32P32>>32); + short val = tsf_read_short_cached(f, pos); + int32_t val32 = (int)val * (int)gainMonoFP; + + *outL++ += val32>>16; + if (f->outputmode != TSF_MONO) *outR++ += val32>>16; + + // Next sample. + tmpSourceSamplePositionF32P32 += pitchRatioF32P32; + if (tmpSourceSamplePositionF32P32 >= tmpLoopEndF32P32 && isLooping) + tmpSourceSamplePositionF32P32 -= (tmpLoopEndF32P32 - tmpLoopStart + (1LL<<32)); + } + + if (tmpSourceSamplePositionF32P32 >= tmpSampleEndF32P32 || v->ampenv.segment == TSF_SEGMENT_DONE) + { + tsf_voice_kill(v); + return; + } + } + + v->sourceSamplePositionF32P32 = tmpSourceSamplePositionF32P32; + if (tmpLowpass.active || dynamicLowpass) v->lowpass = tmpLowpass; +} + + + + +TSFDEF tsf* tsf_load(struct tsf_stream* stream) +{ + tsf* res = TSF_NULL; + struct tsf_riffchunk chunkHead; + struct tsf_riffchunk chunkList; + struct tsf_hydra hydra; + int fontSamplesOffset = 0; + int fontSampleCount = 0; + + if (!tsf_riffchunk_read(TSF_NULL, &chunkHead, stream) || !TSF_FourCCEquals(chunkHead.id, "sfbk")) + { + //if (e) *e = TSF_INVALID_NOSF2HEADER; + return res; + } + + // Read hydra and locate sample data. + TSF_MEMSET(&hydra, 0, sizeof(hydra)); + hydra.stream = stream; + while (tsf_riffchunk_read(&chunkHead, &chunkList, stream)) + { + struct tsf_riffchunk chunk; + if (TSF_FourCCEquals(chunkList.id, "pdta")) + { + while (tsf_riffchunk_read(&chunkList, &chunk, stream)) + { + #define GetChunkOffset(chunkName) (TSF_FourCCEquals(chunk.id, #chunkName) && !(chunk.size % chunkName##SizeInFile)) \ + { \ + int num = chunk.size / chunkName##SizeInFile; \ + hydra.chunkName##Num = num; \ + hydra.chunkName##Offset = stream->tell(stream->data); \ + stream->skip(stream->data, num * chunkName##SizeInFile); \ + } + if GetChunkOffset(phdr) + else if GetChunkOffset(pbag) + else if GetChunkOffset(pmod) + else if GetChunkOffset(pgen) + else if GetChunkOffset(inst) + else if GetChunkOffset(ibag) + else if GetChunkOffset(imod) + else if GetChunkOffset(igen) + else if GetChunkOffset(shdr) + else stream->skip(stream->data, chunk.size); + #undef HandleChunk + } + } + else if (TSF_FourCCEquals(chunkList.id, "sdta")) + { + while (tsf_riffchunk_read(&chunkList, &chunk, stream)) + { + if (TSF_FourCCEquals(chunk.id, "smpl")) + { + tsf_load_samples(&fontSamplesOffset, &fontSampleCount, &chunk, stream); + } + else stream->skip(stream->data, chunk.size); + } + } + else stream->skip(stream->data, chunkList.size); + } + if (!hydra.phdrNum || !hydra.pbagNum || !hydra.pmodNum || !hydra.pgenNum || !hydra.instNum || !hydra.ibagNum || !hydra.imodNum || !hydra.igenNum || !hydra.shdrNum) + { + //if (e) *e = TSF_INVALID_INCOMPLETE; + } + else if (fontSamplesOffset == 0) + { + //if (e) *e = TSF_INVALID_NOSAMPLEDATA; + } + else + { + res = (tsf*)TSF_MALLOC(sizeof(tsf)); + TSF_MEMSET(res, 0, sizeof(tsf)); + res->presetNum = hydra.phdrNum - 1; + res->presets = (struct tsf_preset*)TSF_MALLOC(res->presetNum * sizeof(struct tsf_preset)); + TSF_MEMSET(res->presets, 0, res->presetNum * sizeof(struct tsf_preset)); + res->fontSamplesOffset = fontSamplesOffset; + res->fontSampleCount = fontSampleCount; + res->outSampleRate = 44100.0f; + res->hydra = (struct tsf_hydra*)TSF_MALLOC(sizeof(struct tsf_hydra)); + TSF_MEMCPY(res->hydra, &hydra, sizeof(*res->hydra)); + res->hydra->stream = (struct tsf_stream*)TSF_MALLOC(sizeof(struct tsf_stream)); + TSF_MEMCPY(res->hydra->stream, stream, sizeof(*res->hydra->stream)); + + // Cached sample + for (int i=0; ibuffer[i] = (short*)TSF_MALLOC(TSF_BUFFSIZE * sizeof(short)); + res->offset[i] = 0xfffffff; + res->timestamp[i] = -1; + } + res->epoch = 0; + } + return res; +} + +TSFDEF void tsf_close(tsf* f) +{ + struct tsf_preset *preset, *presetEnd; + if (!f) return; + for (preset = f->presets, presetEnd = preset + f->presetNum; preset != presetEnd; preset++) + TSF_FREE(preset->regions); + TSF_FREE(f->presets); + TSF_FREE(f->voices); + TSF_FREE(f->outputSamples); + f->hydra->stream->close(f->hydra->stream->data); + TSF_FREE(f->hydra->stream); + TSF_FREE(f->hydra); + for (int i=0; ibuffer[i]); + TSF_FREE(f); +} + +TSFDEF int tsf_get_presetcount(tsf* f) +{ + return f->presetNum; +} + +TSFDEF const char* tsf_get_presetname(tsf* f, int preset) +{ + if (f->presets[preset].regions == NULL && preset >= 0 && preset < f->presetNum) tsf_load_preset(f, f->hydra, preset); + return (preset < 0 || preset >= f->presetNum ? TSF_NULL : f->presets[preset].presetName); +} + +TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float globalgaindb) +{ + f->outSampleRate = (float)(samplerate >= 1 ? samplerate : 44100.0f); + f->outputmode = outputmode; + f->globalGainDB = globalgaindb; +} + +TSFDEF void tsf_note_on(tsf* f, int preset, int key, float vel) +{ + int midiVelocity = (int)(vel * 127); + TSF_BOOL haveGroupedNotesPlaying = TSF_FALSE; + struct tsf_voice *v, *vEnd; struct tsf_region *region, *regionEnd; + + if (preset < 0 || preset >= f->presetNum) return; + if (f->presets[preset].regions == NULL) tsf_load_preset(f, f->hydra, preset); + + // Are any grouped notes playing? (Needed for group stopping) Also stop any voices still playing this note. + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) + { + if (v->playingPreset != preset) continue; + if (v->playingKey == key) tsf_voice_endquick(v, f->outSampleRate); + if (v->region->group) haveGroupedNotesPlaying = TSF_TRUE; + } + + // Play all matching regions. + for (region = f->presets[preset].regions, regionEnd = region + f->presets[preset].regionNum; region != regionEnd; region++) + { + struct tsf_voice* voice = TSF_NULL; double adjustedPan; TSF_BOOL doLoop; float filterQDB; + if (key < region->lokey || key > region->hikey || midiVelocity < region->lovel || midiVelocity > region->hivel) continue; + + if (haveGroupedNotesPlaying && region->group) + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) + if (v->playingPreset == preset && v->region->group == region->group) + tsf_voice_endquick(v, f->outSampleRate); + + for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++) if (v->playingPreset == -1) { voice = v; break; } + if (!voice) + { + f->voiceNum += 4; + f->voices = (struct tsf_voice*)TSF_REALLOC(f->voices, f->voiceNum * sizeof(struct tsf_voice)); + voice = &f->voices[f->voiceNum - 4]; + voice[1].playingPreset = voice[2].playingPreset = voice[3].playingPreset = -1; + } + + voice->region = region; + voice->playingPreset = preset; + voice->playingKey = key; + + // Pitch. + voice->curPitchWheel = 8192; + tsf_voice_calcpitchratio(voice, f->outSampleRate); + + // Gain. + voice->noteGainDB = f->globalGainDB + region->volume; + // Thanks to for explaining the velocity curve in a way that I could understand, although they mean "log10" when they say "log". + voice->noteGainDB += (float)(-20.0 * TSF_LOG10(1.0 / vel)); + // The SFZ spec is silent about the pan curve, but a 3dB pan law seems common. This sqrt() curve matches what Dimension LE does; Alchemy Free seems closer to sin(adjustedPan * pi/2). + adjustedPan = (region->pan + 100.0) / 200.0; + voice->panFactorLeft = (float)TSF_SQRT(1.0 - adjustedPan); + voice->panFactorRight = (float)TSF_SQRT(adjustedPan); + + // Offset/end. + voice->sourceSamplePosition = region->offset; + voice->sourceSamplePositionF32P32 = ((int64_t)region->offset)<< 32; + voice->sampleEnd = f->fontSampleCount; + if (region->end > 0 && region->end < voice->sampleEnd) voice->sampleEnd = region->end + 1; + + // Loop. + doLoop = (region->loop_mode != TSF_LOOPMODE_NONE && region->loop_start < region->loop_end); + voice->loopStart = (doLoop ? region->loop_start : 0); + voice->loopEnd = (doLoop ? region->loop_end : 0); + + // Setup envelopes. + tsf_voice_envelope_setup(&voice->ampenv, ®ion->ampenv, key, TSF_TRUE, f->outSampleRate); + tsf_voice_envelope_setup(&voice->modenv, ®ion->modenv, key, TSF_FALSE, f->outSampleRate); + + // Setup lowpass filter. + filterQDB = region->initialFilterQ / 10.0f; + voice->lowpass.QInv = 1.0 / TSF_POW(10.0, (filterQDB / 20.0)); + voice->lowpass.z1 = voice->lowpass.z2 = 0; + voice->lowpass.active = (region->initialFilterFc <= 13500); + if (voice->lowpass.active) tsf_voice_lowpass_setup(&voice->lowpass, tsf_cents2Hertz((float)region->initialFilterFc) / f->outSampleRate); + + // Setup LFO filters. + tsf_voice_lfo_setup(&voice->modlfo, region->delayModLFO, region->freqModLFO, f->outSampleRate); + tsf_voice_lfo_setup(&voice->viblfo, region->delayVibLFO, region->freqVibLFO, f->outSampleRate); + } +} + +TSFDEF void tsf_note_off(tsf* f, int preset, int key) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + for (; v != vEnd; v++) + if (v->playingPreset == preset && v->playingKey == key) + tsf_voice_end(v, f->outSampleRate); +} + +TSFDEF void tsf_render_short(tsf* f, short* buffer, int samples, int flag_mixing) +{ + float *floatSamples; + int channelSamples = (f->outputmode == TSF_MONO ? 1 : 2) * samples, floatBufferSize = channelSamples * sizeof(float); + short* bufferEnd = buffer + channelSamples; + if (floatBufferSize > f->outputSampleSize) + { + TSF_FREE(f->outputSamples); + f->outputSamples = (float*)TSF_MALLOC(floatBufferSize); + f->outputSampleSize = floatBufferSize; + } + + tsf_render_float(f, f->outputSamples, samples, TSF_FALSE); + + floatSamples = f->outputSamples; + if (flag_mixing) + while (buffer != bufferEnd) + { + float v = *floatSamples++; + int vi = *buffer + (v < -1.00004566f ? (int)-32768 : (v > 1.00001514f ? (int)32767 : (int)(v * 32767.5f))); + *buffer++ = (vi < -32768 ? (short)-32768 : (vi > 32767 ? (short)32767 : (short)vi)); + } + else + while (buffer != bufferEnd) + { + float v = *floatSamples++; + *buffer++ = (v < -1.00004566f ? (short)-32768 : (v > 1.00001514f ? (short)32767 : (short)(v * 32767.5f))); + } +} + +TSFDEF void tsf_render_float(tsf* f, float* buffer, int samples, int flag_mixing) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + if (!flag_mixing) TSF_MEMSET(buffer, 0, (f->outputmode == TSF_MONO ? 1 : 2) * sizeof(float) * samples); + for (; v != vEnd; v++) + if (v->playingPreset != -1) + tsf_voice_render(f, v, buffer, samples); +} + +TSFDEF void tsf_render_short_fast(tsf* f, short* buffer, int samples, int flag_mixing) +{ + struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; + if (!flag_mixing) TSF_MEMSET(buffer, 0, (f->outputmode == TSF_MONO ? 1 : 2) * sizeof(short) * samples); + for (; v != vEnd; v++) { + if (v->playingPreset != -1) + tsf_voice_render_fast(f, v, buffer, samples); + yield(); + } +} + + + +#ifdef __cplusplus +} +#endif + +#endif //TSF_IMPLEMENTATION