View Issue Details

IDProjectCategoryView StatusLast Update
0001284OpenMPTlibopenmptpublic2020-01-02 19:35
Reportermanx Assigned Tomanx  
PrioritynormalSeverityminorReproducibilityhave not tried
Status resolvedResolutionfixed 
Target VersionOpenMPT 1.29.01.00 / libopenmpt 0.5.0 (upgrade first)Fixed in VersionOpenMPT 1.29.01.00 / libopenmpt 0.5.0 (upgrade first) 
Summary0001284: openmpt123: Remove SDL1 support.
Description

SDL2 is widely available now, and there are probably no platforms with SDL1 support, but without SDL2 support, and a C++17 compiler.

TagsNo tags attached.
Has the bug occurred in previous versions?
Tested code revision (in case you know it)

Activities

manx

manx

2019-12-24 11:06

administrator   ~0004171

remove-sdl1-v1.patch (20,762 bytes)   
Index: Makefile
===================================================================
--- Makefile	(revision 12411)
+++ Makefile	(working copy)
@@ -91,7 +91,6 @@
 #  (defaults are 0):
 #
 #  NO_PULSEAUDIO=1     Avoid using PulseAudio, even if found
-#  NO_SDL=1            Avoid using SDL, even if found
 #  NO_SDL2=1           Avoid using SDL2, even if found
 #  NO_FLAC=1           Avoid using FLAC, even if found
 #  NO_SNDFILE=1        Avoid using libsndfile, even if found
@@ -617,11 +616,10 @@
 ifeq ($(NO_SDL2),1)
 else
 #LDLIBS   += -lsdl2
-ifeq ($(shell pkg-config$(TOOLCHAIN_SUFFIX) --exists sdl2 && echo yes),yes)
-CPPFLAGS_SDL := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --cflags-only-I sdl2 ) -DMPT_WITH_SDL2
-LDFLAGS_SDL  := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-L   sdl2 ) $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-other sdl2 )
-LDLIBS_SDL   := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-l   sdl2 )
-NO_SDL:=1
+ifeq ($(shell pkg-config$(TOOLCHAIN_SUFFIX) --exists 'sdl2 >= 2.0.4' && echo yes),yes)
+CPPFLAGS_SDL2 := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --cflags-only-I 'sdl2 >= 2.0.4' ) -DMPT_WITH_SDL2
+LDFLAGS_SDL2  := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-L   'sdl2 >= 2.0.4' ) $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-other 'sdl2 >= 2.0.4' )
+LDLIBS_SDL2   := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-l   'sdl2 >= 2.0.4' )
 else
 ifeq ($(FORCE_DEPS),1)
 $(error sdl2 not found)
@@ -632,23 +630,6 @@
 endif
 endif
 
-ifeq ($(NO_SDL),1)
-else
-#LDLIBS   += -lsdl
-ifeq ($(shell pkg-config$(TOOLCHAIN_SUFFIX) --exists sdl && echo yes),yes)
-CPPFLAGS_SDL := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --cflags-only-I sdl ) -DMPT_WITH_SDL
-LDFLAGS_SDL  := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-L   sdl ) $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-other sdl )
-LDLIBS_SDL   := $(shell pkg-config$(TOOLCHAIN_SUFFIX) --libs-only-l   sdl )
-else
-ifeq ($(FORCE_DEPS),1)
-$(error sdl not found)
-else
-$(warning warning: sdl not found)
-endif
-NO_SDL:=1
-endif
-endif
-
 ifeq ($(NO_PORTAUDIO),1)
 else
 #LDLIBS   += -lportaudio
@@ -756,9 +737,9 @@
 LDFLAGS += $(LDFLAGS_ZLIB) $(LDFLAGS_MPG123) $(LDFLAGS_OGG) $(LDFLAGS_VORBIS) $(LDFLAGS_VORBISFILE)
 LDLIBS += $(LDLIBS_ZLIB) $(LDLIBS_MPG123) $(LDLIBS_OGG) $(LDLIBS_VORBIS) $(LDLIBS_VORBISFILE)
 
