diff --git a/dsptools/CMakeLists.txt b/dsptools/CMakeLists.txt index b0154b5..9add31a 100644 --- a/dsptools/CMakeLists.txt +++ b/dsptools/CMakeLists.txt @@ -1,12 +1,6 @@ cmake_minimum_required(VERSION 3.15 FATAL_ERROR) project(dsptools LANGUAGES C) -include(CheckIncludeFile) -check_include_file("endian.h" HAVE_ENDIAN_H) -if (HAVE_ENDIAN_H) - add_definitions(HAVE_ENDIAN_H) -endif() - add_subdirectory(libdsptool) set(HEADERS wave.h) diff --git a/dsptools/common.h b/dsptools/common.h index 325c711..af034af 100644 --- a/dsptools/common.h +++ b/dsptools/common.h @@ -1,39 +1,67 @@ #ifndef COMMON_H #define COMMON_H -#ifdef HAVE_ENDIAN_H +#ifdef __APPLE__ +# include +#elif defined( __linux__ ) || defined( __CYGWIN__ ) || defined( __OpenBSD__ ) # include -# define BYTE_ORDER __BYTE_ORDER -# define LITTLE_ENDIAN __LITTLE_ENDIAN -# define BIG_ENDIAN __BIG_ENDIAN -#else -# define LITTLE_ENDIAN 1234 -# define BIG_ENDIAN 4321 -# if defined( __APPLE__ ) || defined( _WIN32 ) -# define BYTE_ORDER LITTLE_ENDIAN -# else -# error Can't detect endianness +#elif defined( __NetBSD__ ) || defined( __FreeBSD__ ) || defined( __DragonFly__ ) +# include +# ifdef __FreeBSD__ +# define LITTLE_ENDIAN _LITTLE_ENDIAN +# define BIG_ENDIAN _BIG_ENDIAN +# define BYTE_ORDER _BYTE_ORDER +# endif +#elif defined( _MSC_VER ) || defined( _WIN16 ) || defined( _WIN32 ) || defined( _WIN64 ) +# ifdef _MSC_VER +# define LITTLE_ENDIAN 1234 +# define BIG_ENDIAN 4321 +# if defined( _M_IX86 ) || defined( _M_X64 ) || defined( _M_AMD64 ) || defined( _M_IA64 ) +# define BYTE_ORDER LITTLE_ENDIAN +# elif defined( _M_PPC ) +// Probably not reliable but eh +# define BYTE_ORDER BIG_ENDIAN +# endif +# elif defined( __GNUC__ ) +# include # endif #endif +#if !defined( BYTE_ORDER ) || !defined( LITTLE_ENDIAN ) || !defined( BIG_ENDIAN ) || \ + !( BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == BIG_ENDIAN ) +# error "Couldn't determine endianness or unsupported platform" +#endif + +#ifdef _MSC_VER +# define swap32(X) _byteswap_ulong((X)) +# define swap16(X) _byteswap_ushort((X)) +#elif ( __GNUC__ == 4 && __GNUC_MINOR__ >= 8 ) || ( __GNUC__ > 4 ) +# pragma message("CLion penis") +# define swap32(X) __builtin_bswap32((X)) +# define swap16(X) __builtin_bswap16((X)) +// Apparently smelly GCC 5 blows up on this test so this is done separately for Clang +#elif defined( __has_builtin ) && __has_builtin( __builtin_bswap32 ) && __has_builtin( __builtin_bswap16 ) +# define swap32(X) __builtin_bswap32((X)) +# define swap16(X) __builtin_bswap16((X)) +#else static inline uint32_t swap32(uint32_t v) { - return (v << 24) | ((v << 8) & 0x00FF0000) | ((v >> 8) & 0x0000FF00) | (v >> 24); + return (v << 24U) | ((v << 8U) & 0x00FF0000U) | ((v >> 8U) & 0x0000FF00U) | (v >> 24U); } - static inline uint16_t swap16(uint16_t v) { - return (v << 8) | (v >> 8); + return (v << 8U) | (v >> 8U); } +#endif #if BYTE_ORDER == LITTLE_ENDIAN # define SWAP_LE32(V) (V) # define SWAP_LE16(V) (V) -# define SWAP_BE32(V) swap32((uint32_t)V) -# define SWAP_BE16(V) swap16((uint16_t)V) +# define SWAP_BE32(V) swap32((V)) +# define SWAP_BE16(V) swap16((V)) #elif BYTE_ORDER == BIG_ENDIAN -# define SWAP_LE32(V) swap32((uint32_t)V) -# define SWAP_LE16(V) swap16((uint16_t)V) +# define SWAP_LE32(V) swap32((V)) +# define SWAP_LE16(V) swap16((V)) # define SWAP_BE32(V) (V) # define SWAP_BE16(V) (V) #endif diff --git a/dsptools/dspdecode.c b/dsptools/dspdecode.c index 71c2df0..50226f7 100644 --- a/dsptools/dspdecode.c +++ b/dsptools/dspdecode.c @@ -182,7 +182,7 @@ int main(int argc, char* argv[]) if (opt || !inPathL) usage(argv[0]); - // C + // Compute output path if one wasn't specified if (!outPath) { const char* base = strrchr(inPathL, '/'); @@ -205,6 +205,7 @@ int main(int argc, char* argv[]) outPathAlloc = true; } + // Convert left (and optionally right) channels to PCM, save as wave int ret; PcmFile left = PCMFILE_CLEAR(), right = PCMFILE_CLEAR(); if ((ret = loadDsp(inPathL, &left))) @@ -253,6 +254,8 @@ int main(int argc, char* argv[]) Cleanup: free(right.pcm); free(left.pcm); + if (outPathAlloc) + free(outPath); return ret; } diff --git a/dsptools/wave.h b/dsptools/wave.h index c1e24a2..0822086 100644 --- a/dsptools/wave.h +++ b/dsptools/wave.h @@ -6,6 +6,7 @@ extern "C" { #endif #include +#include typedef enum {