Convert the examples from SDL_sound to libsndfile
This commit is contained in:
parent
586bc94d51
commit
cae78e79e8
@ -1132,12 +1132,9 @@ IF(ALSOFT_UTILS AND NOT ALSOFT_NO_CONFIG_UTIL)
|
||||
add_subdirectory(utils/alsoft-config)
|
||||
ENDIF()
|
||||
IF(ALSOFT_EXAMPLES)
|
||||
IF(NOT SDL2_FOUND)
|
||||
FIND_PACKAGE(SDL2)
|
||||
ENDIF()
|
||||
FIND_PACKAGE(SndFile)
|
||||
FIND_PACKAGE(SDL2)
|
||||
IF(SDL2_FOUND)
|
||||
FIND_PACKAGE(SDL_sound)
|
||||
FIND_PACKAGE(SndFile)
|
||||
FIND_PACKAGE(FFmpeg COMPONENTS AVFORMAT AVCODEC AVUTIL SWSCALE SWRESAMPLE)
|
||||
ENDIF()
|
||||
ENDIF()
|
||||
@ -1484,8 +1481,34 @@ IF(ALSOFT_EXAMPLES)
|
||||
TARGET_INCLUDE_DIRECTORIES(alplay PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(alplay PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(alstream examples/alstream.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alstream PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(alstream PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(alreverb examples/alreverb.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alreverb PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(alreverb PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(almultireverb examples/almultireverb.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(almultireverb PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(almultireverb
|
||||
PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common ${MATH_LIB})
|
||||
|
||||
ADD_EXECUTABLE(allatency examples/allatency.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(allatency PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(allatency PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(alhrtf examples/alhrtf.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alhrtf PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(alhrtf
|
||||
PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common ${MATH_LIB})
|
||||
|
||||
ADD_EXECUTABLE(alstreamcb examples/alstreamcb.cpp)
|
||||
TARGET_INCLUDE_DIRECTORIES(alstreamcb PRIVATE ${SNDFILE_INCLUDE_DIRS})
|
||||
TARGET_LINK_LIBRARIES(alstreamcb PRIVATE ${LINKER_FLAGS} ${SNDFILE_LIBRARIES} ex-common)
|
||||
|
||||
IF(ALSOFT_INSTALL)
|
||||
INSTALL(TARGETS alplay
|
||||
INSTALL(TARGETS alplay alstream alreverb almultireverb allatency alhrtf
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
@ -1495,60 +1518,20 @@ IF(ALSOFT_EXAMPLES)
|
||||
ENDIF()
|
||||
|
||||
IF(SDL2_FOUND)
|
||||
IF(SDL_SOUND_FOUND)
|
||||
ADD_EXECUTABLE(alstream examples/alstream.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alstream
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alstream
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common)
|
||||
ADD_EXECUTABLE(alloopback examples/alloopback.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alloopback PRIVATE ${SDL2_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alloopback
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL2_LIBRARY} ex-common ${MATH_LIB})
|
||||
|
||||
ADD_EXECUTABLE(alstreamcb examples/alstreamcb.cpp)
|
||||
TARGET_INCLUDE_DIRECTORIES(alstreamcb
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alstreamcb
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(alreverb examples/alreverb.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alreverb
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alreverb
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(almultireverb examples/almultireverb.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(almultireverb
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(almultireverb
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common
|
||||
${MATH_LIB})
|
||||
|
||||
ADD_EXECUTABLE(allatency examples/allatency.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(allatency
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(allatency
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common)
|
||||
|
||||
ADD_EXECUTABLE(alloopback examples/alloopback.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alloopback
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alloopback
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common ${MATH_LIB})
|
||||
|
||||
ADD_EXECUTABLE(alhrtf examples/alhrtf.c)
|
||||
TARGET_INCLUDE_DIRECTORIES(alhrtf
|
||||
PRIVATE ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
TARGET_LINK_LIBRARIES(alhrtf
|
||||
PRIVATE ${LINKER_FLAGS} ${SDL_SOUND_LIBRARIES} ${SDL2_LIBRARY} ex-common ${MATH_LIB})
|
||||
|
||||
IF(ALSOFT_INSTALL)
|
||||
INSTALL(TARGETS alstream alreverb almultireverb allatency alloopback alhrtf
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
ENDIF()
|
||||
|
||||
MESSAGE(STATUS "Building SDL_sound example programs")
|
||||
IF(ALSOFT_INSTALL)
|
||||
INSTALL(TARGETS alloopback
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
ENDIF()
|
||||
|
||||
MESSAGE(STATUS "Building SDL example programs")
|
||||
|
||||
SET(FFVER_OK FALSE)
|
||||
IF(FFMPEG_FOUND)
|
||||
SET(FFVER_OK TRUE)
|
||||
|
@ -1,429 +0,0 @@
|
||||
# - Locates the SDL_sound library
|
||||
#
|
||||
# This module depends on SDL being found and
|
||||
# must be called AFTER FindSDL.cmake or FindSDL2.cmake is called.
|
||||
#
|
||||
# This module defines
|
||||
# SDL_SOUND_INCLUDE_DIR, where to find SDL_sound.h
|
||||
# SDL_SOUND_FOUND, if false, do not try to link to SDL_sound
|
||||
# SDL_SOUND_LIBRARIES, this contains the list of libraries that you need
|
||||
# to link against. This is a read-only variable and is marked INTERNAL.
|
||||
# SDL_SOUND_EXTRAS, this is an optional variable for you to add your own
|
||||
# flags to SDL_SOUND_LIBRARIES. This is prepended to SDL_SOUND_LIBRARIES.
|
||||
# This is available mostly for cases this module failed to anticipate for
|
||||
# and you must add additional flags. This is marked as ADVANCED.
|
||||
# SDL_SOUND_VERSION_STRING, human-readable string containing the version of SDL_sound
|
||||
#
|
||||
# This module also defines (but you shouldn't need to use directly)
|
||||
# SDL_SOUND_LIBRARY, the name of just the SDL_sound library you would link
|
||||
# against. Use SDL_SOUND_LIBRARIES for you link instructions and not this one.
|
||||
# And might define the following as needed
|
||||
# MIKMOD_LIBRARY
|
||||
# MODPLUG_LIBRARY
|
||||
# OGG_LIBRARY
|
||||
# VORBIS_LIBRARY
|
||||
# SMPEG_LIBRARY
|
||||
# FLAC_LIBRARY
|
||||
# SPEEX_LIBRARY
|
||||
#
|
||||
# Typically, you should not use these variables directly, and you should use
|
||||
# SDL_SOUND_LIBRARIES which contains SDL_SOUND_LIBRARY and the other audio libraries
|
||||
# (if needed) to successfully compile on your system.
|
||||
#
|
||||
# Created by Eric Wing.
|
||||
# This module is a bit more complicated than the other FindSDL* family modules.
|
||||
# The reason is that SDL_sound can be compiled in a large variety of different ways
|
||||
# which are independent of platform. SDL_sound may dynamically link against other 3rd
|
||||
# party libraries to get additional codec support, such as Ogg Vorbis, SMPEG, ModPlug,
|
||||
# MikMod, FLAC, Speex, and potentially others.
|
||||
# Under some circumstances which I don't fully understand,
|
||||
# there seems to be a requirement
|
||||
# that dependent libraries of libraries you use must also be explicitly
|
||||
# linked against in order to successfully compile. SDL_sound does not currently
|
||||
# have any system in place to know how it was compiled.
|
||||
# So this CMake module does the hard work in trying to discover which 3rd party
|
||||
# libraries are required for building (if any).
|
||||
# This module uses a brute force approach to create a test program that uses SDL_sound,
|
||||
# and then tries to build it. If the build fails, it parses the error output for
|
||||
# known symbol names to figure out which libraries are needed.
|
||||
#
|
||||
# Responds to the $SDLDIR and $SDLSOUNDDIR environmental variable that would
|
||||
# correspond to the ./configure --prefix=$SDLDIR used in building SDL.
|
||||
#
|
||||
# On OSX, this will prefer the Framework version (if found) over others.
|
||||
# People will have to manually change the cache values of
|
||||
# SDL_LIBRARY or SDL2_LIBRARY to override this selection or set the CMake
|
||||
# environment CMAKE_INCLUDE_PATH to modify the search paths.
|
||||
|
||||
#=============================================================================
|
||||
# Copyright 2005-2009 Kitware, Inc.
|
||||
# Copyright 2012 Benjamin Eikel
|
||||
#
|
||||
# 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(SDL_SOUND_EXTRAS "" CACHE STRING "SDL_sound extra flags")
|
||||
mark_as_advanced(SDL_SOUND_EXTRAS)
|
||||
|
||||
# Find SDL_sound.h
|
||||
find_path(SDL_SOUND_INCLUDE_DIR SDL_sound.h
|
||||
HINTS
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
PATH_SUFFIXES SDL SDL12 SDL11
|
||||
)
|
||||
|
||||
find_library(SDL_SOUND_LIBRARY
|
||||
NAMES SDL_sound
|
||||
HINTS
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
)
|
||||
|
||||
if(SDL2_FOUND OR SDL_FOUND)
|
||||
if(SDL_SOUND_INCLUDE_DIR AND SDL_SOUND_LIBRARY)
|
||||
# CMake is giving me problems using TRY_COMPILE with the CMAKE_FLAGS
|
||||
# for the :STRING syntax if I have multiple values contained in a
|
||||
# single variable. This is a problem for the SDL2_LIBRARY variable
|
||||
# because it does just that. When I feed this variable to the command,
|
||||
# only the first value gets the appropriate modifier (e.g. -I) and
|
||||
# the rest get dropped.
|
||||
# To get multiple single variables to work, I must separate them with a "\;"
|
||||
# I could go back and modify the FindSDL2.cmake module, but that's kind of painful.
|
||||
# The solution would be to try something like:
|
||||
# set(SDL2_TRY_COMPILE_LIBRARY_LIST "${SDL2_TRY_COMPILE_LIBRARY_LIST}\;${CMAKE_THREAD_LIBS_INIT}")
|
||||
# Instead, it was suggested on the mailing list to write a temporary CMakeLists.txt
|
||||
# with a temporary test project and invoke that with TRY_COMPILE.
|
||||
# See message thread "Figuring out dependencies for a library in order to build"
|
||||
# 2005-07-16
|
||||
# try_compile(
|
||||
# MY_RESULT
|
||||
# ${CMAKE_BINARY_DIR}
|
||||
# ${PROJECT_SOURCE_DIR}/DetermineSoundLibs.c
|
||||
# CMAKE_FLAGS
|
||||
# -DINCLUDE_DIRECTORIES:STRING=${SDL2_INCLUDE_DIR}\;${SDL_SOUND_INCLUDE_DIR}
|
||||
# -DLINK_LIBRARIES:STRING=${SDL_SOUND_LIBRARY}\;${SDL2_LIBRARY}
|
||||
# OUTPUT_VARIABLE MY_OUTPUT
|
||||
# )
|
||||
|
||||
# To minimize external dependencies, create a sdlsound test program
|
||||
# which will be used to figure out if additional link dependencies are
|
||||
# required for the link phase.
|
||||
file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/DetermineSoundLibs.c
|
||||
"#include \"SDL_sound.h\"
|
||||
#include \"SDL.h\"
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
Sound_AudioInfo desired;
|
||||
Sound_Sample* sample;
|
||||
|
||||
SDL_Init(0);
|
||||
Sound_Init();
|
||||
|
||||
/* This doesn't actually have to work, but Init() is a no-op
|
||||
* for some of the decoders, so this should force more symbols
|
||||
* to be pulled in.
|
||||
*/
|
||||
sample = Sound_NewSampleFromFile(argv[1], &desired, 4096);
|
||||
|
||||
Sound_Quit();
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}"
|
||||
)
|
||||
|
||||
# Calling
|
||||
# target_link_libraries(DetermineSoundLibs "${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
|
||||
# causes problems when SDL2_LIBRARY looks like
|
||||
# /Library/Frameworks/SDL2.framework;-framework Cocoa
|
||||
# The ;-framework Cocoa seems to be confusing CMake once the OS X
|
||||
# framework support was added. I was told that breaking up the list
|
||||
# would fix the problem.
|
||||
set(TMP_LIBS "")
|
||||
if(SDL2_FOUND)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
|
||||
foreach(lib ${SDL_SOUND_LIBRARY} ${SDL2_LIBRARY})
|
||||
set(TMP_LIBS "${TMP_LIBS} \"${lib}\"")
|
||||
endforeach()
|
||||
set(TMP_INCLUDE_DIRS ${SDL2_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
else()
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
|
||||
foreach(lib ${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
|
||||
set(TMP_LIBS "${TMP_LIBS} \"${lib}\"")
|
||||
endforeach()
|
||||
set(TMP_INCLUDE_DIRS ${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
|
||||
endif()
|
||||
|
||||
# Keep trying to build a temp project until we find all missing libs.
|
||||
set(TRY_AGAIN TRUE)
|
||||
WHILE(TRY_AGAIN)
|
||||
set(TRY_AGAIN FALSE)
|
||||
# message("TMP_TRY_LIBS ${TMP_TRY_LIBS}")
|
||||
|
||||
# Write the CMakeLists.txt and test project
|
||||
# Weird, this is still sketchy. If I don't quote the variables
|
||||
# in the TARGET_LINK_LIBRARIES, I seem to loose everything
|
||||
# in the SDL2_LIBRARY string after the "-framework".
|
||||
# But if I quote the stuff in INCLUDE_DIRECTORIES, it doesn't work.
|
||||
file(WRITE ${PROJECT_BINARY_DIR}/CMakeTmp/CMakeLists.txt
|
||||
"cmake_minimum_required(VERSION 2.8)
|
||||
project(DetermineSoundLibs C)
|
||||
include_directories(${TMP_INCLUDE_DIRS})
|
||||
add_executable(DetermineSoundLibs DetermineSoundLibs.c)
|
||||
target_link_libraries(DetermineSoundLibs ${TMP_LIBS})"
|
||||
)
|
||||
|
||||
try_compile(
|
||||
MY_RESULT
|
||||
${PROJECT_BINARY_DIR}/CMakeTmp
|
||||
${PROJECT_BINARY_DIR}/CMakeTmp
|
||||
DetermineSoundLibs
|
||||
OUTPUT_VARIABLE MY_OUTPUT
|
||||
)
|
||||
# message("${MY_RESULT}")
|
||||
# message(${MY_OUTPUT})
|
||||
|
||||
if(NOT MY_RESULT)
|
||||
# I expect that MPGLIB, VOC, WAV, AIFF, and SHN are compiled in statically.
|
||||
# I think Timidity is also compiled in statically.
|
||||
# I've never had to explcitly link against Quicktime, so I'll skip that for now.
|
||||
|
||||
# Find libmath
|
||||
if("${MY_OUTPUT}" MATCHES "cos@@GLIBC")
|
||||
find_library(MATH_LIBRARY NAMES m)
|
||||
if(MATH_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MATH_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MATH_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif(MATH_LIBRARY)
|
||||
endif("${MY_OUTPUT}" MATCHES "cos@@GLIBC")
|
||||
|
||||
# Find MikMod
|
||||
if("${MY_OUTPUT}" MATCHES "MikMod_")
|
||||
find_library(MIKMOD_LIBRARY
|
||||
NAMES libmikmod-coreaudio mikmod
|
||||
PATHS
|
||||
ENV MIKMODDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(MIKMOD_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MIKMOD_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MIKMOD_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif(MIKMOD_LIBRARY)
|
||||
endif("${MY_OUTPUT}" MATCHES "MikMod_")
|
||||
|
||||
# Find ModPlug
|
||||
if("${MY_OUTPUT}" MATCHES "MODPLUG_")
|
||||
find_library(MODPLUG_LIBRARY
|
||||
NAMES modplug
|
||||
PATHS
|
||||
ENV MODPLUGDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(MODPLUG_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${MODPLUG_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${MODPLUG_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Find Ogg and Vorbis
|
||||
if("${MY_OUTPUT}" MATCHES "ov_")
|
||||
find_library(VORBISFILE_LIBRARY
|
||||
NAMES vorbisfile VorbisFile VORBISFILE
|
||||
PATHS
|
||||
ENV VORBISDIR
|
||||
ENV OGGDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(VORBISFILE_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBISFILE_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${VORBISFILE_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
|
||||
find_library(VORBIS_LIBRARY
|
||||
NAMES vorbis Vorbis VORBIS
|
||||
PATHS
|
||||
ENV OGGDIR
|
||||
ENV VORBISDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(VORBIS_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${VORBIS_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${VORBIS_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
|
||||
find_library(OGG_LIBRARY
|
||||
NAMES ogg Ogg OGG
|
||||
PATHS
|
||||
ENV OGGDIR
|
||||
ENV VORBISDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(OGG_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${OGG_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Find SMPEG
|
||||
if("${MY_OUTPUT}" MATCHES "SMPEG_")
|
||||
find_library(SMPEG_LIBRARY
|
||||
NAMES smpeg SMPEG Smpeg SMpeg
|
||||
PATHS
|
||||
ENV SMPEGDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(SMPEG_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SMPEG_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${SMPEG_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
# Find FLAC
|
||||
if("${MY_OUTPUT}" MATCHES "FLAC_")
|
||||
find_library(FLAC_LIBRARY
|
||||
NAMES flac FLAC
|
||||
PATHS
|
||||
ENV FLACDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(FLAC_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${FLAC_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${FLAC_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
# Hmmm...Speex seems to depend on Ogg. This might be a problem if
|
||||
# the TRY_COMPILE attempt gets blocked at SPEEX before it can pull
|
||||
# in the Ogg symbols. I'm not sure if I should duplicate the ogg stuff
|
||||
# above for here or if two ogg entries will screw up things.
|
||||
if("${MY_OUTPUT}" MATCHES "speex_")
|
||||
find_library(SPEEX_LIBRARY
|
||||
NAMES speex SPEEX
|
||||
PATHS
|
||||
ENV SPEEXDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(SPEEX_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${SPEEX_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${SPEEX_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
|
||||
# Find OGG (needed for Speex)
|
||||
# We might have already found Ogg for Vorbis, so skip it if so.
|
||||
if(NOT OGG_LIBRARY)
|
||||
find_library(OGG_LIBRARY
|
||||
NAMES ogg Ogg OGG
|
||||
PATHS
|
||||
ENV OGGDIR
|
||||
ENV VORBISDIR
|
||||
ENV SPEEXDIR
|
||||
ENV SDLSOUNDDIR
|
||||
ENV SDLDIR
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt
|
||||
PATH_SUFFIXES lib
|
||||
)
|
||||
if(OGG_LIBRARY)
|
||||
set(SDL_SOUND_LIBRARIES_TMP ${SDL_SOUND_LIBRARIES_TMP} ${OGG_LIBRARY})
|
||||
set(TMP_LIBS "${SDL_SOUND_LIBRARIES_TMP} \"${OGG_LIBRARY}\"")
|
||||
set(TRY_AGAIN TRUE)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
ENDWHILE()
|
||||
unset(TMP_INCLUDE_DIRS)
|
||||
unset(TMP_LIBS)
|
||||
|
||||
set(SDL_SOUND_LIBRARIES ${SDL_SOUND_EXTRAS} ${SDL_SOUND_LIBRARIES_TMP} CACHE INTERNAL "SDL_sound and dependent libraries")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(SDL_SOUND_INCLUDE_DIR AND EXISTS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h")
|
||||
file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SOUND_VER_MAJOR[ \t]+[0-9]+$")
|
||||
file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_MINOR_LINE REGEX "^#define[ \t]+SOUND_VER_MINOR[ \t]+[0-9]+$")
|
||||
file(STRINGS "${SDL_SOUND_INCLUDE_DIR}/SDL_sound.h" SDL_SOUND_VERSION_PATCH_LINE REGEX "^#define[ \t]+SOUND_VER_PATCH[ \t]+[0-9]+$")
|
||||
string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MAJOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MAJOR "${SDL_SOUND_VERSION_MAJOR_LINE}")
|
||||
string(REGEX REPLACE "^#define[ \t]+SOUND_VER_MINOR[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_MINOR "${SDL_SOUND_VERSION_MINOR_LINE}")
|
||||
string(REGEX REPLACE "^#define[ \t]+SOUND_VER_PATCH[ \t]+([0-9]+)$" "\\1" SDL_SOUND_VERSION_PATCH "${SDL_SOUND_VERSION_PATCH_LINE}")
|
||||
set(SDL_SOUND_VERSION_STRING ${SDL_SOUND_VERSION_MAJOR}.${SDL_SOUND_VERSION_MINOR}.${SDL_SOUND_VERSION_PATCH})
|
||||
unset(SDL_SOUND_VERSION_MAJOR_LINE)
|
||||
unset(SDL_SOUND_VERSION_MINOR_LINE)
|
||||
unset(SDL_SOUND_VERSION_PATCH_LINE)
|
||||
unset(SDL_SOUND_VERSION_MAJOR)
|
||||
unset(SDL_SOUND_VERSION_MINOR)
|
||||
unset(SDL_SOUND_VERSION_PATCH)
|
||||
endif()
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL_sound
|
||||
REQUIRED_VARS SDL_SOUND_LIBRARIES SDL_SOUND_INCLUDE_DIR
|
||||
VERSION_VAR SDL_SOUND_VERSION_STRING)
|
@ -25,13 +25,14 @@
|
||||
/* This file contains an example for selecting an HRTF. */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
#include "AL/alc.h"
|
||||
@ -52,68 +53,62 @@ static LPALCRESETDEVICESOFT alcResetDeviceSOFT;
|
||||
*/
|
||||
static ALuint LoadSound(const char *filename)
|
||||
{
|
||||
Sound_Sample *sample;
|
||||
ALenum err, format;
|
||||
ALuint buffer;
|
||||
Uint32 slen;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
sf_count_t num_frames;
|
||||
ALsizei num_bytes;
|
||||
|
||||
/* Open the audio file */
|
||||
sample = Sound_NewSampleFromFile(filename, NULL, 65536);
|
||||
if(!sample)
|
||||
/* Open the audio file and check that it's usable. */
|
||||
sndfile = sf_open(filename, SFM_READ, &sfinfo);
|
||||
if(!sndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
|
||||
return 0;
|
||||
}
|
||||
if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
|
||||
{
|
||||
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
if(sample->actual.channels == 1)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_MONO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(sample->actual.channels == 2)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_STEREO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(sfinfo.channels == 1)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else if(sfinfo.channels == 2)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
|
||||
Sound_FreeSample(sample);
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decode the whole audio stream to a buffer. */
|
||||
slen = Sound_DecodeAll(sample);
|
||||
if(!sample->buffer || slen == 0)
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
|
||||
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
if(num_frames < 1)
|
||||
{
|
||||
fprintf(stderr, "Failed to read audio from %s\n", filename);
|
||||
Sound_FreeSample(sample);
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
|
||||
return 0;
|
||||
}
|
||||
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
|
||||
|
||||
/* Buffer the audio data into a new buffer object, then free the data and
|
||||
* close the file. */
|
||||
* close the file.
|
||||
*/
|
||||
buffer = 0;
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
|
||||
Sound_FreeSample(sample);
|
||||
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
|
||||
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
err = alGetError();
|
||||
@ -240,14 +235,10 @@ int main(int argc, char **argv)
|
||||
}
|
||||
fflush(stdout);
|
||||
|
||||
/* Initialize SDL_sound. */
|
||||
Sound_Init();
|
||||
|
||||
/* Load the sound into a buffer. */
|
||||
buffer = LoadSound(soundname);
|
||||
if(!buffer)
|
||||
{
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
@ -291,11 +282,9 @@ int main(int argc, char **argv)
|
||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
||||
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
|
||||
|
||||
/* All done. Delete resources, and close down SDL_sound and OpenAL. */
|
||||
/* All done. Delete resources, and close down OpenAL. */
|
||||
alDeleteSources(1, &source);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
|
||||
return 0;
|
||||
|
@ -24,12 +24,13 @@
|
||||
|
||||
/* This file contains an example for checking the latency of a sound. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
#include "AL/alext.h"
|
||||
@ -55,68 +56,62 @@ static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT;
|
||||
*/
|
||||
static ALuint LoadSound(const char *filename)
|
||||
{
|
||||
Sound_Sample *sample;
|
||||
ALenum err, format;
|
||||
ALuint buffer;
|
||||
Uint32 slen;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
sf_count_t num_frames;
|
||||
ALsizei num_bytes;
|
||||
|
||||
/* Open the audio file */
|
||||
sample = Sound_NewSampleFromFile(filename, NULL, 65536);
|
||||
if(!sample)
|
||||
/* Open the audio file and check that it's usable. */
|
||||
sndfile = sf_open(filename, SFM_READ, &sfinfo);
|
||||
if(!sndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
|
||||
return 0;
|
||||
}
|
||||
if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
|
||||
{
|
||||
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
if(sample->actual.channels == 1)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_MONO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(sample->actual.channels == 2)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_STEREO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(sfinfo.channels == 1)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else if(sfinfo.channels == 2)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
|
||||
Sound_FreeSample(sample);
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decode the whole audio stream to a buffer. */
|
||||
slen = Sound_DecodeAll(sample);
|
||||
if(!sample->buffer || slen == 0)
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
|
||||
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
if(num_frames < 1)
|
||||
{
|
||||
fprintf(stderr, "Failed to read audio from %s\n", filename);
|
||||
Sound_FreeSample(sample);
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
|
||||
return 0;
|
||||
}
|
||||
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
|
||||
|
||||
/* Buffer the audio data into a new buffer object, then free the data and
|
||||
* close the file. */
|
||||
* close the file.
|
||||
*/
|
||||
buffer = 0;
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
|
||||
Sound_FreeSample(sample);
|
||||
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
|
||||
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
err = alGetError();
|
||||
@ -173,14 +168,10 @@ int main(int argc, char **argv)
|
||||
LOAD_PROC(LPALGETSOURCEI64VSOFT, alGetSourcei64vSOFT);
|
||||
#undef LOAD_PROC
|
||||
|
||||
/* Initialize SDL_sound. */
|
||||
Sound_Init();
|
||||
|
||||
/* Load the sound into a buffer. */
|
||||
buffer = LoadSound(argv[0]);
|
||||
if(!buffer)
|
||||
{
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
@ -206,11 +197,9 @@ int main(int argc, char **argv)
|
||||
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
|
||||
printf("\n");
|
||||
|
||||
/* All done. Delete resources, and close down SDL_sound and OpenAL. */
|
||||
/* All done. Delete resources, and close down OpenAL. */
|
||||
alDeleteSources(1, &source);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
|
||||
return 0;
|
||||
|
@ -29,14 +29,16 @@
|
||||
* listener.
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
#include "AL/alc.h"
|
||||
@ -151,68 +153,62 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
|
||||
*/
|
||||
static ALuint LoadSound(const char *filename)
|
||||
{
|
||||
Sound_Sample *sample;
|
||||
ALenum err, format;
|
||||
ALuint buffer;
|
||||
Uint32 slen;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
sf_count_t num_frames;
|
||||
ALsizei num_bytes;
|
||||
|
||||
/* Open the audio file */
|
||||
sample = Sound_NewSampleFromFile(filename, NULL, 65536);
|
||||
if(!sample)
|
||||
/* Open the audio file and check that it's usable. */
|
||||
sndfile = sf_open(filename, SFM_READ, &sfinfo);
|
||||
if(!sndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
|
||||
return 0;
|
||||
}
|
||||
if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
|
||||
{
|
||||
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
if(sample->actual.channels == 1)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_MONO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(sample->actual.channels == 2)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_STEREO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(sfinfo.channels == 1)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else if(sfinfo.channels == 2)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
|
||||
Sound_FreeSample(sample);
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decode the whole audio stream to a buffer. */
|
||||
slen = Sound_DecodeAll(sample);
|
||||
if(!sample->buffer || slen == 0)
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
|
||||
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
if(num_frames < 1)
|
||||
{
|
||||
fprintf(stderr, "Failed to read audio from %s\n", filename);
|
||||
Sound_FreeSample(sample);
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
|
||||
return 0;
|
||||
}
|
||||
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
|
||||
|
||||
/* Buffer the audio data into a new buffer object, then free the data and
|
||||
* close the file. */
|
||||
* close the file.
|
||||
*/
|
||||
buffer = 0;
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
|
||||
Sound_FreeSample(sample);
|
||||
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
|
||||
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
err = alGetError();
|
||||
@ -561,15 +557,11 @@ int main(int argc, char **argv)
|
||||
LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
|
||||
#undef LOAD_PROC
|
||||
|
||||
/* Initialize SDL_sound. */
|
||||
Sound_Init();
|
||||
|
||||
/* Load the sound into a buffer. */
|
||||
buffer = LoadSound(argv[0]);
|
||||
if(!buffer)
|
||||
{
|
||||
CloseAL();
|
||||
Sound_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -585,7 +577,6 @@ int main(int argc, char **argv)
|
||||
{
|
||||
alDeleteEffects(2, effects);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
@ -684,14 +675,13 @@ int main(int argc, char **argv)
|
||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
||||
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING && loops < MaxTransitions);
|
||||
|
||||
/* All done. Delete resources, and close down SDL_sound and OpenAL. */
|
||||
/* All done. Delete resources, and close down OpenAL. */
|
||||
alDeleteSources(1, &source);
|
||||
alDeleteAuxiliaryEffectSlots(2, slots);
|
||||
alDeleteEffects(2, effects);
|
||||
alDeleteFilters(1, &direct_filter);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
|
||||
return 0;
|
||||
|
@ -158,7 +158,7 @@ int main(int argc, char **argv)
|
||||
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
|
||||
printf("\n");
|
||||
|
||||
/* All done. Delete resources, and close down SDL_sound and OpenAL. */
|
||||
/* All done. Delete resources, and close down OpenAL. */
|
||||
alDeleteSources(1, &source);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
|
||||
|
@ -24,12 +24,13 @@
|
||||
|
||||
/* This file contains an example for applying reverb to a sound. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
#include "AL/alc.h"
|
||||
@ -149,68 +150,62 @@ static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
|
||||
*/
|
||||
static ALuint LoadSound(const char *filename)
|
||||
{
|
||||
Sound_Sample *sample;
|
||||
ALenum err, format;
|
||||
ALuint buffer;
|
||||
Uint32 slen;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
sf_count_t num_frames;
|
||||
ALsizei num_bytes;
|
||||
|
||||
/* Open the audio file */
|
||||
sample = Sound_NewSampleFromFile(filename, NULL, 65536);
|
||||
if(!sample)
|
||||
/* Open the audio file and check that it's usable. */
|
||||
sndfile = sf_open(filename, SFM_READ, &sfinfo);
|
||||
if(!sndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
|
||||
return 0;
|
||||
}
|
||||
if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
|
||||
{
|
||||
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
if(sample->actual.channels == 1)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_MONO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if(sample->actual.channels == 2)
|
||||
{
|
||||
if(sample->actual.format == AUDIO_U8)
|
||||
format = AL_FORMAT_STEREO8;
|
||||
else if(sample->actual.format == AUDIO_S16SYS)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
|
||||
Sound_FreeSample(sample);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(sfinfo.channels == 1)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else if(sfinfo.channels == 2)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
|
||||
Sound_FreeSample(sample);
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decode the whole audio stream to a buffer. */
|
||||
slen = Sound_DecodeAll(sample);
|
||||
if(!sample->buffer || slen == 0)
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
|
||||
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
if(num_frames < 1)
|
||||
{
|
||||
fprintf(stderr, "Failed to read audio from %s\n", filename);
|
||||
Sound_FreeSample(sample);
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
|
||||
return 0;
|
||||
}
|
||||
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
|
||||
|
||||
/* Buffer the audio data into a new buffer object, then free the data and
|
||||
* close the file. */
|
||||
* close the file.
|
||||
*/
|
||||
buffer = 0;
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
|
||||
Sound_FreeSample(sample);
|
||||
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
|
||||
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
err = alGetError();
|
||||
@ -278,15 +273,11 @@ int main(int argc, char **argv)
|
||||
LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
|
||||
#undef LOAD_PROC
|
||||
|
||||
/* Initialize SDL_sound. */
|
||||
Sound_Init();
|
||||
|
||||
/* Load the sound into a buffer. */
|
||||
buffer = LoadSound(argv[0]);
|
||||
if(!buffer)
|
||||
{
|
||||
CloseAL();
|
||||
Sound_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -295,7 +286,6 @@ int main(int argc, char **argv)
|
||||
if(!effect)
|
||||
{
|
||||
alDeleteBuffers(1, &buffer);
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
@ -330,13 +320,12 @@ int main(int argc, char **argv)
|
||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
||||
} while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
|
||||
|
||||
/* All done. Delete resources, and close down SDL_sound and OpenAL. */
|
||||
/* All done. Delete resources, and close down OpenAL. */
|
||||
alDeleteSources(1, &source);
|
||||
alDeleteAuxiliaryEffectSlots(1, &slot);
|
||||
alDeleteEffects(1, &effect);
|
||||
alDeleteBuffers(1, &buffer);
|
||||
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
|
||||
return 0;
|
||||
|
@ -24,32 +24,24 @@
|
||||
|
||||
/* This file contains a relatively simple streaming audio player. */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
|
||||
#ifndef SDL_AUDIO_MASK_BITSIZE
|
||||
#define SDL_AUDIO_MASK_BITSIZE (0xFF)
|
||||
#endif
|
||||
#ifndef SDL_AUDIO_BITSIZE
|
||||
#define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
|
||||
#endif
|
||||
|
||||
/* Define the number of buffers and buffer size (in milliseconds) to use. 4
|
||||
* buffers with 200ms each gives a nice per-chunk size, and lets the queue last
|
||||
* for almost one second. */
|
||||
* buffers with 8192 samples each gives a nice per-chunk size, and lets the
|
||||
* queue last for almost one second at 44.1khz. */
|
||||
#define NUM_BUFFERS 4
|
||||
#define BUFFER_TIME_MS 200
|
||||
#define BUFFER_SAMPLES 8192
|
||||
|
||||
typedef struct StreamPlayer {
|
||||
/* These are the buffers and source to play out through OpenAL with */
|
||||
@ -57,11 +49,12 @@ typedef struct StreamPlayer {
|
||||
ALuint source;
|
||||
|
||||
/* Handle for the audio file */
|
||||
Sound_Sample *sample;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
|
||||
/* The format of the output stream */
|
||||
/* The format of the output stream (sample rate is in sfinfo) */
|
||||
ALenum format;
|
||||
ALsizei srate;
|
||||
} StreamPlayer;
|
||||
|
||||
static StreamPlayer *NewPlayer(void);
|
||||
@ -118,73 +111,46 @@ static void DeletePlayer(StreamPlayer *player)
|
||||
* it will be closed first. */
|
||||
static int OpenPlayerFile(StreamPlayer *player, const char *filename)
|
||||
{
|
||||
Uint32 frame_size;
|
||||
size_t frame_size;
|
||||
|
||||
ClosePlayerFile(player);
|
||||
|
||||
/* Open the file and get the first stream from it */
|
||||
player->sample = Sound_NewSampleFromFile(filename, NULL, 0);
|
||||
if(!player->sample)
|
||||
/* Open the audio file and check that it's usable. */
|
||||
player->sndfile = sf_open(filename, SFM_READ, &player->sfinfo);
|
||||
if(!player->sndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
goto error;
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(NULL));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the stream format, and figure out the OpenAL format */
|
||||
if(player->sample->actual.channels == 1)
|
||||
{
|
||||
if(player->sample->actual.format == AUDIO_U8)
|
||||
player->format = AL_FORMAT_MONO8;
|
||||
else if(player->sample->actual.format == AUDIO_S16SYS)
|
||||
player->format = AL_FORMAT_MONO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", player->sample->actual.format);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else if(player->sample->actual.channels == 2)
|
||||
{
|
||||
if(player->sample->actual.format == AUDIO_U8)
|
||||
player->format = AL_FORMAT_STEREO8;
|
||||
else if(player->sample->actual.format == AUDIO_S16SYS)
|
||||
player->format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x\n", player->sample->actual.format);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
if(player->sfinfo.channels == 1)
|
||||
player->format = AL_FORMAT_MONO16;
|
||||
else if(player->sfinfo.channels == 2)
|
||||
player->format = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", player->sample->actual.channels);
|
||||
goto error;
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", player->sfinfo.channels);
|
||||
sf_close(player->sndfile);
|
||||
player->sndfile = NULL;
|
||||
return 0;
|
||||
}
|
||||
player->srate = (ALsizei)player->sample->actual.rate;
|
||||
|
||||
frame_size = player->sample->actual.channels *
|
||||
SDL_AUDIO_BITSIZE(player->sample->actual.format) / 8;
|
||||
|
||||
/* Set the buffer size, given the desired millisecond length. */
|
||||
Sound_SetBufferSize(player->sample, (Uint32)((Uint64)player->srate*BUFFER_TIME_MS/1000) *
|
||||
frame_size);
|
||||
frame_size = (size_t)(BUFFER_SAMPLES * player->sfinfo.channels) * sizeof(short);
|
||||
player->membuf = malloc(frame_size);
|
||||
|
||||
return 1;
|
||||
|
||||
error:
|
||||
if(player->sample)
|
||||
Sound_FreeSample(player->sample);
|
||||
player->sample = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Closes the audio file stream */
|
||||
static void ClosePlayerFile(StreamPlayer *player)
|
||||
{
|
||||
if(player->sample)
|
||||
Sound_FreeSample(player->sample);
|
||||
player->sample = NULL;
|
||||
if(player->sndfile)
|
||||
sf_close(player->sndfile);
|
||||
player->sndfile = NULL;
|
||||
|
||||
free(player->membuf);
|
||||
player->membuf = NULL;
|
||||
}
|
||||
|
||||
|
||||
@ -201,11 +167,12 @@ static int StartPlayer(StreamPlayer *player)
|
||||
for(i = 0;i < NUM_BUFFERS;i++)
|
||||
{
|
||||
/* Get some data to give it to the buffer */
|
||||
Uint32 slen = Sound_Decode(player->sample);
|
||||
if(slen == 0) break;
|
||||
sf_count_t slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
if(slen < 1) break;
|
||||
|
||||
alBufferData(player->buffers[i], player->format, player->sample->buffer, (ALsizei)slen,
|
||||
player->srate);
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
|
||||
alBufferData(player->buffers[i], player->format, player->membuf, (ALsizei)slen,
|
||||
player->sfinfo.samplerate);
|
||||
}
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
{
|
||||
@ -242,21 +209,19 @@ static int UpdatePlayer(StreamPlayer *player)
|
||||
while(processed > 0)
|
||||
{
|
||||
ALuint bufid;
|
||||
Uint32 slen;
|
||||
sf_count_t slen;
|
||||
|
||||
alSourceUnqueueBuffers(player->source, 1, &bufid);
|
||||
processed--;
|
||||
|
||||
if((player->sample->flags&(SOUND_SAMPLEFLAG_EOF|SOUND_SAMPLEFLAG_ERROR)))
|
||||
continue;
|
||||
|
||||
/* Read the next chunk of data, refill the buffer, and queue it
|
||||
* back on the source */
|
||||
slen = Sound_Decode(player->sample);
|
||||
slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
if(slen > 0)
|
||||
{
|
||||
alBufferData(bufid, player->format, player->sample->buffer, (ALsizei)slen,
|
||||
player->srate);
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
|
||||
alBufferData(bufid, player->format, player->membuf, (ALsizei)slen,
|
||||
player->sfinfo.samplerate);
|
||||
alSourceQueueBuffers(player->source, 1, &bufid);
|
||||
}
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
@ -304,8 +269,6 @@ int main(int argc, char **argv)
|
||||
if(InitAL(&argv, &argc) != 0)
|
||||
return 1;
|
||||
|
||||
Sound_Init();
|
||||
|
||||
player = NewPlayer();
|
||||
|
||||
/* Play each file listed on the command line */
|
||||
@ -323,7 +286,8 @@ int main(int argc, char **argv)
|
||||
else
|
||||
namepart = argv[i];
|
||||
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format), player->srate);
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
|
||||
player->sfinfo.samplerate);
|
||||
fflush(stdout);
|
||||
|
||||
if(!StartPlayer(player))
|
||||
@ -340,11 +304,10 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Done.\n");
|
||||
|
||||
/* All files done. Delete the player, and close down SDL_sound and OpenAL */
|
||||
/* All files done. Delete the player, and close down OpenAL */
|
||||
DeletePlayer(player);
|
||||
player = NULL;
|
||||
|
||||
Sound_Quit();
|
||||
CloseAL();
|
||||
|
||||
return 0;
|
||||
|
@ -36,9 +36,7 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "SDL_sound.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_stdinc.h"
|
||||
#include "sndfile.h"
|
||||
|
||||
#include "AL/al.h"
|
||||
#include "AL/alc.h"
|
||||
@ -46,14 +44,6 @@
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
|
||||
#ifndef SDL_AUDIO_MASK_BITSIZE
|
||||
#define SDL_AUDIO_MASK_BITSIZE (0xFF)
|
||||
#endif
|
||||
#ifndef SDL_AUDIO_BITSIZE
|
||||
#define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef AL_SOFT_callback_buffer
|
||||
#define AL_SOFT_callback_buffer
|
||||
typedef unsigned int ALbitfieldSOFT;
|
||||
@ -87,13 +77,12 @@ struct StreamPlayer {
|
||||
size_t mStartOffset{0};
|
||||
|
||||
/* Handle for the audio file to decode. */
|
||||
Sound_Sample *mSample{nullptr};
|
||||
Uint32 mAvailableData{0};
|
||||
SNDFILE *mSndfile{nullptr};
|
||||
SF_INFO mSfInfo{};
|
||||
size_t mDecoderOffset{0};
|
||||
|
||||
/* The format of the callback samples. */
|
||||
ALenum mFormat;
|
||||
ALsizei mSampleRate;
|
||||
|
||||
StreamPlayer()
|
||||
{
|
||||
@ -111,18 +100,18 @@ struct StreamPlayer {
|
||||
{
|
||||
alDeleteSources(1, &mSource);
|
||||
alDeleteBuffers(1, &mBuffer);
|
||||
if(mSample)
|
||||
Sound_FreeSample(mSample);
|
||||
if(mSndfile)
|
||||
sf_close(mSndfile);
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if(mSample)
|
||||
if(mSndfile)
|
||||
{
|
||||
alSourceRewind(mSource);
|
||||
alSourcei(mSource, AL_BUFFER, 0);
|
||||
Sound_FreeSample(mSample);
|
||||
mSample = nullptr;
|
||||
sf_close(mSndfile);
|
||||
mSndfile = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,50 +119,30 @@ struct StreamPlayer {
|
||||
{
|
||||
close();
|
||||
|
||||
/* Open the file in its normal format. */
|
||||
mSample = Sound_NewSampleFromFile(filename, nullptr, 0);
|
||||
if(!mSample)
|
||||
/* Open the file and figure out the OpenAL format. */
|
||||
mSndfile = sf_open(filename, SFM_READ, &mSfInfo);
|
||||
if(!mSndfile)
|
||||
{
|
||||
fprintf(stderr, "Could not open audio in %s\n", filename);
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(mSndfile));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Figure out the OpenAL format from the sample's format. */
|
||||
mFormat = AL_NONE;
|
||||
if(mSample->actual.channels == 1)
|
||||
if(mSfInfo.channels == 1)
|
||||
mFormat = AL_FORMAT_MONO16;
|
||||
else if(mSfInfo.channels == 2)
|
||||
mFormat = AL_FORMAT_STEREO16;
|
||||
else
|
||||
{
|
||||
if(mSample->actual.format == AUDIO_U8)
|
||||
mFormat = AL_FORMAT_MONO8;
|
||||
else if(mSample->actual.format == AUDIO_S16SYS)
|
||||
mFormat = AL_FORMAT_MONO16;
|
||||
}
|
||||
else if(mSample->actual.channels == 2)
|
||||
{
|
||||
if(mSample->actual.format == AUDIO_U8)
|
||||
mFormat = AL_FORMAT_STEREO8;
|
||||
else if(mSample->actual.format == AUDIO_S16SYS)
|
||||
mFormat = AL_FORMAT_STEREO16;
|
||||
}
|
||||
if(!mFormat)
|
||||
{
|
||||
fprintf(stderr, "Unsupported sample format: 0x%04x, %d channels\n",
|
||||
mSample->actual.format, mSample->actual.channels);
|
||||
Sound_FreeSample(mSample);
|
||||
mSample = nullptr;
|
||||
fprintf(stderr, "Unsupported channel count: %d\n", mSfInfo.channels);
|
||||
sf_close(mSndfile);
|
||||
mSndfile = nullptr;
|
||||
|
||||
return false;
|
||||
}
|
||||
mSampleRate = static_cast<ALsizei>(mSample->actual.rate);
|
||||
|
||||
const auto frame_size = Uint32{mSample->actual.channels} *
|
||||
SDL_AUDIO_BITSIZE(mSample->actual.format) / 8;
|
||||
|
||||
/* Set a 50ms decode buffer size. */
|
||||
Sound_SetBufferSize(mSample, static_cast<Uint32>(mSampleRate)*50/1000 * frame_size);
|
||||
mAvailableData = 0;
|
||||
|
||||
/* Set a 1s ring buffer size. */
|
||||
mBufferDataSize = static_cast<Uint32>(mSampleRate) * size_t{frame_size};
|
||||
mBufferDataSize = static_cast<ALuint>(mSfInfo.samplerate*mSfInfo.channels) * sizeof(short);
|
||||
mBufferData.reset(new ALbyte[mBufferDataSize]);
|
||||
mReadPos.store(0, std::memory_order_relaxed);
|
||||
mWritePos.store(0, std::memory_order_relaxed);
|
||||
@ -239,34 +208,27 @@ struct StreamPlayer {
|
||||
|
||||
bool prepare()
|
||||
{
|
||||
alBufferCallbackSOFT(mBuffer, mFormat, mSampleRate, bufferCallbackC, this, 0);
|
||||
alBufferCallbackSOFT(mBuffer, mFormat, mSfInfo.samplerate, bufferCallbackC, this, 0);
|
||||
alSourcei(mSource, AL_BUFFER, static_cast<ALint>(mBuffer));
|
||||
if(ALenum err{alGetError()})
|
||||
{
|
||||
fprintf(stderr, "Failed to set callback: %s (0x%04x)\n", alGetString(err), err);
|
||||
return false;
|
||||
}
|
||||
|
||||
mAvailableData = Sound_Decode(mSample);
|
||||
if(!mAvailableData)
|
||||
fprintf(stderr, "Failed to decode any samples: %s\n", Sound_GetError());
|
||||
return mAvailableData != 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool update()
|
||||
{
|
||||
constexpr int BadFlags{SOUND_SAMPLEFLAG_EOF | SOUND_SAMPLEFLAG_ERROR};
|
||||
|
||||
ALenum state;
|
||||
ALint pos;
|
||||
alGetSourcei(mSource, AL_SAMPLE_OFFSET, &pos);
|
||||
alGetSourcei(mSource, AL_SOURCE_STATE, &state);
|
||||
|
||||
const size_t frame_size{static_cast<ALuint>(mSfInfo.channels) * sizeof(short)};
|
||||
size_t woffset{mWritePos.load(std::memory_order_acquire)};
|
||||
if(state != AL_INITIAL)
|
||||
{
|
||||
const auto frame_size = Uint32{mSample->actual.channels} *
|
||||
SDL_AUDIO_BITSIZE(mSample->actual.format) / 8;
|
||||
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
|
||||
const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
|
||||
roffset};
|
||||
@ -276,15 +238,17 @@ struct StreamPlayer {
|
||||
* the playback offset the source was started with.
|
||||
*/
|
||||
const size_t curtime{((state==AL_STOPPED) ? (mDecoderOffset-readable) / frame_size
|
||||
: (static_cast<ALuint>(pos) + mStartOffset/frame_size)) / mSample->actual.rate};
|
||||
: (static_cast<ALuint>(pos) + mStartOffset/frame_size))
|
||||
/ static_cast<ALuint>(mSfInfo.samplerate)};
|
||||
printf("\r%3zus (%3zu%% full)", curtime, readable * 100 / mBufferDataSize);
|
||||
}
|
||||
else
|
||||
fputs("Starting...", stdout);
|
||||
fflush(stdout);
|
||||
|
||||
while(mAvailableData > 0)
|
||||
while(!sf_error(mSndfile))
|
||||
{
|
||||
size_t read_bytes;
|
||||
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
|
||||
if(roffset > woffset)
|
||||
{
|
||||
@ -294,45 +258,39 @@ struct StreamPlayer {
|
||||
* instead of full.
|
||||
*/
|
||||
const size_t writable{roffset-woffset-1};
|
||||
/* Don't copy the sample data if it can't all fit. */
|
||||
if(writable < mAvailableData) break;
|
||||
if(writable < frame_size) break;
|
||||
|
||||
memcpy(&mBufferData[woffset], mSample->buffer, mAvailableData);
|
||||
woffset += mAvailableData;
|
||||
sf_count_t num_frames{sf_readf_short(mSndfile,
|
||||
reinterpret_cast<short*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable/frame_size))};
|
||||
if(num_frames < 1) break;
|
||||
|
||||
read_bytes = static_cast<size_t>(num_frames) * frame_size;
|
||||
woffset += read_bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If the read offset is at or behind the write offset, the
|
||||
* writeable area (might) wrap around. Make sure the sample
|
||||
* data can fit, and calculate how much goes in front and in
|
||||
* back.
|
||||
* data can fit, and calculate how much can go in front before
|
||||
* wrapping.
|
||||
*/
|
||||
const size_t writable{mBufferDataSize+roffset-woffset-1};
|
||||
if(writable < mAvailableData) break;
|
||||
const size_t writable{!roffset ? mBufferDataSize-woffset-1 :
|
||||
(mBufferDataSize-woffset)};
|
||||
if(writable < frame_size) break;
|
||||
|
||||
const size_t todo1{std::min<size_t>(mAvailableData, mBufferDataSize-woffset)};
|
||||
const size_t todo2{mAvailableData - todo1};
|
||||
sf_count_t num_frames{sf_readf_short(mSndfile,
|
||||
reinterpret_cast<short*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable/frame_size))};
|
||||
if(num_frames < 1) break;
|
||||
|
||||
memcpy(&mBufferData[woffset], mSample->buffer, todo1);
|
||||
woffset += todo1;
|
||||
read_bytes = static_cast<size_t>(num_frames) * frame_size;
|
||||
woffset += read_bytes;
|
||||
if(woffset == mBufferDataSize)
|
||||
{
|
||||
woffset = 0;
|
||||
if(todo2 > 0)
|
||||
{
|
||||
memcpy(&mBufferData[woffset], static_cast<ALbyte*>(mSample->buffer)+todo1,
|
||||
todo2);
|
||||
woffset += todo2;
|
||||
}
|
||||
}
|
||||
}
|
||||
mWritePos.store(woffset, std::memory_order_release);
|
||||
mDecoderOffset += mAvailableData;
|
||||
|
||||
if(!(mSample->flags&BadFlags))
|
||||
mAvailableData = Sound_Decode(mSample);
|
||||
else
|
||||
mAvailableData = 0;
|
||||
mDecoderOffset += read_bytes;
|
||||
}
|
||||
|
||||
if(state != AL_PLAYING && state != AL_PAUSED)
|
||||
@ -364,15 +322,14 @@ struct StreamPlayer {
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* A simple RAII container for OpenAL and SDL_sound startup and shutdown. */
|
||||
/* A simple RAII container for OpenAL startup and shutdown. */
|
||||
struct AudioManager {
|
||||
AudioManager(char ***argv_, int *argc_)
|
||||
{
|
||||
if(InitAL(argv_, argc_) != 0)
|
||||
throw std::runtime_error{"Failed to initialize OpenAL"};
|
||||
Sound_Init();
|
||||
}
|
||||
~AudioManager() { Sound_Quit(); CloseAL(); }
|
||||
~AudioManager() { CloseAL(); }
|
||||
};
|
||||
|
||||
/* Print out usage if no arguments were specified */
|
||||
@ -413,7 +370,7 @@ int main(int argc, char **argv)
|
||||
namepart = argv[i];
|
||||
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->mFormat),
|
||||
player->mSampleRate);
|
||||
player->mSfInfo.samplerate);
|
||||
fflush(stdout);
|
||||
|
||||
if(!player->prepare())
|
||||
|
Loading…
x
Reference in New Issue
Block a user