-CPPFLAGS_OPENMPT123 += $(CPPFLAGS_SDL2) $(CPPFLAGS_SDL) $(CPPFLAGS_PORTAUDIO) $(CPPFLAGS_PULSEAUDIO) $(CPPFLAGS_FLAC) $(CPPFLAGS_SNDFILE) $(CPPFLAGS_ALLEGRO42)
-LDFLAGS_OPENMPT123  += $(LDFLAGS_SDL2) $(LDFLAGS_SDL) $(LDFLAGS_PORTAUDIO) $(LDFLAGS_PULSEAUDIO) $(LDFLAGS_FLAC) $(LDFLAGS_SNDFILE) $(LDFLAGS_ALLEGRO42)
-LDLIBS_OPENMPT123   += $(LDLIBS_SDL2) $(LDLIBS_SDL) $(LDLIBS_PORTAUDIO) $(LDLIBS_PULSEAUDIO) $(LDLIBS_FLAC) $(LDLIBS_SNDFILE) $(LDLIBS_ALLEGRO42)
+CPPFLAGS_OPENMPT123 += $(CPPFLAGS_SDL2) $(CPPFLAGS_PORTAUDIO) $(CPPFLAGS_PULSEAUDIO) $(CPPFLAGS_FLAC) $(CPPFLAGS_SNDFILE) $(CPPFLAGS_ALLEGRO42)
+LDFLAGS_OPENMPT123  += $(LDFLAGS_SDL2) $(LDFLAGS_PORTAUDIO) $(LDFLAGS_PULSEAUDIO) $(LDFLAGS_FLAC) $(LDFLAGS_SNDFILE) $(LDFLAGS_ALLEGRO42)
+LDLIBS_OPENMPT123   += $(LDLIBS_SDL2) $(LDLIBS_PORTAUDIO) $(LDLIBS_PULSEAUDIO) $(LDLIBS_FLAC) $(LDLIBS_SNDFILE) $(LDLIBS_ALLEGRO42)
 
 
 %: %.o
Index: build/autotools/Makefile.am
===================================================================
--- build/autotools/Makefile.am	(revision 12411)
+++ build/autotools/Makefile.am	(working copy)
@@ -466,9 +466,9 @@
 if ENABLE_OPENMPT123
 
 bin_PROGRAMS += bin/openmpt123
-bin_openmpt123_CPPFLAGS = $(MINGWSTDTHREADS_CPPFLAGS) -I$(srcdir)/src/openmpt123 $(PORTAUDIO_CFLAGS) $(PULSEAUDIO_CFLAGS) $(SDL2_CFLAGS) $(SDL_CFLAGS) $(SNDFILE_CFLAGS) $(FLAC_CFLAGS)
+bin_openmpt123_CPPFLAGS = $(MINGWSTDTHREADS_CPPFLAGS) -I$(srcdir)/src/openmpt123 $(PORTAUDIO_CFLAGS) $(PULSEAUDIO_CFLAGS) $(SDL2_CFLAGS) $(SNDFILE_CFLAGS) $(FLAC_CFLAGS)
 bin_openmpt123_CXXFLAGS = $(WIN32_CONSOLE_CXXFLAGS)
-bin_openmpt123_LDADD = libopenmpt.la $(PORTAUDIO_LIBS) $(PULSEAUDIO_LIBS) $(SDL2_LIBS) $(SDL_LIBS) $(SNDFILE_LIBS) $(FLAC_LIBS) $(OPENMPT123_WIN32_LIBS)
+bin_openmpt123_LDADD = libopenmpt.la $(PORTAUDIO_LIBS) $(PULSEAUDIO_LIBS) $(SDL2_LIBS) $(SNDFILE_LIBS) $(FLAC_LIBS) $(OPENMPT123_WIN32_LIBS)
 bin_openmpt123_SOURCES = 
 bin_openmpt123_SOURCES += openmpt123/openmpt123_allegro42.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_config.hpp
@@ -479,7 +479,6 @@
 bin_openmpt123_SOURCES += openmpt123/openmpt123_portaudio.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_pulseaudio.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_raw.hpp
-bin_openmpt123_SOURCES += openmpt123/openmpt123_sdl.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_sdl2.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_sndfile.hpp
 bin_openmpt123_SOURCES += openmpt123/openmpt123_stdout.hpp
Index: build/autotools/configure.ac
===================================================================
--- build/autotools/configure.ac	(revision 12411)
+++ build/autotools/configure.ac	(working copy)
@@ -240,30 +240,15 @@
 AM_CONDITIONAL([HAVE_PORTAUDIOCPP], [test x$have_portaudio = x1])
 
 # Optional disabled openmpt123 dependency: libsdl2
-AC_ARG_WITH([sdl2], AS_HELP_STRING([--with-sdl2], [Enable use of libsdl2. Enabling libsdl2 automatically disables libsdl.]))
+AC_ARG_WITH([sdl2], AS_HELP_STRING([--with-sdl2], [Enable use of libsdl2.]))
 AS_IF([test "x$enable_openmpt123" != "xno"],[
 AS_IF([test "x$with_sdl2" = "xyes"],
  [
-  PKG_CHECK_MODULES([SDL2], [sdl2], [AC_DEFINE([MPT_WITH_SDL2], [], [with libsdl2])], [AC_MSG_ERROR([Unable to find libsdl2.])])
+  PKG_CHECK_MODULES([SDL2], [sdl2 >= 2.0.4], [AC_DEFINE([MPT_WITH_SDL2], [], [with libsdl2])], [AC_MSG_ERROR([Unable to find libsdl2.])])
  ]
 )
 ])
 
-# Optional disabled openmpt123 dependency: libsdl
-AC_ARG_WITH([sdl], AS_HELP_STRING([--with-sdl], [Enable use of libsdl.]))
-AS_IF([test "x$enable_openmpt123" != "xno"],[
-AS_IF([test "x$with_sdl2" != "xno"],[
-AS_IF([test "x$with_sdl" = "xyes"],
- [
-  PKG_CHECK_MODULES([SDL], [sdl], [AC_DEFINE([MPT_WITH_SDL], [], [with libsdl])], [AC_MSG_ERROR([Unable to find libsdl.])])
- ]
-)],
- [
-  AC_MSG_WARN([Skipping libsdl because libsdl2 is enabled.])
- ]
-)
-])
-
 # Optional openmpt123 dependency: libsndfile
 AC_ARG_WITH([sndfile], AS_HELP_STRING([--without-sndfile], [Disable use of libsndfile.]))
 AS_IF([test "x$enable_openmpt123" != "xno"],[
Index: openmpt123/openmpt123.cpp
===================================================================
--- openmpt123/openmpt123.cpp	(revision 12411)
+++ openmpt123/openmpt123.cpp	(working copy)
@@ -100,7 +100,6 @@
 #include "openmpt123_allegro42.hpp"
 #include "openmpt123_portaudio.hpp"
 #include "openmpt123_pulseaudio.hpp"
-#include "openmpt123_sdl.hpp"
 #include "openmpt123_sdl2.hpp"
 #include "openmpt123_waveout.hpp"
 
@@ -515,18 +514,6 @@
 	log << "API: " << static_cast<int>( sdlver.major ) << "." << static_cast<int>( sdlver.minor ) << "." << static_cast<int>( sdlver.patch ) << "";
 	log << " <https://libsdl.org/>" << std::endl;
 #endif
-#ifdef MPT_WITH_SDL
-	const SDL_version * linked_sdlver = SDL_Linked_Version();
-	log << " libSDL ";
-	if ( linked_sdlver ) {
-		log << static_cast<int>( linked_sdlver->major ) << "." << static_cast<int>( linked_sdlver->minor ) << "." << static_cast<int>( linked_sdlver->patch ) << " ";
-	}
-	SDL_version sdlver;
-	std::memset( &sdlver, 0, sizeof( SDL_version ) );
-	SDL_VERSION( &sdlver );
-	log << "(API: " << static_cast<int>( sdlver.major ) << "." << static_cast<int>( sdlver.minor ) << "." << static_cast<int>( sdlver.patch ) << ")";
-	log << " <https://libsdl.org/>" << std::endl;
-#endif
 #ifdef MPT_WITH_PULSEAUDIO
 	log << " " << "libpulse, libpulse-simple" << " (headers " << pa_get_headers_version()  << ", API " << PA_API_VERSION << ", PROTOCOL " << PA_PROTOCOL_VERSION << ", library " << ( pa_get_library_version() ? pa_get_library_version() : "unknown" ) << ") <https://www.freedesktop.org/wiki/Software/PulseAudio/>" << std::endl;
 #endif
@@ -2052,9 +2039,6 @@
 #if defined( MPT_WITH_SDL2 )
 					drivers << "    " << "sdl2" << std::endl;
 #endif
-#if defined( MPT_WITH_SDL )
-					drivers << "    " << "sdl" << std::endl;
-#endif
 #if defined( MPT_WITH_PORTAUDIO )
 					drivers << "    " << "portaudio" << std::endl;
 #endif
@@ -2448,11 +2432,6 @@
 					sdl2_stream_raii sdl2_stream( flags, log );
 					render_files( flags, log, sdl2_stream, prng );
 #endif
-#if defined( MPT_WITH_SDL )
-				} else if ( flags.driver == "sdl" || flags.driver.empty() ) {
-					sdl_stream_raii sdl_stream( flags, log );
-					render_files( flags, log, sdl_stream, prng );
-#endif
 #if defined( MPT_WITH_PORTAUDIO )
 				} else if ( flags.driver == "portaudio" || flags.driver.empty() ) {
 					portaudio_stream_raii portaudio_stream( flags, log );
@@ -2509,12 +2488,6 @@
 		std_err.writeout();
 		return 1;
 #endif
-#ifdef MPT_WITH_SDL
-	} catch ( sdl_exception & e ) {
-		std_err << "SDL error: " << e.what() << std::endl;
-		std_err.writeout();
-		return 1;
-#endif
 #ifdef MPT_WITH_SDL2
 	} catch ( sdl2_exception & e ) {
 		std_err << "SDL2 error: " << e.what() << std::endl;
Index: openmpt123/openmpt123.hpp
===================================================================
--- openmpt123/openmpt123.hpp	(revision 12411)
+++ openmpt123/openmpt123.hpp	(working copy)
@@ -1308,77 +1308,6 @@
 	}
 };
 
