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