Add Sdl2 driver (#478)

This commit is contained in:
carlo-bramini 2018-12-14 16:43:35 +01:00 committed by Tom M
parent d5ba910f3c
commit 978283bbf0
11 changed files with 472 additions and 4 deletions

View file

@ -70,6 +70,7 @@ option ( enable-oss "compile OSS support (if it is available)" on )
option ( enable-dsound "compile DirectSound support (if it is available)" on )
option ( enable-waveout "compile Windows WaveOut support (if it is available)" on )
option ( enable-winmidi "compile Windows MIDI support (if it is available)" on )
option ( enable-sdl2 "compile SDL2 audio support (if it is available)" on )
option ( enable-pkgconfig "use pkg-config to locate fluidsynth's (mostly optional) dependencies" on )
option ( enable-pulseaudio "compile PulseAudio support (if it is available)" on )
option ( enable-readline "compile readline lib line editing (if it is available)" on )
@ -559,6 +560,20 @@ if ( enable-oss )
set ( OSS_SUPPORT ${OSS_FOUND} )
endif ( enable-oss )
unset ( SDL2_SUPPORT CACHE )
unset ( SDL2_INCLUDE_DIR CACHE )
unset ( SDL2_LIBRARY CACHE )
if ( enable-sdl2 )
find_package ( SDL2 )
if ( SDL2_FOUND )
set ( SDL2_SUPPORT ${SDL2_FOUND} )
else ( SDL2_FOUND)
unset ( SDL2_INCLUDE_DIR CACHE )
unset ( SDL2_LIBRARY CACHE )
endif ( SDL2_FOUND )
endif ( enable-sdl2 )
unset ( MIDISHARE_SUPPORT CACHE )
if ( enable-midishare )
find_package ( MidiShare QUIET )

164
cmake_admin/FindSDL2.cmake Normal file
View file

@ -0,0 +1,164 @@
# Locate SDL2 library
# This module defines
# SDL2_LIBRARY, the name of the library to link against
# SDL2_FOUND, if false, do not try to link to SDL2
# SDL2_INCLUDE_DIR, where to find SDL.h
#
# This module responds to the the flag:
# SDL2_BUILDING_LIBRARY
# If this is defined, then no SDL2main will be linked in because
# only applications need main().
# Otherwise, it is assumed you are building an application and this
# module will attempt to locate and set the the proper link flags
# as part of the returned SDL2_LIBRARY variable.
#
# Don't forget to include SDLmain.h and SDLmain.m your project for the
# OS X framework based version. (Other versions link to -lSDL2main which
# this module will try to find on your behalf.) Also for OS X, this
# module will automatically add the -framework Cocoa on your behalf.
#
#
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
# as appropriate. These values are used to generate the final SDL2_LIBRARY
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
#
#
# $SDL2DIR is an environment variable that would
# correspond to the ./configure --prefix=$SDL2DIR
# used in building SDL2.
# l.e.galup 9-20-02
#
# Modified by Eric Wing.
# Added code to assist with automated building by using environmental variables
# and providing a more controlled/consistent search behavior.
# Added new modifications to recognize OS X frameworks and
# additional Unix paths (FreeBSD, etc).
# Also corrected the header search path to follow "proper" SDL guidelines.
# Added a search for SDL2main which is needed by some platforms.
# Added a search for threads which is needed by some platforms.
# Added needed compile switches for MinGW.
#
# On OSX, this will prefer the Framework version (if found) over others.
# People will have to manually change the cache values of
# SDL2_LIBRARY to override this selection or set the CMake environment
# CMAKE_INCLUDE_PATH to modify the search paths.
#
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
# This needed to change because "proper" SDL convention
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
# reasons because not all systems place things in SDL2/ (see FreeBSD).
#=============================================================================
# Copyright 2003-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
SET(SDL2_SEARCH_PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_PATH(SDL2_INCLUDE_DIR SDL.h
HINTS
$ENV{SDL2DIR} ${SDL2_ROOT}
PATH_SUFFIXES include/SDL2 include
PATHS ${SDL2_SEARCH_PATHS}
)
FIND_LIBRARY(SDL2_LIBRARY_TEMP
NAMES SDL2
HINTS
$ENV{SDL2DIR} ${SDL2_ROOT}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)
IF(NOT SDL2_BUILDING_LIBRARY)
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
# Non-OS X framework versions expect you to also dynamically link to
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
# seem to provide SDL2main for compatibility even though they don't
# necessarily need it.
FIND_LIBRARY(SDL2MAIN_LIBRARY
NAMES SDL2main
HINTS
$ENV{SDL2DIR} ${SDL2_ROOT}
PATH_SUFFIXES lib64 lib
PATHS ${SDL2_SEARCH_PATHS}
)
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# SDL2 may require threads on your system.
# The Apple build may not need an explicit flag because one of the
# frameworks may already provide it.
# But for non-OSX systems, I will use the CMake Threads package.
IF(NOT APPLE)
FIND_PACKAGE(Threads)
ENDIF(NOT APPLE)
# MinGW needs an additional library, mwindows
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
# (Actually on second look, I think it only needs one of the m* libraries.)
IF(MINGW)
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
ENDIF(MINGW)
IF(SDL2_LIBRARY_TEMP)
# For SDL2main
IF(NOT SDL2_BUILDING_LIBRARY)
IF(SDL2MAIN_LIBRARY)
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(SDL2MAIN_LIBRARY)
ENDIF(NOT SDL2_BUILDING_LIBRARY)
# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
# CMake doesn't display the -framework Cocoa string in the UI even
# though it actually is there if I modify a pre-used variable.
# I think it has something to do with the CACHE STRING.
# So I use a temporary variable until the end so I can set the
# "real" variable in one-shot.
IF(APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
ENDIF(APPLE)
# For threads, as mentioned Apple doesn't need this.
# In fact, there seems to be a problem if I used the Threads package
# and try using this line, so I'm just skipping it entirely for OS X.
IF(NOT APPLE)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
ENDIF(NOT APPLE)
# For MinGW library
IF(MINGW)
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
ENDIF(MINGW)
# Set the final string here so the GUI reflects the final state.
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
ENDIF(SDL2_LIBRARY_TEMP)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)

View file

@ -92,6 +92,12 @@ else ( WINMIDI_SUPPORT )
message ( "WinMidi support: no" )
endif ( WINMIDI_SUPPORT )
if ( SDL2_SUPPORT )
message ( "SDL2 support: yes" )
else ( SDL2_SUPPORT )
message ( "SDL2 support: no" )
endif ( SDL2_SUPPORT )
if ( LADSPA_SUPPORT )
message ( "LADSPA support: yes" )
else ( LADSPA_SUPPORT )

View file

@ -367,9 +367,9 @@ Developers: Settings can be deprecated by adding: <deprecated>SOME TEXT</depreca
coreaudio (Mac OS X),<br />
dart (OS/2)
</def>
<vals>alsa, coreaudio, dart, dsound, file, jack, oss, portaudio, pulseaudio, sndman, waveout</vals>
<vals>alsa, coreaudio, dart, dsound, file, jack, oss, portaudio, pulseaudio, sdl2, sndman, waveout</vals>
<desc>
The audio system to be used.
The audio system to be used. In order to use sdl2 as audio driver, the application is responsible for initializing SDL's audio subsystem.
</desc>
</setting>
<setting>

View file

@ -199,7 +199,7 @@ For a full list of available <strong>synthesizer settings</strong>, please refer
The synthesizer itself does not write any audio to the audio output. This allows application developers to manage the audio output themselves if they wish. The next section describes the use of the synthesizer without an audio driver in more detail.
Creating the audio driver is straightforward: set the appropriate settings and create the driver object. Because the FluidSynth has support for several audio systems, you may want to change which one you want to use. The list below shows the audio systems that are currently supported. It displays the name, as used by the fluidsynth library, and a description.
Creating the audio driver is straightforward: set the <code>audio.driver</code> settings and create the driver object. Because the FluidSynth has support for several audio systems, you may want to change which one you want to use. The list below shows the audio systems that are currently supported. It displays the name, as used by the fluidsynth library, and a description.
- jack: JACK Audio Connection Kit (Linux, Mac OS X, Windows)
- alsa: Advanced Linux Sound Architecture (Linux)
@ -211,6 +211,7 @@ Creating the audio driver is straightforward: set the appropriate settings and c
- sndman: Apple SoundManager (Mac OS Classic)
- dart: DART sound driver (OS/2)
- file: Driver to output audio to a file
- sdl2*: Simple DirectMedia Layer (Linux, Windows, Mac OS X, iOS, Android, FreeBSD, Haiku, etc.)
The default audio driver depends on the settings with which FluidSynth was compiled. You can get the default driver with fluid_settings_getstr_default(). To get the list of available drivers use the fluid_settings_foreach_option() function. Finally, you can set the driver with fluid_settings_setstr(). In most cases, the default driver should work out of the box.
@ -238,7 +239,7 @@ As soon as the audio driver is created, it will start playing. The audio driver
There are a number of general audio driver settings. The audio.driver settings define the audio subsystem that will be used. The audio.periods and audio.period-size settings define the latency and robustness against scheduling delays. There are additional settings for the audio subsystems used. For a full list of available <strong>audio driver settings</strong>, please refer to <a href="fluidsettings.xml" target="_blank"><strong>FluidSettings Documentation</strong></a>.
<strong>*Note:</strong> In order to use sdl2 as audio driver, the application is responsible for initializing SDL (e.g. with SDL_Init()). This must be done <strong>before</strong> the first call to <code>new_fluid_settings()</code>! Also make sure to call SDL_Quit() after all fluidsynth instances have been destroyed.
\section UsingSynth Using the synthesizer without an audio driver

View file

@ -32,6 +32,7 @@ include_directories (
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PTHREADS_INCLUDE_DIR}
${SDL2_INCLUDE_DIR}
)
include_directories (
@ -90,6 +91,10 @@ if ( WINMIDI_SUPPORT )
set ( fluid_winmidi_SOURCES drivers/fluid_winmidi.c )
endif ( WINMIDI_SUPPORT )
if ( SDL2_SUPPORT )
set ( fluid_sdl2_SOURCES drivers/fluid_sdl2.c )
endif ( SDL2_SUPPORT )
if ( OSS_SUPPORT )
set ( fluid_oss_SOURCES drivers/fluid_oss.c )
endif ( OSS_SUPPORT )
@ -254,6 +259,7 @@ add_library ( libfluidsynth-OBJ OBJECT
${fluid_dsound_SOURCES}
${fluid_waveout_SOURCES}
${fluid_winmidi_SOURCES}
${fluid_sdl2_SOURCES}
${libfluidsynth_SOURCES}
${public_HEADERS}
${public_main_HEADER}
@ -321,6 +327,7 @@ target_link_libraries ( libfluidsynth
${PULSE_LIBRARIES}
${PORTAUDIO_LIBRARIES}
${LIBSNDFILE_LIBRARIES}
${SDL2_LIBRARY}
${DBUS_LIBRARIES}
${READLINE_LIBS}
${DART_LIBS}

View file

@ -202,6 +202,9 @@
/* Define to enable Windows MIDI driver */
#cmakedefine WINMIDI_SUPPORT @WINMIDI_SUPPORT@
/* Define to enable SDL2 audio driver */
#cmakedefine SDL2_SUPPORT @SDL2_SUPPORT@
/* Define to 1 if you have the ANSI C header files. */
#cmakedefine STDC_HEADERS @STDC_HEADERS@

View file

@ -139,6 +139,16 @@ static const fluid_audriver_definition_t fluid_audio_drivers[] =
},
#endif
#if SDL2_SUPPORT
{
"sdl2",
new_fluid_sdl2_audio_driver,
NULL,
delete_fluid_sdl2_audio_driver,
fluid_sdl2_audio_driver_settings
},
#endif
#if AUFILE_SUPPORT
{
"file",

View file

@ -121,6 +121,13 @@ void delete_fluid_dart_audio_driver(fluid_audio_driver_t *p);
void fluid_dart_audio_driver_settings(fluid_settings_t *settings);
#endif
#if SDL2_SUPPORT
fluid_audio_driver_t *new_fluid_sdl2_audio_driver(fluid_settings_t *settings,
fluid_synth_t *synth);
void delete_fluid_sdl2_audio_driver(fluid_audio_driver_t *p);
void fluid_sdl2_audio_driver_settings(fluid_settings_t *settings);
#endif
#if AUFILE_SUPPORT
fluid_audio_driver_t *new_fluid_file_audio_driver(fluid_settings_t *settings,
fluid_synth_t *synth);

View file

@ -29,6 +29,8 @@
#include "fluid_settings.h"
#if AUFILE_SUPPORT
/** fluid_file_audio_driver_t
*
* This structure should not be accessed directly. Use audio port
@ -129,3 +131,5 @@ static int fluid_file_audio_run_s16(void *d, unsigned int clock_time)
return fluid_file_renderer_process_block(dev->renderer) == FLUID_OK ? 1 : 0;
}
#endif /* AUFILE_SUPPORT */

251
src/drivers/fluid_sdl2.c Normal file
View file

@ -0,0 +1,251 @@
/* FluidSynth - A Software Synthesizer
*
* Copyright (C) 2003 Peter Hanappe and others.
* Copyright (C) 2018 Carlo Bramini
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "fluid_synth.h"
#include "fluid_adriver.h"
#include "fluid_settings.h"
#if SDL2_SUPPORT
#include "SDL.h"
typedef struct
{
fluid_audio_driver_t driver;
fluid_synth_t *synth;
fluid_audio_callback_t write_ptr;
SDL_AudioDeviceID devid;
int frame_size;
} fluid_sdl2_audio_driver_t;
static void
SDLAudioCallback(void *data, void *stream, int len)
{
fluid_sdl2_audio_driver_t *dev = (fluid_sdl2_audio_driver_t *)data;
len /= dev->frame_size;
dev->write_ptr(dev->synth, len, stream, 0, 2, stream, 1, 2);
}
void fluid_sdl2_audio_driver_settings(fluid_settings_t *settings)
{
int n, nDevs;
fluid_settings_register_str(settings, "audio.sdl2.device", "default", 0);
fluid_settings_add_option(settings, "audio.sdl2.device", "default");
if(!SDL_WasInit(SDL_INIT_AUDIO))
{
FLUID_LOG(FLUID_ERR, "SDL2 not initialized");
return;
}
nDevs = SDL_GetNumAudioDevices(0);
for(n = 0; n < nDevs; n++)
{
const char *dev_name = SDL_GetAudioDeviceName(n, 0);
if(dev_name != NULL)
{
FLUID_LOG(FLUID_DBG, "Testing audio device: %s", dev_name);
fluid_settings_add_option(settings, "audio.sdl2.device", dev_name);
}
}
}
/*
* new_fluid_sdl2_audio_driver
*/
fluid_audio_driver_t *
new_fluid_sdl2_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
{
fluid_sdl2_audio_driver_t *dev = NULL;
fluid_audio_callback_t write_ptr;
double sample_rate;
int period_size, sample_size;
SDL_AudioSpec aspec, rspec;
char *device;
const char *dev_name;
/* Retrieve the settings */
fluid_settings_getnum(settings, "synth.sample-rate", &sample_rate);
fluid_settings_getint(settings, "audio.period-size", &period_size);
/* Lower values do not seem to give good results */
if(period_size < 1024)
{
period_size = 1024;
}
else
/* According to documentation, it MUST be a power of two */
if((period_size & (period_size - 1)) != 0)
{
FLUID_LOG(FLUID_DBG, "\"audio.period-size\" must be a power of 2");
return NULL;
}
/* Clear the format buffer */
FLUID_MEMSET(&aspec, 0, sizeof(aspec));
/* Setup mixing frequency */
aspec.freq = (int)sample_rate;
/* Check the format */
if(fluid_settings_str_equal(settings, "audio.sample-format", "float"))
{
FLUID_LOG(FLUID_DBG, "Selected 32 bit sample format");
sample_size = sizeof(float);
write_ptr = fluid_synth_write_float;
aspec.format = AUDIO_F32SYS;
}
else if(fluid_settings_str_equal(settings, "audio.sample-format", "16bits"))
{
FLUID_LOG(FLUID_DBG, "Selected 16 bit sample format");
sample_size = sizeof(short);
write_ptr = fluid_synth_write_s16;
aspec.format = AUDIO_S16SYS;
}
else
{
FLUID_LOG(FLUID_ERR, "Unhandled sample format");
return NULL;
}
/* Compile the format buffer */
aspec.channels = 2;
aspec.samples = aspec.channels * ((period_size + 7) & ~7);
aspec.callback = (SDL_AudioCallback)SDLAudioCallback;
/* Check if SDL library has been started */
if(!SDL_WasInit(SDL_INIT_AUDIO))
{
FLUID_LOG(FLUID_ERR, "SDL2 not initialized");
return NULL;
}
/* Set default device to use */
device = NULL;
dev_name = NULL;
/* get the selected device name. if none is specified, use default device. */
if(fluid_settings_dupstr(settings, "audio.sdl2.device", &device) == FLUID_OK
&& device != NULL && device[0] != '\0')
{
int n, nDevs = SDL_GetNumAudioDevices(0);
for(n = 0; n < nDevs; n++)
{
dev_name = SDL_GetAudioDeviceName(n, 0);
if(FLUID_STRCASECMP(dev_name, device) == 0)
{
FLUID_LOG(FLUID_DBG, "Selected audio device GUID: %s", dev_name);
break;
}
}
if(n >= nDevs)
{
FLUID_LOG(FLUID_DBG, "Audio device %s, using \"default\"", device);
dev_name = NULL;
}
}
if(device != NULL)
{
FLUID_FREE(device);
}
do
{
/* create and clear the driver data */
dev = FLUID_NEW(fluid_sdl2_audio_driver_t);
if(dev == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
break;
}
FLUID_MEMSET(dev, 0, sizeof(fluid_sdl2_audio_driver_t));
/* set device pointer to userdata */
aspec.userdata = dev;
/* Save copy of synth */
dev->synth = synth;
/* Save copy of other variables */
dev->write_ptr = write_ptr;
dev->frame_size = sample_size * aspec.channels;
/* Open audio device */
dev->devid = SDL_OpenAudioDevice(dev_name, 0, &aspec, &rspec, 0);
if(!dev->devid)
{
FLUID_LOG(FLUID_ERR, "Failed to open audio device");
break;
}
/* Start to play */
SDL_PauseAudioDevice(dev->devid, 0);
return (fluid_audio_driver_t *) dev;
}
while(0);
delete_fluid_sdl2_audio_driver(&dev->driver);
return NULL;
}
void delete_fluid_sdl2_audio_driver(fluid_audio_driver_t *d)
{
fluid_sdl2_audio_driver_t *dev = (fluid_sdl2_audio_driver_t *) d;
if(dev != NULL)
{
if(dev->devid)
{
/* Stop audio and close */
SDL_PauseAudioDevice(dev->devid, 1);
SDL_CloseAudioDevice(dev->devid);
}
FLUID_FREE(dev);
}
}
#endif /* SDL2_SUPPORT */