Skip to content

Instantly share code, notes, and snippets.

@DenisBelmondo
Last active January 21, 2024 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisBelmondo/8eb3049df06be849600a53e69cf7a97e to your computer and use it in GitHub Desktop.
Save DenisBelmondo/8eb3049df06be849600a53e69cf7a97e to your computer and use it in GitHub Desktop.
libADLMIDI + raylib example
#include <adlmidi.h>
#include <raylib.h>
#include <stdio.h>
#define MAX_SAMPLES_PER_UPDATE 4096
static struct ADL_MIDIPlayer *midi_player;
static struct ADLMIDI_AudioFormat adl_audio_format = {
ADLMIDI_SampleType_F32,
sizeof(float),
sizeof(float) * 2,
};
void AudioInputCallback(void *buffer, unsigned int frames)
{
adl_playFormat(midi_player, frames * 2, (ADL_UInt8 *)buffer, (ADL_UInt8 *)buffer + adl_audio_format.containerSize, &adl_audio_format);
}
int main(void)
{
int err = 0;
midi_player = adl_init(44100);
if (!midi_player)
{
fprintf(stderr, "Couldn't initialize ADLMIDI: %s\n", adl_errorString());
return 1;
}
adl_switchEmulator(midi_player, ADLMIDI_EMU_NUKED);
adl_setNumChips(midi_player, 2);
adl_setBank(midi_player, 72);
adl_setLoopEnabled(midi_player, 1);
if (adl_openFile(midi_player, "dd3level1b.mid") < 0)
{
fprintf(stderr, "Couldn't open music file: %s\n", adl_errorInfo(midi_player));
err = 1;
goto close;
}
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming");
InitAudioDevice();
SetAudioStreamBufferSizeDefault(MAX_SAMPLES_PER_UPDATE);
// sample rate: 44100, sample size: 32 bits, 2 channels (stereo)
AudioStream stream = LoadAudioStream(44100, 32, 2);
SetAudioStreamCallback(stream, AudioInputCallback);
PlayAudioStream(stream);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
close:
UnloadAudioStream(stream);
CloseAudioDevice();
adl_close(midi_player);
CloseWindow();
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment