### Note Playback (tsf_note_on / tsf_note_off) Source: https://context7.com/schellingb/tinysoundfont/llms.txt Functions to start and stop playing notes using a preset index. ```APIDOC ## tsf_note_on / tsf_note_off ### Description Start and stop playing notes using preset index. Key values range from 0-127 (60 = middle C), velocity from 0.0-1.0. ### Parameters - **soundFont** (tsf*) - Required - The soundfont instance. - **preset** (int) - Required - The preset index. - **key** (int) - Required - The MIDI key (0-127). - **velocity** (float) - Required - The note velocity (0.0-1.0). ``` -------------------------------- ### Load and Play SoundFont2 with TinySoundFont Source: https://github.com/schellingb/tinysoundfont/blob/main/README.md Include the library implementation and load a SoundFont2 file. Set the audio output and render a short segment of audio by playing a note. ```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); ``` -------------------------------- ### SFOTool Command Line Usage Source: https://github.com/schellingb/tinysoundfont/blob/main/sfotool/README.md Lists the available command-line arguments for inspecting, dumping, and creating SFO and SF2 files. ```sh sfotool : Show type of sample stream contained (PCM or OGG) sfotool : Dump PCM sample stream to .WAV file sfotool : Dump OGG sample stream to .OGG file sfotool : Write new .SF2 soundfont file using PCM sample stream from .WAV file sfotool : Write new .SFO soundfont file using OGG sample stream from .OGG file ``` -------------------------------- ### Load and Parse MIDI File in C Source: https://context7.com/schellingb/tinysoundfont/llms.txt Demonstrates loading a MIDI file, retrieving file information, and iterating through the message list. Requires defining TML_IMPLEMENTATION before including tml.h. ```c #define TML_IMPLEMENTATION #include "tml.h" int main() { // Load MIDI file tml_message* midi = tml_load_filename("song.mid"); if (!midi) { fprintf(stderr, "Failed to load MIDI file\n"); return 1; } // Get MIDI file information int usedChannels, usedPrograms, totalNotes; unsigned int timeFirstNote, timeLength; tml_get_info(midi, &usedChannels, &usedPrograms, &totalNotes, &timeFirstNote, &timeLength); printf("Channels: %d\n", usedChannels); printf("Programs: %d\n", usedPrograms); printf("Notes: %d\n", totalNotes); printf("First note at: %u ms\n", timeFirstNote); printf("Duration: %u ms\n", timeLength); // Iterate through messages for (tml_message* msg = midi; msg; msg = msg->next) { switch (msg->type) { case TML_NOTE_ON: printf("Note ON: ch=%d key=%d vel=%d @ %u ms\n", msg->channel, msg->key, msg->velocity, msg->time); break; case TML_NOTE_OFF: printf("Note OFF: ch=%d key=%d @ %u ms\n", msg->channel, msg->key, msg->time); break; case TML_PROGRAM_CHANGE: printf("Program Change: ch=%d prog=%d @ %u ms\n", msg->channel, msg->program, msg->time); break; case TML_SET_TEMPO: printf("Tempo: %d microsec/quarter @ %u ms\n", tml_get_tempo_value(msg), msg->time); break; } } // Free memory tml_free(midi); return 0; } ``` -------------------------------- ### Build SFO File Source: https://github.com/schellingb/tinysoundfont/blob/main/sfotool/README.md Creates a new SFO file by combining an existing SF2 file with an OGG compressed sample stream. ```sh sfotool ``` -------------------------------- ### Configure Audio Output Source: https://context7.com/schellingb/tinysoundfont/llms.txt Sets audio rendering parameters such as sample rate, channel mode, and gain. Must be called before rendering. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("soundfont.sf2"); if (!soundFont) return 1; // Output mode options: // TSF_STEREO_INTERLEAVED - L,R,L,R,L,R... (most common) // TSF_STEREO_UNWEAVED - L,L,L...,R,R,R... // TSF_MONO - Single channel output // Configure for stereo output at 48kHz with -6dB gain reduction tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 48000, -6.0f); // Alternative: Set volume as a linear factor (1.0 = 100%) tsf_set_volume(soundFont, 0.5f); // 50% volume tsf_close(soundFont); return 0; } ``` -------------------------------- ### Real-Time MIDI Playback with Audio Callback Source: https://context7.com/schellingb/tinysoundfont/llms.txt Combines TinySoundFont and TinyMidiLoader for real-time MIDI playback. The AudioCallback function processes MIDI messages and renders audio. Ensure your audio system calls this callback. ```c #define TSF_IMPLEMENTATION #include "tsf.h" #define TML_IMPLEMENTATION #include "tml.h" // Global state for audio callback static tsf* g_SoundFont; static tml_message* g_MidiMessage; static double g_MsecTime; // Audio callback (called by audio system) void AudioCallback(float* output, int sampleCount) { int sampleRate = 44100; for (int blockSize = 64; sampleCount > 0; sampleCount -= blockSize) { if (blockSize > sampleCount) blockSize = sampleCount; // Advance playback time g_MsecTime += blockSize * (1000.0 / sampleRate); // Process all MIDI messages up to current time while (g_MidiMessage && g_MsecTime >= g_MidiMessage->time) { switch (g_MidiMessage->type) { case TML_PROGRAM_CHANGE: tsf_channel_set_presetnumber(g_SoundFont, g_MidiMessage->channel, g_MidiMessage->program, (g_MidiMessage->channel == 9)); // Drums on ch9 break; case TML_NOTE_ON: tsf_channel_note_on(g_SoundFont, g_MidiMessage->channel, g_MidiMessage->key, g_MidiMessage->velocity / 127.0f); break; case TML_NOTE_OFF: tsf_channel_note_off(g_SoundFont, g_MidiMessage->channel, g_MidiMessage->key); break; case TML_PITCH_BEND: tsf_channel_set_pitchwheel(g_SoundFont, g_MidiMessage->channel, g_MidiMessage->pitch_bend); break; case TML_CONTROL_CHANGE: tsf_channel_midi_control(g_SoundFont, g_MidiMessage->channel, g_MidiMessage->control, g_MidiMessage->control_value); break; } g_MidiMessage = g_MidiMessage->next; } // Render audio block tsf_render_float(g_SoundFont, output, blockSize, 0); output += blockSize * 2; // Stereo } } int main(int argc, char* argv[]) { // Load SoundFont g_SoundFont = tsf_load_filename("soundfont.sf2"); if (!g_SoundFont) { fprintf(stderr, "Failed to load SoundFont\n"); return 1; } // Load MIDI file tml_message* midiFile = tml_load_filename("song.mid"); if (!midiFile) { fprintf(stderr, "Failed to load MIDI file\n"); tsf_close(g_SoundFont); return 1; } // Initialize g_MidiMessage = midiFile; g_MsecTime = 0; tsf_set_output(g_SoundFont, TSF_STEREO_INTERLEAVED, 44100, -10.0f); // Set up percussion channel (MIDI channel 9) tsf_channel_set_bank_preset(g_SoundFont, 9, 128, 0); // In a real application, integrate AudioCallback with your audio system // (e.g., miniaudio, SDL_Audio, PortAudio, CoreAudio, etc.) // Simulate playback by rendering to buffer float buffer[44100 * 2 * 10]; // 10 seconds buffer AudioCallback(buffer, 44100 * 10); // Cleanup tml_free(midiFile); tsf_close(g_SoundFont); return 0; } ``` -------------------------------- ### Load SoundFont from File Source: https://context7.com/schellingb/tinysoundfont/llms.txt Loads a .sf2 file from the disk. Returns a tsf instance pointer or NULL on failure. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { // Load a SoundFont file tsf* soundFont = tsf_load_filename("piano.sf2"); if (!soundFont) { fprintf(stderr, "Failed to load SoundFont\n"); return 1; } // Configure output: stereo interleaved, 44.1kHz, 0dB gain tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // Use the soundfont... // Clean up when done tsf_close(soundFont); return 0; } ``` -------------------------------- ### Play notes with bank and preset using tsf_bank_note_on Source: https://context7.com/schellingb/tinysoundfont/llms.txt Selects instruments using MIDI-style bank and preset numbers instead of a flat index. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("gm.sf2"); // General MIDI soundfont if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // General MIDI: Bank 0, Preset 0 = Acoustic Grand Piano // Bank 0, Preset 40 = Violin // Bank 128 = Percussion // Play piano note (bank 0, preset 0) if (!tsf_bank_note_on(soundFont, 0, 0, 60, 1.0f)) { printf("Piano preset not found\n"); } // Play violin note (bank 0, preset 40) if (!tsf_bank_note_on(soundFont, 0, 40, 67, 0.9f)) { printf("Violin preset not found\n"); } // Get preset name by bank/preset number const char* name = tsf_bank_get_presetname(soundFont, 0, 0); if (name) printf("Bank 0, Preset 0: %s\n", name); // Render audio float buffer[44100 * 2]; tsf_render_float(soundFont, buffer, 44100, 0); // Stop notes tsf_bank_note_off(soundFont, 0, 0, 60); // Returns 1 on success tsf_bank_note_off(soundFont, 0, 40, 67); // Stop all notes immediately tsf_note_off_all(soundFont); tsf_close(soundFont); return 0; } ``` -------------------------------- ### Load SoundFont via Custom Stream Source: https://context7.com/schellingb/tinysoundfont/llms.txt Uses a custom stream structure for advanced loading scenarios like network streams or compressed archives. ```c #define TSF_IMPLEMENTATION #include "tsf.h" // Custom stream callbacks int my_read(void* data, void* ptr, unsigned int size) { FILE* f = (FILE*)data; return (int)fread(ptr, 1, size, f); } int my_skip(void* data, unsigned int count) { FILE* f = (FILE*)data; return !fseek(f, count, SEEK_CUR); } int main() { FILE* f = fopen("soundfont.sf2", "rb"); if (!f) return 1; // Set up custom stream struct tsf_stream stream; stream.data = f; stream.read = my_read; stream.skip = my_skip; // Load using custom stream tsf* soundFont = tsf_load(&stream); fclose(f); if (!soundFont) { fprintf(stderr, "Failed to load SoundFont\n"); return 1; } tsf_set_output(soundFont, TSF_MONO, 44100, 0.0f); tsf_close(soundFont); return 0; } ``` -------------------------------- ### Load SoundFont from Memory Source: https://context7.com/schellingb/tinysoundfont/llms.txt Loads a SoundFont from a memory buffer. Useful for embedded resources. ```c #define TSF_IMPLEMENTATION #include "tsf.h" // Minimal embedded SoundFont (can be generated or loaded from resources) static const unsigned char embeddedSF2[] = { /* SoundFont data bytes */ }; int main() { // Load SoundFont from memory buffer tsf* soundFont = tsf_load_memory(embeddedSF2, sizeof(embeddedSF2)); if (!soundFont) { fprintf(stderr, "Failed to load embedded SoundFont\n"); return 1; } tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, -10.0f); // Play middle C at full velocity using preset 0 tsf_note_on(soundFont, 0, 60, 1.0f); // Render 22050 samples (0.5 seconds at 44100Hz) short buffer[22050 * 2]; // stereo tsf_render_short(soundFont, buffer, 22050, 0); tsf_close(soundFont); return 0; } ``` -------------------------------- ### Create Independent TinySoundFont Playback Instances Source: https://context7.com/schellingb/tinysoundfont/llms.txt Uses `tsf_copy` to create independent playback instances that share underlying soundfont data. This avoids reloading the soundfont for each instance. Each copy can be configured independently. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { // Load soundfont once tsf* original = tsf_load_filename("orchestra.sf2"); if (!original) return 1; // Create independent copies that share sample data tsf* player1 = tsf_copy(original); tsf* player2 = tsf_copy(original); if (!player1 || !player2) { fprintf(stderr, "Failed to copy instance\n"); tsf_close(original); return 1; } // Each instance can be configured independently tsf_set_output(player1, TSF_STEREO_INTERLEAVED, 44100, 0.0f); tsf_set_output(player2, TSF_STEREO_INTERLEAVED, 48000, -6.0f); // Play different notes on each instance tsf_note_on(player1, 0, 60, 1.0f); tsf_note_on(player2, 0, 72, 1.0f); // Render separately float buffer1[1024 * 2], buffer2[1024 * 2]; tsf_render_float(player1, buffer1, 1024, 0); tsf_render_float(player2, buffer2, 1024, 0); // Close all instances (sample data freed when last instance closes) tsf_close(player1); tsf_close(player2); tsf_close(original); return 0; } ``` -------------------------------- ### Manage MIDI Channels with tsf_channel_set Source: https://context7.com/schellingb/tinysoundfont/llms.txt Configure instrument presets, panning, volume, and pitch settings per channel for MIDI-style playback. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("gm.sf2"); if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // Channel 0: Piano tsf_channel_set_bank_preset(soundFont, 0, 0, 0); // Bank 0, Preset 0 (Piano) tsf_channel_set_pan(soundFont, 0, 0.3f); // Pan left (0.0=left, 0.5=center, 1.0=right) tsf_channel_set_volume(soundFont, 0, 0.8f); // 80% volume // Channel 1: Strings tsf_channel_set_bank_preset(soundFont, 1, 0, 48); // Bank 0, Preset 48 (Strings) tsf_channel_set_pan(soundFont, 1, 0.7f); // Pan right tsf_channel_set_volume(soundFont, 1, 0.6f); // Channel 9: Drums (MIDI convention) tsf_channel_set_presetnumber(soundFont, 9, 0, 1); // flag_mididrums=1 // Set pitch bend (0-16383, center=8192) tsf_channel_set_pitchwheel(soundFont, 0, 8192); // No bend tsf_channel_set_pitchrange(soundFont, 0, 2.0f); // +/- 2 semitones range // Set tuning in semitones tsf_channel_set_tuning(soundFont, 0, 0.5f); // Tune up half semitone // Enable sustain pedal tsf_channel_set_sustain(soundFont, 0, 1); // Play notes on channels tsf_channel_note_on(soundFont, 0, 60, 1.0f); // Piano C4 tsf_channel_note_on(soundFont, 1, 67, 0.8f); // Strings G4 tsf_channel_note_on(soundFont, 9, 36, 1.0f); // Kick drum // Render audio float buffer[44100 * 2]; tsf_render_float(soundFont, buffer, 44100, 0); // Stop notes tsf_channel_note_off(soundFont, 0, 60); tsf_channel_note_off_all(soundFont, 1); // Stop all notes on channel with release tsf_channel_sounds_off_all(soundFont, 9); // Stop immediately (no release) tsf_close(soundFont); return 0; } ``` -------------------------------- ### Render Audio Samples with tsf_render Source: https://context7.com/schellingb/tinysoundfont/llms.txt Use these functions to output synthesized audio to a buffer. The flag_mixing parameter determines whether to clear the buffer or mix into existing data. ```c #define TSF_IMPLEMENTATION #include "tsf.h" #include int main() { tsf* soundFont = tsf_load_filename("piano.sf2"); if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // Play a note tsf_note_on(soundFont, 0, 60, 1.0f); // Render to 16-bit buffer (common for audio files/hardware) // Buffer size = samples * channels * sizeof(short) short shortBuffer[1024 * 2]; // 1024 samples, stereo tsf_render_short(soundFont, shortBuffer, 1024, 0); // flag_mixing=0 clears buffer first // Render to float buffer (common for audio processing) float floatBuffer[1024 * 2]; tsf_render_float(soundFont, floatBuffer, 1024, 0); // Mix into existing buffer (flag_mixing=1) // Useful for combining multiple sound sources tsf_render_float(soundFont, floatBuffer, 1024, 1); // adds to existing data // Check how many voices are currently playing int activeVoices = tsf_active_voice_count(soundFont); printf("Active voices: %d\n", activeVoices); tsf_close(soundFont); return 0; } ``` -------------------------------- ### Audio Rendering: tsf_render_short / tsf_render_float Source: https://context7.com/schellingb/tinysoundfont/llms.txt Functions to render synthesized audio samples into a buffer. tsf_render_short is for 16-bit integer output, and tsf_render_float is for 32-bit floating-point output. The flag_mixing parameter controls whether the buffer is cleared or if new audio is mixed into existing data. ```APIDOC ## tsf_render_short / tsf_render_float ### Description Render synthesized audio samples into a buffer. Use tsf_render_short for 16-bit integer output or tsf_render_float for 32-bit floating point. ### Method `tsf_render_short(tsf *soundfont, short *buffer, int num_samples, int flag_mixing)` `tsf_render_float(tsf *soundfont, float *buffer, int num_samples, int flag_mixing)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **soundfont** (tsf*) - Pointer to the TinySoundFont instance. - **buffer** (short* or float*) - Pointer to the output audio buffer. - **num_samples** (int) - The number of samples to render per channel. - **flag_mixing** (int) - If 0, the buffer is cleared before rendering. If 1, audio is mixed into the existing buffer. ### Request Example ```c // Render to 16-bit buffer short shortBuffer[1024 * 2]; tsf_render_short(soundFont, shortBuffer, 1024, 0); // Render to float buffer and mix float floatBuffer[1024 * 2]; tsf_render_float(soundFont, floatBuffer, 1024, 1); ``` ### Response #### Success Response (200) No explicit return value, audio is written to the provided buffer. #### Response Example None ``` -------------------------------- ### tsf_load_filename Source: https://context7.com/schellingb/tinysoundfont/llms.txt Loads a SoundFont from a file path on disk. ```APIDOC ## tsf_load_filename ### Description Loads a SoundFont directly from a .sf2 file path on disk. Returns a pointer to a tsf instance. ### Parameters - **filename** (const char*) - Required - Path to the .sf2 file. ### Response - **tsf*** (pointer) - Returns a pointer to the loaded tsf instance, or NULL if the file cannot be opened or contains invalid data. ``` -------------------------------- ### Channel-Based API: tsf_channel_set_* Functions Source: https://context7.com/schellingb/tinysoundfont/llms.txt A higher-level API for MIDI-style playback, allowing control over individual channels. Functions include setting instruments, pan, volume, pitch wheel, and other parameters per channel. ```APIDOC ## Channel-Based API: tsf_channel_set_* Functions ### Description Higher-level channel-based API for MIDI-style playback. Set instrument presets, pan, volume, pitch wheel, and other parameters per channel. ### Method `tsf_channel_set_bank_preset(tsf *soundfont, int channel, int bank, int preset)` `tsf_channel_set_pan(tsf *soundfont, int channel, float pan)` `tsf_channel_set_volume(tsf *soundfont, int channel, float volume)` `tsf_channel_set_presetnumber(tsf *soundfont, int channel, int bank, int preset)` `tsf_channel_set_pitchwheel(tsf *soundfont, int channel, int pitch)` `tsf_channel_set_pitchrange(tsf *soundfont, int channel, float range)` `tsf_channel_set_tuning(tsf *soundfont, int channel, float tuning)` `tsf_channel_set_sustain(tsf *soundfont, int channel, int sustain)` `tsf_channel_note_on(tsf *soundfont, int channel, int note, float velocity)` `tsf_channel_note_off(tsf *soundfont, int channel, int note)` `tsf_channel_note_off_all(tsf *soundfont, int channel)` `tsf_channel_sounds_off_all(tsf *soundfont, int channel)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **soundfont** (tsf*) - Pointer to the TinySoundFont instance. - **channel** (int) - The MIDI channel (0-15). - **bank** (int) - The sound bank number. - **preset** (int) - The instrument preset number. - **pan** (float) - Pan position (0.0=left, 0.5=center, 1.0=right). - **volume** (float) - Volume level (0.0 to 1.0). - **pitch** (int) - Pitch wheel value (0-16383, center=8192). - **range** (float) - Pitch bend range in semitones. - **tuning** (float) - Tuning adjustment in semitones. - **sustain** (int) - Sustain pedal state (1=on, 0=off). - **note** (int) - MIDI note number (0-127). - **velocity** (float) - Note velocity (0.0 to 1.0). ### Request Example ```c // Set Piano on channel 0 tsf_channel_set_bank_preset(soundFont, 0, 0, 0); tsf_channel_set_volume(soundFont, 0, 0.8f); // Play a note tsf_channel_note_on(soundFont, 0, 60, 1.0f); // Stop a note tsf_channel_note_off(soundFont, 0, 60); ``` ### Response #### Success Response (200) No explicit return value. Operations modify the state of the soundfont instance. #### Response Example None ``` -------------------------------- ### Play notes with tsf_note_on and tsf_note_off Source: https://context7.com/schellingb/tinysoundfont/llms.txt Triggers and releases notes using preset indices, with key values from 0-127 and velocity from 0.0-1.0. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("piano.sf2"); if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // Get preset information int presetCount = tsf_get_presetcount(soundFont); printf("SoundFont has %d presets\n", presetCount); for (int i = 0; i < presetCount && i < 5; i++) { printf("Preset %d: %s\n", i, tsf_get_presetname(soundFont, i)); } // Play a C major chord using preset 0 int preset = 0; tsf_note_on(soundFont, preset, 60, 1.0f); // C4 at full velocity tsf_note_on(soundFont, preset, 64, 0.8f); // E4 at 80% velocity tsf_note_on(soundFont, preset, 67, 0.8f); // G4 at 80% velocity // Render 1 second of audio short buffer[44100 * 2]; tsf_render_short(soundFont, buffer, 44100, 0); // Stop the notes (with natural release envelope) tsf_note_off(soundFont, preset, 60); tsf_note_off(soundFont, preset, 64); tsf_note_off(soundFont, preset, 67); // Render the release tail tsf_render_short(soundFont, buffer, 44100, 0); tsf_close(soundFont); return 0; } ``` -------------------------------- ### Pre-allocate voices with tsf_set_max_voices Source: https://context7.com/schellingb/tinysoundfont/llms.txt Enables thread-safe operation by pre-allocating memory for voices, preventing allocations during note_on calls. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("soundfont.sf2"); if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); // Pre-allocate 256 voices for thread-safe operation // Returns 0 if allocation fails, 1 on success if (!tsf_set_max_voices(soundFont, 256)) { fprintf(stderr, "Failed to allocate voices\n"); tsf_close(soundFont); return 1; } // Now safe to call tsf_note_on from a different thread // than tsf_render_* (though a mutex is still recommended) tsf_close(soundFont); return 0; } ``` -------------------------------- ### Dump PCM Data Source: https://github.com/schellingb/tinysoundfont/blob/main/sfotool/README.md Extracts the PCM sample stream from an SF2 file into a WAV file. ```sh sfotool ``` -------------------------------- ### Bank-based Note Playback (tsf_bank_note_on / tsf_bank_note_off) Source: https://context7.com/schellingb/tinysoundfont/llms.txt Functions to play notes using bank and preset numbers, useful for MIDI-style instrument selection. ```APIDOC ## tsf_bank_note_on / tsf_bank_note_off ### Description Play notes using bank and preset number instead of preset index. Useful for MIDI-style instrument selection. ### Parameters - **soundFont** (tsf*) - Required - The soundfont instance. - **bank** (int) - Required - The bank number. - **preset** (int) - Required - The preset number. - **key** (int) - Required - The MIDI key (0-127). - **velocity** (float) - Required - The note velocity (0.0-1.0). ### Response - **return** (int) - Returns 1 on success. ``` -------------------------------- ### tsf_load Source: https://context7.com/schellingb/tinysoundfont/llms.txt Loads a SoundFont using a custom stream structure. ```APIDOC ## tsf_load ### Description Generic loading method using a custom stream structure for advanced use cases like network streams or custom file systems. ### Parameters - **stream** (struct tsf_stream*) - Required - Pointer to a stream structure containing read and skip callbacks. ### Response - **tsf*** (pointer) - Returns a pointer to the loaded tsf instance, or NULL if loading fails. ``` -------------------------------- ### tsf_load_memory Source: https://context7.com/schellingb/tinysoundfont/llms.txt Loads a SoundFont from a memory buffer. ```APIDOC ## tsf_load_memory ### Description Loads a SoundFont from a block of memory, useful for embedded soundfonts. ### Parameters - **data** (const void*) - Required - Pointer to the data buffer. - **size** (unsigned int) - Required - Size of the data buffer in bytes. ### Response - **tsf*** (pointer) - Returns a pointer to the loaded tsf instance, or NULL if loading fails. ``` -------------------------------- ### tsf_set_output Source: https://context7.com/schellingb/tinysoundfont/llms.txt Configures the audio output parameters for rendering. ```APIDOC ## tsf_set_output ### Description Configures the audio output parameters for rendering. Must be called before rendering audio. ### Parameters - **soundFont** (tsf*) - Required - The tsf instance. - **outputMode** (int) - Required - Output mode (TSF_STEREO_INTERLEAVED, TSF_STEREO_UNWEAVED, or TSF_MONO). - **sampleRate** (int) - Required - Sample rate in Hz (e.g., 44100). - **gain** (float) - Required - Gain in dB. ``` -------------------------------- ### Apply MIDI Control Changes with tsf_channel_midi_control Source: https://context7.com/schellingb/tinysoundfont/llms.txt Send standard MIDI control messages to channels to adjust parameters like volume, pan, and sustain pedal state. ```c #define TSF_IMPLEMENTATION #include "tsf.h" int main() { tsf* soundFont = tsf_load_filename("gm.sf2"); if (!soundFont) return 1; tsf_set_output(soundFont, TSF_STEREO_INTERLEAVED, 44100, 0.0f); tsf_channel_set_bank_preset(soundFont, 0, 0, 0); // MIDI Controller numbers (common ones): // 7 = Volume MSB // 10 = Pan MSB // 11 = Expression MSB // 64 = Sustain Pedal (>=64 = on) // 120 = All Sound Off // 121 = Reset All Controllers // 123 = All Notes Off // Set channel volume via MIDI controller (0-127) tsf_channel_midi_control(soundFont, 0, 7, 100); // Volume = 100 // Set pan via MIDI controller tsf_channel_midi_control(soundFont, 0, 10, 64); // Pan center // Sustain pedal on tsf_channel_midi_control(soundFont, 0, 64, 127); tsf_channel_note_on(soundFont, 0, 60, 1.0f); // Render some audio float buffer[22050 * 2]; tsf_render_float(soundFont, buffer, 22050, 0); // Release sustain - held notes will now release tsf_channel_midi_control(soundFont, 0, 64, 0); // All notes off on channel tsf_channel_midi_control(soundFont, 0, 123, 0); // Reset all controllers to defaults tsf_channel_midi_control(soundFont, 0, 121, 0); tsf_close(soundFont); return 0; } ``` -------------------------------- ### tsf_set_max_voices Source: https://context7.com/schellingb/tinysoundfont/llms.txt Configures the maximum number of voices for thread-safe operation, preventing memory allocation during note_on calls. ```APIDOC ## tsf_set_max_voices ### Description Pre-allocates a maximum number of voices for thread-safe operation. When set, no memory allocation occurs during note_on, enabling safer multi-threaded audio rendering. ### Parameters - **soundFont** (tsf*) - Required - The soundfont instance. - **max_voices** (int) - Required - The number of voices to pre-allocate. ### Response - **return** (int) - Returns 0 if allocation fails, 1 on success. ``` -------------------------------- ### MIDI Control Change: tsf_channel_midi_control Source: https://context7.com/schellingb/tinysoundfont/llms.txt Applies MIDI control change messages to a specified channel. This function supports common controllers like volume, pan, sustain, expression, and reset messages. ```APIDOC ## MIDI Control Change: tsf_channel_midi_control ### Description Apply MIDI control change messages to a channel. Supports common controllers like volume, pan, sustain, expression, and more. ### Method `tsf_channel_midi_control(tsf *soundfont, int channel, int controller, int value)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **soundfont** (tsf*) - Pointer to the TinySoundFont instance. - **channel** (int) - The MIDI channel (0-15). - **controller** (int) - The MIDI controller number (e.g., 7 for Volume, 10 for Pan, 64 for Sustain). - **value** (int) - The value for the controller (typically 0-127). ### Request Example ```c // Set channel volume via MIDI controller tsf_channel_midi_control(soundFont, 0, 7, 100); // Sustain pedal on tsf_channel_midi_control(soundFont, 0, 64, 127); // All notes off on channel tsf_channel_midi_control(soundFont, 0, 123, 0); ``` ### Response #### Success Response (200) No explicit return value. The specified MIDI control change is applied to the channel. #### Response Example None ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.