diff options
author | kcat <[email protected]> | 2017-05-05 04:53:11 -0700 |
---|---|---|
committer | GitHub <[email protected]> | 2017-05-05 04:53:11 -0700 |
commit | af997bf853c5991341658ed8ace5ae4cd6d350aa (patch) | |
tree | 86d9896ad323613e5457a0765de12f0737f15e2b | |
parent | 9c9ad2f60aead15416c8b582e20fadf847a13ce8 (diff) | |
parent | 24bc8070c89c2c8ca74f1e80eda1ea4504064b8b (diff) |
Merge pull request #112 from alexey-lysiuk/embed_hrtf_pr
Cross-platform embedding of HRTF data
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | Alc/hrtf.c | 77 | ||||
-rw-r--r-- | Alc/hrtf_res.h | 5 | ||||
-rw-r--r-- | Alc/hrtf_res.rc | 4 | ||||
-rw-r--r-- | CMakeLists.txt | 67 | ||||
-rw-r--r-- | appveyor.yml | 2 | ||||
-rw-r--r-- | utils/native-tools/CMakeLists.txt | 8 | ||||
-rw-r--r-- | utils/native-tools/bin2h.c | 100 |
8 files changed, 149 insertions, 117 deletions
diff --git a/.travis.yml b/.travis.yml index c24cd95b..e0a64c84 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,6 +49,7 @@ script: -DALSOFT_REQUIRE_PORTAUDIO=ON \ -DALSOFT_REQUIRE_PULSEAUDIO=ON \ -DALSOFT_REQUIRE_JACK=ON \ + -DALSOFT_EMBED_HRTF_DATA=YES \ . fi - > @@ -56,12 +57,14 @@ script: cmake \ -DCMAKE_TOOLCHAIN_FILE=~/android-ndk-r14/build/cmake/android.toolchain.cmake \ -DALSOFT_REQUIRE_OPENSL=ON \ + -DALSOFT_EMBED_HRTF_DATA=YES \ . fi - > if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then cmake \ -DALSOFT_REQUIRE_COREAUDIO=ON \ + -DALSOFT_EMBED_HRTF_DATA=YES \ . fi - make -j2 @@ -869,99 +869,38 @@ static void AddBuiltInEntry(vector_EnumeratedHrtf *list, const_al_string filenam } -#ifndef ALSOFT_EMBED_HRTF_DATA #define IDR_DEFAULT_44100_MHR 1 #define IDR_DEFAULT_48000_MHR 2 -static const ALubyte *GetResource(int UNUSED(name), size_t *size) -{ - *size = 0; - return NULL; -} - -#else -#include "hrtf_res.h" - -#ifdef _WIN32 -static const ALubyte *GetResource(int name, size_t *size) -{ - HMODULE handle; - HGLOBAL res; - HRSRC rc; - - GetModuleHandleExW( - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - (LPCWSTR)GetResource, &handle - ); - rc = FindResourceW(handle, MAKEINTRESOURCEW(name), MAKEINTRESOURCEW(MHRTYPE)); - res = LoadResource(handle, rc); - - *size = SizeofResource(handle, rc); - return LockResource(res); -} - -#elif defined(__APPLE__) - -#include <Availability.h> -#include <mach-o/getsect.h> -#include <mach-o/ldsyms.h> +#ifndef ALSOFT_EMBED_HRTF_DATA -static const ALubyte *GetResource(int name, size_t *size) +static const ALubyte *GetResource(int UNUSED(name), size_t *size) { -#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) - /* NOTE: OSX 10.7 and up need to call getsectiondata(&_mh_dylib_header, ...). However, that - * call requires 10.7. - */ - if(name == IDR_DEFAULT_44100_MHR) - return getsectiondata(&_mh_dylib_header, "binary", "default_44100", size); - if(name == IDR_DEFAULT_48000_MHR) - return getsectiondata(&_mh_dylib_header, "binary", "default_48000", size); -#else - if(name == IDR_DEFAULT_44100_MHR) - return getsectdata("binary", "default_44100", size); - if(name == IDR_DEFAULT_48000_MHR) - return getsectdata("binary", "default_48000", size); -#endif *size = 0; return NULL; } #else -extern const ALubyte _binary_default_44100_mhr_start[] HIDDEN_DECL; -extern const ALubyte _binary_default_44100_mhr_end[] HIDDEN_DECL; -extern const ALubyte _binary_default_44100_mhr_size[] HIDDEN_DECL; - -extern const ALubyte _binary_default_48000_mhr_start[] HIDDEN_DECL; -extern const ALubyte _binary_default_48000_mhr_end[] HIDDEN_DECL; -extern const ALubyte _binary_default_48000_mhr_size[] HIDDEN_DECL; +#include "default-44100.mhr.h" +#include "default-48000.mhr.h" static const ALubyte *GetResource(int name, size_t *size) { if(name == IDR_DEFAULT_44100_MHR) { - /* Make sure all symbols are referenced, to ensure the compiler won't - * ignore the declarations and lose the visibility attribute used to - * hide them (would be nice if ld or objcopy could automatically mark - * them as hidden when generating them, but apparently they can't). - */ - const void *volatile ptr =_binary_default_44100_mhr_size; - (void)ptr; - *size = _binary_default_44100_mhr_end - _binary_default_44100_mhr_start; - return _binary_default_44100_mhr_start; + *size = sizeof(hrtf_default_44100); + return hrtf_default_44100; } if(name == IDR_DEFAULT_48000_MHR) { - const void *volatile ptr =_binary_default_48000_mhr_size; - (void)ptr; - *size = _binary_default_48000_mhr_end - _binary_default_48000_mhr_start; - return _binary_default_48000_mhr_start; + *size = sizeof(hrtf_default_48000); + return hrtf_default_48000; } *size = 0; return NULL; } #endif -#endif vector_EnumeratedHrtf EnumerateHrtf(const_al_string devname) { diff --git a/Alc/hrtf_res.h b/Alc/hrtf_res.h deleted file mode 100644 index 2dc39cf1..00000000 --- a/Alc/hrtf_res.h +++ /dev/null @@ -1,5 +0,0 @@ - -#define MHRTYPE 256 - -#define IDR_DEFAULT_44100_MHR 1 -#define IDR_DEFAULT_48000_MHR 2 diff --git a/Alc/hrtf_res.rc b/Alc/hrtf_res.rc deleted file mode 100644 index a17b7447..00000000 --- a/Alc/hrtf_res.rc +++ /dev/null @@ -1,4 +0,0 @@ -#include "hrtf_res.h" - -IDR_DEFAULT_44100_MHR MHRTYPE "../hrtf/default-44100.mhr" -IDR_DEFAULT_48000_MHR MHRTYPE "../hrtf/default-48000.mhr" diff --git a/CMakeLists.txt b/CMakeLists.txt index efb06ebf..f15bcea2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1102,44 +1102,35 @@ SET(BACKENDS "${BACKENDS} Null") option(ALSOFT_EMBED_HRTF_DATA "Embed the HRTF data files (increases library footprint)" OFF) if(ALSOFT_EMBED_HRTF_DATA) - if(WIN32) - set(ALC_OBJS ${ALC_OBJS} Alc/hrtf_res.rc) - elseif(APPLE) - macro(add_custom_binary FILENAME BIN_NAME) - set(outfile ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${FILENAME}${CMAKE_C_OUTPUT_EXTENSION}) - set(stubsrcfile ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${FILENAME}.stub.c) - set(stubfile ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${FILENAME}.stub${CMAKE_C_OUTPUT_EXTENSION}) - add_custom_command(OUTPUT ${outfile} - DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/hrtf/${FILENAME}" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/hrtf" - COMMAND touch "${stubsrcfile}" - COMMAND "${CMAKE_C_COMPILER}" -o "${stubfile}" -c "${stubsrcfile}" - COMMAND "${CMAKE_LINKER}" -r -o "${outfile}" -sectcreate binary ${BIN_NAME} ${FILENAME} "${stubfile}" - COMMAND rm "${stubsrcfile}" "${stubfile}" - COMMENT "Generating ${FILENAME}${CMAKE_C_OUTPUT_EXTENSION}" - VERBATIM - ) - set(ALC_OBJS ${ALC_OBJS} ${outfile}) - endmacro() - add_custom_binary(default-44100.mhr "default_44100") - add_custom_binary(default-48000.mhr "default_48000") - else() - set(FILENAMES default-44100.mhr default-48000.mhr) - foreach(FILENAME ${FILENAMES}) - set(outfile ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${FILENAME}${CMAKE_C_OUTPUT_EXTENSION}) - add_custom_command(OUTPUT ${outfile} - DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/hrtf/${FILENAME}" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/hrtf" - COMMAND "${CMAKE_LINKER}" -r -b binary -o "${outfile}" ${FILENAME} - COMMAND "${CMAKE_OBJCOPY}" --rename-section .data=.rodata,alloc,load,readonly,data,contents "${outfile}" "${outfile}" - COMMENT "Generating ${FILENAME}${CMAKE_C_OUTPUT_EXTENSION}" - VERBATIM - ) - set(ALC_OBJS ${ALC_OBJS} ${outfile}) - endforeach() - unset(outfile) - unset(FILENAMES) - endif() + SET(NATIVE_SRC_DIR "${OpenAL_SOURCE_DIR}/utils/native-tools/") + SET(NATIVE_BIN_DIR "${OpenAL_BINARY_DIR}/native-tools/") + FILE(MAKE_DIRECTORY "${NATIVE_BIN_DIR}") + + SET(BIN2H_COMMAND "${NATIVE_BIN_DIR}bin2h") + ADD_CUSTOM_COMMAND(OUTPUT "${BIN2H_COMMAND}" + COMMAND ${CMAKE_COMMAND} "${NATIVE_SRC_DIR}" + COMMAND ${CMAKE_COMMAND} --build . --config "Release" + WORKING_DIRECTORY "${NATIVE_BIN_DIR}" + DEPENDS "${NATIVE_SRC_DIR}CMakeLists.txt" "${NATIVE_SRC_DIR}bin2h.c" + VERBATIM + ) + + MACRO(make_hrtf_header FILENAME VARNAME) + SET(infile "${OpenAL_SOURCE_DIR}/hrtf/${FILENAME}") + SET(outfile "${OpenAL_BINARY_DIR}/${FILENAME}.h") + + ADD_CUSTOM_COMMAND(OUTPUT "${outfile}" + COMMAND "${BIN2H_COMMAND}" "${infile}" "${outfile}" ${VARNAME} + DEPENDS "${BIN2H_COMMAND}" "${infile}" + COMMENT "Generating ${FILENAME}.h" + VERBATIM + ) + + SET(ALC_OBJS ${ALC_OBJS} "${outfile}") + ENDMACRO() + + make_hrtf_header(default-44100.mhr "hrtf_default_44100") + make_hrtf_header(default-48000.mhr "hrtf_default_48000") endif() diff --git a/appveyor.yml b/appveyor.yml index 0d60cf61..9ea05d4e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,6 +14,6 @@ install: build_script: - cd build - - cmake -G"%GEN%" -DALSOFT_REQUIRE_WINMM=ON -DALSOFT_REQUIRE_DSOUND=ON -DALSOFT_REQUIRE_MMDEVAPI=ON .. + - cmake -G"%GEN%" -DALSOFT_REQUIRE_WINMM=ON -DALSOFT_REQUIRE_DSOUND=ON -DALSOFT_REQUIRE_MMDEVAPI=ON -DALSOFT_EMBED_HRTF_DATA=YES .. - cmake --build . --config %CFG% --clean-first diff --git a/utils/native-tools/CMakeLists.txt b/utils/native-tools/CMakeLists.txt new file mode 100644 index 00000000..4713dc56 --- /dev/null +++ b/utils/native-tools/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.0.2) +project(native-tools) +add_executable(bin2h bin2h.c) +# Enforce no dressing for executable names, so the main script can find it +set_target_properties(bin2h PROPERTIES OUTPUT_NAME bin2h) +# Avoid configuration-dependent subdirectories while building with Visual Studio +set_target_properties(bin2h PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}") +set_target_properties(bin2h PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}") diff --git a/utils/native-tools/bin2h.c b/utils/native-tools/bin2h.c new file mode 100644 index 00000000..d9f5eb9a --- /dev/null +++ b/utils/native-tools/bin2h.c @@ -0,0 +1,100 @@ + +#ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +int main (int argc, char *argv[]) +{ + char* input_name; + FILE* input_file; + + char* output_name; + FILE* output_file; + + char* variable_name; + + if (4 != argc) + { + puts("Usage: bin2h [input] [output] [variable]"); + return EXIT_FAILURE; + } + + input_name = argv[1]; + output_name = argv[2]; + variable_name = argv[3]; + + input_file = fopen(input_name, "rb"); + + if (NULL == input_file) + { + printf("Could not open input file '%s': %s\n", input_name, strerror(errno)); + return EXIT_FAILURE; + } + + output_file = fopen(output_name, "w"); + + if (NULL == output_file) + { + printf("Could not open output file '%s': %s\n", output_name, strerror(errno)); + return EXIT_FAILURE; + } + + if (fprintf(output_file, "unsigned char %s[] = {", variable_name) < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + while (0 == feof(input_file)) + { + unsigned char buffer[4096]; + size_t i, count = fread(buffer, 1, sizeof(buffer), input_file); + + if (sizeof(buffer) != count) + { + if (0 == feof(input_file) || 0 != ferror(input_file)) + { + printf("Could not read from input file '%s': %s\n", input_name, strerror(ferror(input_file))); + return EXIT_FAILURE; + } + } + + for (i = 0; i < count; ++i) + { + if ((i & 15) == 0) + { + if (fprintf(output_file, "\n ") < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + } + + if (fprintf(output_file, "0x%2.2x, ", buffer[i]) < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + } + } + + if (fprintf(output_file, "\n};\n") < 0) + { + printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + if (fclose(output_file) < 0) + { + printf("Could not close output file '%s': %s\n", output_name, strerror(ferror(output_file))); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} |