TinySoundFont
TinySoundFont copied to clipboard
Examples with raylib
trafficstars
Is there any examples of usage of this library alongside raylib?
I don't know much about raylib, but just browsing the website, it looks like the audio_raw_stream example basically should be all you need. If you take that example, switch it to use stereo by changing the 1 in LoadAudioStream(44100, 16, 1); to a 2, then replace the body of AudioInputCallback with the body of the AudioCallback from one of the examples of TSF, I can't see why it wouldn't work.
Basically something like this (untested):
#include "raylib.h"
#include <stdlib.h> // Required for: malloc(), free()
#include <math.h> // Required for: sinf()
#include <string.h> // Required for: memcpy()
#define MAX_SAMPLES 512
#define MAX_SAMPLES_PER_UPDATE 4096
#define TSF_IMPLEMENTATION
#include "tsf.h"
//This is a minimal SoundFont with a single loopin saw-wave sample/instrument/preset (484 bytes)
const static unsigned char MinimalSoundFont[] =
{
#define TEN0 0,0,0,0,0,0,0,0,0,0
'R','I','F','F',220,1,0,0,'s','f','b','k',
'L','I','S','T',88,1,0,0,'p','d','t','a',
'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,
'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,
'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,
'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,
'i','g','e','n',12,0,0,0,54,0,1,0,53,0,0,0,0,0,0,0,
'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,
'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,
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
};
// Holds the global instance pointer
static tsf* g_TinySoundFont;
// Audio input processing callback
void AudioInputCallback(void *buffer, unsigned int frames)
{
tsf_render_short(g_TinySoundFont, (short*)buffer, frames, 0);
}
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Load the SoundFont from the memory block
g_TinySoundFont = tsf_load_memory(MinimalSoundFont, sizeof(MinimalSoundFont));
if (!g_TinySoundFont)
{
fprintf(stderr, "Could not load soundfont\n");
return 1;
}
// Set the rendering output mode to 44.1khz and -10 decibel gain
tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, -10);
// Start two notes before starting the audio playback
tsf_note_on(g_TinySoundFont, 0, 48, 1.0f); //C2
tsf_note_on(g_TinySoundFont, 0, 52, 1.0f); //E2
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming");
InitAudioDevice(); // Initialize audio device
SetAudioStreamBufferSizeDefault(MAX_SAMPLES_PER_UPDATE);
// Init raw audio stream (sample rate: 44100, sample size: 16bit-short, channels: 1-mono)
AudioStream stream = LoadAudioStream(44100, 16, 1);
SetAudioStreamCallback(stream, AudioInputCallback);
... remaining code of the audio_raw_stream.c example ..
Can you try that and report back?
Will try! Thank you so much!