aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorWohlstand <admin@wohlnet.ru>2021-01-09 21:19:05 +0300
committerWohlstand <admin@wohlnet.ru>2021-01-09 21:19:05 +0300
commit891610782412b69bb5f5589d45f6ba06e6713c5e (patch)
tree2432ac8f0502aacd4f58f3b3a48ba3871677cbaf /examples
parent914286779ff5154dd811a8827becd03cea672f0e (diff)
downloadlibADLMIDI-891610782412b69bb5f5589d45f6ba06e6713c5e.tar.gz
libADLMIDI-891610782412b69bb5f5589d45f6ba06e6713c5e.tar.bz2
libADLMIDI-891610782412b69bb5f5589d45f6ba06e6713c5e.zip
Tune the SDL2 Audio C example
Diffstat (limited to 'examples')
-rw-r--r--examples/sdl2_audio/CMakeLists.txt21
-rw-r--r--examples/sdl2_audio/sdl2_sample.c64
2 files changed, 76 insertions, 9 deletions
diff --git a/examples/sdl2_audio/CMakeLists.txt b/examples/sdl2_audio/CMakeLists.txt
index 0732c10..d02631f 100644
--- a/examples/sdl2_audio/CMakeLists.txt
+++ b/examples/sdl2_audio/CMakeLists.txt
@@ -1,9 +1,24 @@
+if(POLICY CMP0111)
+ cmake_policy(SET CMP0111 NEW)
+endif()
+
find_package(SDL2 REQUIRED)
-string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)
+add_library(ADLMIDI_SDL2 INTERFACE)
+if(TARGET SDL2::SDL2)
+ if(MINGW)
+ target_link_libraries(ADLMIDI_SDL2 INTERFACE mingw32 SDL2::SDL2main SDL2::SDL2)
+ else()
+ target_link_libraries(ADLMIDI_SDL2 INTERFACE SDL2::SDL2)
+ endif()
+ target_include_directories(ADLMIDI_SDL2 INTERFACE ${SDL2_INCLUDE_DIRS})
+else()
+ string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)
+ target_include_directories(ADLMIDI_SDL2 INTERFACE ${SDL2_INCLUDE_DIRS})
+ target_link_libraries(ADLMIDI_SDL2 INTERFACE ${SDL2_LIBRARIES})
+endif()
add_executable(adlmidi_sdl2_demo sdl2_sample.c)
-target_include_directories(adlmidi_sdl2_demo PRIVATE ${SDL2_INCLUDE_DIRS})
-target_link_libraries(adlmidi_sdl2_demo PRIVATE ADLMIDI ${SDL2_LIBRARIES})
+target_link_libraries(adlmidi_sdl2_demo PRIVATE ADLMIDI ADLMIDI_SDL2)
set_nopie(adlmidi_sdl2_demo)
diff --git a/examples/sdl2_audio/sdl2_sample.c b/examples/sdl2_audio/sdl2_sample.c
index b6307d7..b5b1a11 100644
--- a/examples/sdl2_audio/sdl2_sample.c
+++ b/examples/sdl2_audio/sdl2_sample.c
@@ -1,4 +1,7 @@
+#include <stdio.h>
+#include <signal.h>
#include <SDL2/SDL.h>
+
#include <adlmidi.h>
/* prototype for our audio callback */
@@ -7,16 +10,26 @@ void my_audio_callback(void *midi_player, Uint8 *stream, int len);
/* variable declarations */
static Uint32 is_playing = 0; /* remaining length of the sample we have to play */
-static short buffer[4096]; /* Audio buffer */
+static Uint8 buffer[8192]; /* Audio buffer */
+static struct ADLMIDI_AudioFormat s_audioFormat;
+
+static void stop_playing(int sig)
+{
+ (void)sig;
+ is_playing = 0;
+}
int main(int argc, char *argv[])
{
/* local variables */
- static SDL_AudioSpec spec; /* the specs of our piece of music */
+ static SDL_AudioSpec spec, obtained; /* the specs of our piece of music */
static struct ADL_MIDIPlayer *midi_player = NULL; /* Instance of ADLMIDI player */
static const char *music_path = NULL; /* Path to music file */
+ signal(SIGINT, stop_playing);
+ signal(SIGTERM, stop_playing);
+
if (argc < 2)
{
fprintf(stderr, "\n"
@@ -47,18 +60,54 @@ int main(int argc, char *argv[])
return 1;
}
+ adl_switchEmulator(midi_player, ADLMIDI_EMU_NUKED);
+
/* set the callback function */
spec.callback = my_audio_callback;
/* set ADLMIDI's descriptor as userdata to use it for sound generation */
spec.userdata = midi_player;
/* Open the audio device */
- if (SDL_OpenAudio(&spec, NULL) < 0)
+ if (SDL_OpenAudio(&spec, &obtained) < 0)
{
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return 1;
}
+ switch(obtained.format)
+ {
+ case AUDIO_S8:
+ s_audioFormat.type = ADLMIDI_SampleType_S8;
+ s_audioFormat.containerSize = sizeof(int8_t);
+ s_audioFormat.sampleOffset = sizeof(int8_t) * 2;
+ break;
+ case AUDIO_U8:
+ s_audioFormat.type = ADLMIDI_SampleType_U8;
+ s_audioFormat.containerSize = sizeof(uint8_t);
+ s_audioFormat.sampleOffset = sizeof(uint8_t) * 2;
+ break;
+ case AUDIO_S16:
+ s_audioFormat.type = ADLMIDI_SampleType_S16;
+ s_audioFormat.containerSize = sizeof(int16_t);
+ s_audioFormat.sampleOffset = sizeof(int16_t) * 2;
+ break;
+ case AUDIO_U16:
+ s_audioFormat.type = ADLMIDI_SampleType_U16;
+ s_audioFormat.containerSize = sizeof(uint16_t);
+ s_audioFormat.sampleOffset = sizeof(uint16_t) * 2;
+ break;
+ case AUDIO_S32:
+ s_audioFormat.type = ADLMIDI_SampleType_S32;
+ s_audioFormat.containerSize = sizeof(int32_t);
+ s_audioFormat.sampleOffset = sizeof(int32_t) * 2;
+ break;
+ case AUDIO_F32:
+ s_audioFormat.type = ADLMIDI_SampleType_F32;
+ s_audioFormat.containerSize = sizeof(float);
+ s_audioFormat.sampleOffset = sizeof(float) * 2;
+ break;
+ }
+
/* Optionally Setup ADLMIDI as you want */
/* Set using of embedded bank by ID */
@@ -106,10 +155,13 @@ void my_audio_callback(void *midi_player, Uint8 *stream, int len)
struct ADL_MIDIPlayer* p = (struct ADL_MIDIPlayer*)midi_player;
/* Convert bytes length into total count of samples in all channels */
- int samples_count = len / 2;
+ int samples_count = len / s_audioFormat.containerSize;
/* Take some samples from the ADLMIDI */
- samples_count = adl_play(p, samples_count, (short*)buffer);
+ samples_count = adl_playFormat(p, samples_count,
+ buffer,
+ buffer + s_audioFormat.containerSize,
+ &s_audioFormat);
if(samples_count <= 0)
{
@@ -119,6 +171,6 @@ void my_audio_callback(void *midi_player, Uint8 *stream, int len)
}
/* Send buffer to the audio device */
- SDL_memcpy(stream, (Uint8*)buffer, samples_count * 2);
+ SDL_memcpy(stream, buffer, samples_count * s_audioFormat.containerSize);
}