-class write_buffers_blocking_wrapper : public write_buffers_interface {
-protected:
-	std::size_t channels;
-	std::size_t sampleQueueMaxFrames;
-	std::deque<float> sampleQueue;
-protected:
-	virtual ~write_buffers_blocking_wrapper() {
-		return;
-	}
-protected:
-	write_buffers_blocking_wrapper( const commandlineflags & flags )
-		: channels(flags.channels)
-		, sampleQueueMaxFrames(0)
-	{
-		return;
-	}
-	void set_queue_size_frames( std::size_t frames ) {
-		sampleQueueMaxFrames = frames;
-	}
-	template < typename Tsample >
-	Tsample pop_queue() {
-		float val = 0.0f;
-		if ( !sampleQueue.empty() ) {
-			val = sampleQueue.front();
-			sampleQueue.pop_front();
-		}
-		return convert_sample_to<Tsample>( val );
-	}
-	template < typename Tsample >
-	void fill_buffer( Tsample * buf, std::size_t framesToRender ) {
-		for ( std::size_t frame = 0; frame < framesToRender; ++frame ) {
-			for ( std::size_t channel = 0; channel < channels; ++channel ) {
-				*buf = pop_queue<Tsample>();
-				buf++;
-			}
-		}
-	}
-private:
-	void wait_for_queue_space() {
-		while ( sampleQueue.size() >= sampleQueueMaxFrames * channels ) {
-			unlock();
-			sleep( 1 );
-			lock();
-		}
-	}
-public:
-	void write( const std::vector<float*> buffers, std::size_t frames ) override {
-		lock();
-		for ( std::size_t frame = 0; frame < frames; ++frame ) {
-			for ( std::size_t channel = 0; channel < channels; ++channel ) {
-				wait_for_queue_space();
-				sampleQueue.push_back( buffers[channel][frame] );
-			}
-		}
-		unlock();
-	}
-	void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
-		lock();
-		for ( std::size_t frame = 0; frame < frames; ++frame ) {
-			for ( std::size_t channel = 0; channel < channels; ++channel ) {
-				wait_for_queue_space();
-				sampleQueue.push_back( buffers[channel][frame] * (1.0f/32768.0f) );
-			}
-		}
-		unlock();
-	}
-	virtual void lock() = 0;
-	virtual void unlock() = 0;
-	bool sleep( int ms ) override = 0;
-};
-
 class write_buffers_polling_wrapper : public write_buffers_interface {
 protected:
 	std::size_t channels;
Index: openmpt123/openmpt123_config.hpp
===================================================================
--- openmpt123/openmpt123_config.hpp	(revision 12411)
+++ openmpt123/openmpt123_config.hpp	(working copy)
@@ -81,14 +81,6 @@
 
 #endif // MPT_BUILD_MSVC
 
-#if defined(MPT_WITH_SDL) && defined(MPT_WITH_SDL2)
-#error "MPT_WITH_SDL2 and MPT_WITH_SDL are mutually exclusive."
-#endif
-
-#if defined(MPT_WITH_SDL)
-MPT_WARNING("SDL1 support is deprecated and will be removed in a future openmpt123 version.")
-#endif
-
 #define OPENMPT123_VERSION_STRING OPENMPT_API_VERSION_STRING
 
 #endif // OPENMPT123_CONFIG_HPP
Index: openmpt123/openmpt123_sdl.hpp
===================================================================
--- openmpt123/openmpt123_sdl.hpp	(revision 12411)
+++ openmpt123/openmpt123_sdl.hpp	(working copy)
@@ -1,140 +0,0 @@
-/*
- * openmpt123_sdl.hpp
- * ------------------
- * Purpose: libopenmpt command line player
- * Notes  : (currently none)
- * Authors: OpenMPT Devs
- * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
- */
-
-#ifndef OPENMPT123_SDL_HPP
-#define OPENMPT123_SDL_HPP
-
-#include "openmpt123_config.hpp"
-#include "openmpt123.hpp"
-
-#if defined(MPT_WITH_SDL)
-
-#if defined(__clang__)
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wreserved-id-macro"
-#endif // __clang__
-#include <SDL.h>
-#if defined(__clang__)
-#pragma clang diagnostic pop
-#endif // __clang__
-#ifdef main
-#undef main
-#endif
-#ifdef SDL_main
-#undef SDL_main
-#endif
-
-namespace openmpt123 {
-
-struct sdl_exception : public exception {
-	sdl_exception( int /*code*/ ) : exception( "SDL error" ) { }
-};
-
-class sdl_stream_raii : public write_buffers_blocking_wrapper {
-private:
-	std::ostream & log;
-	std::size_t channels;
-protected:
-	void check_sdl_error( int e ) {
-		if ( e < 0 ) {
-			throw sdl_exception( e );
-			return;
-		}
-	}
-	std::uint32_t round_up_power2(std::uint32_t x)
-	{
-		std::uint32_t result = 1;
-		while ( result < x ) {
-			result *= 2;
-		}
-		return result;
-	}
-public:
-	sdl_stream_raii( commandlineflags & flags, std::ostream & log_ )
-		: write_buffers_blocking_wrapper(flags)
-		, log(log_)
-		, channels(flags.channels)
-	{
-		if ( flags.buffer == default_high ) {
-			flags.buffer = 160;
-		} else if ( flags.buffer == default_low ) {
-			flags.buffer = 80;
-		}
-		if ( flags.period == default_high ) {
-			flags.period = 20;
-		} else if ( flags.period == default_low ) {
-			flags.period = 10;
-		}
-		flags.apply_default_buffer_sizes();
-		check_sdl_error( SDL_Init( SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | SDL_INIT_AUDIO ) );
-		SDL_AudioSpec audiospec;
-		std::memset( &audiospec, 0, sizeof( SDL_AudioSpec ) );
-		audiospec.freq = flags.samplerate;
-		audiospec.format = AUDIO_S16SYS;
-		audiospec.channels = flags.channels;
-		audiospec.silence = 0;
-		audiospec.samples = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
-		audiospec.size = audiospec.samples * audiospec.channels * sizeof( std::int16_t );
-		audiospec.callback = &sdl_callback_wrapper;
-		audiospec.userdata = this;
-		if ( flags.verbose ) {
-			log << "SDL:" << std::endl;
-			log << " latency: " << ( audiospec.samples * 2.0 / flags.samplerate ) << " (2 * " << audiospec.samples << ")" << std::endl;
-			log << std::endl;
-		}
-		set_queue_size_frames( round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) ) );
-		check_sdl_error( SDL_OpenAudio( &audiospec, NULL ) );
-		SDL_PauseAudio( 0 );
-	}
-	~sdl_stream_raii() {
-		SDL_PauseAudio( 1 );
-		SDL_CloseAudio();
-		SDL_Quit();
-	}
-private:
-	static void sdl_callback_wrapper( void * userdata, Uint8 * stream, int len ) {
-		return reinterpret_cast<sdl_stream_raii*>( userdata )->sdl_callback( stream, len );
-	}
-	void sdl_callback( Uint8 * stream, int len ) {
-		std::size_t framesToRender = len / sizeof( std::int16_t ) / channels;
-		for ( std::size_t frame = 0; frame < framesToRender; ++frame ) {
-			for ( std::size_t channel = 0; channel < channels; ++channel ) {
-				std::int16_t sample = pop_queue<std::int16_t>();
-				std::memcpy( stream, &sample, sizeof( std::int16_t ) );
-				stream += sizeof( std::int16_t );
-			}
-		}		
-	}
-public:
-	bool pause() override {
-		SDL_PauseAudio( 1 );
-		return true;
-	}
-	bool unpause() override {
-		SDL_PauseAudio( 0 );
-		return true;
-	}
-	void lock() override {
-		SDL_LockAudio();
-	}
-	void unlock() override {
-		SDL_UnlockAudio();
-	}
-	bool sleep( int ms ) override {
-		SDL_Delay( ms );
-		return true;
-	}
-};
-
-} // namespace openmpt123
-
-#endif // MPT_WITH_SDL
-
-#endif // OPENMPT123_SDL_HPP
-
Index: openmpt123/openmpt123_sdl2.hpp
===================================================================
--- openmpt123/openmpt123_sdl2.hpp	(revision 12411)
+++ openmpt123/openmpt123_sdl2.hpp	(working copy)
@@ -57,7 +57,7 @@
 	}
 };
 
