1#define TSF_IMPLEMENTATION
2#include "../tsf.h"
3
4#include "../minisdl_audio.h"
5
6//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes)
7const static unsigned char MinimalSoundFont[] = {
8#define TEN0 0,0,0,0,0,0,0,0,0,0
9 'R','I','F','F',220,1,0,0,'s','f','b','k',
10 'L','I','S','T',88,1,0,0,'p','d','t','a',
11 'p','h','d','r',76,TEN0,TEN0,TEN0,TEN0,0,0,0,0,TEN0,0,0,0,0,0,0,0,255,0,255,0,1,TEN0,0,0,0,
12 'p','b','a','g',8,0,0,0,0,0,0,0,1,0,0,0,'p','m','o','d',10,TEN0,0,0,0,'p','g','e','n',8,0,0,0,41,0,0,0,0,0,0,0,
13 'i','n','s','t',44,TEN0,TEN0,0,0,0,0,0,0,0,0,TEN0,0,0,0,0,0,0,0,1,0,
14 'i','b','a','g',8,0,0,0,0,0,0,0,2,0,0,0,'i','m','o','d',10,TEN0,0,0,0,
15 'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0,
16 's','h','d','r',92,TEN0,TEN0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,0,0,34,86,0,0,60,0,0,0,1,TEN0,TEN0,TEN0,TEN0,0,0,0,0,0,0,0,
17 'L','I','S','T',112,0,0,0,'s','d','t','a','s','m','p','l',100,0,0,0,86,0,119,3,31,7,147,10,43,14,169,17,58,21,189,24,73,28,204,31,73,35,249,38,46,42,71,46,250,48,150,53,242,55,126,60,151,63,108,66,126,72,207,
18 70,86,83,100,72,74,100,163,39,241,163,59,175,59,179,9,179,134,187,6,186,2,194,5,194,15,200,6,202,96,206,159,209,35,213,213,216,45,220,221,223,76,227,221,230,91,234,242,237,105,241,8,245,118,248,32,252
19};
20
21// Holds the global instance pointer
22static tsf* g_TinySoundFont;
23
24// Callback function called by the audio thread
25static void AudioCallback(void* data, Uint8 *stream, int len) {
26 // Note we don't do any thread concurrency control here because in this
27 // example all notes are started before the audio playback begins.
28 // If you do play notes while the audio thread renders output you
29 // will need a mutex of some sort.
30 int SampleCount = (len / (2 * sizeof(short))); //2 output channels
31 tsf_render_short(g_TinySoundFont, (short*)stream, SampleCount, 0);
32}
33
34int main(int argc, char *argv[]) {
35 // Define the desired audio output format we request
36 SDL_AudioSpec OutputAudioSpec;
37 OutputAudioSpec.freq = 44100;
38 OutputAudioSpec.format = AUDIO_S16;
39 OutputAudioSpec.channels = 2;
40 OutputAudioSpec.samples = 4096;
41 OutputAudioSpec.callback = AudioCallback;
42
43 // Initialize the audio system
44 if (SDL_AudioInit(NULL) < 0) {
45 fprintf(stderr, "Could not initialize audio hardware or driver\n");
46 return 1;
47 }
48
49 // Load the SoundFont from the memory block
50 g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont));
51 if (!g_TinySoundFont) {
52 fprintf(stderr, "Could not load soundfont\n");
53 return 1;
54 }
55
56 // Set the rendering output mode to 44.1khz and -10 decibel gain
57 tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, -10);
58
59 // Start two notes before starting the audio playback
60 tsf_note_on(g_TinySoundFont, 0, 48, 1.0f); //C2
61 tsf_note_on(g_TinySoundFont, 0, 52, 1.0f); //E2
62
63 // Request the desired audio output format
64 if (SDL_OpenAudio(&OutputAudioSpec, NULL) < 0) {
65 fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n");
66 return 1;
67 }
68
69 // Start the actual audio playback here
70 // The audio thread will begin to call our AudioCallback function
71 SDL_PauseAudio(0);
72
73 // Let the audio callback play some sound for 3 seconds
74 SDL_Delay(3000);
75
76 // We could call tsf_close(g_TinySoundFont) and SDL_DestroyMutex(g_Mutex)
77 // here to free the memory and resources but we just let the OS clean up
78 // because the process ends here.
79 return 0;
80}
81