1# libsndfile 2 3![C/C++ CI](https://github.com/libsndfile/libsndfile/workflows/C/C++%20CI/badge.svg) 4 5libsndfile is a C library for reading and writing files containing sampled audio 6data. 7 8## Authors 9 10The libsndfile project was originally developed and maintained by 11Erik de Castro Lopo <erikd@mega-nerd.com> aka @erikd. The project was developed 12on Github at <https://github.com/erikd/libsndfile>. 13 14After the release of version 1.0.30, @erikd transferred the project to 15[the libsndfile team](https://github.com/libsndfile), see [AUTHORS](AUTHORS) 16for details. 17 18## Hacking 19 20The canonical source code repository for libsndfile is at 21[http://libsndfile.github.io/libsndfile/][github]. 22 23You can grab the source code using: 24 25 git clone https://github.com/libsndfile/libsndfile.git 26 27For building for Android see [BuildingForAndroid][BuildingForAndroid]. 28 29There are currently two build systems: the traditional GNU autotool based one and 30modern CMake based build system. Use of the CMake build system is documented 31below. 32 33Setting up a build environment for libsndfile on Debian or Ubuntu is as simple as: 34 35 sudo apt install autoconf autogen automake build-essential libasound2-dev \ 36 libflac-dev libogg-dev libtool libvorbis-dev libopus-dev pkg-config python 37 38For other Linux distributions or any of the *BSDs, the setup should be similar 39although the package install tools and package names may be slightly different. 40 41Similarly on Mac OS X, assuming [brew] is already installed: 42 43 brew install autoconf autogen automake flac libogg libtool libvorbis opus pkg-config 44 45Once the build environment has been set up, building and testing libsndfile is 46as simple as: 47 48 ./autogen.sh 49 ./configure --enable-werror 50 make 51 make check 52 53## The CMake build system 54 55Although Autotools is the primary and recommended build toolchain, CMake meta 56build generator is also available. The build process with CMake takes 57place in two stages. First, standard build files are created from configuration 58scripts. Then the platform's native build tools are used for the actual 59building. CMake can produce Microsoft Visual Studio project and solution files, 60Unix Makefiles, Xcode projects and [many more](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html). 61 62Some IDE support CMake natively or with plugins, check you IDE documentation 63 for details. 64 65### Requirements 66 671. C99-compliant compiler toolchain (tested with GCC, Clang and Visual 68 Studio 2015) 692. CMake 3.1.3 or newer 70 71There are some recommended packages to enable all features of libsndfile: 72 731. Ogg, Vorbis and FLAC libraries and headers to enable these formats support 742. ALSA development package under Linux to build sndfile-play utility 753. Sndio development package under BSD to build sndfile-play utility 76 77### Building from command line 78 79CMake can handle out-of-place builds, enabling several builds from 80the same source tree, and cross-compilation. The ability to build a directory 81tree outside the source tree is a key feature, ensuring that if a build 82directory is removed, the source files remain unaffected. 83 84 mkdir CMakeBuild 85 cd CMakeBuild 86 87Then run `cmake` command with directory where CMakeLists.txt script is located 88as argument (relative paths are supported): 89 90 cmake .. 91 92This command will configure and write build script or solution to CMakeBuild 93directory. CMake is smart enough to create Unix makefiles under Linux or Visual 94Studio solution if you have Visual Studio installed, but you can configure 95[generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) 96with `-G` command line parameter: 97 98 cmake .. -G"Unix Makefiles" 99 100The build procedure depends on the selected generator. With "Unix Makefiles" you 101can type: 102 103 make & make install 104 105With "Visual Studio" and some other generators you can open solution or project 106from `CMakeBuild` directory and build using IDE. 107 108Finally, you can use unified command: 109 110 cmake --build . 111 112CMake also provides Qt-based cross platform GUI, cmake-gui. Using it is trivial 113and does not require detailed explanations. 114 115### Configuring CMake 116 117You can pass additional options with `/D<parameter>=<value>` when you run 118`cmake` command. Some useful system options: 119 120* `CMAKE_C_FLAGS` - additional C compiler flags 121* `CMAKE_BUILD_TYPE` - configuration type, `DEBUG`, `RELEASE`, `RELWITHDEBINFO` 122 or `MINSIZEREL`. `DEBUG` is default 123* `CMAKE_INSTALL_PREFIX` - build install location, the same as `--prefix` option 124 of `configure` script 125 126 Useful libsndfile options: 127 128* `BUILD_SHARED_LIBS` - build shared library (DLL under Windows) when `ON`, 129 build static library othervise. This option is `OFF` by default. 130* `BUILD_PROGRAMS` - build libsndfile's utilities from `programs/` directory, 131 `ON` by default. 132* `BUILD_EXAMPLES` - build examples, `ON` by default. 133* `BUILD_TESTING` - build tests. Then you can run tests with `ctest` command, 134 `ON` by default. Setting `BUILD_SHARED_LIBS` to `ON` disables this option. 135* `ENABLE_EXTERNAL_LIBS` - enable Ogg, Vorbis, FLAC and Opus support. This 136 option is available and set to `ON` if all dependency libraries were found. 137* `ENABLE_CPU_CLIP` - enable tricky cpu specific clipper. Enabled and set to 138 `ON` when CPU clips negative\positive. Don't touch it if you are not sure 139* `ENABLE_BOW_DOCS` - enable black-on-white documentation theme, `OFF` by 140 default. 141* `ENABLE_EXPERIMENTAL` - enable experimental code. Don't use it if you are 142 not sure. This option is `OFF` by default. 143* `ENABLE_CPACK` - enable [CPack](https://cmake.org/cmake/help/latest/module/CPack.html) support. 144 This option is `ON` by default. 145* `ENABLE_PACKAGE_CONFIG` - generate and install [package config file](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#config-file-packages). 146* `INSTALL_PKGCONFIG_MODULE` - generate and install [pkg-config module](https://people.freedesktop.org/~dbn/pkg-config-guide.html). 147* `INSTALL_MANPAGES` - install [man pages](https://en.wikipedia.org/wiki/Man_page) for programs. This option is `ON` by default 148* `ENABLE_STATIC_RUNTIME` - enable static runtime on Windows platform (MSVC and 149 MinGW), `OFF` by default. 150 151 **Note**: For MSVC compiler this option is deprecated for CMake >= 3.15, see 152 policy [CMP0091](https://cmake.org/cmake/help/latest/policy/CMP0091.html). 153 Use `CMAKE_MSVC_RUNTIME_LIBRARY` option instead. 154 155 **Note**: For MinGW toolchain this option is experimental. If you enabled it 156 and then disabled again, you need to clear CMake cache (delete CMakeCache.txt). 157* `ENABLE_COMPATIBLE_LIBSNDFILE_NAME` - set DLL name to `libsndfile-1.dll` 158 (canonical name) on Windows platform, `sndfile.dll` otherwise, `OFF` by 159 default. Library name can be different depending on platform. The well known 160 DLL name on Windows platform is `libsndfile-1.dll`, because the only way to 161 build Windows library before was MinGW toolchain with Autotools. This name 162 is native for MinGW ecosystem, Autotools constructs it using MinGW platform 163 rules from `sndfile` target. But when you build with CMake using native 164 Windows compiler, the name is `sndfile.dll`. This is name for native Windows 165 platform, because Windows has no library naming rules. It is preffered 166 because you can search library using package manager or CMake's 167 `find_library` command on any platform using the same `sndfile` name. 168 169* `ENABLE_SSE2` - add compiler flag to enable SSE2 if required, `ON` by default. 170 171 This option is for X86 and GCC compatible compilers configurations only. 172 173 If you compile for other SIMD set, e.g. AVX2, you may want to set 174 `ENABLE_SSE2` to `OFF`. 175 176 **Note**: This option is not active for X64 configuration, because SSE2 is 177 always available in this mode and all optimizations are enabled by default. 178 179Deprecated options: 180 181* `DISABLE_EXTERNAL_LIBS` - disable Ogg, Vorbis and FLAC support. Replaced by 182 `ENABLE_EXTERNAL_LIBS` 183* `DISABLE_CPU_CLIP` - disable tricky cpu specific clipper. Replaced by 184 `ENABLE_CPU_CLIP` 185* `BUILD_STATIC_LIBS` - build static library. Use `BUILD_SHARED_LIBS` instead 186 187### Linking from CMake projects 188 189First you need to add `FindOgg.cmake`, `FindVorbis.cmake`, `FindFLAC.cmake` and 190`FindOpus.cmake` files to some directory inside your CMake project (usually 191`cmake`) and add it to `CMAKE_MODULE_PATH`: 192 193 project(SomeApplication) 194 195 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) 196 197Now you can search `libsndfile` library from your `CMakeLists.txt` 198 with this command: 199 200 find_package(SndFile) 201 202`SndFile_FOUND` is set to `ON` when library is found. 203 204If `libsndfile` dependency is critical, you can add `REQUIRED` to 205 `find_package`: 206 207 find_package(SndFile REQUIRED) 208 209With with option `find_package` will terminate configuration process 210 if `libsndfile` is not found. 211 212You can also add version check: 213 214 find_package(SndFile 1.0.29) 215 216`find_package` will report error, if `libsndfile` version is < 1.0.29. 217 218You can combine `REQUIRED` and version if you need. 219 220To link `libsndfile` library use: 221 222 target_link_libraries(my_application PRIVATE SndFile::sndfile) 223 224### Notes for Windows users 225 226#### System CRT library 227 228First advice about Visual Studio [system CRT libraries](https://docs.microsoft.com/en-us/cpp/c-runtime-library/c-run-time-library-reference?view=vs-2019), 229it is system code linked as static or dynamic library to every C application. 230 231You can find related option in Visual Studio project properties: 232 233 C/C++ -> Code Generation -> Runtime Library 234 235Dynamic version of system CRT library is defaut and it means that end user needs 236to have the same runtime library installed on his system. Most likely it is so, 237but if it is not, the user will see this error message using libsndfile DLL: 238 239 "The program can't start because <crt-dll-name>.dll is missing from your computer. Try reinstalling the program to fix this problem. " 240 241To avoid this, you may want to enable static CRT library linking. In this case 242the size of your DLL will increase slightly the size will increase slightly, but 243you can redistribute the libsndfile DLL without having to install the correct 244version of the system CRT library. 245 246CMake project will use dynamic system CRT libraries by default, just like 247Visual Studio does. But you can change it using `ENABLE_STATIC_RUNTIME` or 248`CMAKE_MSVC_RUNTIME_LIBRARY` options. 249 250**Note**: You cannot use both options at the same time, it will lead to a 251configuration error. 252 253If you have CMake >= 3.15 you should use 254[`CMAKE_MSVC_RUNTIME_LIBRARY`](https://cmake.org/cmake/help/v3.15/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html) option. 255 256This will enable static linking: 257 258 cmake .. -D"MultiThreaded$<$<CONFIG:Debug>:Debug>" 259 260You can use libsndfile `ENABLE_STATIC_RUNTIME` option to to control CRT library 261linking for CMake project: `OFF` or unset (default) for dynamic, and `ON` for 262static linking: 263 264 cmake .. -DENABLE_STATIC_RUNTIME=ON 265 266**Note**: This option is deprecated and may be removed in far future because we 267have standard option `CMAKE_MSVC_RUNTIME_LIBRARY` now. 268 269#### Using Vcpkg package manager 270 271Second advice is about Ogg, Vorbis FLAC and Opus support. Searching external 272libraries under Windows is a little bit tricky. The best way is to use 273[Vcpkg](https://github.com/Microsoft/vcpkg). You need to install static libogg, 274libvorbis, libflac and libopus libraries: 275 276 vcpkg install libogg:x64-windows-static libvorbis:x64-windows-static 277 libflac:x64-windows-static opus:x64-windows-static libogg:x86-windows-static 278 libvorbis:x86-windows-static libflac:x86-windows-static opus:x86-windows-static 279 280Then and add this parameter to cmake command line: 281 282 -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake 283 284You also need to set `VCPKG_TARGET_TRIPLET` because you use static libraries: 285 286 -DVCPKG_TARGET_TRIPLET=x64-windows-static 287 288**Note**: Use must use the same CRT library for external libraries and the 289libsndfile library itself. For `*-static` triplets Vcpkg uses 290[static CRT](https://vcpkg.readthedocs.io/en/latest/users/triplets/). 291 292## Submitting Patches 293 294See [CONTRIBUTING.md](CONTRIBUTING.md) for details. 295 296[brew]: http://brew.sh/ 297[github]: http://libsndfile.github.io/libsndfile/ 298[BuildingForAndroid]: https://github.com/libsndfile/libsndfile/blob/master/Building-for-Android.md 299