shithub: sf2mid

Download patch

ref: 2aa076933897c693f2b6c977c521f7b7f21c121d
parent: f1a31b134c8a03fac753f9ae1b3ee83bbab1d8a5
author: Bernhard Schelling <schellingb@gmail.com>
date: Tue Oct 17 19:47:15 EDT 2017

Added tst_bank_* functons to play by instrument bank and preset number instead of preset index

--- a/tsf.h
+++ b/tsf.h
@@ -94,14 +94,17 @@
 TSFDEF void tsf_close(tsf* f);
 
 // Returns the preset index from a bank and preset number, or -1 if it does not exist in the loaded SoundFont
-TSFDEF int tsf_get_presetindex(tsf* f, int bank, int preset_number);
+TSFDEF int tsf_get_presetindex(const tsf* f, int bank, int preset_number);
 
 // Returns the number of presets in the loaded SoundFont
-TSFDEF int tsf_get_presetcount(tsf* f);
+TSFDEF int tsf_get_presetcount(const tsf* f);
 
 // Returns the name of a preset index >= 0 and < tsf_get_presetcount()
-TSFDEF const char* tsf_get_presetname(tsf* f, int preset_index);
+TSFDEF const char* tsf_get_presetname(const tsf* f, int preset_index);
 
+// Returns the name of a preset by bank and preset number
+TSFDEF const char* tsf_bank_get_presetname(const tsf* f, int bank, int preset_number);
+
 // Supported output modes by the render methods
 enum TSFOutputMode
 {
@@ -127,13 +130,16 @@
 
 // Start playing a note
 //   preset_index: preset index >= 0 and < tsf_get_presetcount()
-//                 can also be looked up by bank with tsf_get_presetindex
 //   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)
+//   bank: instrument bank number (alternative to preset_index)
+//   preset_number: preset number (alternative to preset_index)
 TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel);
+TSFDEF void tsf_bank_note_on(tsf* f, int bank, int preset_number, int key, float vel);
 
 // Stop playing a note
 TSFDEF void tsf_note_off(tsf* f, int preset_index, int key);
+TSFDEF void tsf_bank_note_off(tsf* f, int bank, int preset_number, int key);
 
 // Render output samples into a buffer
 // You can either render as signed 16-bit values (tsf_render_short) or
@@ -1097,9 +1103,9 @@
 	TSF_FREE(f);
 }
 
-TSFDEF int tsf_get_presetindex(tsf* f, int bank, int preset_number)
+TSFDEF int tsf_get_presetindex(const tsf* f, int bank, int preset_number)
 {
-	struct tsf_preset *presets;
+	const struct tsf_preset *presets;
 	int i, iMax;
 	for (presets = f->presets, i = 0, iMax = f->presetNum; i < iMax; i++)
 		if (presets[i].preset == preset_number && presets[i].bank == bank)
@@ -1107,16 +1113,21 @@
 	return -1;
 }
 
-TSFDEF int tsf_get_presetcount(tsf* f)
+TSFDEF int tsf_get_presetcount(const tsf* f)
 {
 	return f->presetNum;
 }
 
-TSFDEF const char* tsf_get_presetname(tsf* f, int preset)
+TSFDEF const char* tsf_get_presetname(const tsf* f, int preset)
 {
 	return (preset < 0 || preset >= f->presetNum ? TSF_NULL : f->presets[preset].presetName);
 }
 
+TSFDEF const char* tsf_bank_get_presetname(const tsf* f, int bank, int preset_number)
+{
+	return tsf_get_presetname(f, tsf_get_presetindex(f, bank, preset_number));
+}
+
 TSFDEF void tsf_set_output(tsf* f, enum TSFOutputMode outputmode, int samplerate, float globalgaindb)
 {
 	f->outSampleRate = (float)(samplerate >= 1 ? samplerate : 44100.0f);
@@ -1204,6 +1215,11 @@
 	}
 }
 
+TSFDEF void tsf_bank_note_on(tsf* f, int bank, int preset_number, int key, float vel)
+{
+	tsf_note_on(f, tsf_get_presetindex(f, bank, preset_number), key, vel);
+}
+
 TSFDEF void tsf_note_off(tsf* f, int preset_index, int key)
 {
 	struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum;
@@ -1210,6 +1226,11 @@
 	for (; v != vEnd; v++)
 		if (v->playingPreset == preset_index && v->playingKey == key)
 			tsf_voice_end(v, f->outSampleRate);
+}
+
+TSFDEF void tsf_bank_note_off(tsf* f, int bank, int preset_number, int key)
+{
+	tsf_note_off(f, tsf_get_presetindex(f, bank, preset_number), key);
 }
 
 TSFDEF void tsf_render_short(tsf* f, short* buffer, int samples, int flag_mixing)