-class sdl2_stream_raii : public write_buffers_blocking_wrapper {
+class sdl2_stream_raii : public write_buffers_interface {
 private:
 	std::ostream & log;
 	sdl2_raii sdl2;
@@ -64,6 +64,9 @@
 	int dev;
 	std::size_t channels;
 	bool use_float;
+	std::size_t sampleQueueMaxFrames;
+	std::vector<float> sampleBufFloat;
+	std::vector<std::int16_t> sampleBufInt;
 protected:
 	std::uint32_t round_up_power2(std::uint32_t x)
 	{
@@ -75,12 +78,12 @@
 	}
 public:
 	sdl2_stream_raii( commandlineflags & flags, std::ostream & log_ )
-		: write_buffers_blocking_wrapper(flags)
-		, log(log_)
+		: log(log_)
 		, sdl2( SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | SDL_INIT_AUDIO )
 		, dev(-1)
 		, channels(flags.channels)
 		, use_float(flags.use_float)
+		, sampleQueueMaxFrames(0)
 	{
 		if ( flags.buffer == default_high ) {
 			flags.buffer = 160;
@@ -101,14 +104,14 @@
 		audiospec.silence = 0;
 		audiospec.samples = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
 		audiospec.size = audiospec.samples * audiospec.channels * ( flags.use_float ? sizeof( float ) : sizeof( std::int16_t ) );
-		audiospec.callback = &sdl2_callback_wrapper;
-		audiospec.userdata = this;
+		audiospec.callback = NULL;
+		audiospec.userdata = NULL;
 		if ( flags.verbose ) {
 			log << "SDL2:" << std::endl;
 			log << " latency: " << ( audiospec.samples * 2.0 / flags.samplerate ) << " (2 * " << audiospec.samples << ")" << std::endl;
 			log << std::endl;
 		}
-		set_queue_size_frames( round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) ) );
+		sampleQueueMaxFrames = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
 		SDL_AudioSpec audiospec_obtained;
 		std::memset( &audiospec_obtained, 0, sizeof( SDL_AudioSpec ) );
 		std::memcpy( &audiospec_obtained, &audiospec, sizeof( SDL_AudioSpec ) );
@@ -125,24 +128,45 @@
 		SDL_CloseAudioDevice( dev );
 	}
 private:
-	static void sdl2_callback_wrapper( void * userdata, Uint8 * stream, int len ) {
-		return reinterpret_cast<sdl2_stream_raii*>( userdata )->sdl2_callback( stream, len );
+	std::size_t get_num_writeable_frames() {
+		std::size_t num_queued_frames = SDL_GetQueuedAudioSize( dev ) / ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) / channels;
+		if ( num_queued_frames > sampleQueueMaxFrames ) {
+			return 0;
+		}
+		return sampleQueueMaxFrames - num_queued_frames;
 	}
-	void sdl2_callback( Uint8 * stream, int len ) {
-		return ( use_float ? sdl2_callback_impl<float>( stream, len ) : sdl2_callback_impl<std::int16_t>( stream, len ) );
+	template<typename Tsample>
+	void write_frames( const Tsample * buffer, std::size_t frames ) {
+		while ( frames > 0 ) {
+			std::size_t chunk_frames = std::min( frames, get_num_writeable_frames() );
+			if ( chunk_frames > 0 ) {
+				check_sdl2_error( SDL_QueueAudio( dev, buffer, chunk_frames * channels * ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) ) );
+				frames -= chunk_frames;
+				buffer += chunk_frames * channels;
+			} else {
+				SDL_Delay( 1 );
+			}
+		}
 	}
-	template < typename Tsample >
-	void sdl2_callback_impl( Uint8 * stream, int len ) {
-		std::size_t framesToRender = len / sizeof( Tsample ) / channels;
-		for ( std::size_t frame = 0; frame < framesToRender; ++frame ) {
+public:
+	void write( const std::vector<float*> buffers, std::size_t frames ) override {
+		sampleBufFloat.clear();
+		for ( std::size_t frame = 0; frame < frames; ++frame ) {
 			for ( std::size_t channel = 0; channel < channels; ++channel ) {
-				Tsample sample = pop_queue<Tsample>();
-				std::memcpy( stream, &sample, sizeof( Tsample ) );
-				stream += sizeof( Tsample );
+				sampleBufFloat.push_back( buffers[channel][frame] );
 			}
 		}
+		write_frames( sampleBufFloat.data(), frames );
 	}
-public:
+	void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
+		sampleBufInt.clear();
+		for ( std::size_t frame = 0; frame < frames; ++frame ) {
+			for ( std::size_t channel = 0; channel < channels; ++channel ) {
+				sampleBufInt.push_back( buffers[channel][frame] );
+			}
+		}
+		write_frames( sampleBufInt.data(), frames );
+	}
 	bool pause() override {
 		SDL_PauseAudioDevice( dev, 1 );
 		return true;
@@ -151,12 +175,6 @@
 		SDL_PauseAudioDevice( dev, 0 );
 		return true;
 	}
-	void lock() override {
-		SDL_LockAudioDevice( dev );
-	}
-	void unlock() override {
-		SDL_UnlockAudioDevice( dev );
-	}
 	bool sleep( int ms ) override {
 		SDL_Delay( ms );
 		return true;
remove-sdl1-v1.patch (20,762 bytes)   
manx

manx

2020-01-02 19:35

administrator   ~0004175

r12442

Issue History

Date Modified Username Field Change
2019-12-24 09:46 manx New Issue
2019-12-24 09:46 manx Status new => assigned
2019-12-24 09:46 manx Assigned To => manx
2019-12-24 11:06 manx Note Added: 0004171
2019-12-24 11:06 manx File Added: remove-sdl1-v1.patch
2020-01-02 12:05 manx Target Version OpenMPT 1.?? (libopenmpt 1.0) (goals) => OpenMPT 1.29.01.00 / libopenmpt 0.5.0 (upgrade first)
2020-01-02 19:35 manx Status assigned => resolved
2020-01-02 19:35 manx Resolution open => fixed
2020-01-02 19:35 manx Fixed in Version => OpenMPT 1.29.01.00 / libopenmpt 0.5.0 (upgrade first)
2020-01-02 19:35 manx Note Added: 0004175