From 40a899bd6ee536eae093337bf2d0dcc8db4e46f1 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Mon, 7 Oct 2024 19:30:56 +0200 Subject: Moved example code examples folder --- examples/Makefile | 5 +++ examples/example1.c | 81 +++++++++++++++++++++++++++++++++ examples/example2.c | 76 +++++++++++++++++++++++++++++++ examples/example4.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 289 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/example1.c create mode 100644 examples/example2.c create mode 100644 examples/example4.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..5f77125 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,5 @@ +tests: + $(CC) -Wall example1.c ../minisdl_audio.c -lm -ldl -lpthread -o example1 + $(CC) -Wall example2.c ../minisdl_audio.c -lm -ldl -lpthread -o example2 + $(CC) -Wall example4.c -lasound -lm -o example4 + diff --git a/examples/example1.c b/examples/example1.c new file mode 100644 index 0000000..91800ad --- /dev/null +++ b/examples/example1.c @@ -0,0 +1,81 @@ +#define TSF_IMPLEMENTATION +#include "../tsf.h" + +#include "../minisdl_audio.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; + +// Callback function called by the audio thread +static void AudioCallback(void* data, Uint8 *stream, int len) { + // Note we don't do any thread concurrency control here because in this + // example all notes are started before the audio playback begins. + // If you do play notes while the audio thread renders output you + // will need a mutex of some sort. + int SampleCount = (len / (2 * sizeof(short))); //2 output channels + tsf_render_short(g_TinySoundFont, (short*)stream, SampleCount, 0); +} + +int main(int argc, char *argv[]) { + // Define the desired audio output format we request + SDL_AudioSpec OutputAudioSpec; + OutputAudioSpec.freq = 44100; + OutputAudioSpec.format = AUDIO_S16; + OutputAudioSpec.channels = 2; + OutputAudioSpec.samples = 4096; + OutputAudioSpec.callback = AudioCallback; + + // Initialize the audio system + if (SDL_AudioInit(NULL) < 0) { + fprintf(stderr, "Could not initialize audio hardware or driver\n"); + return 1; + } + + // 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 + + // Request the desired audio output format + if (SDL_OpenAudio(&OutputAudioSpec, NULL) < 0) { + fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n"); + return 1; + } + + // Start the actual audio playback here + // The audio thread will begin to call our AudioCallback function + SDL_PauseAudio(0); + + // Let the audio callback play some sound for 3 seconds + SDL_Delay(3000); + + // We could call tsf_close(g_TinySoundFont) and SDL_DestroyMutex(g_Mutex) + // here to free the memory and resources but we just let the OS clean up + // because the process ends here. + return 0; +} + diff --git a/examples/example2.c b/examples/example2.c new file mode 100644 index 0000000..b4c7d73 --- /dev/null +++ b/examples/example2.c @@ -0,0 +1,76 @@ +#include "../minisdl_audio.h" + +#define TSF_IMPLEMENTATION +#include "../tsf.h" + +// Holds the global instance pointer +static tsf* g_TinySoundFont; + +// A Mutex so we don't call note_on/note_off while rendering audio samples +static SDL_mutex* g_Mutex; + +static void AudioCallback(void* data, Uint8 *stream, int len) { + // Render the audio samples in float format + int SampleCount = (len / (2 * sizeof(float))); //2 output channels + SDL_LockMutex(g_Mutex); //get exclusive lock + tsf_render_float(g_TinySoundFont, (float*)stream, SampleCount, 0); + SDL_UnlockMutex(g_Mutex); +} + +int main(int argc, char *argv[]) { + int i, Notes[7] = { 48, 50, 52, 53, 55, 57, 59 }; + + // Define the desired audio output format we request + SDL_AudioSpec OutputAudioSpec; + OutputAudioSpec.freq = 44100; + OutputAudioSpec.format = AUDIO_F32; + OutputAudioSpec.channels = 2; + OutputAudioSpec.samples = 4096; + OutputAudioSpec.callback = AudioCallback; + + // Initialize the audio system + if (SDL_AudioInit(TSF_NULL) < 0) { + fprintf(stderr, "Could not initialize audio hardware or driver\n"); + return 1; + } + + // Load the SoundFont from a file + g_TinySoundFont = tsf_load_filename("../sf2/florestan-subset.sf2"); + if (!g_TinySoundFont) { + fprintf(stderr, "Could not load SoundFont\n"); + return 1; + } + + // Set the SoundFont rendering output mode + tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, 0); + + // Create the mutex + g_Mutex = SDL_CreateMutex(); + + // Request the desired audio output format + if (SDL_OpenAudio(&OutputAudioSpec, TSF_NULL) < 0) { + fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n"); + return 1; + } + + // Start the actual audio playback here + // The audio thread will begin to call our AudioCallback function + SDL_PauseAudio(0); + + // Loop through all the presets in the loaded SoundFont + for (i = 0; i < tsf_get_presetcount(g_TinySoundFont); i++) { + //Get exclusive mutex lock, end the previous note and play a new note + printf("Play note %d with preset #%d '%s'\n", Notes[i % 7], i, tsf_get_presetname(g_TinySoundFont, i)); + SDL_LockMutex(g_Mutex); + tsf_note_off(g_TinySoundFont, i - 1, Notes[(i - 1) % 7]); + tsf_note_on(g_TinySoundFont, i, Notes[i % 7], 1.0f); + SDL_UnlockMutex(g_Mutex); + SDL_Delay(1000); + } + + // We could call tsf_close(g_TinySoundFont) and SDL_DestroyMutex(g_Mutex) + // here to free the memory and resources but we just let the OS clean up + // because the process ends here. + return 0; +} + diff --git a/examples/example4.c b/examples/example4.c new file mode 100644 index 0000000..1dc9ebe --- /dev/null +++ b/examples/example4.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include + +#define CLIENT_NAME "ttdaw" +#define MAX_MIDI_PORTS 1 + +static snd_seq_t *seq_handle; +static snd_seq_addr_t *ports; +static int rate = 44100; +static int stop = 0; + +static void sighandler(int sig ATTRIBUTE_UNUSED) { + stop = 1; +} + +int main(void) { + fprintf(stdout, "Example: Reading MIDI input\n"); + + snd_seq_t *seq_handle; + + if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) { + fprintf(stderr, "Error opening ALSA sequencer.\n"); + exit(1); + } + + if (snd_seq_set_client_name(seq_handle, CLIENT_NAME) < 0) { + fprintf(stderr, "Could not set up client name.\n"); + exit(1); + } + + if (snd_seq_create_simple_port(seq_handle, CLIENT_NAME, + SND_SEQ_PORT_CAP_WRITE | + SND_SEQ_PORT_CAP_SUBS_WRITE, + SND_SEQ_PORT_TYPE_MIDI_GENERIC | + SND_SEQ_PORT_TYPE_APPLICATION) < 0) { + fprintf(stderr, "Error creating sequencer port.\n"); + exit(1); + } + + // Connecting ports. + char *port_name = "28:0"; + ports = realloc(ports, MAX_MIDI_PORTS * sizeof(snd_seq_addr_t)); + if (snd_seq_parse_address(seq_handle, &ports[0], port_name) < 0) { + fprintf(stderr, "Invalid port %s.\n", port_name); + exit(1); + } + + // Listing assigned ports. + fprintf(stdout, "Ports:\n"); + for (int j = 0; j < MAX_MIDI_PORTS; j++) { + fprintf(stdout, " client: %d, port: %d\n", ports[j].client, ports[j].port); + } + + // Connecting ports. + for (int i = 0; i < MAX_MIDI_PORTS; ++i) { + int err = snd_seq_connect_from(seq_handle, 0, ports[i].client, ports[i].port); + if (err < 0) { + fprintf(stderr, "Cannot connect from port %d:%d - %s", ports[i].client, ports[i].port, snd_strerror(err)); + exit(1); + } + } + + if (snd_seq_nonblock(seq_handle, 1) < 0) { + fprintf(stderr, "Set nonblock mode failed."); + exit(1); + } + + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); + + // Reading MIDI device. + struct pollfd *pfds; + size_t npfds; + + npfds = snd_seq_poll_descriptors_count(seq_handle, POLLIN); + pfds = alloca(sizeof(*pfds) * npfds); + + for (;;) { + snd_seq_poll_descriptors(seq_handle, pfds, npfds, POLLIN); + if (poll(pfds, npfds, -1) < 0) { + break; + } + + for (;;) { + snd_seq_event_t *ev; + + if (snd_seq_event_input(seq_handle, &ev) < 0) { + break; + } + + if (ev) { + switch (ev->type) { + case SND_SEQ_EVENT_NOTEON: + printf("%3d:%-3dNote on %2d, note %d, velocity: %3d\n", + ev->source.client, ev->source.port, + ev->data.note.channel, + ev->data.note.note, + ev->data.note.velocity); + break; + + case SND_SEQ_EVENT_NOTEOFF: + printf("%3d:%-3dNote off\t%2d, note %d\n", + ev->source.client, ev->source.port, + ev->data.note.channel, + ev->data.note.note); + break; + + + } + } + } + + fflush(stdout); + + if (stop) { + break; + } + } + + fprintf(stdout, "Finishing up\n"); + snd_seq_close(seq_handle); + return 0; +} + -- cgit v1.2.3