Skip to content

Commit

Permalink
feat: make some sound on linux, power by sdl3 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-LZW committed May 20, 2023
1 parent 7a04bd8 commit ec66e54
Show file tree
Hide file tree
Showing 15 changed files with 2,797 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "third_party/mdspan"]
path = third_party/mdspan
url = git@github.com:kokkos/mdspan.git
[submodule "third_party/SDL"]
path = third_party/SDL
url = git@github.com:libsdl-org/SDL.git
58 changes: 58 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# libstdaudio
# Copyright (c) 2018 - Timur Doumler
# Copyright (c) 2023 - Lan Zongwei
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

Expand All @@ -15,6 +16,20 @@ set(CMAKE_CXX_STANDARD 23)

option(AUDIO_ENABLE_TESTS "Enable tests." ON)
option(AUDIO_ENABLE_EXAMPLES "Build examples." ON)
option(AUDIO_WITH_SDL3 "Enable SDL backend." ON)
option(AUDIO_STATIC "Use static libraries" OFF)

find_package(Git QUIET)

if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive\
failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()

if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX")
Expand All @@ -31,6 +46,41 @@ if (NOT MDSPAN_EXIST)
add_subdirectory(third_party/mdspan)
endif()

find_package(PkgConfig)

if (AUDIO_WITH_SDL3)
pkg_search_module(SDL3 sdl3)

add_compile_definitions(AUDIO_USE_SDL3 DEBUG_AUDIO)

if (SDL3_FOUND)
message(STATUS "System SDL3 found.")
else()
message(STATUS "Use internal SDL3.")

# Disable all subsystems except Audio, Events, Threads, and loadso(for shared library)
set(SDL_SUBSYSTEMS
Atomic Video Render Joystick Haptic Hidapi Power Timers
File CPUinfo Filesystem Sensor Locale Misc)
foreach(_SUB ${SDL_SUBSYSTEMS})
string(TOUPPER ${_SUB} _OPT)
set(SDL_${_OPT} OFF CACHE BOOL "Enable the ${_SUB} subsystem" FORCE)
endforeach()

if (AUDIO_STATIC)
set(SDL_STATIC ON CACHE BOOL "Build a static version of SDL" FORCE)
set(SDL_LOADSO OFF CACHE BOOL "Enable the LOADSO subsystem" FORCE)

foreach(AUDIO_LIB ALSA JACK PIPEWIRE PULSEAUDIO SNDIO)
set(SDL_${AUDIO_LIB}_SHARED OFF CACHE BOOL "Dynamically load ${AUDIO_LIB} audio support" FORCE)
endforeach()
endif()

set(SDL_DISABLE_INSTALL OFF CACHE BOOL "Disable installation of SDL3" FORCE)
add_subdirectory(third_party/SDL)
endif()
endif()

add_library(audio INTERFACE)
add_library(std::audio ALIAS audio)
target_include_directories(audio INTERFACE
Expand All @@ -39,6 +89,14 @@ target_include_directories(audio INTERFACE
)
target_link_libraries(audio INTERFACE std::mdspan)

if (AUDIO_WITH_SDL3)
if (AUDIO_STATIC)
target_link_libraries(audio INTERFACE SDL3::SDL3-static)
else()
target_link_libraries(audio INTERFACE SDL3::SDL3)
endif()
endif()

###################################################

install(TARGETS audio EXPORT audioTargets
Expand Down
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,43 @@ Personally, I think this proposal wouldn't accept recently, but I really like an

Basically, this repo's custom backend is a P1386-like SLD3 C++ wrapper. So, may not send pr to upstream. Because it is no longer a header-only library. (except the proposal owner accepts that)

Unlike the [original repo](https://github.com/stdcpp-audio/libstdaudio), I decide to flollow the API design presented in P1386 try my best.
So you can use it since you already read P1386.
<del>Unlike the [original repo](https://github.com/stdcpp-audio/libstdaudio), I decide to flollow the API design presented in P1386 try my best.
So you can use it since you already read P1386.</del> I found P1386 is not so suitable to wrapper SDL, so I decide to make some change base on P1386.

Here are some stuffs different with the [original repo](https://github.com/stdcpp-audio/libstdaudio).
Here are some stuffs different with the [original repo](https://github.com/stdcpp-audio/libstdaudio) and P1386.

1. use mdspan for contiguous data view, and `std::span` vector for distant view.

2. add multidimensional subscript operation since it supported by compiler.

3. access operator's argument is `(channel, frame)` instead of `(frame, channel)`, which follow P1386.

4. if any error happen, a `std::runtime_error` will be throw, is in the feature, exception is not suitable, another `try_xxx` api will be add, which use `std::expected` as return type.

5. sdl backend API changes:

```
class audio_device:
modify:
bool start(AudioDeviceCallback auto&& startCallback = no_op,
AudioDeviceCallback auto&& stopCallback = no_op);
=>
bool start();
just play the audio device.
add:
bool pause();
Pause the audio device.
bool set_sample_type();
Set undering sample type, return false if device is running.
```

## Repository structure

`include` contains the `audio` header, which is the only header users of the library should include. It also contains the header files of the different classes and functions, prefixed with `__audio_`. Please refer to these header files for a documentation of the API as implemented here. (We plan to set up proper documentation soon.)
Expand Down
1 change: 0 additions & 1 deletion include/experimental/__p1386/audio_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ template <typename SampleType> class audio_buffer {
std::variant<contiguous_view_type, distant_view_type> _data_view;
};

// TODO: this is currently macOS specific!
using audio_clock_t = chrono::steady_clock;

template <typename SampleType> struct audio_device_io {
Expand Down
4 changes: 3 additions & 1 deletion include/experimental/audio
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "experimental/__p1386/audio_device.h"
#include "experimental/__p1386/audio_event.h"

#ifdef __APPLE__
#if defined(AUDIO_USE_SDL3)
#include "experimental/audio_backend/sdl_backend.h"
#elif defined(__APPLE__)
#include "experimental/audio_backend/__coreaudio_backend.h"
#elif defined(_WIN32)
#include "experimental/audio_backend/__wasapi_backend.h"
Expand Down

0 comments on commit ec66e54

Please sign in to comment.